Added models and views
This commit is contained in:
22
ref-test/app/tools/auth.py
Normal file
22
ref-test/app/tools/auth.py
Normal file
@ -0,0 +1,22 @@
|
||||
from .data import load
|
||||
from ..models import User
|
||||
|
||||
from flask import abort, redirect
|
||||
from flask.helpers import url_for
|
||||
from flask_login import current_user
|
||||
|
||||
from functools import wraps
|
||||
|
||||
def require_account_creation(function):
|
||||
@wraps(function)
|
||||
def wrapper(*args, **kwargs):
|
||||
if User.query.count() == 0: return redirect(url_for('views._register'))
|
||||
return function(*args, **kwargs)
|
||||
return wrapper
|
||||
|
||||
def disable_if_logged_in(function):
|
||||
@wraps(function)
|
||||
def wrapper(*args, **kwargs):
|
||||
if current_user.is_authenticated: return abort(404)
|
||||
return function(*args, **kwargs)
|
||||
return wrapper
|
@ -1,4 +1,3 @@
|
||||
from main import Config
|
||||
from ..data import data as data_dir
|
||||
import json
|
||||
|
||||
|
36
ref-test/app/tools/forms.py
Normal file
36
ref-test/app/tools/forms.py
Normal file
@ -0,0 +1,36 @@
|
||||
|
||||
from ..modules import db
|
||||
|
||||
from wtforms.validators import ValidationError
|
||||
|
||||
import json
|
||||
from sqlalchemy.ext import mutable
|
||||
|
||||
class JsonEncodedDict(db.TypeDecorator):
|
||||
"""Enables JSON storage by encoding and decoding on the fly."""
|
||||
impl = db.Text
|
||||
|
||||
def process_bind_param(self, value, dialect):
|
||||
if value is None:
|
||||
return '{}'
|
||||
else:
|
||||
return json.dumps(value)
|
||||
|
||||
def process_result_value(self, value, dialect):
|
||||
if value is None:
|
||||
return {}
|
||||
else:
|
||||
return json.loads(value)
|
||||
|
||||
mutable.MutableDict.associate_with(JsonEncodedDict)
|
||||
|
||||
def value(min:int=0, max:int=None):
|
||||
if not max:
|
||||
message = f'Value must be greater than {min}.'
|
||||
else:
|
||||
message = f'Value must be between {min} and {max}.'
|
||||
def length(form, field):
|
||||
value = field.data or 0
|
||||
if value < min or max != None and value > max:
|
||||
raise ValidationError(message)
|
||||
return length
|
@ -1,4 +1,3 @@
|
||||
from main import Config
|
||||
from ..data import data
|
||||
from datetime import datetime
|
||||
|
||||
|
Reference in New Issue
Block a user