All the shit and still nobody understands.
Finally a bit cleaner changelist. Working index page as of today.dev
parent
b5c2f35bac
commit
a955ebfcae
@ -1,40 +0,0 @@
|
||||
from flask import request
|
||||
from flask_wtf import FlaskForm
|
||||
from wtforms import StringField, PasswordField, BooleanField, SubmitField
|
||||
from wtforms.validators import DataRequired
|
||||
from flask_login import current_user, login_user, logout_user
|
||||
from werkzeug.urls import url_parse
|
||||
|
||||
from yadc.models import User
|
||||
|
||||
class LoginForm(FlaskForm):
|
||||
username = StringField('Username', validators=[DataRequired()])
|
||||
password = PasswordField('Password', validators=[DataRequired()])
|
||||
remember_me = BooleanField('Remember me')
|
||||
submit = SubmitField('Log In')
|
||||
|
||||
from flask import Blueprint, render_template, flash, redirect, url_for
|
||||
|
||||
bp = Blueprint('auth', __name__)
|
||||
|
||||
@bp.route('/login', methods=['GET', 'POST'])
|
||||
def login():
|
||||
form = LoginForm()
|
||||
if form.validate_on_submit():
|
||||
#flash('Login requested for user {}, remember_me={}'.format(form.username.data, form.remember_me.data))
|
||||
user = User.query.filter_by(username=form.username.data).first()
|
||||
if user is None or not user.check_password(form.password.data):
|
||||
flash('Invalid username or password.')
|
||||
return redirect(url_for('auth.login'))
|
||||
login_user(user, remember=form.remember_me.data)
|
||||
nextpg = request.args.get('next')
|
||||
if not nextpg and url_parse(nextpg).netloc != '':
|
||||
nextpg = url_for('main.index')
|
||||
#flash('Logged in as {}'.format(user.username))
|
||||
return redirect(nextpg)
|
||||
return render_template('login.html', form=form)
|
||||
|
||||
@bp.route('/logout')
|
||||
def logout():
|
||||
logout_user()
|
||||
return redirect(url_for('main.index'))
|
@ -0,0 +1,82 @@
|
||||
import flask_login as fl
|
||||
from flask import Blueprint, flash, redirect, render_template, request, url_for
|
||||
from werkzeug.urls import url_parse
|
||||
from wtforms import BooleanField, PasswordField, StringField, SubmitField
|
||||
from wtforms.validators import DataRequired
|
||||
|
||||
from yadc import db
|
||||
from yadc.forms import LoginForm, RegisterForm, ResetPasswordForm
|
||||
from yadc.models import User
|
||||
|
||||
bp = Blueprint('auth', __name__)
|
||||
|
||||
def nextpage():
|
||||
nextpg = request.args.get('next')
|
||||
if not nextpg or url_parse(nextpg).netloc != '':
|
||||
nextpg = url_for('main.index')
|
||||
return nextpg
|
||||
|
||||
@bp.route('/login/', methods=['GET', 'POST'])
|
||||
def login():
|
||||
if fl.current_user.is_authenticated:
|
||||
return redirect(url_for('main.index'))
|
||||
|
||||
form = LoginForm(request.form)
|
||||
if request.method == 'POST' and form.validate():
|
||||
user = User.query.filter_by(username=form.username.data).first()
|
||||
if user is None or not user.check_password(form.password.data):
|
||||
flash('Invalid username or password.')
|
||||
return redirect(url_for('.login'))
|
||||
|
||||
user.login(remember=form.remember_me.data)
|
||||
db.session.commit()
|
||||
|
||||
flash('Logged in as {}'.format(user.username))
|
||||
|
||||
return redirect(nextpage())
|
||||
|
||||
return render_template('login.html', form=form)
|
||||
|
||||
@bp.route('/logout/')
|
||||
def logout():
|
||||
fl.current_user.logout()
|
||||
|
||||
return redirect(nextpage())
|
||||
|
||||
@bp.route('/reset_password/', methods=['GET', 'POST'])
|
||||
def reset_password():
|
||||
if fl.current_user.is_authenticated:
|
||||
return redirect(url_for('main.index'))
|
||||
|
||||
form = ResetPasswordForm(request.form)
|
||||
if request.method == 'POST' and form.validate():
|
||||
user = User.query.filter_by(email=form.email.data).first()
|
||||
|
||||
if user:
|
||||
user.create_password('kuxaman')
|
||||
db.session.commit()
|
||||
#do something to reset the password
|
||||
|
||||
flash('Password successfully reset. Check your email.')
|
||||
return redirect(url_for('.login'))
|
||||
|
||||
return render_template('reset_password.html', form=form)
|
||||
|
||||
|
||||
@bp.route('/register/', methods=['GET', 'POST'])
|
||||
def register():
|
||||
if fl.current_user.is_authenticated:
|
||||
return redirect(url_for('main.index'))
|
||||
|
||||
form = RegisterForm(request.form)
|
||||
if request.method == 'POST' and form.validate():
|
||||
user = User(username=form.username.data, email=form.email.data)
|
||||
user.create_password(form.password.data)
|
||||
|
||||
db.session.add(user)
|
||||
db.session.commit()
|
||||
|
||||
flash('Your account has been successfully registered. You can now login.')
|
||||
return redirect(url_for('.login'))
|
||||
|
||||
return render_template('register.html', form=form)
|
@ -0,0 +1,123 @@
|
||||
from flask import Blueprint, render_template, flash, redirect, url_for, request, abort
|
||||
from flask_login import login_required
|
||||
|
||||
bp = Blueprint('main', __name__)
|
||||
|
||||
@bp.route('/')
|
||||
def index():
|
||||
return posts()
|
||||
|
||||
# @bp.route('/')
|
||||
@bp.route('/post/')
|
||||
def posts():
|
||||
posts = Post.query.order_by(Post.created).limit(20).all()
|
||||
# flash(posts)
|
||||
|
||||
tagset = set()
|
||||
taglist = list()
|
||||
for post in posts:
|
||||
for tag in post.tags:
|
||||
tagset.add(tag)
|
||||
taglist.append(tag)
|
||||
|
||||
for tag in tagset:
|
||||
tag.count = taglist.count(tag)
|
||||
|
||||
tags = list(tagset)
|
||||
tags.sort(key=lambda x: (x.category.value, x.content) )
|
||||
# flash([tag.count for tag in tags])
|
||||
|
||||
return render_template('index.html', posts=posts, tags=tags)
|
||||
|
||||
@bp.route('/post/show/<id>/')
|
||||
def post_show(id):
|
||||
post = Post.query.filter_by(id=id)
|
||||
flash(post)
|
||||
|
||||
|
||||
|
||||
return render_template('post.html', post=post, tags=tags)
|
||||
|
||||
from flask_login import current_user
|
||||
from yadc import db
|
||||
from yadc.forms import UploadPostForm
|
||||
from yadc.models import Post, RATING, FILETYPE
|
||||
|
||||
from flask import current_app
|
||||
from PIL import Image
|
||||
import io
|
||||
import os
|
||||
|
||||
# @bp.route('/post/new/')
|
||||
# @bp.route('/post/create/')
|
||||
@bp.route('/post/upload/', methods=['GET', 'POST'])
|
||||
@login_required
|
||||
def post_upload():
|
||||
form = UploadPostForm(request.form)
|
||||
if request.method == 'POST' and form.validate():
|
||||
file = request.files.get(form.post_img.name)
|
||||
file.data = io.BytesIO(file.read())
|
||||
|
||||
# tagy
|
||||
|
||||
post = Post(file, source=form.sauce.data, rating=RATING[form.rating.data], author=current_user)
|
||||
|
||||
|
||||
with open(post.image_path, "wb") as f:
|
||||
f.write(file.data.getbuffer())
|
||||
# file.seek(0)
|
||||
# file.save(post.image_path)
|
||||
|
||||
with Image.open(file.data) as im:
|
||||
im = im.convert('RGB')
|
||||
|
||||
if post.jpeg_path is not None:
|
||||
im.save(post.jpeg_path, 'JPEG', quality=80)
|
||||
|
||||
im.thumbnail([512,512])
|
||||
im.save(post.thumb_path, 'JPEG', quality=80)
|
||||
|
||||
|
||||
db.session.add(post)
|
||||
db.session.commit()
|
||||
|
||||
flash('Successfully submitted {}'.format(str(post)))
|
||||
|
||||
return redirect(url_for('main.post_upload'))
|
||||
|
||||
return render_template('upload.html', form=form)
|
||||
|
||||
from flask import send_from_directory
|
||||
|
||||
@bp.route('/i/<path:path>')
|
||||
def uploaded_img(path, store='img', exten=None):
|
||||
ext = os.path.splitext(path)[1]
|
||||
|
||||
if exten is not None and ext.split('.')[1] != exten:
|
||||
abort(404)
|
||||
|
||||
filename = path.split('/')[0].split('.')[0]
|
||||
# print(os.path.join(current_app.config.get('POST_UPLOADS'), store, "{}{}".format(filename, ext)))
|
||||
return send_from_directory(os.path.join(current_app.config.get('POST_UPLOADS'), store), "{}{}".format(filename, ext))
|
||||
|
||||
@bp.route('/jpeg/<path:path>')
|
||||
def uploaded_jpeg(*args, **kwargs):
|
||||
return uploaded_img(*args, **kwargs, store='jpeg', exten='jpg')
|
||||
|
||||
@bp.route('/thumb/<path:path>')
|
||||
def uploaded_thumb(*args, **kwargs):
|
||||
return uploaded_img(*args, **kwargs, store='thumb', exten='jpg')
|
||||
|
||||
|
||||
@bp.route('/threads/')
|
||||
def threads():
|
||||
return render_template('index.html')
|
||||
|
||||
@bp.route('/user/show/<username>/')
|
||||
def user_profile(username):
|
||||
pass
|
||||
|
||||
@bp.route('/user/settings/')
|
||||
@login_required
|
||||
def user_settings():
|
||||
pass
|
@ -0,0 +1,81 @@
|
||||
from wtforms import Form
|
||||
from wtforms import StringField, PasswordField, BooleanField, SubmitField, FileField, MultipleFileField, ValidationError, RadioField
|
||||
from wtforms.validators import DataRequired, InputRequired, Email, EqualTo, AnyOf
|
||||
|
||||
from werkzeug.utils import cached_property
|
||||
|
||||
from flask import current_app
|
||||
from flask_wtf.csrf import _FlaskFormCSRF
|
||||
|
||||
class CSRFForm(Form):
|
||||
class Meta:
|
||||
csrf = True
|
||||
csrf_class = _FlaskFormCSRF
|
||||
|
||||
@cached_property
|
||||
def csrf_secret(self):
|
||||
return current_app.secret_key
|
||||
|
||||
csrf_time_limit = 3600
|
||||
csrf_field_name = 'csrf_token'
|
||||
|
||||
class LoginForm(CSRFForm):
|
||||
username = StringField('Username', validators=[DataRequired()])
|
||||
password = PasswordField('Password', validators=[DataRequired()])
|
||||
remember_me = BooleanField('Remember me')
|
||||
submit = SubmitField('Log In')
|
||||
|
||||
from yadc.models import User
|
||||
|
||||
class ResetPasswordForm(CSRFForm):
|
||||
email = StringField('E-mail', validators=[DataRequired(), Email()])
|
||||
submit = SubmitField('Reset password')
|
||||
|
||||
# def validate_email(form, field):
|
||||
# email = User.query.filter_by(email=field.data).first()
|
||||
# if not email:
|
||||
# raise ValidationError('This')
|
||||
|
||||
class RegisterForm(CSRFForm):
|
||||
username = StringField('Username', validators=[DataRequired()])
|
||||
email = StringField('E-mail', validators=[DataRequired(), Email()])
|
||||
password = PasswordField('Password', validators=[DataRequired()])
|
||||
password_again = PasswordField('Repeat password', validators=[DataRequired(), EqualTo('password')])
|
||||
submit = SubmitField('Register')
|
||||
|
||||
def validate_username(form, field):
|
||||
user = User.query.filter_by(username=field.data).first()
|
||||
if user is not None:
|
||||
raise ValidationError('Username already taken. Try different username.')
|
||||
|
||||
def validate_email(form, field):
|
||||
email = User.query.filter_by(email=field.data).first()
|
||||
if email is not None:
|
||||
raise ValidationError('This email address is already registered. Maybe try logging in instead?')
|
||||
|
||||
from flask import request
|
||||
# from magic import Magic
|
||||
|
||||
def validate_file(form, field):
|
||||
file = request.files.get(field.name)
|
||||
if not file or file.filename == '':
|
||||
raise ValidationError('Please select a file')
|
||||
|
||||
class UploadPostForm(CSRFForm):
|
||||
post_img = FileField('Image', validators=[validate_file], render_kw={'required':''})
|
||||
sauce = StringField('Sauce', validators=[DataRequired()])
|
||||
tags = StringField('Tags')
|
||||
rating = RadioField('Rating',
|
||||
choices=[('safe', 'S'), ('questionable', 'Q'), ('explicit', 'E')],
|
||||
default='safe',
|
||||
validators=[DataRequired()])
|
||||
submit = SubmitField('Upload')
|
||||
|
||||
def validate_post_img(form, field):
|
||||
file = request.files.get(field.name)
|
||||
|
||||
client_mimetype = file.mimetype
|
||||
# Not sure if safe
|
||||
# real_mimetype = Magic(mime=True).from_buffer(file.stream.read())
|
||||
if client_mimetype not in ['image/png','image/jpeg']:
|
||||
raise ValidationError('Please select an image file of PNG or JPEG format')
|
@ -1,40 +0,0 @@
|
||||
from flask import Blueprint, render_template, flash, redirect
|
||||
from flask_login import login_required
|
||||
|
||||
bp = Blueprint('main', __name__)
|
||||
|
||||
@bp.route('/index')
|
||||
def index():
|
||||
return render_template('index.html')
|
||||
|
||||
@bp.route('/humm')
|
||||
@login_required
|
||||
def humm():
|
||||
return render_template('index.html')
|
||||
|
||||
@bp.route('/')
|
||||
@bp.route('/post')
|
||||
def post():
|
||||
return render_template('index.html')
|
||||
pass
|
||||
|
||||
@bp.route('/post/show/<id>')
|
||||
def post_show(id):
|
||||
return render_template('post.html')
|
||||
pass
|
||||
|
||||
@bp.route('/post/new')
|
||||
@bp.route('/post/create')
|
||||
@bp.route('/post/upload')
|
||||
@login_required
|
||||
def post_upload():
|
||||
pass
|
||||
|
||||
@bp.route('/user/show/<username>')
|
||||
def user_profile(username):
|
||||
pass
|
||||
|
||||
@bp.route('/user/settings')
|
||||
@login_required
|
||||
def user_settings():
|
||||
pass
|
@ -0,0 +1,9 @@
|
||||
{% macro errors(field) %}
|
||||
{% if field.errors %}
|
||||
<ul class=errors>
|
||||
{% for error in field.errors %}
|
||||
<li>{{ error }}</li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
{% endif %}
|
||||
{% endmacro %}
|
@ -0,0 +1,23 @@
|
||||
{% extends 'base.html' %}
|
||||
{% from "_formhelpers.html" import errors %}
|
||||
|
||||
{% block content %}
|
||||
<h1>Log In</h1>
|
||||
<form action="" method="post">
|
||||
{{ form.csrf_token }}
|
||||
<div>
|
||||
{{ form.username.label }} {{ form.username(size=32) }}
|
||||
{{ errors(form.username) }}
|
||||
</div>
|
||||
<div>
|
||||
{{ form.password.label }} {{ form.password(size=32) }}
|
||||
{{ errors(form.password) }}
|
||||
</div>
|
||||
<div>
|
||||
{{ form.remember_me() }} {{ form.remember_me.label }}
|
||||
{{ errors(form.remember_me) }}
|
||||
</div>
|
||||
<div>{{ form.submit() }}</div>
|
||||
</form>
|
||||
<a href="{{ url_for('auth.reset_password') }}">Forgoten password?</a>
|
||||
{% endblock content %}
|
@ -0,0 +1,26 @@
|
||||
{% extends 'base.html' %}
|
||||
{% from "_formhelpers.html" import errors %}
|
||||
|
||||
{% block content %}
|
||||
<h1>Log In</h1>
|
||||
<form action="" method="post">
|
||||
{{ form.csrf_token }}
|
||||
<div>
|
||||
{{ form.username.label }} {{ form.username(size=32) }}
|
||||
{{ errors(form.username) }}
|
||||
</div>
|
||||
<div>
|
||||
{{ form.email.label }} {{ form.email(size=32) }}
|
||||
{{ errors(form.email) }}
|
||||
</div>
|
||||
<div>
|
||||
{{ form.password.label }} {{ form.password(size=32) }}
|
||||
{{ errors(form.password) }}
|
||||
</div>
|
||||
<div>
|
||||
{{ form.password_again.label }} {{ form.password_again(size=32) }}
|
||||
{{ errors(form.password_again) }}
|
||||
</div>
|
||||
<div>{{ form.submit() }}</div>
|
||||
</form>
|
||||
{% endblock content %}
|
@ -0,0 +1,14 @@
|
||||
{% extends 'base.html' %}
|
||||
{% from "_formhelpers.html" import errors %}
|
||||
|
||||
{% block content %}
|
||||
<h1>Log In</h1>
|
||||
<form action="" method="post">
|
||||
{{ form.csrf_token }}
|
||||
<div>
|
||||
{{ form.email.label }} {{ form.email(size=32) }}
|
||||
{{ errors(form.email) }}
|
||||
</div>
|
||||
<div>{{ form.submit() }}</div>
|
||||
</form>
|
||||
{% endblock content %}
|
@ -0,0 +1,26 @@
|
||||
{% extends 'base.html' %}
|
||||
{% from "_formhelpers.html" import errors %}
|
||||
|
||||
{% block content %}
|
||||
<h1>Upload image</h1>
|
||||
<form action="" method="post" enctype="multipart/form-data">
|
||||
{{ form.csrf_token }}
|
||||
<div>
|
||||
{{ form.post_img.label }} {{ form.post_img() }}
|
||||
{{ errors(form.post_img) }}
|
||||
</div>
|
||||
<div>
|
||||
{{ form.sauce.label }} {{ form.sauce() }}
|
||||
{{ errors(form.sauce) }}
|
||||
</div>
|
||||
<div>
|
||||
{{ form.tags.label }} {{ form.tags() }}
|
||||
{{ errors(form.tags) }}
|
||||
</div>
|
||||
<div>
|
||||
{{ form.rating.label }} {% for r in form.rating %}{{ r.label }}{{ r() }}{% endfor %}
|
||||
{{ errors(form.rating) }}
|
||||
</div>
|
||||
<div>{{ form.submit() }}</div>
|
||||
</form>
|
||||
{% endblock content %}
|
Loading…
Reference in New Issue