Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions app/views/calculator.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from flask import Blueprint, render_template, request, jsonify
from flask_login import current_user
from rectpack import (
skyline,
guillotine,
Expand Down Expand Up @@ -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"):
Expand Down Expand Up @@ -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)
28 changes: 28 additions & 0 deletions migrations/versions/ec58c3ac5211_user_calculation_enabled.py
Original file line number Diff line number Diff line change
@@ -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 ###
11 changes: 11 additions & 0 deletions wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()