parent
cafe8130c2
commit
3ebe1c0b67
@ -0,0 +1,178 @@
|
||||
from flask import (Blueprint, abort, current_app, flash, redirect,
|
||||
render_template, request, send_from_directory, url_for)
|
||||
from flask_login import login_required, current_user
|
||||
from yadc.forms import UserForm, PostForm, TagForm, CommentForm
|
||||
|
||||
from yadc import db
|
||||
from yadc.models import User, USER_STATUS, Post, Tag, TAG_CATEGORY, Comment
|
||||
|
||||
bp = Blueprint('manage', __name__)
|
||||
|
||||
@bp.route('/users', defaults={'page': 1})
|
||||
@bp.route('/users/<int:page>')
|
||||
@login_required
|
||||
def manage_users(page):
|
||||
users = User.query.order_by(User.id.desc()).paginate(page, current_app.config.get('MANAGE_PER_PAGE'))
|
||||
|
||||
for user in users.items:
|
||||
user.editform = UserForm(
|
||||
id=user.id,
|
||||
username=user.username,
|
||||
email=user.email,
|
||||
op_level=user.op_level.name,
|
||||
user_status=user.user_status.name)
|
||||
|
||||
return render_template('manage/users.html', users=users, createform=UserForm())
|
||||
|
||||
@bp.route('/posts', defaults={'page': 1})
|
||||
@bp.route('/posts/<int:page>')
|
||||
@login_required
|
||||
def manage_posts(page):
|
||||
posts = Post.query.order_by(Post.id.desc()).paginate(page, current_app.config.get('MANAGE_PER_PAGE'))
|
||||
|
||||
for post in posts.items:
|
||||
post.editform = PostForm(
|
||||
id=post.id,
|
||||
rating=post.rating.name,
|
||||
status=post.status.name,
|
||||
source=post.source)
|
||||
|
||||
return render_template('manage/posts.html', posts=posts)
|
||||
|
||||
@bp.route('/tags', defaults={'page': 1})
|
||||
@bp.route('/tags/<int:page>')
|
||||
@login_required
|
||||
def manage_tags(page):
|
||||
tags = Tag.query.order_by(Tag.content).paginate(page, current_app.config.get('MANAGE_PER_PAGE'))
|
||||
|
||||
for tag in tags.items:
|
||||
tag.editform = TagForm(
|
||||
id=tag.id,
|
||||
content=tag.content_deser,
|
||||
category=tag.category.name)
|
||||
|
||||
return render_template('manage/tags.html', tags=tags, createform=TagForm())
|
||||
|
||||
|
||||
|
||||
@bp.route('/modify_user', methods=['POST'])
|
||||
@login_required
|
||||
def modify_user():
|
||||
form = UserForm(request.form)
|
||||
flash(str(request.form))
|
||||
if form.validate():
|
||||
if form.create.data:
|
||||
el = User(username=form.username.data)
|
||||
db.session.add(el)
|
||||
|
||||
if form.email.data: el.email = form.email.data
|
||||
if form.user_status.data: el.user_status = form.user_status.data
|
||||
if form.op_level.data: el.op_level = form.op_level.data
|
||||
|
||||
db.session.commit()
|
||||
flash('New {} has been created.'.format(str(el)))
|
||||
else:
|
||||
el = User.query.filter_by(id=form.id.data).first()
|
||||
if form.delete.data:
|
||||
db.session.delete(el)
|
||||
db.session.commit()
|
||||
flash('{} deleted.'.format(str(el)))
|
||||
elif form.edit.data:
|
||||
print(form.username)
|
||||
if form.username.data: el.username = form.username.data
|
||||
|
||||
if form.email.data: el.email = form.email.data
|
||||
if form.user_status.data: el.user_status = form.user_status.data
|
||||
if form.op_level.data: el.op_level = form.op_level.data
|
||||
|
||||
db.session.commit()
|
||||
flash('Changes to {} have been applied.'.format(str(el)))
|
||||
|
||||
return redirect(url_for('.manage_users'))
|
||||
|
||||
|
||||
@bp.route('/modify_post', methods=['POST'])
|
||||
@login_required
|
||||
def modify_post():
|
||||
form = PostForm(request.form)
|
||||
flash(str(request.form))
|
||||
if form.validate():
|
||||
if form.create.data:
|
||||
pass
|
||||
else:
|
||||
el = Post.query.filter_by(id=form.id.data).first()
|
||||
if form.delete.data:
|
||||
db.session.delete(el)
|
||||
db.session.commit()
|
||||
flash('{} deleted.'.format(str(el)))
|
||||
elif form.edit.data:
|
||||
if form.rating.data: el.rating = form.rating.data
|
||||
if form.status.data: el.status = form.status.data
|
||||
if form.source.data: el.source = form.source.data
|
||||
|
||||
db.session.commit()
|
||||
flash('Changes to {} have been applied.'.format(str(el)))
|
||||
|
||||
return redirect(url_for('.manage_posts'))
|
||||
|
||||
# Example perfect create/edit/delete form endpoint
|
||||
@bp.route('/modify_tag', methods=['POST'])
|
||||
@login_required
|
||||
def modify_tag():
|
||||
form = TagForm(request.form)
|
||||
flash(str(request.form))
|
||||
if form.validate():
|
||||
if form.create.data:
|
||||
el = Tag(content_deser=form.content.data)
|
||||
db.session.add(el)
|
||||
|
||||
if form.category.data: el.category = form.category.data
|
||||
|
||||
db.session.commit()
|
||||
flash('New {} has been created.'.format(str(el)))
|
||||
else:
|
||||
el = Tag.query.filter_by(id=form.id.data).first()
|
||||
if form.delete.data:
|
||||
db.session.delete(el)
|
||||
db.session.commit()
|
||||
flash('{} deleted.'.format(str(el)))
|
||||
elif form.edit.data:
|
||||
# if form.content.data: el.content = form.content.data
|
||||
if form.category.data: el.category = form.category.data
|
||||
|
||||
db.session.commit()
|
||||
flash('Changes to {} have been applied.'.format(str(el)))
|
||||
|
||||
return redirect(url_for('.manage_tags'))
|
||||
|
||||
# return redirect(url_for('main.index'))
|
||||
|
||||
# Creation/editing only through post page
|
||||
@bp.route('/modify_comment', methods=['POST'])
|
||||
@login_required
|
||||
def modify_comment():
|
||||
form = CommentForm(request.form)
|
||||
flash(str(request.form))
|
||||
if form.validate():
|
||||
if form.create.data:
|
||||
el = Comment(content=form.content.data, post_id=form.post_id.data, user=current_user)
|
||||
db.session.add(el)
|
||||
|
||||
db.session.commit()
|
||||
flash('Successfully submitted {}'.format(str(el)))
|
||||
return redirect(url_for('.post_show', id=form.post_id.data))
|
||||
else:
|
||||
el = Comment.query.filter_by(id=form.id.data).first()
|
||||
if form.delete.data:
|
||||
db.session.delete(el)
|
||||
db.session.commit()
|
||||
flash('{} deleted.'.format(str(el)))
|
||||
elif form.edit.data:
|
||||
if form.content.data: el.content = form.content.data
|
||||
|
||||
db.session.commit()
|
||||
flash('Changes to {} have been applied.'.format(str(el)))
|
||||
|
||||
return redirect(url_for('.post_show', id=el.post_id))
|
||||
|
||||
return redirect(url_for('main.posts'))
|
File diff suppressed because one or more lines are too long
@ -0,0 +1,17 @@
|
||||
let comment_container = document.querySelector('section.comments .comment_container')
|
||||
|
||||
let comments = comment_container.querySelectorAll('.comment')
|
||||
|
||||
function triggerEdit(comment) {
|
||||
let form = comment.querySelector('form.editingable')
|
||||
if (!form.classList.contains('time-to-edit')) {
|
||||
form.classList.add('time-to-edit')
|
||||
} else {
|
||||
form.classList.remove('time-to-edit')
|
||||
}
|
||||
}
|
||||
|
||||
comments.forEach((comment) => {
|
||||
// comment.querySelector('p.comment_content').addEventListener('dblclick', (event) => triggerEdit(comment))
|
||||
comment.querySelector('.comment_head .control_edit').addEventListener('click', (event) => triggerEdit(comment))
|
||||
})
|
@ -0,0 +1,47 @@
|
||||
{% extends 'layout/settings.html' %}
|
||||
{% from '_includes.html' import render_pagination %}
|
||||
{% from "_formhelpers.html" import management_gen_line, errors %}
|
||||
|
||||
{% block main_content %}
|
||||
<section class="management_table manage_posts">
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<!-- <th>ID</th> -->
|
||||
<th>Content</th>
|
||||
<th>Category</th>
|
||||
<th>Manage</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for tag in tags.items %}
|
||||
<form action="{{ url_for('manage.modify_tag') }}" method="post">
|
||||
{{ tag.editform.csrf_token }}
|
||||
{{ tag.editform.id() }}
|
||||
<tr>
|
||||
<!-- <td>{{ tag.id }}</td> -->
|
||||
<td>{{ tag.content_deser }}</td>
|
||||
<td>{{ management_gen_line(tag.category.name.capitalize(), tag.editform.category) }}</td>
|
||||
<td>
|
||||
<label class="show to-edit"><span class="fa fa-edit"></span></label>
|
||||
<label class="edit to-close"><span class="fa fa-close"></span></label>
|
||||
<label class="edit"><span class="fa fa-check"></span>{{ tag.editform.edit() }}</label>
|
||||
<label><span class="fa fa-trash-o"></span>{{ tag.editform.delete() }}</label>
|
||||
</td>
|
||||
</tr>
|
||||
</form>
|
||||
{% endfor %}
|
||||
<form action="{{ url_for('manage.modify_tag') }}" method="post">
|
||||
{{ createform.csrf_token }}
|
||||
<tr>
|
||||
<!-- <td></td> -->
|
||||
<td>{{ createform.content() }}</td>
|
||||
<td>{{ createform.category() }}</td>
|
||||
<td>{{ createform.create() }}</td>
|
||||
</tr>
|
||||
</form>
|
||||
</tbody>
|
||||
</table>
|
||||
{{ render_pagination('manage.manage_tags', tags) }}
|
||||
</section>
|
||||
{% endblock %}
|
Loading…
Reference in New Issue