adding admin

master
lza_menace 2 years ago
parent e3a3e7aa19
commit d2092ab496

@ -18,6 +18,28 @@ def cli(app):
model = peewee.Model.__subclasses__() model = peewee.Model.__subclasses__()
db.create_tables(model) 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') @app.cli.command('verify_tips')
def verify_tips(): def verify_tips():
txes = Transaction.select().where(Transaction.verified == False) 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 flask_login import current_user
from functools import wraps from functools import wraps
from nerochan.models import User, CreatorProfile, BackerProfile, Subscription from nerochan.models import User
def admin_required(f):
# 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):
@wraps(f) @wraps(f)
def decorated_function(*args, **kwargs): def decorated_function(*args, **kwargs):
print(current_user) if current_user.is_admin:
# m = Moderator.filter(username=session["auth"]["preferred_username"]) return f(*args, **kwargs)
# if m: else:
# return f(*args, **kwargs) flash('Must be an admin to access that page.', 'warning')
# else: if request.referrer:
# flash("You are not a moderator") u = request.referrer
# return redirect(url_for("index")) else:
u = url_for('main.index')
return redirect(u)
return decorated_function return decorated_function

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

@ -3,6 +3,7 @@ from wtforms import StringField, FloatField
from wtforms.validators import DataRequired, ValidationError from wtforms.validators import DataRequired, ValidationError
from monero.address import address from monero.address import address
from nerochan.models import User
from nerochan import config from nerochan import config
@ -10,7 +11,6 @@ def is_valid_xmr_address(form, field):
try: try:
# Ensure the provided address is valid address/subaddress/integrated address # Ensure the provided address is valid address/subaddress/integrated address
a = address(field.data) a = address(field.data)
print(config.XMR_WALLET_NETWORK)
# Ensure the provided address matches the network that the application's wallet is using # Ensure the provided address matches the network that the application's wallet is using
if not config.XMR_WALLET_NETWORK.startswith(a.net): if not config.XMR_WALLET_NETWORK.startswith(a.net):
raise ValidationError('Provided Monero address does not match the configured network. Application: {}. Provided: {}'.format( 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: except ValueError:
raise ValidationError('Invalid Monero address provided') 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): class UserRegistration(FlaskForm):
handle = StringField('Handle:', validators=[DataRequired()], render_kw={'placeholder': 'online handle', 'class': 'u-full-width', 'type': 'text'}) 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'}) 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): class UserForm(FlaskForm):
handle = StringField('Handle:', validators=[DataRequired()], render_kw={'placeholder': 'online handle', 'class': 'u-full-width', 'type': 'text'}) handle = StringField('Handle:', validators=[DataRequired(), is_valid_user], render_kw={'placeholder': 'handle', 'class': 'u-full-width', 'type': 'text'})
class UserChallenge(FlaskForm): class UserChallenge(FlaskForm):

@ -1,20 +1,37 @@
from flask import Blueprint, render_template, redirect, url_for, flash from flask import Blueprint, render_template, redirect, request, flash, url_for
from flask_login import login_required from flask_login import login_required, current_user
from nerochan.forms import UserForm
from nerochan.decorators import admin_required
from nerochan.models import User from nerochan.models import User
bp = Blueprint('admin', 'admin', url_prefix='/admin') bp = Blueprint('admin', 'admin', url_prefix='/admin')
@bp.route('/') @bp.route('', methods=['GET', 'POST'])
def main(handle: str): @login_required
user = User.select().where(User.handle == handle).first() @admin_required
if not user: def main():
flash('That user does not exist.', 'warning') user_form = UserForm()
return redirect(url_for('main.index')) 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( return render_template(
'user/show.html', 'admin/main.html',
user=user admins=admins,
user_form=user_form
) )
# approve artwork # approve artwork
@ -22,6 +39,4 @@ def main(handle: str):
# allow user # allow user
# hide artwork # hide artwork
# promote mod # promote mod
# demote 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 import flash, redirect, url_for
from flask_login import login_user, logout_user, current_user 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.helpers import make_wallet_rpc
from nerochan.models import User from nerochan.models import User
@ -38,7 +38,7 @@ def register():
@bp.route("/login", methods=["GET", "POST"]) @bp.route("/login", methods=["GET", "POST"])
def login(): def login():
form = UserLogin() form = UserForm()
if current_user.is_authenticated: if current_user.is_authenticated:
flash('Already logged in.') flash('Already logged in.')
return redirect(url_for('main.index')) return redirect(url_for('main.index'))

@ -8,6 +8,14 @@ a, a:visited {
color: white; color: white;
} }
.ml-2 {
margin-left: 2em;
}
ul {
list-style: circle;
}
.container-wide { .container-wide {
max-width: 90%; max-width: 90%;
margin-left: 3em; 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> <body>
{% include 'includes/navbar.html' %} {% include 'includes/navbar.html' %}
{% block content %}{% endblock %} {% block content %}{% endblock %}
{% include 'includes/debug.html' %}
</body> </body>
</html> </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.index') }}">Home</a></li>
<li class="navbar-item"><a class="navbar-link" href="{{ url_for('main.about') }}">About</a></li> <li class="navbar-item"><a class="navbar-link" href="{{ url_for('main.about') }}">About</a></li>
{%- if current_user.is_authenticated %} {%- 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 %} {%- 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 %} {%- endif %}
</ul> </ul>
</div> </div>

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

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

Loading…
Cancel
Save