Converted webapp portion to python 3.5. Resolves #32

This commit is contained in:
Jeremy Zhang 2017-08-19 00:33:50 +00:00
parent 15011078c8
commit fb8cfe3abc
13 changed files with 46 additions and 53 deletions

View File

@ -14,11 +14,11 @@ There was a time when Discord doesn't support embedding the chat on a webpage. B
# Installation # Installation
Would you like to run your own copy of Titan Embeds? There are two parts that integrate nicely together. The webapp (website) handles the frontend and communication with the database to retrieve server messages, etc. The discordbot (bot) handles the communcation Would you like to run your own copy of Titan Embeds? There are two parts that integrate nicely together. The webapp (website) handles the frontend and communication with the database to retrieve server messages, etc. The discordbot (bot) handles the communcation
between Discord's websockets and pushing out the data to the database for the webapp. Check out the respective folder for their installation (pay attention to the python versions!) instructions. between Discord's websockets and pushing out the data to the database for the webapp. Check out the respective folder for their installation instructions.
# Database installation # Database installation
To set up the database for it to work with the webapp and the discordbot, one must use **alembic** to *migrate* their databases to the current database state. To do so, please follow these instructions. To set up the database for it to work with the webapp and the discordbot, one must use **alembic** to *migrate* their databases to the current database state. To do so, please follow these instructions.
1. Install alembic with **Python 2.7's pip** `pip install alembic` 1. Install alembic with **Python 3.5's pip** `pip install alembic`
2. Change your directory to the webapp where the alembic files are located `cd webapp` 2. Change your directory to the webapp where the alembic files are located `cd webapp`
3. Clone `alembic.example.ini` into your own `alembic.ini` file to find and edit the following line `sqlalchemy.url` to equal your database uri. [See here](http://docs.sqlalchemy.org/en/latest/core/engines.html#database-urls) if you need help understanding how database uri works in SQLalchemy. 3. Clone `alembic.example.ini` into your own `alembic.ini` file to find and edit the following line `sqlalchemy.url` to equal your database uri. [See here](http://docs.sqlalchemy.org/en/latest/core/engines.html#database-urls) if you need help understanding how database uri works in SQLalchemy.
4. In your terminal, run `alembic upgrade head` to upgrade your database tables to the current version on git. As long as there are only *INFO* messages and no errors, you should be fine. 4. In your terminal, run `alembic upgrade head` to upgrade your database tables to the current version on git. As long as there are only *INFO* messages and no errors, you should be fine.

View File

@ -9,17 +9,13 @@ cp ~/workspace/webapp/config.example.py ~/workspace/webapp/config.py
cp ~/workspace/discordbot/config.example.py ~/workspace/discordbot/config.py cp ~/workspace/discordbot/config.example.py ~/workspace/discordbot/config.py
cp ~/workspace/webapp/alembic.example.ini ~/workspace/webapp/alembic.ini cp ~/workspace/webapp/alembic.example.ini ~/workspace/webapp/alembic.ini
echo "[C9Setup] Installing discordbot dependencies" echo "[C9Setup] Installing Titan dependencies"
cd ~/workspace/discordbot/ cd ~/workspace/
sudo python3.5 -m pip install -r requirements.txt sudo python3.5 -m pip install -r requirements.txt
sudo python3.5 -m pip install pymysql sudo python3.5 -m pip install alembic pymysql
echo "[C9Setup] Installing webapp dependencies"
cd ~/workspace/webapp
sudo pip install -r requirements.txt
sudo pip install alembic pymysql
echo "[C9Setup] Auto populating alembic.ini database url and titan database table" echo "[C9Setup] Auto populating alembic.ini database url and titan database table"
cd ~/workspace/webapp
#sqlalchemy.url = mysql+pymysql://root@localhost/titan #sqlalchemy.url = mysql+pymysql://root@localhost/titan
sed -i '32s/.*/sqlalchemy.url = mysql+pymysql:\/\/root@localhost\/titan/' ~/workspace/webapp/alembic.ini sed -i '32s/.*/sqlalchemy.url = mysql+pymysql:\/\/root@localhost\/titan/' ~/workspace/webapp/alembic.ini
alembic upgrade head alembic upgrade head
@ -34,6 +30,7 @@ sed -i "11s/.*/\'database-uri\': \"mysql+pymysql:\/\/root@localhost\/titan\",/"
sed -i "8s/.*/\'app-location\': \"\/home\/ubuntu\/workspace\/webapp\/\",/" ~/workspace/webapp/config.py sed -i "8s/.*/\'app-location\': \"\/home\/ubuntu\/workspace\/webapp\/\",/" ~/workspace/webapp/config.py
echo "[C9Setup] Making sure everything can be ran" echo "[C9Setup] Making sure everything can be ran"
cd ~/workspace/
sudo chmod -R 777 * sudo chmod -R 777 *
echo "------------------------------" echo "------------------------------"

View File

@ -1,3 +1,8 @@
flask
flask-sqlalchemy
flask_limiter
requests_oauthlib
Flask-SSLify
paypalrestsdk
https://github.com/TitanEmbeds/discord.py/archive/async.zip#egg=discord.py[voice] https://github.com/TitanEmbeds/discord.py/archive/async.zip#egg=discord.py[voice]
sqlalchemy
asyncio_extras asyncio_extras

View File

@ -2,7 +2,7 @@
The webapp portion handles the frontend (it's what the users see). The webapp highly depends on the discordbot to push websockets data to the database. The webapp portion handles the frontend (it's what the users see). The webapp highly depends on the discordbot to push websockets data to the database.
# Installation # Installation
1. Clone the repo (make sure you have **Python 2.7** installed on your system. This webapp portion depends on that specific python version) 1. Clone the repo (make sure you have **Python 3.5** installed on your system. This webapp portion depends on that specific python version)
2. Install the pip requirements `pip install -r requirements.txt` 2. Install the pip requirements `pip install -r requirements.txt`
3. Clone `config.example.py` and rename it to `config.py`. Edit the file to your standards 3. Clone `config.example.py` and rename it to `config.py`. Edit the file to your standards
4. Run the development web via `python run.py` -- Though we suggest to use a better server software (look into gunicorn, nginx, uwsgi, etc) 4. Run the development web via `python run.py` -- Though we suggest to use a better server software (look into gunicorn, nginx, uwsgi, etc)

View File

@ -1,6 +0,0 @@
flask
flask-sqlalchemy
flask_limiter
requests_oauthlib
Flask-SSLify
paypalrestsdk

View File

@ -1,12 +1,9 @@
from config import config from config import config
from database import db from .database import db
from flask import Flask, render_template, request, session, url_for, redirect, jsonify from flask import Flask, render_template, request, session, url_for, redirect, jsonify
from flask_sslify import SSLify from flask_sslify import SSLify
from titanembeds.utils import rate_limiter, discord_api, bot_alive from titanembeds.utils import rate_limiter, discord_api, bot_alive
import blueprints.api from .blueprints import api, user, admin, embed
import blueprints.user
import blueprints.admin
import blueprints.embed
import os import os
from titanembeds.database import get_administrators_list from titanembeds.database import get_administrators_list
@ -24,10 +21,10 @@ db.init_app(app)
rate_limiter.init_app(app) rate_limiter.init_app(app)
sslify = SSLify(app, permanent=True) sslify = SSLify(app, permanent=True)
app.register_blueprint(blueprints.api.api, url_prefix="/api", template_folder="/templates") app.register_blueprint(api.api, url_prefix="/api", template_folder="/templates")
app.register_blueprint(blueprints.admin.admin, url_prefix="/admin", template_folder="/templates") app.register_blueprint(admin.admin, url_prefix="/admin", template_folder="/templates")
app.register_blueprint(blueprints.user.user, url_prefix="/user", template_folder="/templates") app.register_blueprint(user.user, url_prefix="/user", template_folder="/templates")
app.register_blueprint(blueprints.embed.embed, url_prefix="/embed", template_folder="/templates") app.register_blueprint(embed.embed, url_prefix="/embed", template_folder="/templates")
@app.route("/") @app.route("/")
def index(): def index():

View File

@ -1 +1 @@
from admin import admin from .admin import admin

View File

@ -1 +1 @@
from api import api from .api import api

View File

@ -1 +1 @@
from embed import embed from .embed import embed

View File

@ -1 +1 @@
from user import user from .user import user

View File

@ -2,18 +2,18 @@ from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy() db = SQLAlchemy()
from guilds import Guilds from .guilds import Guilds
from unauthenticated_users import UnauthenticatedUsers from .unauthenticated_users import UnauthenticatedUsers
from unauthenticated_bans import UnauthenticatedBans from .unauthenticated_bans import UnauthenticatedBans
from authenticated_users import AuthenticatedUsers from .authenticated_users import AuthenticatedUsers
from guild_members import GuildMembers, list_all_guild_members, get_guild_member from .guild_members import GuildMembers, list_all_guild_members, get_guild_member
from keyvalue_properties import KeyValueProperties, set_keyvalproperty, get_keyvalproperty, getexpir_keyvalproperty, setexpir_keyvalproperty, ifexists_keyvalproperty, delete_keyvalproperty 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 .messages import Messages, get_channel_messages
from cosmetics import Cosmetics from .cosmetics import Cosmetics
from user_css import UserCSS from .user_css import UserCSS
from administrators import Administrators, get_administrators_list from .administrators import Administrators, get_administrators_list
from titan_tokens import TitanTokens, get_titan_token from .titan_tokens import TitanTokens, get_titan_token
from token_transactions import TokenTransactions from .token_transactions import TokenTransactions
def set_titan_token(user_id, amt_change, action): def set_titan_token(user_id, amt_change, action):
token_count = get_titan_token(user_id) token_count = get_titan_token(user_id)

View File

@ -45,14 +45,14 @@ def user_has_permission(permission, index):
return bool((int(permission) >> index) & 1) return bool((int(permission) >> index) & 1)
def get_user_guilds(): def get_user_guilds():
cache = get_keyvalproperty("OAUTH/USERGUILDS/"+make_user_cache_key()) cache = get_keyvalproperty("OAUTH/USERGUILDS/"+str(make_user_cache_key()))
if cache: if cache:
return cache return cache
req = discordrest_from_user("/users/@me/guilds") req = discordrest_from_user("/users/@me/guilds")
if req.status_code != 200: if req.status_code != 200:
abort(req.status_code) abort(req.status_code)
req = json.dumps(req.json()) req = json.dumps(req.json())
set_keyvalproperty("OAUTH/USERGUILDS/"+make_user_cache_key(), req, 250) set_keyvalproperty("OAUTH/USERGUILDS/"+str(make_user_cache_key()), req, 250)
return req return req
def get_user_managed_servers(): def get_user_managed_servers():

View File

@ -16,12 +16,12 @@ def get_client_ipaddr():
ip = request.headers['X-Real-IP'] ip = request.headers['X-Real-IP']
else: # general else: # general
ip = request.remote_addr ip = request.remote_addr
return hashlib.sha512(config['app-secret'] + ip).hexdigest()[:15] return hashlib.sha512((config['app-secret'] + ip).encode('utf-8')).hexdigest()[:15]
def generate_session_key(): def generate_session_key():
sess = session.get("sessionunique", None) sess = session.get("sessionunique", None)
if not sess: if not sess:
rand_str = lambda n: ''.join([random.choice(string.lowercase) for i in xrange(n)]) rand_str = lambda n: ''.join([random.choice(string.ascii_lowercase) for i in range(n)])
session['sessionunique'] = rand_str(25) session['sessionunique'] = rand_str(25)
sess = session['sessionunique'] sess = session['sessionunique']
return sess #Totally unique return sess #Totally unique
@ -31,33 +31,33 @@ def make_cache_key(*args, **kwargs):
args = str(hash(frozenset(request.args.items()))) args = str(hash(frozenset(request.args.items())))
ip = get_client_ipaddr() ip = get_client_ipaddr()
sess = generate_session_key() sess = generate_session_key()
return (path + args + sess + ip).encode('utf-8') return (path + args + sess + ip)
def make_user_cache_key(*args, **kwargs): def make_user_cache_key(*args, **kwargs):
ip = get_client_ipaddr() ip = get_client_ipaddr()
sess = generate_session_key() sess = generate_session_key()
return (sess + ip).encode('utf-8') return (sess + ip)
def make_guilds_cache_key(): def make_guilds_cache_key():
sess = generate_session_key() sess = generate_session_key()
ip = get_client_ipaddr() ip = get_client_ipaddr()
return (sess + ip + "user_guilds").encode('utf-8') return (sess + ip + "user_guilds")
def make_guildchannels_cache_key(): def make_guildchannels_cache_key():
guild_id = request.values.get('guild_id', "0") guild_id = request.values.get('guild_id', "0")
sess = generate_session_key() sess = generate_session_key()
ip = get_client_ipaddr() ip = get_client_ipaddr()
return (sess + ip + guild_id + "user_guild_channels").encode('utf-8') return (sess + ip + guild_id + "user_guild_channels")
def channel_ratelimit_key(): # Generate a bucket with given channel & unique session key def channel_ratelimit_key(): # Generate a bucket with given channel & unique session key
sess = generate_session_key() sess = generate_session_key()
channel_id = request.values.get('channel_id', "0") channel_id = request.values.get('channel_id', "0")
return (sess + channel_id).encode('utf-8') return (sess + channel_id)
def guild_ratelimit_key(): def guild_ratelimit_key():
ip = get_client_ipaddr() ip = get_client_ipaddr()
guild_id = request.values.get('guild_id', "0") guild_id = request.values.get('guild_id', "0")
return (ip + guild_id).encode('utf-8') return (ip + guild_id)
def check_guild_existance(guild_id): def check_guild_existance(guild_id):
dbGuild = Guilds.query.filter_by(guild_id=guild_id).first() dbGuild = Guilds.query.filter_by(guild_id=guild_id).first()