aboutsummaryrefslogtreecommitdiff
path: root/wqflask/wqflask/model.py
diff options
context:
space:
mode:
authorLei Yan2013-11-05 17:56:52 +0000
committerLei Yan2013-11-05 17:56:52 +0000
commitf1e215c78f2d5889cad18d36c1c28623c924b768 (patch)
tree06623fe120c2b58570a713f9aac8c8a82504fae6 /wqflask/wqflask/model.py
parentcced8bc03e6b32f561e554d021710486ac138df0 (diff)
parentc8d5bd327f0a73d2b7ac1d9935fd2bb6f11cc262 (diff)
downloadgenenetwork2-f1e215c78f2d5889cad18d36c1c28623c924b768.tar.gz
Merge /home/zas1024/gene
Conflicts: wqflask/wqflask/templates/correlation_page.html
Diffstat (limited to 'wqflask/wqflask/model.py')
-rw-r--r--wqflask/wqflask/model.py150
1 files changed, 93 insertions, 57 deletions
diff --git a/wqflask/wqflask/model.py b/wqflask/wqflask/model.py
index 5c514bde..b508f18e 100644
--- a/wqflask/wqflask/model.py
+++ b/wqflask/wqflask/model.py
@@ -3,85 +3,112 @@ from __future__ import print_function, division, absolute_import
import uuid
import datetime
+import simplejson as json
+
from flask import request
from flask.ext.sqlalchemy import SQLAlchemy
-#from flask.ext.security import Security, SQLAlchemyUserDatastore, UserMixin, RoleMixin
-
-#from flask_security.forms import TextField
-#from flask_security.forms import RegisterForm
from wqflask import app
-from sqlalchemy import Column, Integer, String, Table, ForeignKey, Unicode, Boolean, DateTime, Text
+from sqlalchemy import (Column, Integer, String, Table, ForeignKey, Unicode, Boolean, DateTime,
+ Text, Index)
from sqlalchemy.orm import relationship, backref
from wqflask.database import Base, init_db
-# Create database connection object
-#db = SQLAlchemy(app)
-
-
-# Is this right? -Sam
-#from sqlalchemy.ext.declarative import declarative_base
-#Base = declarative_base()
-
-#@classmethod
-#def get(cls, key):
-# """Convenience get method using the primary key
-#
-# If record doesn't exist, returns None
-#
-# Allows the following: User.get('121')
-#
-# """
-# print("in get cls is:", cls)
-# print(" key is {} : {}".format(type(key), key))
-# query = Model.query(cls)
-# print("query is: ", query)
-# record = query.get(key)
-# return record
-#
-#
-#print("Model is:", vars(Model))
-#Model.get = get
+
# Define models
#roles_users = Table('roles_users',
# Column('user_id', Integer(), ForeignKey('user.the_id')),
# Column('role_id', Integer(), ForeignKey('role.the_id')))
-class Role(Base):
- __tablename__ = "role"
- id = Column(Unicode(36), primary_key=True, default=lambda: unicode(uuid.uuid4()))
- name = Column(Unicode(80), unique=True, nullable=False)
- description = Column(Unicode(255))
+#class Role(Base):
+# __tablename__ = "role"
+# id = Column(Unicode(36), primary_key=True, default=lambda: unicode(uuid.uuid4()))
+# name = Column(Unicode(80), unique=True, nullable=False)
+# description = Column(Unicode(255))
class User(Base):
__tablename__ = "user"
id = Column(Unicode(36), primary_key=True, default=lambda: unicode(uuid.uuid4()))
email_address = Column(Unicode(50), unique=True, nullable=False)
-
+
# Todo: Turn on strict mode for Mysql
password = Column(Text, nullable=False)
-
+
full_name = Column(Unicode(50))
organization = Column(Unicode(50))
-
+
active = Column(Boolean(), nullable=False, default=True)
registration_info = Column(Text) # json detailing when they were registered, etc.
-
+
confirmed = Column(Text) # json detailing when they confirmed, etc.
- #last_login_at = Column(DateTime())
- #current_login_at = Column(DateTime())
- #last_login_ip = Column(Unicode(39))
- #current_login_ip = Column(Unicode(39))
- #login_count = Column(Integer())
+ superuser = Column(Text) # json detailing when they became a superuser, otherwise empty
+ # if not superuser
+
+ logins = relationship("Login",
+ order_by="desc(Login.timestamp)",
+ lazy='dynamic', # Necessary for filter in login_count
+ foreign_keys="Login.user",
+ )
+
+ user_collections = relationship("UserCollection",
+ order_by="asc(UserCollection.name)",
+ )
+
+ @property
+ def name_and_org(self):
+ """Nice shortcut for printing out who the user is"""
+ if self.organization:
+ return "{} from {}".format(self.full_name, self.organization)
+ else:
+ return self.full_name
+
+ @property
+ def login_count(self):
+ return self.logins.filter_by(successful=True).count()
+
+
+ @property
+ def confirmed_at(self):
+ if self.confirmed:
+ confirmed_info = json.loads(self.confirmed)
+ return confirmed_info['timestamp']
+ else:
+ return None
+
+ @property
+ def superuser_info(self):
+ if self.superuser:
+ return json.loads(self.superuser)
+ else:
+ return None
+
+ @property
+ def crowner(self):
+ """If made superuser, returns object of person who did the crowning"""
+ if self.superuser:
+ superuser_info = json.loads(self.superuser)
+ crowner = User.query.get(superuser_info['crowned_by'])
+ return crowner
+ else:
+ return None
+
+ @property
+ def most_recent_login(self):
+ try:
+ return self.logins[0]
+ except IndexError:
+ return None
+
#roles = relationship('Role', secondary=roles_users,
# backref=backref('users', lazy='dynamic'))
+
class Login(Base):
__tablename__ = "login"
id = Column(Unicode(36), primary_key=True, default=lambda: unicode(uuid.uuid4()))
@@ -90,22 +117,31 @@ class Login(Base):
ip_address = Column(Unicode(39))
successful = Column(Boolean(), nullable=False) # False if wrong password was entered
session_id = Column(Text) # Set only if successfully logged in, otherwise should be blank
-
+
+ # Set to user who assumes identity if this was a login for debugging purposes by a superuser
+ assumed_by = Column(Unicode(36), ForeignKey('user.id'))
+
def __init__(self, user):
self.user = user.id
self.ip_address = request.remote_addr
-
-# Setup Flask-Security
-#user_datastore = SQLAlchemyUserDatastore(db, User, Role)
-#class ExtendedRegisterForm(RegisterForm):
-# name = TextField('name')
-# #print("name is:", name['_name'], vars(name))
-# organization = TextField('organization')
-#
-#security = Security(app, user_datastore, register_form=ExtendedRegisterForm)
+##################################################################################################
+class UserCollection(Base):
+ __tablename__ = "user_collection"
+ id = Column(Unicode(36), primary_key=True, default=lambda: unicode(uuid.uuid4()))
+ user = Column(Unicode(36), ForeignKey('user.id'))
-#user_datastore.create_role(name="Genentech", description="Genentech Beta Project(testing)")
+ # I'd prefer this to not have a length, but for the index below it needs one
+ name = Column(Unicode(50))
+ created_timestamp = Column(DateTime(), default=lambda: datetime.datetime.utcnow())
+ changed_timestamp = Column(DateTime(), default=lambda: datetime.datetime.utcnow())
+ members = Column(Text) # We're going to store them as a json list
+ # This index ensures a user doesn't have more than one collection with the same name
+ __table_args__ = (Index('usercollection_index', "user", "name"), )
+ @property
+ def num_members(self):
+ print("members are:", json.loads(self.members))
+ return len(json.loads(self.members))