move user ban functionality to the database instead of config

revamp
lza_menace 3 years ago
parent 6ef47009b8
commit 226e7bbe15

@ -9,7 +9,7 @@ from flask import Flask, request, session, redirect
from flask import render_template, flash, url_for from flask import render_template, flash, url_for
from flask_session import Session from flask_session import Session
from suchwow import config from suchwow import config
from suchwow.models import Post, Profile, Comment, Notification, db, Moderator from suchwow.models import Post, Profile, Comment, Notification, db, Moderator, Ban
from suchwow.routes import auth, comment, post, profile, leaderboard, api from suchwow.routes import auth, comment, post, profile, leaderboard, api
from suchwow.utils.decorators import login_required, moderator_required from suchwow.utils.decorators import login_required, moderator_required
from suchwow.utils.helpers import post_webhook, get_latest_tipped_posts from suchwow.utils.helpers import post_webhook, get_latest_tipped_posts
@ -85,7 +85,7 @@ def init():
makedirs(f"{config.DATA_FOLDER}/{i}", exist_ok=True) makedirs(f"{config.DATA_FOLDER}/{i}", exist_ok=True)
# init db # init db
db.create_tables([Post, Profile, Comment, Notification, Moderator]) db.create_tables([Post, Profile, Comment, Notification, Moderator, Ban])
@app.cli.command("post_reddit") @app.cli.command("post_reddit")
@click.argument('last_hours') @click.argument('last_hours')
@ -153,6 +153,40 @@ def remove_admin(username):
else: else:
print("That moderator doesn't exist") print("That moderator doesn't exist")
@app.cli.command("ban_user")
@click.argument("username")
@click.argument("reason", nargs=-1)
def ban_user(username, reason):
u = Profile.filter(username=username).first()
b = Ban.filter(user=u).first()
if b:
print("User already banned")
return False
if u:
b = Ban(user=u, reason=' '.join(reason))
b.save()
print(f"Banned {username}")
return
else:
print("That user doesn't exist")
return
@app.cli.command("unban_user")
@click.argument("username")
def ban_user(username):
u = Profile.filter(username=username).first()
if not u:
print("That user doesn't exist")
return False
b = Ban.filter(user=u).first()
if b:
b.delete_instance()
print(f"Unbanned {username}")
return True
else:
print("That user isn't banned")
return False
@app.cli.command("show") @app.cli.command("show")
@click.argument("post_id") @click.argument("post_id")
def post_id(post_id): def post_id(post_id):

@ -21,9 +21,6 @@ WALLET_RPC_USER = getenv('WALLET_RPC_USER', 'suchwow') #
WALLET_RPC_PASS = getenv('WALLET_RPC_PASS', 'suchwow') # WALLET_RPC_PASS = getenv('WALLET_RPC_PASS', 'suchwow') #
WALLET_PASS = getenv('WALLET_PASS', 'zzzzzzz') # You specify all these wallet details in .env WALLET_PASS = getenv('WALLET_PASS', 'zzzzzzz') # You specify all these wallet details in .env
# Optional for banning users who post crazy shit (they do)
BANNED_USERS = {'username': 'reason for the ban'}
# Optional for posting to Reddit # Optional for posting to Reddit
PRAW_CLIENT_SECRET = getenv('PRAW_CLIENT_SECRET', None) PRAW_CLIENT_SECRET = getenv('PRAW_CLIENT_SECRET', None)
PRAW_CLIENT_ID = getenv('PRAW_CLIENT_ID', None) PRAW_CLIENT_ID = getenv('PRAW_CLIENT_ID', None)

@ -123,3 +123,12 @@ class Notification(Model):
class Meta: class Meta:
database = db database = db
class Ban(Model):
id = AutoField()
user = ForeignKeyField(Profile)
reason = TextField()
timestamp = DateTimeField(default=datetime.now)
class Meta:
database = db

@ -9,7 +9,7 @@ from werkzeug.utils import secure_filename
from secrets import token_urlsafe from secrets import token_urlsafe
from suchwow import wownero from suchwow import wownero
from suchwow import config from suchwow import config
from suchwow.models import Post, Comment from suchwow.models import Post, Profile, Comment, Ban
from suchwow.utils.decorators import login_required, profile_required, moderator_required from suchwow.utils.decorators import login_required, profile_required, moderator_required
from suchwow.utils.helpers import allowed_file, is_moderator, get_session_user from suchwow.utils.helpers import allowed_file, is_moderator, get_session_user
from suchwow.utils.helpers import rw_cache, post_webhook from suchwow.utils.helpers import rw_cache, post_webhook
@ -54,12 +54,14 @@ def read(id):
@login_required @login_required
@profile_required @profile_required
def create(): def create():
if request.method == "POST":
submitter = get_session_user() submitter = get_session_user()
if submitter in config.BANNED_USERS: u = Profile.filter(username=submitter)
reason = config.BANNED_USERS[submitter] banned = Ban.filter(user=u).first()
flash(f"You can't post for now: {reason}", "is-danger") print(banned)
if banned:
flash(f"You can't post: {banned.reason}", "is-danger")
return redirect("/") return redirect("/")
if request.method == "POST":
post_title = request.form.get("title") post_title = request.form.get("title")
# check if the post request has the file part # check if the post request has the file part
if "file" not in request.files: if "file" not in request.files:

Loading…
Cancel
Save