Skip to content
Snippets Groups Projects
model.py 3.00 KiB
import logging
import sqlalchemy as sa
from enum import Enum

from typing import Any

from sqlalchemy import MetaData
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship

logger = logging.getLogger(__name__)

convention = {
    "ix": "ix_%(column_0_label)s",
    "uq": "uq_%(table_name)s_%(column_0_name)s",
    "ck": "ck_%(table_name)s_%(constraint_name)s",
    "fk": "fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s",
    "pk": "pk_%(table_name)s",
}

metadata_obj = MetaData(naming_convention=convention)

# https://github.com/python/mypy/issues/2477
base_schema: Any = declarative_base(metadata=metadata_obj)


class NREN(base_schema):
    __tablename__ = 'nren'
    id = sa.Column(sa.Integer, primary_key=True)
    name = sa.Column(sa.String(128), nullable=False)


class BudgetEntry(base_schema):
    __tablename__ = 'budgets'
    nren_id = sa.Column(
        sa.Integer, sa.schema.ForeignKey(NREN.id), primary_key=True)
    nren = relationship(NREN, lazy='joined')
    year = sa.Column(sa.Integer, primary_key=True)
    budget = sa.Column(sa.Numeric(asdecimal=False), nullable=False)


class FundingSource(base_schema):
    __tablename__ = 'funding_source'
    nren_id = sa.Column(
        sa.Integer, sa.schema.ForeignKey(NREN.id), primary_key=True)
    nren = relationship(NREN, lazy='joined')
    year = sa.Column(sa.Integer, primary_key=True)
    client_institutions = sa.Column(
        sa.Numeric(asdecimal=False), nullable=False)
    european_funding = sa.Column(sa.Numeric(asdecimal=False), nullable=False)
    gov_public_bodies = sa.Column(sa.Numeric(asdecimal=False), nullable=False)
    commercial = sa.Column(sa.Numeric(asdecimal=False), nullable=False)
    other = sa.Column(sa.Numeric(asdecimal=False), nullable=False)


class FeeType(Enum):
    flat_fee = "flat_fee"
    usage_based_fee = "usage_based_fee"
    combination = "combination"
    no_charge = "no_charge"
    other = "other"


class ChargingStructure(base_schema):
    __tablename__ = 'charging_structure'
    nren_id = sa.Column(
        sa.Integer, sa.schema.ForeignKey(NREN.id), primary_key=True)
    nren = relationship(NREN, lazy='joined')
    year = sa.Column(sa.Integer, primary_key=True)
    fee_type = sa.Column('fee_type', sa.Enum("flat_fee", "usage_based_fee",
                                             "combination", "no_charge",
                                             "other",
                                             name="fee_type"), nullable=True)


class NrenStaff(base_schema):
    __tablename__ = 'nren_staff'
    nren_id = sa.Column(sa.Integer, sa.schema.ForeignKey(NREN.id), primary_key=True)
    nren = relationship(NREN, lazy='joined')
    year = sa.Column(sa.Integer, primary_key=True)
    permanent_fte = sa.Column(sa.Numeric(asdecimal=False), nullable=False)
    subcontracted_fte = sa.Column(sa.Numeric(asdecimal=False), nullable=False)
    technical_fte = sa.Column(sa.Numeric(asdecimal=False), nullable=False)
    non_technical_fte = sa.Column(sa.Numeric(asdecimal=False), nullable=False)