adding admin

master
lza_menace 2 years ago
parent e3a3e7aa19
commit d2092ab496

@ -18,6 +18,28 @@ def cli(app):
model = peewee.Model.__subclasses__()
db.create_tables(model)
@click.argument('handle')
@app.cli.command('add_admin')
def add_admin(handle):
user = User.select().where(User.handle == handle).first()
if not user:
click.echo('user does not exist')
return False
user.is_admin = True
user.save()
click.echo(f'{handle} is now an admin')
@click.argument('handle')
@app.cli.command('remove_admin')
def remove_admin(handle):
user = User.select().where(User.handle == handle).first()
if not user:
click.echo('user does not exist')
return False
user.is_admin = False
user.save()
click.echo(f'{handle} is no longer an admin')
@app.cli.command('verify_tips')
def verify_tips():
txes = Transaction.select().where(Transaction.verified == False)

@ -1,25 +1,18 @@
from flask import session, redirect, url_for, flash
from flask import redirect, flash, request, url_for
from flask_login import current_user
from functools import wraps
from nerochan.models import User, CreatorProfile, BackerProfile, Subscription
from nerochan.models import User
# def login_required(f):
# @wraps(f)
# def decorated_function(*args, **kwargs):
# if "auth" not in session or not session["auth"]:
# return redirect(url_for("auth.login"))
# return f(*args, **kwargs)
# return decorated_function
def subscription_required(f):
def admin_required(f):
@wraps(f)
def decorated_function(*args, **kwargs):
print(current_user)
# m = Moderator.filter(username=session["auth"]["preferred_username"])
# if m:
# return f(*args, **kwargs)
# else:
# flash("You are not a moderator")
# return redirect(url_for("index"))
if current_user.is_admin:
return f(*args, **kwargs)
else:
flash('Must be an admin to access that page.', 'warning')
if request.referrer:
u = request.referrer
else:
u = url_for('main.index')
return redirect(u)
return decorated_function

@ -28,13 +28,14 @@ def create_app():
return User.get_or_none(user_id)
with app.app_context():
from nerochan.routes import api, auth, main, artwork, user
from nerochan.routes import api, auth, main, artwork, user, admin
from nerochan import filters
app.register_blueprint(main.bp)
app.register_blueprint(api.bp)
app.register_blueprint(auth.bp)
app.register_blueprint(artwork.bp)
app.register_blueprint(user.bp)
app.register_blueprint(admin.bp)
app.register_blueprint(filters.bp)
return app

@ -3,6 +3,7 @@ from wtforms import StringField, FloatField
from wtforms.validators import DataRequired, ValidationError
from monero.address import address
from nerochan.models import User
from nerochan import config
@ -10,7 +11,6 @@ def is_valid_xmr_address(form, field):
try:
# Ensure the provided address is valid address/subaddress/integrated address
a = address(field.data)
print(config.XMR_WALLET_NETWORK)
# Ensure the provided address matches the network that the application's wallet is using
if not config.XMR_WALLET_NETWORK.startswith(a.net):
raise ValidationError('Provided Monero address does not match the configured network. Application: {}. Provided: {}'.format(
@ -19,14 +19,23 @@ def is_valid_xmr_address(form, field):
except ValueError:
raise ValidationError('Invalid Monero address provided')
def is_valid_user(form, field):
try:
u = User.select().where(User.handle == field.data).first()
if not u:
raise ValidationError('User does not exist')
return True
except ValueError:
raise ValidationError('Error looking up user')
class UserRegistration(FlaskForm):
handle = StringField('Handle:', validators=[DataRequired()], render_kw={'placeholder': 'online handle', 'class': 'u-full-width', 'type': 'text'})
wallet_address = StringField('Wallet Address:', validators=[DataRequired(), is_valid_xmr_address], render_kw={'placeholder': 'monero wallet address', 'class': 'u-full-width', 'type': 'text'})
class UserLogin(FlaskForm):
handle = StringField('Handle:', validators=[DataRequired()], render_kw={'placeholder': 'online handle', 'class': 'u-full-width', 'type': 'text'})
class UserForm(FlaskForm):
handle = StringField('Handle:', validators=[DataRequired(), is_valid_user], render_kw={'placeholder': 'handle', 'class': 'u-full-width', 'type': 'text'})
class UserChallenge(FlaskForm):

@ -1,20 +1,37 @@
from flask import Blueprint, render_template, redirect, url_for, flash
from flask_login import login_required
from flask import Blueprint, render_template, redirect, request, flash, url_for
from flask_login import login_required, current_user
from nerochan.forms import UserForm
from nerochan.decorators import admin_required
from nerochan.models import User
bp = Blueprint('admin', 'admin', url_prefix='/admin')
@bp.route('/')
def main(handle: str):
user = User.select().where(User.handle == handle).first()
if not user:
flash('That user does not exist.', 'warning')
return redirect(url_for('main.index'))
@bp.route('', methods=['GET', 'POST'])
@login_required
@admin_required
def main():
user_form = UserForm()
if user_form.validate_on_submit():
u = User.select().where(User.handle == user_form.handle.data).first()
u.is_admin = True
u.save()
return redirect(request.referrer)
if request.args.get('remove'):
a = User.select().where(User.handle == request.args.get('remove')).first()
if a == current_user:
flash('cannot delete yourself')
return redirect(url_for('admin.main'))
if a:
a.is_admin = False
a.save()
return redirect(url_for('admin.main'))
admins = User.select().where(User.is_admin == True).order_by(User.register_date.desc())
return render_template(
'user/show.html',
user=user
'admin/main.html',
admins=admins,
user_form=user_form
)
# approve artwork
@ -23,5 +40,3 @@ def main(handle: str):
# hide artwork
# promote mod
# demote mod
# promote admin
# demote admin

@ -2,7 +2,7 @@ from flask import Blueprint, render_template
from flask import flash, redirect, url_for
from flask_login import login_user, logout_user, current_user
from nerochan.forms import UserLogin, UserRegistration, UserChallenge
from nerochan.forms import UserForm, UserRegistration, UserChallenge
from nerochan.helpers import make_wallet_rpc
from nerochan.models import User
@ -38,7 +38,7 @@ def register():
@bp.route("/login", methods=["GET", "POST"])
def login():
form = UserLogin()
form = UserForm()
if current_user.is_authenticated:
flash('Already logged in.')
return redirect(url_for('main.index'))

@ -8,6 +8,14 @@ a, a:visited {
color: white;
}
.ml-2 {
margin-left: 2em;
}
ul {
list-style: circle;
}
.container-wide {
max-width: 90%;
margin-left: 3em;

@ -0,0 +1,33 @@
{% extends 'includes/base.html' %}
{% block content %}
<div class="container">
<div class="row">
<h1>admins</h1>
<p>Add a homie</p>
<form method="post" action="">
{{ user_form.csrf_token }}
<div class="row">
<div class="two columns">
{{ user_form.handle }}
</div>
<div class="two columns">
<input class="button-primary" type="submit" value="Submit">
</div>
</div>
<ul>
{%- for field, errors in user_form.errors.items() %}
<li>{{ ', '.join(errors) }}</li>
{%- endfor %}
</ul>
</form>
<ul>
{% for admin in admins %}
<li><h6>{{ admin.handle }} - <a href="?remove={{ admin.handle }}">remove</a></h6></li>
{% endfor %}
</ul>
</div>
</div>
{% endblock %}

@ -4,6 +4,5 @@
<body>
{% include 'includes/navbar.html' %}
{% block content %}{% endblock %}
{% include 'includes/debug.html' %}
</body>
</html>

@ -4,9 +4,12 @@
<li class="navbar-item"><a class="navbar-link" href="{{ url_for('main.index') }}">Home</a></li>
<li class="navbar-item"><a class="navbar-link" href="{{ url_for('main.about') }}">About</a></li>
{%- if current_user.is_authenticated %}
<li class="navbar-item"><a class="navbar-link" href="{{ url_for('auth.logout') }}">Logout</a></li>
{%- if current_user.is_admin %}
<li class="navbar-item"><a class="navbar-link" href="{{ url_for('admin.main') }}">Admin</a></li>
{% endif %}
<li class="navbar-item ml-2"><a class="navbar-link" href="{{ url_for('auth.logout') }}">Logout</a></li>
{%- else %}
<li class="navbar-item"><a class="navbar-link" href="{{ url_for('auth.login') }}">Login</a></li>
<li class="navbar-item"><a class="navbar-link" href="{{ url_for('auth.login') }}">Login</a></li>
{%- endif %}
</ul>
</div>

@ -4,7 +4,7 @@
<div class="container">
<div class="row">
<h2>artworks</h2>
<h1>artworks</h1>
{% if feed['artwork'] %}
{%- for _artwork in feed['artwork'] | batch(4) %}
{%- for artwork in _artwork %}
@ -19,7 +19,7 @@
{% endif %}
</div>
<div class="row">
<h2>artists</h2>
<h1>artists</h1>
{% if feed['users'] %}
{% for user in feed['users'] %}
<p><a href="{{ url_for('user.show', handle=user.handle) }}">{{ user.handle }}</a></p>

@ -2,8 +2,6 @@
{% block content %}
<div class="container">
<div class="row">
<h1>{{ user.handle }}</h1>
@ -11,6 +9,4 @@
</div>
</div>
{% endblock %}

Loading…
Cancel
Save