devtools/cwtwill.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Fri, 19 Feb 2010 09:34:14 +0100
branchstable
changeset 4643 921737d2e3a8
parent 4212 ab6573088b4a
child 5421 8167de96c523
permissions -rw-r--r--
fix optimisation with super session that may lead to integrity loss at some point I've decided to stop ensuring ?1 cardinality was respected when adding a new relation using a super session, to avoid the cost of the delete query. That was yet discussable because it introduced unexpected difference between execute and unsafe_execute, which is imo not worth it. Also, now that rql() in migration script default to unsafe_execute, we definitly don't want that implicit behaviour change (which already cause bug when for instance adding another default workflow for an entity type: without that fix we end up with *two* default workflows while the schema tells we can have only one. IMO we should go to the direction that super session skip all security check, but nothing else, unless explicitly asked.

"""cubicweb extensions for twill

:organization: Logilab
:copyright: 2001-2010 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2.
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses
"""

import re
from urllib import quote

from twill import commands as twc

# convenience / consistency renaming
has_text = twc.find
hasnt_text = twc.notfind


# specific commands
_LINK = re.compile('<a.*?href="(.*?)".*?>(.*?)</a>', re.I | re.S)

def has_link(text, url=''):
    browser = twc.get_browser()
    html = browser.get_html()
    if html:
        for match in _LINK.finditer(html):
            linkurl = match.group(1)
            linktext = match.group(2)
            if linktext == text:
                # if url is specified linkurl must match
                if url and linkurl != url:
                    continue
                return
    raise AssertionError('link %s (%s) not found' % (text, url))


def view(rql, vid=''):
    """
    >> view 'Project P'

    apply <vid> to <rql>'s rset
    """
    if vid:
        twc.go('view?rql=%s&vid=%s' % (quote(rql), vid))
    else:
        twc.go('view?rql=%s' % quote(rql))

def create(etype):
    """
    >> create Project

    go to <etype>'s creation page
    """
    twc.go('view?etype=%s&vid=creation' % etype)

def edit(rql):
    """
    >> edit "Project P WHERE P eid 123"

    calls edition view for <rql>
    """
    twc.go('view?rql=%s&vid=edition' % quote(rql))




def setvalue(formname, fieldname, value):
    """
    >> setvalue entityForm name pylint

    sets the field's value in the form
    <forname> should either be the form's index, the form's name
    or the form's id
    """
    browser = twc.get_browser()
    form = browser.get_form(formname)
    if form is None:
        # try to find if one of the forms has <formname> as id
        for index, form in enumerate(browser._browser.forms()):
            # forms in cubicweb don't always have a name
            if form.attrs.get('id') == formname:
                # browser.get_form_field knows how to deal with form index
                formname = str(index+1)
                break
        else:
            raise ValueError('could not find form named <%s>' % formname)
    eid = browser.get_form_field(form, 'eid').value
    twc.formvalue(formname, '%s:%s' % (fieldname, eid), value)


def submitform(formname, submit_button=None):
    """
    >> submitform entityForm

    Submit the form named entityForm. This is useful when the form is pre-filed
    and we only want to click on submit.
    (The original submit command chooses the form to submit according to the last
    formvalue instruction)
    """
    browser = twc.get_browser()
    form = browser.get_form(formname)
    if form is None:
        # try to find if one of the forms has <formname> as id
        for form in browser._browser.forms():
            # forms in cubicweb don't always have a name
            if form.attrs.get('id') == formname:
                break
        else:
            raise ValueError('could not find form named <%s>' % formname)
    browser._browser.form = form
    browser.submit(submit_button)


# missing actions: delete, copy, changeview