Skip to content

Commit

Permalink
feat(global): setup inital global settings page (#121)
Browse files Browse the repository at this point in the history
  • Loading branch information
drahamim authored Mar 3, 2024
1 parent 7d761ef commit 0611c35
Show file tree
Hide file tree
Showing 8 changed files with 157 additions and 4 deletions.
38 changes: 38 additions & 0 deletions migrations/versions/6e10d0ad5159_.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"""empty message
Revision ID: 6e10d0ad5159
Revises: 863f81a1b742
Create Date: 2024-02-27 16:25:21.514371
"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = '6e10d0ad5159'
down_revision = '863f81a1b742'
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table('globalset', schema=None) as batch_op:
batch_op.alter_column('settingid',
existing_type=sa.INTEGER(),
type_=sa.String(),
existing_nullable=False)

# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table('globalset', schema=None) as batch_op:
batch_op.alter_column('settingid',
existing_type=sa.String(),
type_=sa.INTEGER(),
existing_nullable=False)

# ### end Alembic commands ###
38 changes: 38 additions & 0 deletions migrations/versions/863f81a1b742_.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"""empty message
Revision ID: 863f81a1b742
Revises: a0d95191a5c0
Create Date: 2024-02-26 17:25:06.957539
"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = '863f81a1b742'
down_revision = 'a0d95191a5c0'
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table('globalset', schema=None) as batch_op:
batch_op.add_column(sa.Column('settingid', sa.Integer(), nullable=False))
batch_op.add_column(sa.Column('setting', sa.String(), nullable=False))
batch_op.drop_column('timezonename')
batch_op.drop_column('id')

# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table('globalset', schema=None) as batch_op:
batch_op.add_column(sa.Column('id', sa.INTEGER(), autoincrement=True, nullable=False))
batch_op.add_column(sa.Column('timezonename', sa.VARCHAR(), autoincrement=False, nullable=False))
batch_op.drop_column('setting')
batch_op.drop_column('settingid')

# ### end Alembic commands ###
32 changes: 32 additions & 0 deletions migrations/versions/a0d95191a5c0_.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""empty message
Revision ID: a0d95191a5c0
Revises: 4994f94014b5
Create Date: 2024-02-23 18:49:45.470611
"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = 'a0d95191a5c0'
down_revision = '4994f94014b5'
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('globalset',
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
sa.Column('timezonename', sa.String(), nullable=False),
sa.PrimaryKeyConstraint('id')
)
# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table('globalset')
# ### end Alembic commands ###
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ sqlalchemy == 2.0.27
flask-sqlalchemy >= 3.0.1
psycopg2 == 2.9.9
flask-migrate == 4.0.5
gunicorn == 21.2.0
gunicorn == 21.2.0
flask-moment == 1.0.5
20 changes: 18 additions & 2 deletions src/invenflask/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
from werkzeug.utils import secure_filename
from flask_moment import Moment

from .models import Asset, Staff, Checkout, History, db

from .models import Asset, Staff, Checkout, History, db, GlobalSet
from .forms import SettingsForm

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = os.getenv(
Expand Down Expand Up @@ -412,3 +412,19 @@ def parseCSV_staff(filePath, first_name=False, last_name=False, staff_id=False,
"Staff upload failed import. This may be due to ID conflicts.", "danger")
return redirect(url_for('staff_create'))
return redirect(url_for('staffs'))


@app.route('/settings', methods=['GET', 'POST'])
def settings():
form = SettingsForm()
if form.validate_on_submit():
tz = GlobalSet.query.filter_by(settingid="timezone").first()
tz.setting = form.timezone.data
db.session.commit()
flash('Your settings have been updated.')
return redirect(url_for('settings'))
elif request.method == 'GET':

form.timezone.data = db.session.query(GlobalSet).filter(
GlobalSet.settingid == "timezone").first().setting
return render_template('settings.html', title='Settings', form=form)
8 changes: 8 additions & 0 deletions src/invenflask/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from flask_wtf import FlaskForm
from wtforms import SelectField
import pytz


class SettingsForm(FlaskForm):
timezone = SelectField('Timezone', choices=[
(tz, tz) for tz in pytz.all_timezones])
9 changes: 8 additions & 1 deletion src/invenflask/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from sqlalchemy import Column, String, Integer, DateTime
from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy()


Expand Down Expand Up @@ -43,3 +42,11 @@ class History(db.Model):
division = Column(String, nullable=False)
checkouttime = Column(DateTime, nullable=False)
returntime = Column(DateTime, nullable=False)


class GlobalSet(db.Model):
__tablename__ = 'globalset'

settingid = Column(String, primary_key=True)
setting = Column(String, nullable=False)
db.UniqueConstraint('settingid', name='setting_id')
13 changes: 13 additions & 0 deletions src/invenflask/templates/settings.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{% extends "base.html" %}

{% block content %}
<h1>Settings</h1>
<form method="POST">
{{ form.hidden_tag() }}
<div class="form-group">
<label for="timezone">{{ form.timezone.label }}</label>
{{ form.timezone(class="form-control") }}
</div>
<button type="submit" class="btn btn-primary btn-lg">Save</button>
</form>
{% endblock %}

0 comments on commit 0611c35

Please sign in to comment.