class Result(db.Model): """Represent an official result from an election for a party.""" id = db.Column(db.Integer, primary_key=True) votes = db.Column(db.Integer) pct = db.Column(db.Float, nullable=False) is_seated = db.Column(db.Boolean, default=False) is_mandated = db.Column(db.Boolean, default=False) source_url = db.Column(db.String(256), nullable=False) source_name = db.Column(db.String(256), nullable=True) # Is there a position for this result in the corresponding wom? wom = db.Column(db.Boolean, default=True) # How the name of the party was written for this election party_repr = db.Column(db.String(256), nullable=False) party_name = db.Column(db.String(32), db.ForeignKey("party.name"), nullable=False) party = db.relationship("Party", backref=db.backref("results", lazy=True)) election_id = db.Column(db.Integer, db.ForeignKey("election.id"), nullable=False) election = db.relationship("Election", backref=db.backref("results", lazy=False))
class Position(db.Model): """Represent a party's position towards a thesis.""" id = db.Column(db.Integer, primary_key=True) value = db.Column(db.Integer, nullable=False) text = db.Column(db.Text) party_name = db.Column(db.String(32), db.ForeignKey("party.name"), nullable=False) party = db.relationship("Party", backref=db.backref("positions", lazy=True)) thesis_id = db.Column(db.String(10), db.ForeignKey("thesis.id"), nullable=False) thesis = db.relationship("Thesis", backref=db.backref("positions", lazy=False)) def __repr__(self): return "<Position {}/{}: {}>".format(self.thesis_id, self.party_name, self.value) def to_dict(self): rv = {"value": self.value, "party": self.party_name} if self.text is not None: rv["text"] = self.text return rv
class Fleet(db.Model): id = db.Column(db.Integer, autoincrement=True, nullable=False, primary_key=True) acf_type = db.Column(db.Integer, db.ForeignKey('aircraft_types.id'), nullable=False) reg_number = db.Column(db.String(10), nullable=False) state = db.Column(db.Integer, default=0) #0 - free, 1 - blocked, 2 - enroute user_id = db.Column(db.Integer, db.ForeignKey('user.vid')) squadron_id = db.Column(db.Integer, db.ForeignKey('squadron.id')) location = db.Column(db.Integer, db.ForeignKey('airports.id'))
class AuthTokens(db.Model): vid = db.Column(db.Integer, db.ForeignKey('user.vid'), nullable=False, primary_key=True) token = db.Column(db.String(120), unique=True, nullable=False) token_expires = db.Column(db.DATETIME, nullable=False)
class User(db.Model): vid = db.Column(db.Integer, primary_key=True, autoincrement=False) country = db.Column(db.String(3), unique=False, nullable=True) division = db.Column(db.String(3), unique=False, nullable=True) firstname = db.Column(db.String(100), unique=False, nullable=False) lastname = db.Column(db.String(100), unique=False, nullable=False) ratingpilot = db.Column(db.Integer, unique=False, nullable=True) email = db.Column(db.String(120), unique=True, nullable=True) password = db.Column(db.String(120), unique=False, nullable=True) location = db.Column(db.Integer, db.ForeignKey('airports.id')) def __repr__(self): return '<User %r>' % self.vid def register_user(self): if self.vid and self.firstname and self.lastname \ and self.country and self.division and self.password: try: db.session.add(self) db.session.commit() except exc.IntegrityError: return { "success": False, "message": "User with same parameters are already exists" } else: return { "success": False, "message": "Expected vid, firstname, lastname, country, division and password" } return {"success": True} def generate_auth_token(self): if self.vid: at = AuthTokens.query.filter( AuthTokens.vid == self.vid, AuthTokens.token_expires > datetime.now()).first() if not at: at = AuthTokens(vid=self.vid, token=str(uuid4()), token_expires=datetime.now() + timedelta(hours=1)) db.session.add(at) db.session.commit() return at.token else: return False
class Documents(db.Model): id = db.Column(db.Integer, primary_key=True, autoincrement=True) name = db.Column(db.String(200), nullable=False) lang = db.Column(db.String(2), nullable=False) body = db.Column(db.TEXT, nullable=True) author = db.Column(db.Integer, db.ForeignKey('user.vid'), nullable=False) def as_dict(self): return {c.name: getattr(self, c.name) for c in self.__table__.columns} def add_doc(self, user, data): try: self.author = user.vid self.name = data['name'] self.lang = data['lang'] self.body = data['body'] db.session.add(self) db.session.commit() return {"success": True} except exc.IntegrityError: return { "success": False, "message": "Some problem with add document" } def del_doc(self): try: db.session.delete(self) db.session.commit() return {"success": True} except exc.IntegrityError: return {"success": False, "message": "No documents found"} def save_doc(self, body): try: self.body = body['content'] db.session.commit() return {"success": True} except exc.IntegrityError: return {"success": False, "message": "No documents found"}
#!/usr/bin/env python # -*- coding: utf-8 -*- from collections import defaultdict from slugify import slugify from services import db tags = db.Table( "tags", db.Column("tag_title", db.String(128), db.ForeignKey("tag.title"), primary_key=True), db.Column("thesis_id", db.String(10), db.ForeignKey("thesis.id"), primary_key=True), ) class Tag(db.Model): """Represent a tag linked to a Wikidata ID.""" aliases = db.Column(db.Text) description = db.Column(db.Text) labels = db.Column(db.Text) title = db.Column(db.String(128), primary_key=True) slug = db.Column(db.String(128), unique=True, nullable=False) url = db.Column(db.Text) wikidata_id = db.Column(db.String(16)) wikipedia_title = db.Column(db.String(256))
class Squadron(db.Model): id = db.Column(db.Integer, primary_key=True, autoincrement=True) name = db.Column(db.String(120), nullable=False) commander = db.Column(db.Integer, db.ForeignKey('user.vid'), nullable=True)