Advertisement
shadefinale

Models.py

Oct 24th, 2014
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.48 KB | None | 0 0
  1. from app import app, db
  2. from datetime import datetime
  3. ROLE_USER = 0
  4. ROLE_ADMIN = 1
  5.  
  6. BANNED = 1
  7. UNBANNED = 0
  8. # Create an auxiliary table
  9. # Because this is an auxiliary table, we do not make it as a model.
  10. suspect_list = db.Table('suspect_list',
  11.                      db.Column('suspect_id', db.Integer, db.ForeignKey('suspect.steam_id')),
  12.                      db.Column('list_id', db.Integer, db.ForeignKey('list.id'))
  13. )
  14.  
  15.  
  16. # We will have a user model for our orm that has an id, nickname, email, and role.
  17. # ID and role are integers, nickname and email are strings.
  18. # ID is the primary key of the database entry, and role signifies if the user is an administrator.
  19. class User(db.Model):
  20.     id = db.Column(db.Integer, primary_key = True)
  21.     nickname = db.Column(db.String(64), unique = True)
  22.     email = db.Column(db.String(120), index = True, unique = True)
  23.     role = db.Column(db.SmallInteger, default = ROLE_USER)
  24.  
  25.     lists = db.relationship('List', backref = 'author', lazy = 'dynamic')
  26.  
  27.     # In order to work with flask-login, we need to add some functions to the user class.
  28.  
  29.     # Misleadingly named, but generally returns true unless the user is not allowed to authenticate
  30.     def is_authenticated(self):
  31.         return True
  32.  
  33.     # Returns true unless the user is inactive due to being banned, suspended, etc.
  34.     def is_active(self):
  35.         return True
  36.  
  37.     # Returns false unless the user is not supposed to be logged into the server
  38.     def is_anonymous(self):
  39.         return False
  40.  
  41.     # Will return a unique identifier for the user according to our database.
  42.     def get_id(self):
  43.         return unicode(self.id)
  44.  
  45.     def __repr__(self):
  46.         return '<User %r>' % (self.nickname)
  47.  
  48.  
  49. class Suspect(db.Model):
  50.     steam_id = db.Column(db.Integer, primary_key=True)
  51.     banned = db.Column(db.SmallInteger, default=UNBANNED)
  52.     vanity_id = db.Column(db.String(120), index=True)
  53.     persona_name = db.Column(db.String(120))
  54.  
  55.     def get_steam_id(self):
  56.         return unicode(self.steam_id)
  57.  
  58.     def get_vanity_id(self):
  59.         return self.vanity_id
  60.  
  61.     def get_persona_name(self):
  62.         return self.persona_name
  63.  
  64.     def is_banned(self):
  65.         if self.banned == BANNED:
  66.             return BANNED
  67.         else:
  68.             return UNBANNED
  69.  
  70.     def __repr__(self):
  71.         return '<Suspect %r>' % self.steam_id
  72.  
  73.  
  74. class List(db.Model):
  75.     id = db.Column(db.Integer, primary_key=True)
  76.     name = db.Column(db.String(20), unique=True)
  77.     user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
  78.     last_updated = db.Column(db.DateTime, default=datetime.utcnow())
  79.  
  80.     watching = db.relationship('Suspect',
  81.                                secondary=suspect_list,
  82.                                primaryjoin=(suspect_list.c.list_id == id),
  83.                                secondaryjoin=(suspect_list.c.suspect_id == Suspect.steam_id),
  84.                                backref=db.backref('suspect_list', lazy='dynamic'),
  85.                                lazy='dynamic')
  86.  
  87.     def get_suspects(self):
  88.         new_query = Suspect.query.join(suspect_list).filter(suspect_list.c.list_id == self.id)
  89.         print new_query
  90.         return new_query
  91.  
  92.     def get_user_id(self):
  93.         return self.user_id
  94.  
  95.     def last_updated(self):
  96.         return self.last_updated
  97.  
  98.     def get_id(self):
  99.         return self.id
  100.  
  101.     def add_suspect(self, suspect):
  102.         self.watching.append(suspect)
  103.  
  104.     def __repr__(self):
  105.         return '<List %r>' % self.id
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement