setting up decorators, routes for subscriptions, subscription enforcement

main
lza_menace 4 years ago
parent 3a3037b82a
commit 3ecdec68aa

@ -14,3 +14,4 @@ WTForms
quart
monero
arrow
flake8

@ -0,0 +1,25 @@
from quart import session, redirect, url_for, flash
from flask_login import current_user
from functools import wraps
from xmrbackers.models import User, CreatorProfile, BackerProfile, Subscription
# 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)
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"))
return decorated_function

@ -20,13 +20,14 @@ def create_app():
app.config.from_envvar('QUART_SECRETS')
@app.before_serving
async def startup():
from xmrbackers.routes import meta, api, auth, creator
from xmrbackers.routes import meta, api, auth, creator, post
from xmrbackers import filters
await _setup_db(app)
app.register_blueprint(meta.bp)
app.register_blueprint(api.bp)
app.register_blueprint(auth.bp)
app.register_blueprint(creator.bp)
app.register_blueprint(post.bp)
app.register_blueprint(filters.bp)
login_manager = LoginManager(app)

@ -1,6 +1,7 @@
import arrow
from datetime import datetime
import arrow
import monero
from quart import Blueprint, current_app
@ -18,3 +19,7 @@ def from_ts(v):
@bp.app_template_filter('xmr_block_explorer')
def xmr_block_explorer(v):
return f'https://www.exploremonero.com/transaction/{v}'
@bp.app_template_filter('from_atomic')
def from_atomic(amt):
return monero.numbers.from_atomic(amt)

@ -1,4 +1,3 @@
import quart.flask_patch
from quart import Blueprint, render_template
from quart import flash, redirect, url_for
from flask_login import login_user, logout_user, current_user

@ -1,6 +1,6 @@
from quart import Blueprint, render_template, flash, redirect
from quart import Blueprint, render_template, flash, redirect, url_for
from xmrbackers.models import User, CreatorProfile
from xmrbackers.models import User, CreatorProfile, TextPost, SubscriptionMeta
bp = Blueprint('creator', 'creator')
@ -19,7 +19,33 @@ async def show(username):
CreatorProfile.user == user
).first()
if creator:
return await render_template('creator/creator.html', creator=creator)
posts = TextPost.select().where(
TextPost.creator == creator,
TextPost.hidden == False
).order_by(TextPost.post_date.desc())
return await render_template(
'creator/creator.html',
creator=creator,
posts=posts
)
else:
flash('That creator does not exist.')
await flash('That creator does not exist.')
return redirect(url_for('meta.index'))
@bp.route('/creator/<username>/subscription')
async def subscription(username):
user = User.select().where(User.username == username)
creator = CreatorProfile.select().where(
CreatorProfile.user == user
)
if creator:
subscription_meta = SubscriptionMeta.select().where(
SubscriptionMeta.creator == creator
).order_by(SubscriptionMeta.create_date.desc()).first()
return await render_template(
'creator/subscription.html',
subscription_meta=subscription_meta
)
else:
await flash('That creator does not exist.')
return redirect(url_for('meta.index'))

@ -0,0 +1,27 @@
from quart import Blueprint, render_template, flash, redirect, url_for
from flask_login import current_user
from xmrbackers.models import TextPost, Subscription
bp = Blueprint('post', 'post')
@bp.route('/post/<int:post_id>')
async def show(post_id):
post = TextPost.get_or_none(post_id)
if post:
if current_user.is_anonymous:
await flash('You must login to view this post.')
return redirect(url_for('creator.show', username=post.creator.user.username))
user_subscriptions = Subscription.select().where(
Subscription.active == True,
Subscription.backer == current_user.backer_profile.first()
)
if user_subscriptions:
return await render_template('post/show.html', post=post)
else:
await flash('Viewing posts requires a subscription.')
return redirect(url_for('creator.subscription', username=post.creator.user.username))
else:
flash('That post does not exist.')
return redirect(url_for('meta.index'))

@ -7,12 +7,19 @@
<div id="page-wrapper">
{% include 'includes/header.html' %}
{% if creator %}
<h1>{{ creator.user.username }}</h1>
<p>Bio: {{ creator.bio }}</p>
{% endif %}
{% if posts %}
<h1>Posts</h1>
{% for post in posts %}
<p><a href="{{ url_for('post.show', post_id=post.id) }}">{{ post.id }} - {{ post.title }} - {{ post.post_date | humanize }}</a></p>
{% endfor %}
{% endif %}
{% include 'includes/footer.html' %}
</div>

@ -0,0 +1,27 @@
<!DOCTYPE HTML>
<html>
{% include 'includes/head.html' %}
<body class="is-preload landing">
<div id="page-wrapper">
{% include 'includes/header.html' %}
{% if subscription_meta %}
<h1>Subscribe</h1>
<p>Creator: <a href="{{ url_for('creator.show', username=subscription_meta.creator.user.username) }}">{{ subscription_meta.creator.user.username }}</a></p>
<p>ID: {{ subscription_meta.id }}</p>
<p>XMR: {{ subscription_meta.atomic_xmr | from_atomic }}</p>
<p>Address: {{ subscription_meta.wallet_address }}</p>
<p>Hours: {{ subscription_meta.number_hours }}</p>
{% endif %}
{% include 'includes/footer.html' %}
</div>
{% include 'includes/scripts.html' %}
</body>
</html>

@ -0,0 +1,25 @@
<!DOCTYPE HTML>
<html>
{% include 'includes/head.html' %}
<body class="is-preload landing">
<div id="page-wrapper">
{% include 'includes/header.html' %}
{% if post %}
<h1>{{ post.title }}</h1>
<p>Posted: {{ post.post_date }}</p>
<p>Edited: {{ post.last_edit_date }}</p>
<p>{{ post.content }}</p>
{% endif %}
{% include 'includes/footer.html' %}
</div>
{% include 'includes/scripts.html' %}
</body>
</html>
Loading…
Cancel
Save