diff --git a/app/add_admins.py b/app/add_admins.py index b3f8112..e9e2fba 100644 --- a/app/add_admins.py +++ b/app/add_admins.py @@ -10,5 +10,5 @@ def add() -> None: """Add users as admin.""" for username in Configuration.HALDIS_ADMINS: user = User() - user.configure(username, True, 0) + user.configure(username, True, 0, associations=["zeus"]) db.session.add(user) diff --git a/app/admin.py b/app/admin.py index 0d0c172..6c443ee 100644 --- a/app/admin.py +++ b/app/admin.py @@ -28,11 +28,12 @@ class OrderAdminModel(ModelBaseView): "Class for the model of a OrderAdmin" # pylint: disable=too-few-public-methods column_default_sort = ("starttime", True) - column_list = ["starttime", "stoptime", "location_name", "location_id", "courier"] + column_list = ["starttime", "stoptime", "location_name", "location_id", "courier", "association"] column_labels = { "starttime": "Start Time", "stoptime": "Closing Time", "location_id": "HLDS Location ID", + "association": "Association", } form_excluded_columns = ["items", "courier_id"] can_delete = False diff --git a/app/forms.py b/app/forms.py index e259563..f847e96 100644 --- a/app/forms.py +++ b/app/forms.py @@ -24,6 +24,7 @@ class OrderForm(Form): "Starttime", default=datetime.now, format="%d-%m-%Y %H:%M" ) stoptime = DateTimeField("Stoptime", format="%d-%m-%Y %H:%M") + association = SelectField("Association", coerce=str, validators=[validators.required()]) submit_button = SubmitField("Submit") def populate(self) -> None: @@ -38,6 +39,7 @@ class OrderForm(Form): (current_user.id, current_user.username), ] self.location_id.choices = [(l.id, l.name) for l in location_definitions] + self.association.choices = current_user.association_list() if self.stoptime.data is None: self.stoptime.data = datetime.now() + timedelta(hours=1) diff --git a/app/migrations/versions/f6a6004bf4b9_add_user_associations.py b/app/migrations/versions/f6a6004bf4b9_add_user_associations.py new file mode 100644 index 0000000..e084687 --- /dev/null +++ b/app/migrations/versions/f6a6004bf4b9_add_user_associations.py @@ -0,0 +1,28 @@ +"""Add user associations + +Revision ID: f6a6004bf4b9 +Revises: 55013fe95bea +Create Date: 2022-05-24 21:23:27.770365 + +""" + +# revision identifiers, used by Alembic. +revision = 'f6a6004bf4b9' +down_revision = '55013fe95bea' + +from alembic import op +import sqlalchemy as sa + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.add_column('order', sa.Column('association', sa.String(length=120), server_default='', nullable=False)) + op.add_column('user', sa.Column('associations', sa.String(), server_default='', nullable=False)) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_column('user', 'associations') + op.drop_column('order', 'association') + # ### end Alembic commands ### diff --git a/app/models/anonymous_user.py b/app/models/anonymous_user.py index 2ae10f2..8e2532a 100644 --- a/app/models/anonymous_user.py +++ b/app/models/anonymous_user.py @@ -1,10 +1,14 @@ "AnonymouseUser for people who are not logged in the normal way" +from typing import List # pylint: disable=R0201,C0111 class AnonymouseUser: id = None + def association_list(self) -> List[str]: + return [] + def is_active(self) -> bool: return False diff --git a/app/models/order.py b/app/models/order.py index 6e5391c..c7b458e 100644 --- a/app/models/order.py +++ b/app/models/order.py @@ -27,6 +27,7 @@ class Order(db.Model): stoptime = db.Column(db.DateTime) public = db.Column(db.Boolean, default=True) slug = db.Column(db.String(7), default=generate_slug, unique=True) + association = db.Column(db.String(120), nullable=False, server_default="") items = db.relationship("OrderItem", backref="order", lazy="dynamic") diff --git a/app/models/user.py b/app/models/user.py index 8e78b2f..6f6a372 100644 --- a/app/models/user.py +++ b/app/models/user.py @@ -1,4 +1,6 @@ "Script for everything User related in the database" +from typing import List, Optional + from models import db @@ -8,6 +10,10 @@ class User(db.Model): username = db.Column(db.String(80), unique=True, nullable=False) admin = db.Column(db.Boolean) bias = db.Column(db.Integer) + # Assocation logic + associations = db.Column(db.String(), nullable=False, server_default="") + + # Relations runs = db.relation( "Order", backref="courier", @@ -16,11 +22,17 @@ class User(db.Model): ) orderItems = db.relationship("OrderItem", backref="user", lazy="dynamic") - def configure(self, username: str, admin: bool, bias: int) -> None: - "Configure the User" + def association_list(self) -> List[str]: + return self.associations.split(",") + + def configure(self, username: str, admin: bool, bias: int, associations: Optional[List[str]] = None) -> None: + """Configure the User""" + if associations is None: + associations = [] self.username = username self.admin = admin self.bias = bias + self.associations = ",".join(associations) # pylint: disable=C0111, R0201 def is_authenticated(self) -> bool: diff --git a/app/templates/orders.html b/app/templates/orders.html index e472461..35fa189 100644 --- a/app/templates/orders.html +++ b/app/templates/orders.html @@ -38,6 +38,11 @@ {{ form.location_id(class='form-control select') }} {{ util.render_form_field_errors(form.location_id) }} +