Пример #1
0
 def load_from_db (self, context):
     cursor = context.get_cursor()
     cursor.execute("""SELECT * FROM """ + self.TABLE_NAME + """
         WHERE ip_address = %s
         """, self.ip_address)
     fields = [d[0] for d in cursor.description]
     desc = dtuple.TupleDescriptor([[f] for f in fields])
     row = cursor.fetchone()
     if row:
         row = dtuple.DatabaseTuple(desc, row)
         for k, v in row.items():
             self.set(k, v) 
     else:
         context.logger.error('No %s with %s %s in local database' % (self.__class__.__name__,
             self.CACHE_ID_FIELD, getattr(self, self.CACHE_ID_FIELD)))
Пример #2
0
 def load (self, context):
     cursor = context.get_cursor()
     if self.uid:
         cursor.execute("""SELECT * FROM """ + self.TABLE_NAME +
             """
             WHERE uid LIKE %s
             """, (self.uid))
         fields = [d[0] for d in cursor.description]
         desc = dtuple.TupleDescriptor([[f] for f in fields])
         row = cursor.fetchone()
         if row:
             row = dtuple.DatabaseTuple(desc, row)
             for field in fields:
                 self.set(field, row[field])
         else:
             context.logger.debug('No %s "%s"', self.TABLE_NAME, self.uid)
             print 'No %s "%s"' % (self.TABLE_NAME, self.uid)
             raise 'InvalidId'
Пример #3
0
def get_users(context):
    """ Return all users.  """
    users = {}
    cursor = context.get_cursor()
    cursor.execute("""
        SELECT * 
        FROM users
        """)
    fields = [d[0] for d in cursor.description]
    desc = dtuple.TupleDescriptor([[f] for f in fields])
    rows = cursor.fetchall()
    for row in rows:
        user = User()
        row = dtuple.DatabaseTuple(desc, row)
        for field in fields:
            user.set(field, row[field])
        users[user.id] = user
    return users
Пример #4
0
    def load(self, context):

        cursor = context.get_cursor()

        if self.id:
            cursor.execute(
                """
                SELECT * 
                FROM users
                WHERE id LIKE %s
                """, (self.id))
            fields = [d[0] for d in cursor.description]
            desc = dtuple.TupleDescriptor([[f] for f in fields])
            row = cursor.fetchone()
            if row:
                row = dtuple.DatabaseTuple(desc, row)
                for field in fields:
                    self.set(field, row[field])
            else:
                self.logger.debug('No user "%s"', self.id)
                raise 'InvalidUserId'
        elif self.email:
            cursor.execute(
                """
                SELECT * 
                FROM users
                WHERE email LIKE %s
                """, (self.email))
            fields = [d[0] for d in cursor.description]
            desc = dtuple.TupleDescriptor([[f] for f in fields])
            row = cursor.fetchone()
            if row:
                row = dtuple.DatabaseTuple(desc, row)
                for field in fields:
                    self.set(field, row[field])
            else:
                self.logger.debug('No user "%s"', self.email)
                raise 'InvalidUserId'

        # Load records
        cursor.execute(
            """
            SELECT * 
            FROM user_records
            WHERE user_id = %s
            ORDER BY uid
            """, self.uid)
        fields = [d[0] for d in cursor.description]
        desc = dtuple.TupleDescriptor([[f] for f in fields])
        for row in cursor.fetchall():
            row = dtuple.DatabaseTuple(desc, row)
            self.records[row['record_id']] = UserRecord(
                uid=row['uid'],
                user_id=self.uid,
                record_id=row['record_id'],
                notes=row['notes'])

        # Load sets
        cursor.execute(
            """
            SELECT uid
            FROM user_sets
            WHERE user_id = %s
            ORDER BY LOWER(name)
            """, self.uid)
        fields = [d[0] for d in cursor.description]
        desc = dtuple.TupleDescriptor([[f] for f in fields])
        set_ids = []
        for row in cursor.fetchall():
            row = dtuple.DatabaseTuple(desc, row)
            set_ids.append(row['uid'])
        try:
            set_map = context.cache_get_multi(
                list('%s:%s' % (UserSet.CACHE_KEY, id) for id in set_ids))
            self.sets.extend(
                list(set_map['%s:%s' % (UserSet.CACHE_KEY, id)]
                     for id in set_ids))
        except:
            for id in set_ids:
                self.sets.append(UserSet(context, id))

        # Load record->set assignments
        for set in self.sets:
            cursor.execute(
                """
                SELECT record_id
                FROM user_set_records
                WHERE user_set_id = %s
                ORDER BY uid
                """, set.uid)
            fields = [d[0] for d in cursor.description]
            desc = dtuple.TupleDescriptor([[f] for f in fields])
            for row in cursor.fetchall():
                row = dtuple.DatabaseTuple(desc, row)
                # Save the id in the set's in-memory list of records
                set.records.append(row['record_id'])
                # Get the whole UserRecord
                rec = self.records[row['record_id']]
                # Save the UserRecord in the set map
                try:
                    self.record_set_map[set.uid].append(rec)
                except:
                    self.record_set_map[set.uid] = [rec]