From 1304ea61533f5e38cc9721530e84889e59f3a6ec Mon Sep 17 00:00:00 2001 From: Jeremy Zhang Date: Sat, 22 Jul 2017 05:02:59 +0000 Subject: [PATCH] Implement Cosmetics Configuration to the Administrators Panel --- webapp/titanembeds/blueprints/admin/admin.py | 58 ++++++++++++- webapp/titanembeds/database/cosmetics.py | 3 +- .../titanembeds/static/js/admin_cosmetics.js | 82 +++++++++++++++++++ .../templates/admin_cosmetics.html.j2 | 80 ++++++++++++++++++ .../titanembeds/templates/admin_index.html.j2 | 11 +++ 5 files changed, 231 insertions(+), 3 deletions(-) create mode 100644 webapp/titanembeds/static/js/admin_cosmetics.js create mode 100644 webapp/titanembeds/templates/admin_cosmetics.html.j2 diff --git a/webapp/titanembeds/blueprints/admin/admin.py b/webapp/titanembeds/blueprints/admin/admin.py index 286d070..dafb2a4 100644 --- a/webapp/titanembeds/blueprints/admin/admin.py +++ b/webapp/titanembeds/blueprints/admin/admin.py @@ -1,6 +1,6 @@ -from flask import Blueprint, url_for, redirect, session, render_template +from flask import Blueprint, url_for, redirect, session, render_template, abort, request from functools import wraps -from titanembeds.database import get_administrators_list +from titanembeds.database import db, get_administrators_list, Cosmetics admin = Blueprint("admin", __name__) @@ -20,3 +20,57 @@ def is_admin(f): @is_admin def index(): return render_template("admin_index.html.j2") + +@admin.route("/cosmetics", methods=["GET"]) +@is_admin +def cosmetics(): + entries = db.session.query(Cosmetics).all() + return render_template("admin_cosmetics.html.j2", cosmetics=entries) + +@admin.route("/cosmetics", methods=["POST"]) +@is_admin +def cosmetics_post(): + user_id = request.form.get("user_id", None) + if not user_id: + abort(400) + css = request.form.get("css", None) + entry = db.session.query(Cosmetics).filter(Cosmetics.user_id == user_id).first() + if entry: + abort(409) + user = Cosmetics(user_id) + if css: + css = css.lower() == "true" + user.css = css + db.session.add(user) + db.session.commit() + return ('', 204) + +@admin.route("/cosmetics", methods=["DELETE"]) +@is_admin +def cosmetics_delete(): + user_id = request.form.get("user_id", None) + if not user_id: + abort(400) + entry = db.session.query(Cosmetics).filter(Cosmetics.user_id == user_id).first() + if not entry: + abort(409) + db.session.delete(entry) + db.session.commit() + return ('', 204) + +@admin.route("/cosmetics", methods=["PATCH"]) +@is_admin +def cosmetics_patch(): + user_id = request.form.get("user_id", None) + if not user_id: + abort(400) + css = request.form.get("css", None) + entry = db.session.query(Cosmetics).filter(Cosmetics.user_id == user_id).first() + if not entry: + abort(409) + if css: + css = css.lower() == "true" + entry.css = css + db.session.commit() + return ('', 204) + \ No newline at end of file diff --git a/webapp/titanembeds/database/cosmetics.py b/webapp/titanembeds/database/cosmetics.py index 305bc7c..a442c0c 100644 --- a/webapp/titanembeds/database/cosmetics.py +++ b/webapp/titanembeds/database/cosmetics.py @@ -7,8 +7,9 @@ class Cosmetics(db.Model): css = db.Column(db.Boolean(), nullable=False) # If they can create/edit custom CSS def __init__(self, user_id, **kwargs): - self.name = name self.user_id = user_id if "css" in kwargs: self.css = kwargs["css"] + else: + self.css = False diff --git a/webapp/titanembeds/static/js/admin_cosmetics.js b/webapp/titanembeds/static/js/admin_cosmetics.js new file mode 100644 index 0000000..79a2e1a --- /dev/null +++ b/webapp/titanembeds/static/js/admin_cosmetics.js @@ -0,0 +1,82 @@ +/* global $, Materialize, location */ + +function postForm(user_id, css) { + var funct = $.ajax({ + dataType: "json", + method: "POST", + data: {"user_id": user_id, "css": css} + }); + return funct.promise(); +} + +function deleteForm(user_id) { + var funct = $.ajax({ + dataType: "json", + method: "DELETE", + data: {"user_id": user_id} + }); + return funct.promise(); +} + +function patchForm(user_id, css) { + var funct = $.ajax({ + dataType: "json", + method: "PATCH", + data: {"user_id": user_id, "css": css} + }); + return funct.promise(); +} + +$(function() { + $("#new_submit").click(function () { + var user_id = $("#new_user_id").val(); + if (user_id.length < 1) { + Materialize.toast("The user ID field can't be blank!", 2000); + return; + } + var css_checked = $("#new_css_switch").is(':checked'); + var formPost = postForm(user_id, css_checked); + formPost.done(function (data) { + location.reload(); + }); + formPost.fail(function (data) { + if (data.status == 409) { + Materialize.toast('This user id already exists!', 10000); + } else { + Materialize.toast('Oh no! Something has failed submitting a new entry!', 10000); + } + }); + }); +}); + +function delete_user(user_id) { + var confirmation = confirm("Are you sure that you want to delete user?"); + if (confirmation) { + var formDelete = deleteForm(user_id); + formDelete.done(function (data) { + location.reload(); + }); + formDelete.fail(function (data) { + if (data.status == 409) { + Materialize.toast('This user id does not exists!', 10000); + } else { + Materialize.toast('Oh no! Something has failed deleting this user entry!', 10000); + } + }); + } +} + +function update_css_switch(user_id, element) { + var css_checked = $(element).is(':checked'); + var formPatch = patchForm(user_id, css_checked); + formPatch.done(function (data) { + Materialize.toast('CSS updated!', 10000); + }); + formPatch.fail(function (data) { + if (data.status == 409) { + Materialize.toast('This user id does not exists!', 10000); + } else { + Materialize.toast('Oh no! Something has failed changing the css toggle!', 10000); + } + }); +} \ No newline at end of file diff --git a/webapp/titanembeds/templates/admin_cosmetics.html.j2 b/webapp/titanembeds/templates/admin_cosmetics.html.j2 new file mode 100644 index 0000000..897ea62 --- /dev/null +++ b/webapp/titanembeds/templates/admin_cosmetics.html.j2 @@ -0,0 +1,80 @@ +{% extends 'site_layout.html.j2' %} +{% set title="Editing User Cosmetics Privilages" %} + +{% block content %} +

Administrating User Cosmetics Privilages

+ +
+
+
+

New Entry

+ + + + + + + + + + + + + + + +
User IDCSSSubmit
+
+ +
+
+
+ +
+
+ Submit +
+
+
+ +
+
+ + + + + + + + + + {% for cosmetic in cosmetics %} + + + + + + {% endfor %} + +
RemoveUser IDCSS
Remove{{ cosmetic.user_id }} +
+ +
+
+
+
+
+{% endblock %} +{% block script %} + +{% endblock %} diff --git a/webapp/titanembeds/templates/admin_index.html.j2 b/webapp/titanembeds/templates/admin_index.html.j2 index 6c2b851..6f9d5ec 100644 --- a/webapp/titanembeds/templates/admin_index.html.j2 +++ b/webapp/titanembeds/templates/admin_index.html.j2 @@ -2,5 +2,16 @@ {% set title="Admin" %} {% block content %} +

Administrate Titan Embeds

+

Select an action.

+
+
+
+

Cosmetics

+

Give or revoke special cosmetics privilages for users.

+ Manage +
+
+
{% endblock %}