Skip to content

Commit

Permalink
Minor customize refactoring and bug fixes
Browse files Browse the repository at this point in the history
- Refactor `customize.py` legacy code
- Add test coverage for `customize.py`
- Add some helpers
- Fix yesterday's refactoring bugs 🤦‍♂️. resolves #119 #118 #115 #112
  • Loading branch information
mrf345 committed Jun 4, 2020
1 parent cf255a2 commit 5c53986
Show file tree
Hide file tree
Showing 5 changed files with 605 additions and 241 deletions.
46 changes: 28 additions & 18 deletions app/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

from flask_login import UserMixin, current_user
from sqlalchemy.sql import and_
from sqlalchemy.sql import and_, or_
from werkzeug.security import generate_password_hash, check_password_hash
from datetime import datetime
from app.middleware import db
Expand All @@ -18,7 +18,10 @@

class Mixin:
@classmethod
def get(cls, id):
def get(cls, id=False):
if id is False:
return cls.query.first()

return cls.query.filter_by(id=id).first()


Expand Down Expand Up @@ -346,7 +349,7 @@ def load_roles(cls):
db.session.commit()


class Printer(db.Model):
class Printer(db.Model, Mixin):
__tablename__ = "printers"
id = db.Column(db.Integer, primary_key=True)
vendor = db.Column(db.String(100), nullable=True, unique=True)
Expand Down Expand Up @@ -374,7 +377,7 @@ def __init__(self, vendor=" ", product=" ",
# -- Touch custimization table


class Touch_store(db.Model):
class Touch_store(db.Model, Mixin):
__tablename__ = 'touchs'
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(300))
Expand Down Expand Up @@ -439,7 +442,7 @@ def __init__(self, id=0, title="Please select a task to pull a tick for",
# -- Touch customization table


class Display_store(db.Model):
class Display_store(db.Model, Mixin):
__tablename__ = 'displays'
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(300))
Expand Down Expand Up @@ -529,7 +532,7 @@ def get(cls):
# -- Slides storage table


class Slides(db.Model):
class Slides(db.Model, Mixin):
__tablename__ = "slides"
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(300))
Expand All @@ -550,7 +553,7 @@ class Slides(db.Model):

# -- ^^ Slides custimization table

class Slides_c(db.Model):
class Slides_c(db.Model, Mixin):
__tablename__ = "slides_c"
id = db.Column(db.Integer, primary_key=True)
rotation = db.Column(db.String(100))
Expand All @@ -568,7 +571,7 @@ def __init__(self, rotation="3000",
self.navigation = navigation


class Media(db.Model):
class Media(db.Model, Mixin):
__tablename__ = "media"
id = db.Column(db.Integer, primary_key=True)
vid = db.Column(db.Boolean())
Expand All @@ -585,8 +588,20 @@ def __init__(self, vid=False, audio=False,
self.used = used
self.name = name

def is_used(self):
return any([
Vid.query.filter_by(vkey=self.id).first(),
Slides.query.filter_by(ikey=self.id).first(),
Display_store.query.filter(or_(
Display_store.ikey == self.id,
Display_store.akey == self.id)).first(),
Touch_store.query.filter(or_(
Touch_store.ikey == self.id,
Touch_store.akey == self.id)).first()
])


class Vid(db.Model):
class Vid(db.Model, Mixin):
__tablename__ = "vids"
id = db.Column(db.Integer, primary_key=True)
vname = db.Column(db.String(300))
Expand All @@ -608,7 +623,7 @@ def __init__(self, vname="", enable=0, ar=1, controls=1,
self.vkey = vkey


class Aliases(db.Model):
class Aliases(db.Model, Mixin):
__tablename__ = "aliases"
id = db.Column(db.Integer, primary_key=True)
office = db.Column(db.String(100))
Expand All @@ -617,9 +632,8 @@ class Aliases(db.Model):
name = db.Column(db.String(100))
number = db.Column(db.String(100))

def __init__(self,
office="office", task="task", ticket="ticket",
name="name", number="number"):
def __init__(self, office="office", task="task", ticket="ticket", name="name",
number="number"):
self.id = 0
self.office = office
self.task = task
Expand All @@ -628,7 +642,7 @@ def __init__(self,
self.number = number


class Settings(db.Model):
class Settings(db.Model, Mixin):
__tablename__ = 'settings'
id = db.Column(db.Integer, primary_key=True)
notifications = db.Column(db.Boolean, nullable=True)
Expand All @@ -640,7 +654,3 @@ def __init__(self, notifications=True, strict_pulling=True, visual_effects=True)
self.notifications = notifications
self.strict_pulling = strict_pulling
self.visual_effects = visual_effects

@classmethod
def get(cls):
return cls.query.first()
51 changes: 51 additions & 0 deletions app/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,57 @@ def decorated(*args, **kwargs):
return decorated


def reject_videos_enabled(function):
''' Decorator to flash and redirect to `cust_app.video`.
Parameters
----------
function: callable
then endpoint to be rejected if videos enabled.
Returns
-------
Decorator for the passed `function`
'''
@wraps(function)
def decorated(*args, **kwargs):
with current_app.app_context():
enabled = data.Vid.query.first().enable == 1

if enabled:
flash('Error: you must disable videos first', 'danger')
return redirect(url_for('cust_app.video'))

return function(*args, **kwargs)

return decorated


def reject_slides_enabled(function):
''' Decorator to flash and redirect to `cust_app.slide_c`.
Parameters
----------
function: callable
then endpoint to be rejected if slides enabled.
Returns
-------
Decorator for the passed `function`
'''
@wraps(function)
def decorated(*args, **kwargs):
with current_app.app_context():
enabled = data.Slides_c.query.first().status == 1

if enabled:
flash('Error: you must disable slide-show first', 'danger')
return redirect(url_for('cust_app.video'))

return function(*args, **kwargs)

return decorated

def get_tts_safely():
''' Helper to read gTTS data from `static/tts.json` file safely.
Expand Down
Loading

0 comments on commit 5c53986

Please sign in to comment.