author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Wed, 26 Aug 2009 10:18:07 +0200 | |
branch | stable |
changeset 3009 | 3deb0fa95761 |
parent 1977 | 606923dff11b |
child 4212 | ab6573088b4a |
permissions | -rw-r--r-- |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
1 |
"""cubicweb extensions for twill |
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
2 |
|
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
3 |
:organization: Logilab |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
4 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
5 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
6 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
7 |
""" |
0 | 8 |
|
9 |
import re |
|
10 |
from urllib import quote |
|
11 |
||
12 |
from twill import commands as twc |
|
13 |
||
14 |
# convenience / consistency renaming |
|
15 |
has_text = twc.find |
|
16 |
hasnt_text = twc.notfind |
|
17 |
||
18 |
||
19 |
# specific commands |
|
20 |
_LINK = re.compile('<a.*?href="(.*?)".*?>(.*?)</a>', re.I | re.S) |
|
21 |
||
22 |
def has_link(text, url=''): |
|
23 |
browser = twc.get_browser() |
|
24 |
html = browser.get_html() |
|
25 |
if html: |
|
26 |
for match in _LINK.finditer(html): |
|
27 |
linkurl = match.group(1) |
|
28 |
linktext = match.group(2) |
|
29 |
if linktext == text: |
|
30 |
# if url is specified linkurl must match |
|
31 |
if url and linkurl != url: |
|
32 |
continue |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
33 |
return |
0 | 34 |
raise AssertionError('link %s (%s) not found' % (text, url)) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
35 |
|
0 | 36 |
|
37 |
def view(rql, vid=''): |
|
38 |
""" |
|
39 |
>> view 'Project P' |
|
40 |
||
41 |
apply <vid> to <rql>'s rset |
|
42 |
""" |
|
43 |
if vid: |
|
44 |
twc.go('view?rql=%s&vid=%s' % (quote(rql), vid)) |
|
45 |
else: |
|
46 |
twc.go('view?rql=%s' % quote(rql)) |
|
47 |
||
48 |
def create(etype): |
|
49 |
""" |
|
50 |
>> create Project |
|
51 |
||
52 |
go to <etype>'s creation page |
|
53 |
""" |
|
54 |
twc.go('view?etype=%s&vid=creation' % etype) |
|
55 |
||
56 |
def edit(rql): |
|
57 |
""" |
|
58 |
>> edit "Project P WHERE P eid 123" |
|
59 |
||
60 |
calls edition view for <rql> |
|
61 |
""" |
|
62 |
twc.go('view?rql=%s&vid=edition' % quote(rql)) |
|
63 |
||
64 |
||
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
65 |
|
0 | 66 |
|
67 |
def setvalue(formname, fieldname, value): |
|
68 |
""" |
|
69 |
>> setvalue entityForm name pylint |
|
70 |
||
71 |
sets the field's value in the form |
|
72 |
<forname> should either be the form's index, the form's name |
|
73 |
or the form's id |
|
74 |
""" |
|
75 |
browser = twc.get_browser() |
|
76 |
form = browser.get_form(formname) |
|
77 |
if form is None: |
|
78 |
# try to find if one of the forms has <formname> as id |
|
79 |
for index, form in enumerate(browser._browser.forms()): |
|
80 |
# forms in cubicweb don't always have a name |
|
81 |
if form.attrs.get('id') == formname: |
|
82 |
# browser.get_form_field knows how to deal with form index |
|
83 |
formname = str(index+1) |
|
84 |
break |
|
85 |
else: |
|
86 |
raise ValueError('could not find form named <%s>' % formname) |
|
87 |
eid = browser.get_form_field(form, 'eid').value |
|
88 |
twc.formvalue(formname, '%s:%s' % (fieldname, eid), value) |
|
89 |
||
90 |
||
91 |
def submitform(formname, submit_button=None): |
|
92 |
""" |
|
93 |
>> submitform entityForm |
|
94 |
||
95 |
Submit the form named entityForm. This is useful when the form is pre-filed |
|
96 |
and we only want to click on submit. |
|
97 |
(The original submit command chooses the form to submit according to the last |
|
98 |
formvalue instruction) |
|
99 |
""" |
|
100 |
browser = twc.get_browser() |
|
101 |
form = browser.get_form(formname) |
|
102 |
if form is None: |
|
103 |
# try to find if one of the forms has <formname> as id |
|
104 |
for form in browser._browser.forms(): |
|
105 |
# forms in cubicweb don't always have a name |
|
106 |
if form.attrs.get('id') == formname: |
|
107 |
break |
|
108 |
else: |
|
109 |
raise ValueError('could not find form named <%s>' % formname) |
|
110 |
browser._browser.form = form |
|
111 |
browser.submit(submit_button) |
|
112 |
||
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
113 |
|
0 | 114 |
# missing actions: delete, copy, changeview |