diff --git a/app/models/user.py b/app/models/user.py index df9b8bf..0072003 100644 --- a/app/models/user.py +++ b/app/models/user.py @@ -54,6 +54,8 @@ class Currency(enum.Enum): sheets = db.relationship("Sheet", viewonly=True) + calculation_enabled = db.Column(db.Boolean, default=True) + @hybrid_property def password(self): return self.password_hash diff --git a/app/views/calculator.py b/app/views/calculator.py index 428d567..d34c18d 100644 --- a/app/views/calculator.py +++ b/app/views/calculator.py @@ -1,4 +1,5 @@ from flask import Blueprint, render_template, request, jsonify +from flask_login import current_user from rectpack import ( skyline, guillotine, @@ -88,8 +89,16 @@ def calculator(printshop: str = None): def calculate(): data = request.json + anon_u = User.query.filter_by(username="anonymous_user_one_calc").first() + log(log.INFO, "Validate required data [%s]", data) + if current_user.is_anonymous: + if not anon_u.calculation_enabled: + return jsonify({"message": "Calculator is busy right now. Login/register or try it later"}), 400 + anon_u.calculation_enabled = False + anon_u.save() + if not data.get("bins"): return jsonify({"message": "Bin(s) not found"}), 400 elif not data.get("rectangles"): @@ -222,4 +231,8 @@ def calculate(): del bin["bin"] del result["algo"] + if current_user.is_anonymous: + anon_u.calculation_enabled = True + anon_u.save() + return jsonify(result) diff --git a/migrations/versions/ec58c3ac5211_user_calculation_enabled.py b/migrations/versions/ec58c3ac5211_user_calculation_enabled.py new file mode 100644 index 0000000..03a7646 --- /dev/null +++ b/migrations/versions/ec58c3ac5211_user_calculation_enabled.py @@ -0,0 +1,28 @@ +"""user calculation_enabled + +Revision ID: ec58c3ac5211 +Revises: 26ea19152d95 +Create Date: 2023-11-03 13:06:10.868668 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = 'ec58c3ac5211' +down_revision = '26ea19152d95' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.add_column('users', sa.Column('calculation_enabled', sa.Boolean(), default=True, server_default='true')) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_column('users', 'calculation_enabled') + # ### end Alembic commands ### diff --git a/wsgi.py b/wsgi.py index 97aa11a..6579b22 100644 --- a/wsgi.py +++ b/wsgi.py @@ -17,5 +17,16 @@ def example_command(): print("Hello World!!!") +@app.cli.command() +def create_anonymous_user(): + """Create anonymous user.""" + user = models.User( + username="anonymous_user_one_calc", + email="anonymous_user_one_calc@anonymous_user_one_calc.com", + ) + user.save() + print("Anonymous user created.") + + if __name__ == "__main__": app.run()