audrid / audrid / models.py

Diff from to

audrid/models.py

 
 from audrid import (app, db)
 from audrid.utils import random_id, random_id_fun
+from audrid import validators as V
 import datetime
 import json
 
+# def create_pool(pool=None, user_id=None):
+#     """ Create a task (validate, persist and audit).
+#     """
+#     pool = V.pool(pool)
+#     try:
+#         doc = Document(id=pool['id'], body=json.dumps(pool))
+#         log = Log(
+#             document_id=pool['id'], 
+#             body=json.dumps(pool), 
+#             tag='pool_created', 
+#             user_id=user_id, 
+#             date=datetime.datetime.now())
+#         db.session.add(doc)
+#         db.session.flush()
+#         db.session.add(log)
+#         db.session.commit()
+#     except Exception as exc:
+#         db.session.rollback()
+#         raise exc
+
+# def update_pool(pool=None, user_id=None):
+#     """ Update a pool.
+#     """
+#     pool = V.pool(pool)
+#     doc = Document.query.filter_by(
+#         id=pool['id']).filter_by(
+#         deleted=False).first_or_404()
+#     try:
+#         doc.body = json.dumps(pool)
+#         log = Log(
+#             document_id=pool['id'], 
+#             body=json.dumps(pool), 
+#             tag='pool_updated', 
+#             user_id=user_id, 
+#             date=datetime.datetime.now())
+#         db.session.add(doc)
+#         db.session.flush()
+#         db.session.add(log)
+#         db.session.commit()
+#     except Exception as exc:
+#         # log error
+#         db.session.rollback()
+
+# def delete_pool(pid=None, user_id=None):
+#     """ Delete a pool.
+#     """
+#     doc = Document.query.filter_by(
+#         id=pid).filter_by(
+#         deleted=False).first_or_404()
+#     try:
+#         doc.deleted = True
+#         log = Log(
+#             document_id=pid, 
+#             body=json.dumps({}), 
+#             tag='pool_deleted', 
+#             user_id=user_id, 
+#             date=datetime.datetime.now())
+#         db.session.add(log)
+#         db.session.commit()
+#     except Exception as exc:
+#         # log error
+#         db.session.rollback()
+
+
 class User(db.Model):
     """ A application user.
     """
 
     id = db.Column(db.String(16), unique=True, nullable=False)
     body = db.Column(db.LargeBinary, nullable=False)
+    type = db.Column(db.String(32), nullable=False)
 
     deleted = db.Column(db.Boolean, nullable=False, default=False)
 
-    def __init__(self, id=None, body=None, deleted=False):
+    def __init__(self, id=None, body=None, type=None, deleted=False):
         """ Last exit database.
         """
         self.id = id or random_id()
         self.body = body
+        self.type = type
         self.deleted = deleted
 
     def __repr__(self):
-        return '<Document %s, length=%s>' % (self.id, len(self.body))
+        return '<Document %s, type=%s, length=%s, deleted=%s>' % (
+            self.id, self.type, len(self.body), self.deleted)
 
     def to_python(self):
         """ Return the python representation (dictionary) of this document.
         self.user_id = user_id
 
     def __repr__(self):
-        return '<Log %s <%s>>' % (self.added_id, self.date)
+        return '<Log %s, %s, %s, %s>' % (self.added_id, self.document_id, self.tag, self.date)
 
 class Exam(db.Model):
     """ Abstract exam.
     public = db.Column(db.Boolean, nullable=False, default=True)    
     secret_key = db.Column(db.String(32), nullable=False, default=random_id_fun(length=32))
 
+    # deleted 
+    deleted = db.Column(db.Boolean, nullable=False, default=False)
+
     def __init__(self, name=None, description=None, public=True, 
         pool_id=None, secret_key=None):
         self.name = name
         if self.secret_key:
             self.secret_key = secret_key
 
+    def as_dict(self):
+        return {
+            "id" : self.id,
+            "name" : self.name,
+            "description" : self.description,
+            "pool_id" : self.pool_id,
+            "public" : self.public,
+            "secret_key" : self.secret_key,
+            "deleted" : self.deleted
+        }
+
     def __repr__(self):
         return '<Exam %s, %s>' % (self.id, self.name)
 
 
     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)
+    # accumulate answers here
+    trial_id = db.Column(db.String(16), db.ForeignKey('document.id'), nullable=False)
 
-    started = db.Column(db.DateTime(), nullable=False)
+    # start, end date and duration
+    started = db.Column(db.DateTime(), nullable=True)
     finished = db.Column(db.DateTime(), nullable=True, default=None)
+    duration = db.Column(db.Integer, nullable=False, default=60) # in minutes
+
+    status = db.Column(db.String(32), nullable=False, default='registered')
 
-    def __init__(self, user_id=None, exam_id=None, audit_id=None, started=None):
+    def __init__(self, user_id=None, exam_id=None, trial_id=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
+        self.trial_id = trial_id
 
     def __repr__(self):
-        return '<Audit %s, %s>' % (self.id, self.started)
-
+        return '<Audit %s, %s, %s>' % (self.id, self.status, self.trial_id)
+
+    def as_dict(self):
+        return {
+            "user_id" : self.user_id,
+            "exam_id" : self.exam_id,
+            "trial_id" : self.trial_id,
+            "started" : self.started,
+            "finished" : self.finished,
+            "duration" : self.duration,
+            "status" : self.status
+        }
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.