Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from app import app, db
- from datetime import datetime
- ROLE_USER = 0
- ROLE_ADMIN = 1
- BANNED = 1
- UNBANNED = 0
- # Create an auxiliary table
- # Because this is an auxiliary table, we do not make it as a model.
- suspect_list = db.Table('suspect_list',
- db.Column('suspect_id', db.Integer, db.ForeignKey('suspect.steam_id')),
- db.Column('list_id', db.Integer, db.ForeignKey('list.id'))
- )
- # We will have a user model for our orm that has an id, nickname, email, and role.
- # ID and role are integers, nickname and email are strings.
- # ID is the primary key of the database entry, and role signifies if the user is an administrator.
- class User(db.Model):
- id = db.Column(db.Integer, primary_key = True)
- nickname = db.Column(db.String(64), unique = True)
- email = db.Column(db.String(120), index = True, unique = True)
- role = db.Column(db.SmallInteger, default = ROLE_USER)
- lists = db.relationship('List', backref = 'author', lazy = 'dynamic')
- # In order to work with flask-login, we need to add some functions to the user class.
- # Misleadingly named, but generally returns true unless the user is not allowed to authenticate
- def is_authenticated(self):
- return True
- # Returns true unless the user is inactive due to being banned, suspended, etc.
- def is_active(self):
- return True
- # Returns false unless the user is not supposed to be logged into the server
- def is_anonymous(self):
- return False
- # Will return a unique identifier for the user according to our database.
- def get_id(self):
- return unicode(self.id)
- def __repr__(self):
- return '<User %r>' % (self.nickname)
- class Suspect(db.Model):
- steam_id = db.Column(db.Integer, primary_key=True)
- banned = db.Column(db.SmallInteger, default=UNBANNED)
- vanity_id = db.Column(db.String(120), index=True)
- persona_name = db.Column(db.String(120))
- def get_steam_id(self):
- return unicode(self.steam_id)
- def get_vanity_id(self):
- return self.vanity_id
- def get_persona_name(self):
- return self.persona_name
- def is_banned(self):
- if self.banned == BANNED:
- return BANNED
- else:
- return UNBANNED
- def __repr__(self):
- return '<Suspect %r>' % self.steam_id
- class List(db.Model):
- id = db.Column(db.Integer, primary_key=True)
- name = db.Column(db.String(20), unique=True)
- user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
- last_updated = db.Column(db.DateTime, default=datetime.utcnow())
- watching = db.relationship('Suspect',
- secondary=suspect_list,
- primaryjoin=(suspect_list.c.list_id == id),
- secondaryjoin=(suspect_list.c.suspect_id == Suspect.steam_id),
- backref=db.backref('suspect_list', lazy='dynamic'),
- lazy='dynamic')
- def get_suspects(self):
- new_query = Suspect.query.join(suspect_list).filter(suspect_list.c.list_id == self.id)
- print new_query
- return new_query
- def get_user_id(self):
- return self.user_id
- def last_updated(self):
- return self.last_updated
- def get_id(self):
- return self.id
- def add_suspect(self, suspect):
- self.watching.append(suspect)
- def __repr__(self):
- return '<List %r>' % self.id
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement