view roundup/backends/sessions_rdbms.py @ 7752:b2dbab2b34bc

fix(refactor): multiple fixups using ruff linter; more testing. Converting to using the ruff linter and its rulesets. Fixed a number of issues. admin.py: sort imports use immutable tuples as default value markers for parameters where a None value is valid. reduced some loops to list comprehensions for performance used ternary to simplify some if statements named some variables to make them less magic (e.g. _default_savepoint_setting = 1000) fixed some tests for argument counts < 2 becomes != 2 so 3 is an error. moved exception handlers outside of loops for performance where exception handler will abort loop anyway. renamed variables called 'id' or 'dir' as they shadow builtin commands. fix translations of form _("string %s" % value) -> _("string %s") % value so translation will be looked up with the key before substitution. end dicts, tuples with a trailing comma to reduce missing comma errors if modified simplified sorted(list(self.setting.keys())) to sorted(self.setting.keys()) as sorted consumes whole list. in if conditions put compared variable on left and threshold condition on right. (no yoda conditions) multiple noqa: suppression removed unneeded noqa as lint rulesets are a bit different do_get - refactor output printing logic: Use fast return if not special formatting is requested; use isinstance with a tuple rather than two isinstance calls; cleaned up flow and removed comments on algorithm as it can be easily read from the code. do_filter, do_find - refactor output printing logic. Reduce duplicate code. do_find - renamed variable 'value' that was set inside a loop. The loop index variable was also named 'value'. do_pragma - added hint to use list subcommand if setting was not found. Replaced condition 'type(x) is bool' with 'isinstance(x, bool)' for various types. test_admin.py added testing for do_list better test coverage for do_get includes: -S and -d for multilinks, error case for -d with non-link. better testing for do_find including all output modes better testing for do_filter including all output modes fixed expected output for do_pragma that now includes hint to use pragma list if setting not found.
author John Rouillard <rouilj@ieee.org>
date Fri, 01 Mar 2024 14:53:18 -0500
parents fe0091279f50
children ee17f62c8341
line wrap: on
line source

"""This module defines a very basic store that's used by the CGI interface
to store session and one-time-key information.

Yes, it's called "sessions" - because originally it only defined a session
class. It's now also used for One Time Key handling too.
"""
__docformat__ = 'restructuredtext'
import time

from roundup.anypy.html import html_escape as escape
from roundup.backends.sessions_common import SessionCommon


class BasicDatabase(SessionCommon):
    ''' Provide a nice encapsulation of an RDBMS table.

        Keys are id strings, values are automatically marshalled data.
    '''
    name = None

    def __init__(self, db):
        self.db = db
        self.conn, self.cursor = self.db.sql_open_connection()

    def clear(self):
        self.cursor.execute('delete from %ss' % self.name)

    def exists(self, infoid):
        n = self.name
        self.cursor.execute('select count(*) from %ss where %s_key=%s' %
                            (n, n, self.db.arg), (infoid,))
        return int(self.cursor.fetchone()[0])

    _marker = []

    def get(self, infoid, value, default=_marker):
        n = self.name
        self.cursor.execute('select %s_value from %ss where %s_key=%s' %
                            (n, n, n, self.db.arg), (infoid,))
        res = self.cursor.fetchone()
        if not res:
            if default != self._marker:
                return default
            raise KeyError('No such %s "%s"' % (self.name, escape(infoid)))
        values = eval(res[0])
        return values.get(value, None)

    def getall(self, infoid):
        n = self.name
        self.cursor.execute('select %s_value from %ss where %s_key=%s' %
                            (n, n, n, self.db.arg), (infoid,))
        res = self.cursor.fetchone()
        if not res:
            raise KeyError('No such %s "%s"' % (self.name, escape(infoid)))
        return eval(res[0])

    def set(self, infoid, **newvalues):
        """ Store all newvalues under key infoid with a timestamp in database.

            If newvalues['__timestamp'] exists and is representable as
            a floating point number (i.e. could be generated by time.time()),
            that value is used for the <name>_time column in the database.
        """
        c = self.cursor
        n = self.name
        a = self.db.arg
        c.execute('select %s_value from %ss where %s_key=%s' %
                  (n, n, n, a), (infoid,))
        res = c.fetchone()

        timestamp = time.time()
        if res:
            values = eval(res[0])
        else:
            values = {}

        if '__timestamp' in newvalues:
            try:
                # __timestamp must be representable as a float. Check it.
                timestamp = float(newvalues['__timestamp'])
            except ValueError:
                if res:
                    # keep the original timestamp
                    del(newvalues['__timestamp'])
                else:
                    # here timestamp is the new timestamp
                    newvalues['__timestamp'] = timestamp
        values.update(newvalues)
        if res:
            sql = ('update %ss set %s_value=%s, %s_time=%s '
                   'where %s_key=%s' % (n, n, a, n, a, n, a))
            args = (repr(values), timestamp, infoid)
        else:
            sql = 'insert into %ss (%s_key, %s_time, %s_value) '\
                'values (%s, %s, %s)' % (n, n, n, n, a, a, a)
            args = (infoid, timestamp, repr(values))
        c.execute(sql, args)

    def list(self):
        c = self.cursor
        n = self.name
        c.execute('select %s_key from %ss' % (n, n))
        return [res[0] for res in c.fetchall()]

    def destroy(self, infoid):
        self.cursor.execute('delete from %ss where %s_key=%s' %
                            (self.name, self.name, self.db.arg), (infoid,))

    def updateTimestamp(self, infoid):
        """ don't update every hit - once a minute should be OK """
        now = time.time()
        self.cursor.execute('''update %ss set %s_time=%s where %s_key=%s '''
            '''and %s_time < %s''' %
                            (self.name, self.name, self.db.arg, self.name,
                             self.db.arg, self.name, self.db.arg),
                            (now, infoid, now-60))

    def clean(self):
        ''' Remove session records that haven't been used for a week. '''
        now = time.time()
        week = 60*60*24*7
        old = now - week
        self.cursor.execute('delete from %ss where %s_time < %s' %
                            (self.name, self.name, self.db.arg), (old, ))

    def commit(self):
        self.log_info('commit %s' % self.name)
        self.conn.commit()
        self.cursor = self.conn.cursor()

    def lifetime(self, item_lifetime=0):
        """Return the proper timestamp for a key with key_lifetime specified
           in seconds. Default lifetime is 0.
        """
        now = time.time()
        week = 60*60*24*7
        return now - week + item_lifetime

    def close(self):
        self.conn.close()


class Sessions(BasicDatabase):
    name = 'session'


class OneTimeKeys(BasicDatabase):
    name = 'otk'

# vim: set et sts=4 sw=4 :

Roundup Issue Tracker: http://roundup-tracker.org/