Beispiel #1
0
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
Beispiel #2
0
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)
Beispiel #3
0
class Model(db.Model, CustomModel):
    __tablename__ = 'model'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    model_name = db.Column(db.String(128), nullable=False)
    api_endpoint = db.Column(db.String(32), nullable=False)

    def __init__(self, model_name, api_endpoint):
        self.model_name = model_name
        self.api_endpoint = api_endpoint
Beispiel #4
0
class Party(db.Model):
    """Represent a party electable in one of the elections."""

    name = db.Column(db.String(32), primary_key=True)
    long_name = db.Column(db.Text)

    def __repr__(self):
        return "<Party {}>".format(self.name)

    def to_dict(self):
        return {"name": self.name, "long_name": self.long_name}
Beispiel #5
0
class Election(db.Model):
    """Represent an election for which WOM data exists."""

    id = db.Column(db.Integer, primary_key=True)
    date = db.Column(db.DateTime)
    territory = db.Column(db.String(32), nullable=False)
    title = db.Column(db.String(128), nullable=False)
    wikidata_id = db.Column(db.String(16))
    wikipedia_title = db.Column(db.Text)
    source = db.Column(db.Text)
    preliminary = db.Column(db.Boolean, default=False)

    def __repr__(self):
        prelim = " (preliminary)" if self.preliminary else ""
        return "<Election {}: {}{}>".format(self.id, self.title, prelim)

    def to_dict(self, thesis_data=False):
        rv = {
            "id": self.id,
            "date": dt_string(self.date),
            "results": self.result_dict(),
            "source": self.source,
            "results_source": {
                "url": self.results[0].source_url
            },
            "territory": self.territory,
            "title": self.title,
            "wikidata_id": self.wikidata_id,
            "wikipedia_title": self.wikipedia_title,
        }

        if self.results[0].source_name != None:
            rv["results_source"]["name"] = self.results[0].source_name

        if self.preliminary:
            rv["preliminary"] = True

        if thesis_data:
            rv["theses"] = dict()
            for thesis in self.theses:
                rv["theses"][thesis.id] = thesis.text

        return rv

    def result_dict(self):
        rv = dict()
        for r in self.results:
            rv[r.party_repr] = {"votes": r.votes, "pct": r.pct}

            if r.party_repr != r.party_name:
                rv[r.party_repr]["linked_position"] = r.party_name

            if r.wom is False:
                rv[r.party_repr]["missing"] = True
        return rv
Beispiel #6
0
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 AircraftTypes(db.Model):
    id = db.Column(db.Integer, autoincrement=True, primary_key=True)
    icao = db.Column(db.String(10), nullable=False)
    description = db.Column(db.String(100), nullable=False)
    wake_turb = db.Column(db.String(1), nullable=False)
    max_pax = db.Column(db.Integer, nullable=False, default=0)

    def feedup(self, app):
        with app.app_context():
            check = AircraftTypes.query.filter_by(icao='ZULU').first()
            if not check:
                with open(
                        os.path.dirname(__file__) + '/../db_seeds/actypes.csv',
                        'r') as csvfile:
                    reader = csv.reader(csvfile)
                    for i in reader:
                        apt = AircraftTypes(icao=i[0],
                                            description=i[1],
                                            wake_turb=i[2])
                        db.session.add(apt)
                db.session.commit()
Beispiel #8
0
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"}
Beispiel #9
0
class Airports(db.Model):
    id = db.Column(db.Integer, autoincrement=True, primary_key=True)
    icao = db.Column(db.String(5), nullable=False)
    name = db.Column(db.String(250), nullable=False)
    lat = db.Column(db.Float, nullable=False)
    lon = db.Column(db.Float, nullable=False)

    def feedup(self, app):
        with app.app_context():
            check = self.query.filter_by(icao='UUDD').first()
            if not check:
                with open(
                        os.path.dirname(__file__) +
                        '/../db_seeds/airfields.csv', 'r') as csvfile:
                    reader = csv.reader(csvfile)
                    for i in reader:
                        apt = Airports(icao=i[0],
                                       name=i[1],
                                       lat=i[2],
                                       lon=i[3])
                        db.session.add(apt)
                db.session.commit()
Beispiel #10
0
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'))
Beispiel #11
0
class Dataset(db.Model, CustomModel):
    __tablename__ = 'dataset'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    dataset_name = db.Column(db.String(128), nullable=False)
    database_name = db.Column(db.String(128), nullable=False)
    url = db.Column(db.String(100), nullable=False)
    task_type = db.Column(db.String(100), nullable=False)
    target_column = db.Column(db.String(100), nullable=False)

    def __init__(self, dataset_name, database_name, url, task_type, target_column, filename=None):
        self.dataset_name = dataset_name
        self.database_name = database_name
        self.url = url
        self.task_type = task_type
        self.target_column = target_column
        if filename is not None:
            pass
Beispiel #12
0
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))
Beispiel #13
0
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))
    image = db.Column(db.String(255))

    def __repr__(self):
        return "<Tag #{}>".format(self.title)

    def make_slug(self):
        self.slug = slugify(self.title)

    def to_dict(
        self,
        thesis_count=None,
        include_theses_ids=False,
        include_related_tags=None,
        query_root_status=False,
    ):
        rv = {
            "title": self.title,
            "slug": self.slug,
            "wikidata_id": self.wikidata_id,
            "url": self.url,
        }

        if self.description is not None:
            rv["description"] = self.description

        if self.wikipedia_title is not None:
            rv["wikipedia_title"] = self.wikipedia_title

        if self.aliases is not None and len(self.aliases) > 0:
            rv["aliases"] = self.aliases.split(";")

        if self.labels is not None and len(self.labels) > 0:
            rv["labels"] = self.labels.split(";")

        if self.image is not None:
            rv["image"] = self.image

        if thesis_count is not None:
            rv["thesis_count"] = thesis_count

        if include_theses_ids:
            rv["theses"] = [thesis.id for thesis in self.theses]

        if include_related_tags is not None:
            rv["related_tags"] = self.related_tags(include_related_tags)

        if query_root_status:
            rv["root"] = self.is_root

        return rv

    def related_tags(self, format):
        """Return a dictionary of related tags.

        The return value distinguishes between parent tags, which are present
        on more than 80% of this tag's theses and themselves have at least as
        many theses as this tag does - and 'linked' tags, which are just tags
        that are present on this tag's theses.

        The number of returned tags in the 'linked' category is limited to ~15.
        """

        if format not in ["simple", "full"]:
            format = "simple"

        tag_counts = defaultdict(int)
        tags = dict()

        for thesis in self.theses:
            for tag in thesis.tags:
                if tag != self:
                    tag_counts[tag.title] += 1
                    tags[tag.title] = tag

        num_related_tags = 15
        try:
            # Determine the amount of tags where n=num_related_tags theses have
            # more related tags
            cutoff = sorted(tag_counts.values())[::-1][:num_related_tags +
                                                       1][-1]
        except IndexError:
            return {}
        else:
            rv = {"parents": {}, "linked": {}}

            self_theses_count = len(self.theses)

            for tag in tag_counts.keys():
                if tag_counts[tag] >= cutoff:
                    if (tag_counts[tag] >= (0.8 * self_theses_count)
                            and len(tags[tag].theses) >= self_theses_count):
                        relation = "parents"
                    else:
                        relation = "linked"

                    if format == "full":
                        related_tag = tags[tag].to_dict()
                    else:
                        related_tag = tags[tag].slug

                    rv[relation][tag] = {
                        "count": tag_counts[tag],
                        "tag": related_tag
                    }

            return rv

    @property
    def is_root(self):
        """Return true if this tag has no parent tagas in its related tags."""
        rl = self.related_tags('simple')
        return len(rl["parents"]) == 0
Beispiel #14
0
#!/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))
Beispiel #15
0
class CustomModel:
    created_at = db.Column(db.DateTime, nullable=False, default=datetime.datetime.utcnow)
Beispiel #16
0
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)