Commit 1d834abe authored by Leon Tappe's avatar Leon Tappe 🔥
Browse files

add settings backend

parent c29b87d0
......@@ -58,6 +58,12 @@ class Color(db.Model):
name = db.Column(db.String(50), unique=False)
class Setting(db.Model):
id = db.Column(db.Integer, primary_key=True)
key = db.Column(db.String(50), unique=True)
value = db.Column(db.String(50), unique=False)
class OAuth2Client(db.Model, OAuth2ClientMixin):
__tablename__ = 'oauth2_client'
......
......@@ -8,7 +8,7 @@ from dateutil.tz import tzutc, gettz
from dateutil import parser, utils
from time import time
from .oauth2 import authorization, require_oauth, OAuth2Client
from .models import AccessCode, Color, ColorAllocation, OAuth2AuthorizationCode, OAuth2Token, db, User
from .models import AccessCode, Color, ColorAllocation, OAuth2AuthorizationCode, OAuth2Token, db, User, Setting
from authlib.oauth2 import OAuth2Error
from authlib.integrations.flask_oauth2 import current_token
import base64
......@@ -566,3 +566,53 @@ def current_color():
if (alloc == None):
return 'no color found for today', 404
return jsonify(color=alloc.color.hex, inverse=alloc.color.inverse)
def get_settings():
settings = Setting.query.all()
settings_map = []
for setting in settings:
settings_map.append({'key': setting.key, 'value': setting.value})
return jsonify(settings_map)
@bp.route('/admin/settings', methods=['GET', 'POST'])
@require_oauth('admin')
def admin_settings():
if request.method == 'GET':
return get_settings()
if request.method == 'POST':
setting = Setting(
day=request.json['key'], color_id=request.json['value'])
try:
db.session.add(setting)
db.session.commit()
new_setting = Setting.query.filter_by(key=setting.key).first()
return jsonify({'key': new_setting.key, 'value': new_setting.value})
except:
return 'failed to add setting, check for duplicate key', 400
@bp.route('/admin/settings/<string:key>', methods=['GET', 'PUT', 'DELETE'])
@require_oauth('admin')
def single_setting(key):
if request.method == 'GET':
setting = Setting.query.filter_by(key=key).first()
return jsonify({'key': setting.key, 'value': setting.value})
if request.method == 'PUT':
if db.session.query(Setting).filter(Setting.key == key).update({Setting.value: request.json['value']}) < 1:
return 'not found', 404
db.session.commit()
new_setting = Setting.query.filter_by(key=key).first()
return jsonify({'key': new_setting.key, 'value': new_setting.value})
if request.method == 'DELETE':
if db.session.query(Setting).filter(Setting.key == key).delete() < 1:
return 'not found', 404
db.session.commit()
return 'success', 200
@bp.route('/settings', methods=['GET'])
@require_oauth('user')
def user_settings():
return get_settings()
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment