Example #1
0
 def ttest_queries(self):
     cursor = database_f.get_test_cursor()
     
     self.test_targets.extend([common_q._make_query, common_q.id_list, common_q.get_one,
         common_q.get_all, common_q.get_where, common_q.get_last])
     
     # ID List
     self.assertEqual(common_q.id_list(cursor, error.Error), [1,2,3,4,5])
     
     # All
     self.assertEqual(len(common_q.get_all(cursor, error.Error)), 5)
     self.assertEqual(type(common_q.get_all(cursor, error.Error, where="id=1")[1]), error.Error)
     self.assertEqual(len(common_q.get_all(cursor, error.Error, where="id>3")), 2)
     
     # One
     result = common_q.get_one(cursor, error.Error, id=1)
     fake = error.Error({
         "id":               1,
         "timestamp":        1000,
         "args":             "a=1",
         "mode":             "list_users",
         "user_id":          1,
         "exception_type":   "Exception",
         "traceback":        "traceback",
     })
     
     if result != fake:
         print(result.compare(fake))
         self.assertEqual(result, fake)
     
     # Where
     self.assertEqual(
         common_q.get_all(cursor, error.Error, where='"timestamp" = 3000'),
         common_q.get_where(cursor, error.Error, timestamp=3000)
     )
     
     # Last
     self.assertEqual(
         common_q.get_all(cursor, error.Error, where='id = 5')[5],
         common_q.get_last(cursor, error.Error)
     )
Example #2
0
def show_form(cursor):
    return """
    <form action="web.py" method="post" accept-charset="utf-8" style="padding:10px;">
        <input type="hidden" name="mode" id="mode" value="emulate_user" />
        
        <table border="0" cellspacing="0" cellpadding="5">
            <tr>
                <td><label for="user_id">User:</label></td>
                <td>{user_id}</td>
            </tr>
            <tr>
                <td><label for="emulate_mode">Mode:</label></td>
                <td><input type="text" name="emulate_mode" id="emulate_mode" value="" /></td>
            </tr>
            <tr>
                <td colspan="2">
                    <input type="submit" value="Emulate" style="width:95%;" />
                </td>
            </tr>
        </table>
    </form>
    """.format(
        user_id = html_f.option_box("user_id", common_q.get_all(cursor, user.User), element_property="username"),
    )
Example #3
0
def main(cursor, query_filter=""):
    query_filter = common_f.get_val("filter", query_filter)

    if query_filter == "":
        log_dict = common_q.get_all(cursor, user_log.UserLog, orderby="access_time DESC")

    elif query_filter == "today":
        today = datetime.date.today()
        today = time.mktime(today.timetuple())
        log_dict = common_q.get_all(
            cursor, user_log.UserLog, where="access_time >= %d" % today, orderby="access_time DESC"
        )

    else:
        log_dict = common_q.get_all(
            cursor, user_log.UserLog, where="mode='%s'" % database_f.escape(query_filter), orderby="access_time DESC"
        )

    output = []

    if len(log_dict) > 0:
        user_dict = common_q.get_all(cursor, user.User)
        user_dict[-1] = user.User(username="******")
        output.append(
            """<table border="0" cellspacing="0" cellpadding="5" style="width:100%;">
            <tr class="row2">
                <th>Date</th>
                <th>Mode</th>
                <th>User</th>
                <th>Load time</th>
                <th colspan="2">&nbsp;</th>
            </tr>
            """
        )

        i = 1
        for log_id, the_log in log_dict.items():
            i += 1

            the_date = the_log.access_time

            output.append(
                """
            <tr class="row{row}">
                <td>{date}</td>
                <td>{page}</td>
                <td>{user}</td>
                <td>{load_time}</td>
                <td class="block_cell"><a href="web.py?mode=edit_log&amp;log={log_id}">View</a></td>
                <td class="block_cell"><a href="web.py?mode=edit_log&amp;log={log_id}&amp;sub_mode=delete">Delete</a></td>
            </tr>""".format(
                    log_id=log_id,
                    row=i % 2,
                    load_time=round(the_log.load_time, 4),
                    date=common_f.display_date(the_date, "%d of %B at %H:%M"),
                    page="No mode specified" if the_log.page == "" else the_log.page,
                    user=user_dict[the_log.user_id].username,
                )
            )

        output.append("</table>")

    else:
        output.append("<div style='padding:10px;'>No logs found</div>")

    return "".join(output)
Example #4
0
def main(cursor, query_filter=""):
    query_filter = common_f.get_val("filter", query_filter)

    if query_filter == "":
        error_dict = common_q.get_all(cursor, error.Error, where="fixed = False", orderby="timestamp DESC")

    elif query_filter == "today":
        today = datetime.date.today()
        today = time.mktime(today.timetuple())
        error_dict = common_q.get_all(
            cursor, error.Error, where="timestamp >= %d AND fixed = False" % today, orderby="timestamp DESC"
        )

    else:
        error_dict = common_q.get_all(
            cursor,
            error.Error,
            where="mode='%s' and fixed = False" % database_f.escape(query_filter),
            orderby="timestamp DESC",
        )

    output = []

    if len(error_dict) > 0:
        user_dict = common_q.get_all(cursor, user.User)
        user_dict[-1] = user.User(username="******")
        output.append(
            """<table border="0" cellspacing="0" cellpadding="5" style="width:100%;">
            <tr class="row2">
                <th>Date</th>
                <th>Mode</th>
                <th>Func Call</th>
                <th>Type</th>
                <th>User</th>
                <th colspan="2">&nbsp;</th>
            </tr>
            """
        )

        i = 1
        for error_id, the_error in error_dict.items():
            i += 1

            the_date = the_error.timestamp

            output.append(
                """
            <tr class="row{row}" id="row{error_id}">
                <td>{date}</td>
                <td>{mode}</td>
                <td>{function_call}</td>
                <td>{etype}</td>
                <td>{user}</td>
                <td class="block_cell"><a href="web.py?mode=edit_error&amp;error={error_id}">View</a></td>
                <td class="block_cell"><a href="#" onclick="{onclick}">Fix</a></td>
            </tr>""".format(
                    error_id=error_id,
                    row=i % 2,
                    etype=the_error.exception_type,
                    date=common_f.display_date(the_date, "%d of %B at %H:%M"),
                    mode="No mode specified" if the_error.mode == "" else the_error.mode,
                    function_call="" if the_error.function_call == "" else the_error.function_call,
                    user=user_dict[the_error.user_id].username,
                    onclick="""$('#ajax_target').load('web.py', {'mode':'edit_error', 'error':'%d', 'sub_mode':'fix'}); $('#row%d').hide(); return false;"""
                    % (error_id, error_id),
                )
            )

        output.append("</table>")

    else:
        output.append("<div style='padding:10px;'>No errors found</div>")

    modes = {}

    # Select all the groups possible
    query = """SELECT mode FROM errors GROUP BY mode"""
    try:
        cursor.execute(query)
    except Exception as e:
        raise Exception("Database error: %s\nQuery: %s" % (str(e.args[0]).replace("\n", ""), query))
    for row in cursor:
        modes[row["mode"]] = row["mode"]

    page_data["Rows"] = len(error_dict)
    page_data["Title"] = "Error list (%d)" % len(error_dict)
    page_data[
        "Filters"
    ] = """
        <form action="web.py" method="get" accept-charset="utf-8" style="float: left;">
            <a href="web.py?mode=list_errors">All errors</a>
            <a href="web.py?mode=list_errors&amp;filter=today">Todays errors</a>
            
            &nbsp;&nbsp;&nbsp;
            
            <input type="hidden" name="mode" value="list_errors" />
            
            {}
            
            <input type="submit" value="Sort by mode" />
        </form>
    """.format(
        html_f.option_box("filter", modes, selected=query_filter)
    )

    return "".join(output)
Example #5
0
def main_shortform(cursor):
    output = []
    
    user_dict = common_q.get_all(cursor, user.User)
    
    output.append("""
    <table border="0" cellspacing="0" cellpadding="5" style="width: 100%;">
        <tr class="row2">
            <th>Name</th>
            <th>Blocked</th>
            <th>Privilages</th>
            <th>&nbsp;</th>
        </tr>""")
        
    count = -1
    for i, u in user_dict.items():
        count += 1
        
        priviliages = []
        for p in user.permission_fields:
            if p == "root": continue
            if getattr(u, p):
                priviliages.append("&#10004;")
            else:
                priviliages.append("&nbsp;")
        
        output.append("""
        <tr class="row{row}" id="{user_id}">
            <td><strong>{username}</strong></td>
            <td>{blocked}</td>
            <td>{privileges}</td>
            <td class="block_cell"><a href="web.py?mode=edit_user&amp;user={user_id}">Edit</a></td>
        </tr>
        """.format(
            row         = count % 2,
            user_id     = u.id,
            username    = u.username,
            blocked     = "True" if u.blocked else "",
            privileges = "&nbsp;".join(priviliages)
        ))
    
    # Add new user
    count += 1
    output.append("""
    <tr class="row{row}">
        <td>
            <form action="web.py" id="add_user_form" method="post" accept-charset="utf-8">
            <input type="hidden" name="mode" value="edit_user" />
            <input type="hidden" name="sub_mode" value="add" />
            
            <input type="text" id="new_name" name="username" value="" />
        </td>
        <td>
            <input type="password" name="password" id="password" value="" size="10"/>
            <input type="password" name="password2" id="password2" value="" size="10"/>
            <input type="hidden" name="salt" value="{salt}" />
        </td>
        <td>&nbsp;</td>
        <td>
            <input type="submit" value="Add" />
            </form>
        </td>
    </tr>
    """.format(
        row     = count % 2,
        salt    = user.make_salt().replace('"', "'"),
        colspan = len(user.permission_fields)-1,
    ))
    
    output.append("</table>")
    
    if common_f.get_val('ajax', False):
        output.append("<br /><br />")
    else:
        # output.append(html_f.onload % "$('#new_name').focus();")
        pass
    
    return "".join(output)