1
1
Fork 0

Rating preference session consistency,

inverted register option
dev
Jan Kužílek 5 years ago
parent 875abc5516
commit c38ae08894

@ -64,7 +64,7 @@ def register():
if fl.current_user.is_authenticated: if fl.current_user.is_authenticated:
return redirect(url_for('main.index')) return redirect(url_for('main.index'))
if not current_app.config.get('ALLOW_REGISTER'): if current_app.config.get('REGISTER_DISABLED'):
flash('Registrations are disabled for now.') flash('Registrations are disabled for now.')
return redirect(url_for('main.index')) return redirect(url_for('main.index'))

@ -10,7 +10,7 @@ from sqlalchemy.orm import aliased
from yadc import db from yadc import db
from yadc.forms import UploadForm, CommentForm, PostForm from yadc.forms import UploadForm, CommentForm, PostForm
from yadc.models import FILETYPE, RATING, POST_STATUS, Post, Tag, Comment, moderator_required from yadc.models import FILETYPE, RATING, POST_STATUS, Post, Tag, Comment, moderator_required
from yadc.utils import query_replace, flasherrors from yadc.utils import query_replace, flasherrors, session_rating
bp = Blueprint('post', __name__) bp = Blueprint('post', __name__)
@ -23,17 +23,23 @@ def posts(page):
for tag,p_ids in tags: for tag,p_ids in tags:
tag.count = len(p_ids) tag.count = len(p_ids)
tag.post_ids = p_ids tag.post_ids = p_ids
tag.endpoint = query_replace({'tags': tag.content}, url_for('.posts'))
tags = [t[0] for t in tags] tags = [t[0] for t in tags]
tags.sort(key=lambda x: (x.category.value, x.content) ) tags.sort(key=lambda x: (x.category.value, x.content) )
return tags return tags
# PARSING ARGUMENTS # PARSING ARGUMENTS
if request.args.get('tags') == '':
return redirect(query_replace({}, url_for('.posts')))
f_tags = request.args.get('tags', '').split() f_tags = request.args.get('tags', '').split()
# f_rating = {r.name : r for r in RATING}.get(request.args.get('rating'), RATING.safe)
f_rating = current_user.rating if current_user.is_authenticated else RATING.safe f_rating = {r.name : r for r in RATING}.get(request.args.get('rating'))
m_ratings = f_rating.matched session['enforced_rating'] = f_rating.name if f_rating else None
# flash(f_rating.name if f_rating else None)
if current_user.is_authenticated and f_rating is None:
f_rating = current_user.rating
m_ratings = f_rating.matched if f_rating else RATING.safe.matched
# filter user's blacklist # filter user's blacklist
if current_user.is_authenticated: if current_user.is_authenticated:
@ -47,9 +53,6 @@ def posts(page):
posts = posts_query.paginate(page, current_app.config.get('POSTS_PER_PAGE')) posts = posts_query.paginate(page, current_app.config.get('POSTS_PER_PAGE'))
tags = tags_prepare(posts.items) tags = tags_prepare(posts.items)
# BRUH
session['index_settings'] = dict(tags=request.args.get('tags', ''), rating=f_rating.name)
return render_template('post/index.html', posts=posts, tags=tags) return render_template('post/index.html', posts=posts, tags=tags)
@bp.route('/show/<int:id>') @bp.route('/show/<int:id>')
@ -61,7 +64,8 @@ def post_show(id):
tags_count = db.session.query(Tag, func.count(Post.id)).join(Tag.posts).filter(Post.id==id).join(aliased(Post), Tag.posts).group_by(Tag).all() tags_count = db.session.query(Tag, func.count(Post.id)).join(Tag.posts).filter(Post.id==id).join(aliased(Post), Tag.posts).group_by(Tag).all()
for tag,tag.count in tags_count: for tag,tag.count in tags_count:
tag.endpoint = query_replace({'tags': tag.content}, url_for('.posts')) # tag.endpoint = query_replace(dict(**session_rating(), tags=tag.content), url_for('.posts'))
pass
for comment in post.comments: for comment in post.comments:
comment.editform = CommentForm(id=comment.id, content=comment.content) comment.editform = CommentForm(id=comment.id, content=comment.content)
@ -82,12 +86,6 @@ def post_show(id):
comment_form=CommentForm(post_id=post.id) comment_form=CommentForm(post_id=post.id)
) )
# from yadc.bp import manage
# @bp.route('/comment', methods=['POST'])
# @login_required
# def comment():
# return manage.modify_comment()
@bp.route('/comment', methods=['POST']) @bp.route('/comment', methods=['POST'])
@login_required @login_required
def comment(): def comment():
@ -120,11 +118,6 @@ def comment():
return redirect(url_for('main.posts')) return redirect(url_for('main.posts'))
# @bp.route('/editpost', methods=['POST'])
# @login_required
# def editpost():
# return manage.modify_post()
@bp.route('/upload', methods=['GET', 'POST']) @bp.route('/upload', methods=['GET', 'POST'])
@login_required @login_required
def upload(): def upload():

@ -1,6 +1,6 @@
INSTANCE_NAME = 'Darkne.su' INSTANCE_NAME = 'Darkne.su'
ALLOW_REGISTER = False REGISTER_DISABLED = False
SECRET_KEY = '<place your key here>' SECRET_KEY = '<place your key here>'
# SQLALCHEMY_ECHO = True # SQLALCHEMY_ECHO = True

@ -16,9 +16,17 @@
<span>{{ config.get('INSTANCE_NAME') }}</span> <span>{{ config.get('INSTANCE_NAME') }}</span>
</a> </a>
<nav id="main-nav"> <nav id="main-nav">
<a href="{{ url_for('post.posts') }}">Posts</a> <a href="{{ url_for('post.posts', **utils.session_rating() ) }}">Posts</a>
<a href="{{ url_for('post.upload') }}">Upload</a> <a href="{{ url_for('post.upload') }}">Upload</a>
{% if not (current_user.is_authenticated and current_user.rating.name == 'explicit') %}
{% if not utils.session_rating().get('rating') %}
<a href="{{ url_for('post.posts', rating='explicit') }}">NSFW</a>
{% else %}
<a href="{{ url_for('post.posts') }}"><strike>NSFW</strike></a>
{% endif %}
{% endif %}
<div class="user"> <div class="user">
{% if current_user.is_anonymous %} {% if current_user.is_anonymous %}
<a href="#" id="user-menu">Logged out</a> <a href="#" id="user-menu">Logged out</a>
@ -29,12 +37,12 @@
<!--<span class="user fa fa-user"></span>--> <!--<span class="user fa fa-user"></span>-->
<div class="user_dropdown"> <div class="user_dropdown">
{% if current_user.is_anonymous %} {% if current_user.is_anonymous %}
<a href="{{ url_for('auth.login') }}">Login</a> <a href="{{ url_for('auth.login', next=url_for('post.posts', **utils.session_rating()) ) }}">Login</a>
<a href="{{ url_for('auth.register') }}">Register</a> <a href="{{ url_for('auth.register') }}">Register</a>
{% else %} {% else %}
<a href="{{ url_for('user.profile', username=current_user.username).replace('%40','@') }}">Profile</a> <a href="{{ url_for('user.profile', username=current_user.username).replace('%40','@') }}">Profile</a>
<a href="{{ url_for('user.settings') }}">Settings</a> <a href="{{ url_for('user.settings') }}">Settings</a>
<a href="{{ url_for('auth.logout') }}">Log out</a> <a href="{{ url_for('auth.logout', next=url_for('post.posts', **utils.session_rating()) ) }}">Log out</a>
{% endif%} {% endif%}
</div> </div>
</div> </div>

@ -17,17 +17,19 @@
<h3>Search</h3> <h3>Search</h3>
<div class="tag-input"> <div class="tag-input">
<form action="{% block search_endpoint %}{{ url_for('post.posts') }}{% endblock %}" method="get"> <form action="{% block search_endpoint %}{{ url_for('post.posts') }}{% endblock %}" method="get">
{% if utils.session_rating().get('rating') %}<input type="hidden" name="rating" value="{{ utils.session_rating()['rating'] }}">{% endif %}
<div class="tag-suggester" data-inputname="tags"> <div class="tag-suggester" data-inputname="tags">
<input type="text" name="tags" autocomplete="off" placeholder="Search for tags" value="{{ request.args.get('tags', '') }}"> <input type="text" name="tags" autocomplete="off" placeholder="Search in tags" value="{{ utils.tag_reference() }}">
<div class="tag-container suggest-dropdown"></div> <div class="tag-container suggest-dropdown"></div>
</div> </div>
<noscript><a href="{{ utils.query_replace(dict(tags=None), url_for('post.posts')) }}">clear selection</a></noscript>
<!-- <input type="submit" value="Search"> --> <!-- <input type="submit" value="Search"> -->
<!-- <h3>Tags</h3> --> <!-- <h3>Tags</h3> -->
<div class="tag-container tags-selected"></div> <div class="tag-container tags-selected"></div>
<div class="tag-container tags-inpage"> <div class="tag-container tags-inpage">
{% for tag in tags %} {% for tag in tags %}
<a class="taginpage" data-tagname="{{ tag.content }}" href="{{ tag.endpoint }}"> <a class="taginpage" data-tagname="{{ tag.content }}" href="{{ utils.tag_endpoint(tag.content) }}">
<span class="tag-icon"> <span class="tag-icon">
<span class="fa fa-tag"></span> <span class="fa fa-tag"></span>
</span> </span>

@ -10,9 +10,36 @@ def sizeof_fmt(num, suffix='B'):
from flask import request from flask import request
from werkzeug.urls import Href from werkzeug.urls import Href
# def query_replace(dic, base=''):
# args = request.args.to_dict()
# return Href(base)(args.update(dic) or args)
def query_replace(dic, base=''): def query_replace(dic, base=''):
args = request.args.to_dict() args = request.args.to_dict()
return Href(base)(args.update(dic) or args) args.update(dic)
return Href(base)({k:v for k,v in args.items() if v})
from flask import session
def session_rating():
rating = session.get('enforced_rating')
if rating and rating != 'safe':
return dict(rating=rating)
return dict()
def tag_reference(tag=None):
tags = request.args.get('tags', '').split()
if tag:
if tag in tags:
tags.remove(tag)
else:
tags.append(tag)
return ' '.join(tags)
def tag_endpoint(tag):
return query_replace(
dict(**session_rating(), tags=tag_reference(tag)),
url_for('.posts')
)
from flask import request, url_for from flask import request, url_for
from werkzeug.urls import url_parse from werkzeug.urls import url_parse

Loading…
Cancel
Save