author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Mon, 10 Sep 2012 17:31:07 +0200 | |
changeset 8540 | fee98af8bb33 |
parent 5424 | 8ecbcbff9777 |
permissions | -rw-r--r-- |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
1 |
# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
2 |
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
3 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
4 |
# This file is part of CubicWeb. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
5 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
6 |
# CubicWeb is free software: you can redistribute it and/or modify it under the |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
7 |
# terms of the GNU Lesser General Public License as published by the Free |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
8 |
# Software Foundation, either version 2.1 of the License, or (at your option) |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
9 |
# any later version. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
10 |
# |
5424
8ecbcbff9777
replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5421
diff
changeset
|
11 |
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
12 |
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
13 |
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
14 |
# details. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
15 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
16 |
# You should have received a copy of the GNU Lesser General Public License along |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
17 |
# with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
18 |
"""cubicweb extensions for twill |
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
19 |
|
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
20 |
""" |
0 | 21 |
|
22 |
import re |
|
23 |
from urllib import quote |
|
24 |
||
25 |
from twill import commands as twc |
|
26 |
||
27 |
# convenience / consistency renaming |
|
28 |
has_text = twc.find |
|
29 |
hasnt_text = twc.notfind |
|
30 |
||
31 |
||
32 |
# specific commands |
|
33 |
_LINK = re.compile('<a.*?href="(.*?)".*?>(.*?)</a>', re.I | re.S) |
|
34 |
||
35 |
def has_link(text, url=''): |
|
36 |
browser = twc.get_browser() |
|
37 |
html = browser.get_html() |
|
38 |
if html: |
|
39 |
for match in _LINK.finditer(html): |
|
40 |
linkurl = match.group(1) |
|
41 |
linktext = match.group(2) |
|
42 |
if linktext == text: |
|
43 |
# if url is specified linkurl must match |
|
44 |
if url and linkurl != url: |
|
45 |
continue |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
46 |
return |
0 | 47 |
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
|
48 |
|
0 | 49 |
|
50 |
def view(rql, vid=''): |
|
51 |
""" |
|
52 |
>> view 'Project P' |
|
53 |
||
54 |
apply <vid> to <rql>'s rset |
|
55 |
""" |
|
56 |
if vid: |
|
57 |
twc.go('view?rql=%s&vid=%s' % (quote(rql), vid)) |
|
58 |
else: |
|
59 |
twc.go('view?rql=%s' % quote(rql)) |
|
60 |
||
61 |
def create(etype): |
|
62 |
""" |
|
63 |
>> create Project |
|
64 |
||
65 |
go to <etype>'s creation page |
|
66 |
""" |
|
67 |
twc.go('view?etype=%s&vid=creation' % etype) |
|
68 |
||
69 |
def edit(rql): |
|
70 |
""" |
|
71 |
>> edit "Project P WHERE P eid 123" |
|
72 |
||
73 |
calls edition view for <rql> |
|
74 |
""" |
|
75 |
twc.go('view?rql=%s&vid=edition' % quote(rql)) |
|
76 |
||
77 |
||
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
78 |
|
0 | 79 |
|
80 |
def setvalue(formname, fieldname, value): |
|
81 |
""" |
|
82 |
>> setvalue entityForm name pylint |
|
83 |
||
84 |
sets the field's value in the form |
|
85 |
<forname> should either be the form's index, the form's name |
|
86 |
or the form's id |
|
87 |
""" |
|
88 |
browser = twc.get_browser() |
|
89 |
form = browser.get_form(formname) |
|
90 |
if form is None: |
|
91 |
# try to find if one of the forms has <formname> as id |
|
92 |
for index, form in enumerate(browser._browser.forms()): |
|
93 |
# forms in cubicweb don't always have a name |
|
94 |
if form.attrs.get('id') == formname: |
|
95 |
# browser.get_form_field knows how to deal with form index |
|
96 |
formname = str(index+1) |
|
97 |
break |
|
98 |
else: |
|
99 |
raise ValueError('could not find form named <%s>' % formname) |
|
100 |
eid = browser.get_form_field(form, 'eid').value |
|
101 |
twc.formvalue(formname, '%s:%s' % (fieldname, eid), value) |
|
102 |
||
103 |
||
104 |
def submitform(formname, submit_button=None): |
|
105 |
""" |
|
106 |
>> submitform entityForm |
|
107 |
||
108 |
Submit the form named entityForm. This is useful when the form is pre-filed |
|
109 |
and we only want to click on submit. |
|
110 |
(The original submit command chooses the form to submit according to the last |
|
111 |
formvalue instruction) |
|
112 |
""" |
|
113 |
browser = twc.get_browser() |
|
114 |
form = browser.get_form(formname) |
|
115 |
if form is None: |
|
116 |
# try to find if one of the forms has <formname> as id |
|
117 |
for form in browser._browser.forms(): |
|
118 |
# forms in cubicweb don't always have a name |
|
119 |
if form.attrs.get('id') == formname: |
|
120 |
break |
|
121 |
else: |
|
122 |
raise ValueError('could not find form named <%s>' % formname) |
|
123 |
browser._browser.form = form |
|
124 |
browser.submit(submit_button) |
|
125 |
||
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
126 |
|
0 | 127 |
# missing actions: delete, copy, changeview |