Implement Cosmetics Configuration to the Administrators Panel

This commit is contained in:
Jeremy Zhang 2017-07-22 05:02:59 +00:00
parent f4608794a0
commit 1304ea6153
5 changed files with 231 additions and 3 deletions

View File

@ -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)

View File

@ -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

View File

@ -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);
}
});
}

View File

@ -0,0 +1,80 @@
{% extends 'site_layout.html.j2' %}
{% set title="Editing User Cosmetics Privilages" %}
{% block content %}
<h1>Administrating User Cosmetics Privilages</h1>
<div class="row">
<div class="col s12">
<div class="card-panel indigo lighten-5 z-depth-3 hoverable black-text">
<p class="flow-text">New Entry</p>
<table class="bordered striped">
<thead>
<tr>
<th>User ID</th>
<th>CSS</th>
<th>Submit</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<div class="input-field inline">
<input id="new_user_id">
</div>
</td>
<td>
<div class="switch">
<label>
Off
<input type="checkbox" id="new_css_switch">
<span class="lever"></span>
On
</label>
</div>
</td>
<td>
<a class="waves-effect waves-light btn" id="new_submit">Submit</a>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="col s12">
<div class="card-panel indigo lighten-5 z-depth-3 hoverable black-text">
<table class="bordered striped">
<thead>
<tr>
<th>Remove</th>
<th>User ID</th>
<th>CSS</th>
</tr>
</thead>
<tbody>
{% for cosmetic in cosmetics %}
<tr>
<td><a class="waves-effect waves-light btn red" id="new_submit" onclick="delete_user('{{ cosmetic.user_id }}');">Remove</a></td>
<td>{{ cosmetic.user_id }}</td>
<td>
<div class="switch">
<label>
Off
<input type="checkbox" id="new_css_switch" {% if cosmetic.css %}checked{% endif %} onchange="update_css_switch('{{ cosmetic.user_id }}', this)">
<span class="lever"></span>
On
</label>
</div>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
</div>
{% endblock %}
{% block script %}
<script type="text/javascript" src="{{ url_for('static', filename='js/admin_cosmetics.js') }}"></script>
{% endblock %}

View File

@ -2,5 +2,16 @@
{% set title="Admin" %}
{% block content %}
<h1>Administrate Titan Embeds</h1>
<p class="flow-text">Select an action.</p>
<div class="row">
<div class="col s12">
<div class="card-panel indigo lighten-5 z-depth-3 hoverable black-text">
<h4>Cosmetics</h4>
<p class="flow-text">Give or revoke special <em>cosmetics privilages</em> for users.</p>
<a class="waves-effect waves-light btn" href="{{ url_for('admin.cosmetics') }}">Manage</a>
</div>
</div>
</div>
{% endblock %}