audrid / audrid / models.py

# coding: utf-8

"""
Database mappings.
"""

from audrid import (app, db)
from audrid.utils import random_id, random_id_fun
import datetime
import json

class User(db.Model):
    """ A application user.
    """
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(80), unique=True, nullable=False)
    password = db.Column(db.String(80), nullable=False)
    
    email = db.Column(db.String(120), unique=True, nullable=False)
    group = db.Column(db.String(120), nullable=False)

    active = db.Column(db.Boolean, nullable=False, default=True)

    def __init__(self, username=None, email=None, password=None, group='guest', active=True):
        self.username = username
        self.password = password
        self.email = email
        self.group = group
        self.active = active

    def __repr__(self):
        return '<User %s, %s>' % (self.username, self.email)


class Document(db.Model):
    """ A document. Can be a pool or an audit.
    """
    added_id = db.Column(db.Integer, primary_key=True)

    id = db.Column(db.String(16), unique=True, nullable=False)
    body = db.Column(db.LargeBinary, nullable=False)

    deleted = db.Column(db.Boolean, nullable=False, default=False)

    def __init__(self, id=None, body=None, deleted=False):
        """ Last exit database.
        """
        self.id = id or random_id()
        self.body = body
        self.deleted = deleted

    def __repr__(self):
        return '<Document %s, length=%s>' % (self.id, len(self.body))

    def to_python(self):
        """ Return the python representation (dictionary) of this document.
        """
        return json.loads(self.body)

class Log(db.Model):
    """ The main audit log.
    """
    added_id = db.Column(db.Integer, primary_key=True)

    document_id = db.Column(db.String(16), db.ForeignKey('document.id'), nullable=False)
    body = db.Column(db.LargeBinary)
    
    # what, when, who
    tag = db.Column(db.String(32), nullable=False)
    date = db.Column(db.DateTime, nullable=False)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)

    def __init__(self, document_id=None, body=None, 
            date=None, tag=None, user_id=None):
        self.document_id = document_id
        self.body = body
        self.tag = tag
        self.date = date
        self.user_id = user_id

    def __repr__(self):
        return '<Log %s <%s>>' % (self.added_id, self.date)

class Exam(db.Model):
    """ Abstract exam.
    """
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(128), nullable=False, unique=True)
    description = db.Column(db.Text())

    # The root pool
    pool_id = db.Column(db.String(16), db.ForeignKey('document.id'), nullable=False)
    
    public = db.Column(db.Boolean, nullable=False, default=True)    
    secret_key = db.Column(db.String(32), nullable=False, default=random_id_fun(length=32))

    def __init__(self, name=None, description=None, public=True, 
        pool_id=None, secret_key=None):
        self.name = name
        self.description = description
        self.public = public
        self.pool_id = pool_id
        if self.secret_key:
            self.secret_key = secret_key

    def __repr__(self):
        return '<Exam %s, %s>' % (self.id, self.name)

class Audit(db.Model):
    """ Connects a user and an exam. E.g. 'Geography 1' to user 12.
    """
    id = db.Column(db.Integer, primary_key=True)

    user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
    exam_id = db.Column(db.Integer, db.ForeignKey('exam.id'), nullable=False)
    audit_id = db.Column(db.String(16), db.ForeignKey('document.id'), nullable=False)

    started = db.Column(db.DateTime(), nullable=False)
    finished = db.Column(db.DateTime(), nullable=True, default=None)

    def __init__(self, user_id=None, exam_id=None, audit_id=None, started=None):
        self.user_id = user_id
        self.exam_id = exam_id
        self.audit_id = audit_id
        if started == None:
            self.started = datetime.datetime.now()
        else:
            self.started = started

    def __repr__(self):
        return '<Audit %s, %s>' % (self.id, self.started)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.