From f4608794a072d94b8cf6072abf938ece1d2d2b33 Mon Sep 17 00:00:00 2001 From: Jeremy Zhang Date: Sat, 22 Jul 2017 02:06:45 +0000 Subject: [PATCH] Move devs to the administrators table --- ...fe130518448_create_administrators_table.py | 148 ++++++++++++++++++ webapp/titanembeds/__init__.py | 2 - webapp/titanembeds/app.py | 5 +- webapp/titanembeds/blueprints/admin/admin.py | 4 +- webapp/titanembeds/blueprints/api/api.py | 4 +- webapp/titanembeds/database/__init__.py | 3 +- webapp/titanembeds/database/administrators.py | 13 ++ webapp/titanembeds/database/cosmetics.py | 9 +- 8 files changed, 177 insertions(+), 11 deletions(-) create mode 100644 webapp/alembic/versions/6fe130518448_create_administrators_table.py create mode 100644 webapp/titanembeds/database/administrators.py diff --git a/webapp/alembic/versions/6fe130518448_create_administrators_table.py b/webapp/alembic/versions/6fe130518448_create_administrators_table.py new file mode 100644 index 0000000..2968436 --- /dev/null +++ b/webapp/alembic/versions/6fe130518448_create_administrators_table.py @@ -0,0 +1,148 @@ +"""Create administrators table + +Revision ID: 6fe130518448 +Revises: 40cbd3e0f22d +Create Date: 2017-07-22 02:00:35.317471 + +""" + +# revision identifiers, used by Alembic. +revision = '6fe130518448' +down_revision = '40cbd3e0f22d' +branch_labels = None +depends_on = None + +from alembic import op +import sqlalchemy as sa +from sqlalchemy.dialects import mysql + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.create_table('administrators', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('user_id', sa.String(length=255), nullable=False), + sa.PrimaryKeyConstraint('id') + ) + op.alter_column(u'cosmetics', 'css', + existing_type=mysql.TINYINT(display_width=1), + type_=sa.Boolean(), + existing_nullable=False) + op.alter_column(u'guild_members', 'active', + existing_type=mysql.TINYINT(display_width=1), + type_=sa.Boolean(), + existing_nullable=False, + existing_server_default=sa.text(u"'1'")) + op.alter_column(u'guild_members', 'banned', + existing_type=mysql.TINYINT(display_width=1), + type_=sa.Boolean(), + existing_nullable=False, + existing_server_default=sa.text(u"'0'")) + op.alter_column(u'guilds', 'bracket_links', + existing_type=mysql.TINYINT(display_width=1), + type_=sa.Boolean(), + existing_nullable=False, + existing_server_default=sa.text(u"'1'")) + op.alter_column(u'guilds', 'channels', + existing_type=mysql.LONGTEXT(collation=u'utf8mb4_unicode_ci'), + type_=sa.Text(length=4294967295), + existing_nullable=False) + op.alter_column(u'guilds', 'chat_links', + existing_type=mysql.TINYINT(display_width=1), + type_=sa.Boolean(), + existing_nullable=False, + existing_server_default=sa.text(u"'1'")) + op.alter_column(u'guilds', 'emojis', + existing_type=mysql.LONGTEXT(collation=u'utf8mb4_unicode_ci'), + type_=sa.Text(length=4294967295), + existing_nullable=False) + op.alter_column(u'guilds', 'roles', + existing_type=mysql.LONGTEXT(collation=u'utf8mb4_unicode_ci'), + type_=sa.Text(length=4294967295), + existing_nullable=False) + op.alter_column(u'guilds', 'unauth_users', + existing_type=mysql.TINYINT(display_width=1), + type_=sa.Boolean(), + existing_nullable=False, + existing_server_default=sa.text(u"'1'")) + op.alter_column(u'guilds', 'visitor_view', + existing_type=mysql.TINYINT(display_width=1), + type_=sa.Boolean(), + existing_nullable=False) + op.alter_column(u'guilds', 'webhooks', + existing_type=mysql.LONGTEXT(collation=u'utf8mb4_unicode_ci'), + type_=sa.Text(length=4294967295), + existing_nullable=False) + op.alter_column(u'unauthenticated_users', 'revoked', + existing_type=mysql.TINYINT(display_width=1), + type_=sa.Boolean(), + existing_nullable=False, + existing_server_default=sa.text(u"'0'")) + op.alter_column(u'user_css', 'css', + existing_type=mysql.LONGTEXT(collation=u'utf8mb4_unicode_ci'), + type_=sa.Text(length=4294967295), + existing_nullable=True) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.alter_column(u'user_css', 'css', + existing_type=sa.Text(length=4294967295), + type_=mysql.LONGTEXT(collation=u'utf8mb4_unicode_ci'), + existing_nullable=True) + op.alter_column(u'unauthenticated_users', 'revoked', + existing_type=sa.Boolean(), + type_=mysql.TINYINT(display_width=1), + existing_nullable=False, + existing_server_default=sa.text(u"'0'")) + op.alter_column(u'guilds', 'webhooks', + existing_type=sa.Text(length=4294967295), + type_=mysql.LONGTEXT(collation=u'utf8mb4_unicode_ci'), + existing_nullable=False) + op.alter_column(u'guilds', 'visitor_view', + existing_type=sa.Boolean(), + type_=mysql.TINYINT(display_width=1), + existing_nullable=False) + op.alter_column(u'guilds', 'unauth_users', + existing_type=sa.Boolean(), + type_=mysql.TINYINT(display_width=1), + existing_nullable=False, + existing_server_default=sa.text(u"'1'")) + op.alter_column(u'guilds', 'roles', + existing_type=sa.Text(length=4294967295), + type_=mysql.LONGTEXT(collation=u'utf8mb4_unicode_ci'), + existing_nullable=False) + op.alter_column(u'guilds', 'emojis', + existing_type=sa.Text(length=4294967295), + type_=mysql.LONGTEXT(collation=u'utf8mb4_unicode_ci'), + existing_nullable=False) + op.alter_column(u'guilds', 'chat_links', + existing_type=sa.Boolean(), + type_=mysql.TINYINT(display_width=1), + existing_nullable=False, + existing_server_default=sa.text(u"'1'")) + op.alter_column(u'guilds', 'channels', + existing_type=sa.Text(length=4294967295), + type_=mysql.LONGTEXT(collation=u'utf8mb4_unicode_ci'), + existing_nullable=False) + op.alter_column(u'guilds', 'bracket_links', + existing_type=sa.Boolean(), + type_=mysql.TINYINT(display_width=1), + existing_nullable=False, + existing_server_default=sa.text(u"'1'")) + op.alter_column(u'guild_members', 'banned', + existing_type=sa.Boolean(), + type_=mysql.TINYINT(display_width=1), + existing_nullable=False, + existing_server_default=sa.text(u"'0'")) + op.alter_column(u'guild_members', 'active', + existing_type=sa.Boolean(), + type_=mysql.TINYINT(display_width=1), + existing_nullable=False, + existing_server_default=sa.text(u"'1'")) + op.alter_column(u'cosmetics', 'css', + existing_type=sa.Boolean(), + type_=mysql.TINYINT(display_width=1), + existing_nullable=False) + op.drop_table('administrators') + # ### end Alembic commands ### diff --git a/webapp/titanembeds/__init__.py b/webapp/titanembeds/__init__.py index 55c0afe..e69de29 100644 --- a/webapp/titanembeds/__init__.py +++ b/webapp/titanembeds/__init__.py @@ -1,2 +0,0 @@ -global devs -devs = [ "138881969185357825" , "197322731115642880", "193908323911860224" ] \ No newline at end of file diff --git a/webapp/titanembeds/app.py b/webapp/titanembeds/app.py index 14afba9..c193b30 100644 --- a/webapp/titanembeds/app.py +++ b/webapp/titanembeds/app.py @@ -8,7 +8,7 @@ import blueprints.user import blueprints.admin import blueprints.embed import os -from titanembeds import devs +from titanembeds.database import get_administrators_list os.chdir(config['app-location']) @@ -39,10 +39,9 @@ def about(): @app.before_request def before_request(): - db.create_all() discord_api.init_discordrest() @app.context_processor def context_processor(): bot_status = bot_alive() - return {"bot_status": bot_status, "devs": devs} \ No newline at end of file + return {"bot_status": bot_status, "devs": get_administrators_list()} \ No newline at end of file diff --git a/webapp/titanembeds/blueprints/admin/admin.py b/webapp/titanembeds/blueprints/admin/admin.py index 381a774..286d070 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 functools import wraps -from titanembeds import devs +from titanembeds.database import get_administrators_list admin = Blueprint("admin", __name__) @@ -10,7 +10,7 @@ def is_admin(f): def decorated_function(*args, **kwargs): if 'user_id' not in session: return redirect(url_for("index")) - if session['user_id'] not in devs: + if session['user_id'] not in get_administrators_list(): return redirect(url_for("index")) return f(*args, **kwargs) return decorated_function diff --git a/webapp/titanembeds/blueprints/api/api.py b/webapp/titanembeds/blueprints/api/api.py index 0572595..4fe7ea9 100644 --- a/webapp/titanembeds/blueprints/api/api.py +++ b/webapp/titanembeds/blueprints/api/api.py @@ -2,7 +2,7 @@ from titanembeds.database import db, Guilds, UnauthenticatedUsers, Unauthenticat from titanembeds.decorators import valid_session_required, discord_users_only from titanembeds.utils import check_guild_existance, guild_accepts_visitors, guild_query_unauth_users_bool, get_client_ipaddr, discord_api, rate_limiter, channel_ratelimit_key, guild_ratelimit_key from titanembeds.oauth import user_has_permission, generate_avatar_url, check_user_can_administrate_guild -from titanembeds import devs +from titanembeds.database import get_administrators_list from flask import Blueprint, abort, jsonify, session, request, url_for from sqlalchemy import and_ import random @@ -381,7 +381,7 @@ def post(): userid = session["user_id"] content = format_everyone_mention(chan, content) webhook = get_channel_webhook_url(guild_id, channel_id) - if userid in devs: + if userid in get_administrators_list(): oldcontent = content content = "(Titan Dev) " + oldcontent if webhook: diff --git a/webapp/titanembeds/database/__init__.py b/webapp/titanembeds/database/__init__.py index 0450efc..6e6bbf0 100644 --- a/webapp/titanembeds/database/__init__.py +++ b/webapp/titanembeds/database/__init__.py @@ -10,4 +10,5 @@ from guild_members import GuildMembers, list_all_guild_members from keyvalue_properties import KeyValueProperties, set_keyvalproperty, get_keyvalproperty, getexpir_keyvalproperty, setexpir_keyvalproperty, ifexists_keyvalproperty, delete_keyvalproperty from messages import Messages, get_channel_messages from cosmetics import Cosmetics -from user_css import UserCSS \ No newline at end of file +from user_css import UserCSS +from administrators import Administrators, get_administrators_list \ No newline at end of file diff --git a/webapp/titanembeds/database/administrators.py b/webapp/titanembeds/database/administrators.py new file mode 100644 index 0000000..41cf3a5 --- /dev/null +++ b/webapp/titanembeds/database/administrators.py @@ -0,0 +1,13 @@ +from titanembeds.database import db + +class Administrators(db.Model): + __tablename__ = "administrators" + id = db.Column(db.Integer, primary_key=True) # Auto increment id + user_id = db.Column(db.String(255), nullable=False) # Discord user id of user of an administrator + +def get_administrators_list(): + q = db.session.query(Administrators).all() + their_ids = [] + for admin in q: + their_ids.append(admin.user_id) + return their_ids \ No newline at end of file diff --git a/webapp/titanembeds/database/cosmetics.py b/webapp/titanembeds/database/cosmetics.py index a7a5295..305bc7c 100644 --- a/webapp/titanembeds/database/cosmetics.py +++ b/webapp/titanembeds/database/cosmetics.py @@ -4,4 +4,11 @@ class Cosmetics(db.Model): __tablename__ = "cosmetics" id = db.Column(db.Integer, primary_key=True) # Auto increment id user_id = db.Column(db.String(255), nullable=False) # Discord user id of user of cosmetics - css = db.Column(db.Boolean(), nullable=False) # If they can create/edit custom CSS \ No newline at end of file + 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"]