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