From 0ec0b52501a5855b362989bb61494ebe5b92468d Mon Sep 17 00:00:00 2001 From: Jeremy Zhang Date: Sat, 12 Oct 2019 22:36:37 -0700 Subject: [PATCH] Donation goal banner --- ..._added_application_settings_table_with_.py | 34 ++++++++++++++++ webapp/titanembeds/app.py | 6 ++- webapp/titanembeds/blueprints/admin/admin.py | 39 ++++++++++++++++++- webapp/titanembeds/database/__init__.py | 13 ++++++- .../database/application_settings.py | 13 +++++++ webapp/titanembeds/static/css/style.css | 6 +-- .../static/js/admin_application_settings.js | 26 +++++++++++++ .../admin_application_settings.html.j2 | 30 ++++++++++++++ .../titanembeds/templates/admin_index.html.j2 | 7 ++++ .../titanembeds/templates/site_layout.html.j2 | 15 +++++++ 10 files changed, 182 insertions(+), 7 deletions(-) create mode 100644 webapp/alembic/versions/ecf3e6bf950e_added_application_settings_table_with_.py create mode 100644 webapp/titanembeds/database/application_settings.py create mode 100644 webapp/titanembeds/static/js/admin_application_settings.js create mode 100644 webapp/titanembeds/templates/admin_application_settings.html.j2 diff --git a/webapp/alembic/versions/ecf3e6bf950e_added_application_settings_table_with_.py b/webapp/alembic/versions/ecf3e6bf950e_added_application_settings_table_with_.py new file mode 100644 index 0000000..281df64 --- /dev/null +++ b/webapp/alembic/versions/ecf3e6bf950e_added_application_settings_table_with_.py @@ -0,0 +1,34 @@ +"""Added application settings table with donation goal related columns + +Revision ID: ecf3e6bf950e +Revises: ce2b9c930a7a +Create Date: 2019-10-12 21:26:27.092295 + +""" + +# revision identifiers, used by Alembic. +revision = 'ecf3e6bf950e' +down_revision = 'ce2b9c930a7a' +branch_labels = None +depends_on = None + +from alembic import op +import sqlalchemy as sa + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.create_table('application_settings', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('donation_goal_progress', sa.Integer(), server_default='0', nullable=False), + sa.Column('donation_goal_total', sa.Integer(), server_default='0', nullable=False), + sa.Column('donation_goal_end', sa.Date(), nullable=True), + sa.PrimaryKeyConstraint('id') + ) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_table('application_settings') + # ### end Alembic commands ### diff --git a/webapp/titanembeds/app.py b/webapp/titanembeds/app.py index 5378ece..6308f81 100644 --- a/webapp/titanembeds/app.py +++ b/webapp/titanembeds/app.py @@ -17,10 +17,11 @@ from flask_sslify import SSLify from titanembeds.utils import rate_limiter, discord_api, socketio, babel, redis_store, language_code_list#, sentry from .blueprints import api, user, admin, embed, gateway import os -from titanembeds.database import get_administrators_list +from titanembeds.database import get_administrators_list, init_application_settings, get_application_settings import titanembeds.constants as constants from datetime import timedelta import datetime +import random os.chdir(config['app-location']) app = Flask(__name__, static_folder="static") @@ -84,11 +85,14 @@ def global_banned_words(): @app.before_first_request def before_first_request(): + init_application_settings() discord_api.init_discordrest() @app.context_processor def context_processor(): return { + "random": random, + "application_settings": get_application_settings(), "devs": get_administrators_list(), "sentry_js_dsn": config.get("sentry-js-dsn", None), "constants": constants, diff --git a/webapp/titanembeds/blueprints/admin/admin.py b/webapp/titanembeds/blueprints/admin/admin.py index 0c4e049..9d566c2 100644 --- a/webapp/titanembeds/blueprints/admin/admin.py +++ b/webapp/titanembeds/blueprints/admin/admin.py @@ -1,7 +1,7 @@ from flask import Blueprint, url_for, redirect, session, render_template, abort, request, jsonify from flask_socketio import emit from functools import wraps -from titanembeds.database import db, get_administrators_list, Cosmetics, Guilds, UnauthenticatedUsers, UnauthenticatedBans, TitanTokens, TokenTransactions, get_titan_token, set_titan_token, list_disabled_guilds, DisabledGuilds, UserCSS, AuthenticatedUsers, DiscordBotsOrgTransactions +from titanembeds.database import db, get_administrators_list, Cosmetics, Guilds, UnauthenticatedUsers, UnauthenticatedBans, TitanTokens, TokenTransactions, get_titan_token, set_titan_token, list_disabled_guilds, DisabledGuilds, UserCSS, AuthenticatedUsers, DiscordBotsOrgTransactions, ApplicationSettings from titanembeds.oauth import generate_guild_icon_url from titanembeds.utils import get_online_embed_user_keys, redisqueue import datetime @@ -497,4 +497,39 @@ def voting_get(): if gmember: u["discord"] = gmember["username"] + "#" + str(gmember["discriminator"]) referrals.append(u) - return render_template("admin_voting.html.j2", overall=overall, referrals=referrals, datestart=datestart, timestart=timestart, dateend=dateend, timeend=timeend) \ No newline at end of file + return render_template("admin_voting.html.j2", overall=overall, referrals=referrals, datestart=datestart, timestart=timestart, dateend=dateend, timeend=timeend) + +@admin.route("/app_settings", methods=["GET"]) +@is_admin +def application_settings_get(): + settings = db.session.query(ApplicationSettings).first() + return render_template("admin_application_settings.html.j2", settings=settings) + +@admin.route("/app_settings", methods=["POST"]) +@is_admin +def application_settings_post(): + settings = db.session.query(ApplicationSettings).first() + if "donation_goal_progress" in request.form: + donation_goal_progress = request.form.get("donation_goal_progress") + settings.donation_goal_progress = int(donation_goal_progress) + if "donation_goal_total" in request.form: + donation_goal_total = request.form.get("donation_goal_total") + settings.donation_goal_total = int(donation_goal_total) + if "donation_goal_end" in request.form: + res = None + donation_goal_end = request.form.get("donation_goal_end") + if donation_goal_end: + donation_goal_end = donation_goal_end.split("/") + month = int(donation_goal_end[0]) + day = int(donation_goal_end[1]) + year = int(donation_goal_end[2]) + res = datetime.date(year, month, day) + settings.donation_goal_end = res + db.session.commit() + return jsonify({ + "donation_goal_progress": settings.donation_goal_progress, + "donation_goal_total": settings.donation_goal_total, + "donation_goal_end": settings.donation_goal_end, + }) + + diff --git a/webapp/titanembeds/database/__init__.py b/webapp/titanembeds/database/__init__.py index 6801885..5642f80 100644 --- a/webapp/titanembeds/database/__init__.py +++ b/webapp/titanembeds/database/__init__.py @@ -14,6 +14,7 @@ from .token_transactions import TokenTransactions from .patreon import Patreon from .disabled_guilds import DisabledGuilds, list_disabled_guilds from .discordbotsorg_transactions import DiscordBotsOrgTransactions +from .application_settings import ApplicationSettings def set_titan_token(user_id, amt_change, action): token_count = get_titan_token(user_id) @@ -29,4 +30,14 @@ def set_titan_token(user_id, amt_change, action): db.session.add(transact) token_usr.tokens = new_token_count db.session.add(token_usr) - return True \ No newline at end of file + return True + +def init_application_settings(): + settings = db.session.query(ApplicationSettings).first() + if not settings: + settings = ApplicationSettings() + db.session.add(settings) + db.session.commit() + +def get_application_settings(): + return db.session.query(ApplicationSettings).first() diff --git a/webapp/titanembeds/database/application_settings.py b/webapp/titanembeds/database/application_settings.py new file mode 100644 index 0000000..48129d5 --- /dev/null +++ b/webapp/titanembeds/database/application_settings.py @@ -0,0 +1,13 @@ +from titanembeds.database import db + +class ApplicationSettings(db.Model): + __tablename__ = "application_settings" + id = db.Column(db.Integer, primary_key=True, nullable=False) # Auto increment id + donation_goal_progress = db.Column(db.Integer, nullable=False, server_default="0") # Current progress towards donation goal + donation_goal_total = db.Column(db.Integer, nullable=False, server_default="0") # Total donation required to hit goal. 0 to now show banners + donation_goal_end = db.Column(db.Date(), nullable=True) # When to end donation goal + + def __init__(self): + self.donation_goal_progress = 0 + self.donation_goal_total = 0 + self.donation_goal_end = None diff --git a/webapp/titanembeds/static/css/style.css b/webapp/titanembeds/static/css/style.css index 3e878f3..33c0d66 100644 --- a/webapp/titanembeds/static/css/style.css +++ b/webapp/titanembeds/static/css/style.css @@ -81,7 +81,7 @@ img.center-align { padding-bottom: 0px; } -#dblbanner { +#dblbanner, #donbanner { background-color: darkblue; text-align: center; font-size: 12pt; @@ -90,11 +90,11 @@ img.center-align { display: none; } -#dblbanner a { +#dblbanner a, #donbanner a { transition: font-size 0.5s; color: lightskyblue; } -#dblbanner a:hover { +#dblbanner a:hover, #donbanner a:hover { font-size: 14pt; } \ No newline at end of file diff --git a/webapp/titanembeds/static/js/admin_application_settings.js b/webapp/titanembeds/static/js/admin_application_settings.js new file mode 100644 index 0000000..5319151 --- /dev/null +++ b/webapp/titanembeds/static/js/admin_application_settings.js @@ -0,0 +1,26 @@ +function postForm(donation_goal_progress, donation_goal_total, donation_goal_end) { + var funct = $.ajax({ + dataType: "json", + method: "POST", + data: { + "donation_goal_progress": donation_goal_progress, + "donation_goal_total": donation_goal_total, + "donation_goal_end": donation_goal_end + } + }); + return funct.promise(); +} + +$("#submit").click(function () { + Materialize.toast("Saving changes...", 2000); + let donation_goal_progress = $("#donation_goal_progress").val(); + let donation_goal_total = $("#donation_goal_total").val(); + let donation_goal_end = $("#donation_goal_end").val(); + let req = postForm(donation_goal_progress, donation_goal_total, donation_goal_end); + req.done(function () { + Materialize.toast("All changes saved!", 2000); + }); + req.fail(function () { + Materialize.toast("There is an error saving changes.", 2000); + }); +}); diff --git a/webapp/titanembeds/templates/admin_application_settings.html.j2 b/webapp/titanembeds/templates/admin_application_settings.html.j2 new file mode 100644 index 0000000..e0070b5 --- /dev/null +++ b/webapp/titanembeds/templates/admin_application_settings.html.j2 @@ -0,0 +1,30 @@ +{% extends 'site_layout.html.j2' %} +{% set title="Manage Guilds as Administrator" %} + +{% block content %} +

Application Settings

+

Manage Titan Embeds

+
+
+

Donation Goal

+
+
+ + +
+
+ + +
+
+ + +
+
+ Submit +
+
+{% endblock %} +{% block script %} + +{% endblock %} diff --git a/webapp/titanembeds/templates/admin_index.html.j2 b/webapp/titanembeds/templates/admin_index.html.j2 index 87f8af6..a1e066c 100644 --- a/webapp/titanembeds/templates/admin_index.html.j2 +++ b/webapp/titanembeds/templates/admin_index.html.j2 @@ -56,5 +56,12 @@ Manage +
+
+

Application Settings

+

Configure Titan Embeds.

+ Manage +
+
{% endblock %} \ No newline at end of file diff --git a/webapp/titanembeds/templates/site_layout.html.j2 b/webapp/titanembeds/templates/site_layout.html.j2 index 11242f4..884490c 100644 --- a/webapp/titanembeds/templates/site_layout.html.j2 +++ b/webapp/titanembeds/templates/site_layout.html.j2 @@ -31,6 +31,7 @@ {% include 'google_analytics.html.j2' %} + {% if random.randrange(100) < 50 %}
Hey! Upvote us on Discord Bots List to show your @@ -38,6 +39,17 @@ golden name and rewards in return for your vote)
+ {% else %} +
+ + Hey! We need your help! Support the Titan Embeds project on Patreon to help us cover the cost of server hosting! + {% if application_settings.donation_goal_total %} +
+ Goal: ${{ application_settings.donation_goal_progress }} raised out of ${{ application_settings.donation_goal_total }}{% if application_settings.donation_goal_end %} by {{ application_settings.donation_goal_end }}{% endif %} + {% endif %} +
+
+ {% endif %}
{% if session['unauthenticated'] is defined and not session['unauthenticated'] %}