예제 #1
0
 def test_form_elements(self):
     """There won't be a lot gained from testing the actual output of these
     functions so what we're going to do is ensure we get _some_ output
     and that we don't cause an exception"""
     
     self.test_targets.extend([
         html_f.check_box,
         html_f.doubleclick_text,
         html_f.doubleclick_text_full,
         html_f.live_option_box,
         html_f.option_box,
         html_f.text_box,
         html_f.textarea,
     ])
     
     # check_box(name, checked=False, custom_id=''):
     vals = itertools.product(
         ["name1", "name2"],
         [True, False],
         ["", "custom_id"],
     )
     
     for args in vals:
         self.assertNotEqual(html_f.check_box(*args), "")
     
     # doubleclick_text(table, field, where_id, value, label_style="", size=10):
     vals = itertools.product(
         ["table1", ""],
         ["field1", ""],
         [0, 1, 2],
         ["value1", 5],
         ["", "font-weight:bold;"],
         [0, 5, 10],
     )
     
     for args in vals:
         self.assertNotEqual(html_f.doubleclick_text(*args), "")
     
     # doubleclick_text_full(table, field, where_field, value, label_style="", size=10):
     vals = itertools.product(
         ["table1", ""],
         ["field1", ""],
         ["id=5", "name='x'"],
         [0, 1, "X"],
         ["", "font-weight:bold;"],
         [0, 5, 10],
     )
     
     for args in vals:
         self.assertNotEqual(html_f.doubleclick_text_full(*args), "")
     
     # live_option_box(elements, table, field, where_id, value, tab_index = -1, style="", disabled=[], element_property="name", sort=True):
     vals = itertools.product(
         [{"1":"a","2":"b","3":"c"}],
         ["table1", ""],
         ["field1", ""],
         [0, 1, 2],
         ["value1", 5],
         [-1, 10, 1000000],
         ["", "font-weight:bold;"],
         [[1,2,3], []],
         ["name", "property-name"],
         [True, False],
     )
     
     for args in vals:
         self.assertNotEqual(html_f.live_option_box(*args), "")
     
     # option_box(name, elements, selected="", tab_index = -1, custom_id="<>", onchange="", style="", disabled=[], insert_dud=False, element_property="name", sort=True):
     vals = itertools.product(
         ["name1", ""],
         [{"1":"a","2":"b","3":"c"}],
         ["selected", ""],
         [-1, 10, 1000000],
         ["custom_id", "<>", ""],
         ["onchange", ""],
         ["style", ""],
         [[1,2,3], []],
         ["Insert dud", False],
         ["", "element property"],
         [True, False],
     )
     
     for args in vals:
         self.assertNotEqual(html_f.option_box(*args), "")
     
     # text_box(name, text='', size=15, tabIndex=-1, onchange='', custom_id='<>', style="", warn_on=None, disabled=False):
     vals = itertools.product(
         ["name1", ""],
         ["", "some text"],
         [0, 1, 15, 2000],
         [-1, 10, 1000000],
         ["onchange", ""],
         ["custom_id", "<>", ""],
         ["style", ""],
         [None],# Needs to be a function or none
         [True, False],
     )
     
     for args in vals:
         self.assertNotEqual(html_f.text_box(*args), "")
     
     # textarea(name, text='', rows=6, cols=30, tabIndex=-1, onchange='', custom_id='<>', style="", warn_on=None):
     vals = itertools.product(
         ["name1", ""],
         ["", "some text"],
         [0, 6, 100],
         [0, 30, 1000],
         [-1, 30, 1000],
         ["onchange", ""],
         ["custom_id", "<>", ""],
         ["style", ""],
         [None],# Needs to be a function or none
     )
     
     for args in vals:
         self.assertNotEqual(html_f.textarea(*args), "")
예제 #2
0
def show_form(cursor, user_id):
    user_id = int(common_f.get_val('user', user_id))
    the_user = common_q.get_one(cursor, user.User, id=user_id)
    
    if the_user == None:
        page_data["Padding"] = 0
        return """&nbsp;
        <div class='error'>
            No user selected, listing all users instead.
        </div>
        {}""".format(list_users.main(cursor))
    
    permissions = []
    i = 1
    for p in user.permission_fields:
        if p == "root": continue
        
        # You can't edit attributes you don't have
        if not getattr(common_f.cache['user'], p) and not common_f.cache['user'].root:
            continue
        
        i += 1
        
        permissions.append("""
        <tr class="row{row}">
            <td><label for="{name}">{name}</label></td>
            <td>{value}</td>
        </tr>""".format(
            row   = i % 2,
            name  = p,
            value = html_f.check_box(p, getattr(the_user, p), custom_id=p),
        ))
    
    output = []
    output.append("""
    <form action="web.py" method="post" accept-charset="utf-8">
        <input type="hidden" name="mode" id="mode" value="edit_user" />
        <input type="hidden" name="sub_mode" value="commit" />
        <input type="hidden" name="id" value="{user_id}" />
        <input type="hidden" name="salt" value="{salt}" />
        {root}
        
        Editing: {name_text}
        <br /><br />
        
        <table border="0" cellspacing="5" cellpadding="5">
            <tr>
                <td><label for="password">New password:</label></td>
                <td style="padding: 1px;"><input type="password" name="password" id="password" value="" /></td>
                
                <td width="5">&nbsp;</td>
                
                <td><label for="password2">Confirm password:</label></td>
                <td style="padding: 1px;"><input type="password" name="password2" id="password2" value="" /></td>
            </tr>
            <tr>
                <td colspan="2">
                    <table border="0" cellspacing="0" cellpadding="5">
                        <tr class="row2">
                            <th>Permission</th>
                            <th>Value</th>
                        </tr>
                        {permissions}
                    </table>
                </td>
            </tr>
        </table>
        <br />
        <input type="submit" value="Perform edit" />
    </form>
    <form id="delete_form" action="web.py" method="post" accept-charset="utf-8">
        <input type="hidden" name="user" value="{user_id}" />
        <input type="hidden" name="mode" value="edit_user" />
        <input type="hidden" name="sub_mode" value="delete" />
        <input style="float:right; margin-right:100px;" type="button" value="Delete user" onclick="var answer = confirm('Delete {name_safe}?')
        if (answer) $('#delete_form').submit();" />
    </form>
    {onload}
    <br /><br />""".format(
        user_id     = user_id,
        name_text   = html_f.text_box("name", the_user.username, size=20, custom_id="user_name"),
        
        name_safe   = html_f.js_name(the_user.username),
        onload      = html_f.onload % "$('#user_name').focus();",
        root        = '<input type="hidden" name="root" value="True" />' if the_user.root else "",
        salt        = the_user.salt,
        
        permissions = "".join(permissions),
    ))
    
    page_data['Title'] = "Edit user ({})".format(the_user.username)
    return "".join(output)