diff --git a/verse/describer/describe_files.py b/verse/describer/describe_files.py index 62ec170..f64b763 100644 --- a/verse/describer/describe_files.py +++ b/verse/describer/describe_files.py @@ -2,7 +2,7 @@ from flask import Blueprint, render_template, redirect, url_for from flask_login import current_user, login_required from models.distribusi_model import Distribusis from models.distribusi_file_model import DistribusiFiles -from describer.forms.describe_file_form import DescribeFileForm +from describer.forms.describe_files_form import DescribeFilesForm describer = Blueprint( "describer", @@ -14,29 +14,53 @@ describer = Blueprint( @describer.route("/") @login_required -def describe_distribusi_files(distribusiname): +def show_distribusi_files(distribusiname): if not current_user.is_authenticated: return redirect(url_for("index")) distribusi = Distribusis.query.filter_by( distribusiname=distribusiname ).first() - distribusi_files = DistribusiFiles.query.filter_by( - distribusi=distribusi.id - ).all() - # distribusi_file_forms = {} - # for distribusi_file in distribusi_files: - describe_form = DescribeFileForm() + distribusi_file_forms = get_distribusi_file_forms(distribusi.id) return render_template( "describe.html", - distribusi_files=distribusi_files, - describe_form=describe_form, + distribusi_file_forms=distribusi_file_forms, ) -@describer.route("/describe_file", methods=["POST"]) +@describer.route("/describe_file/", methods=["POST"]) @login_required -def describe_file(file_name): - describe_form = DescribeFileForm() - if describe_form.validate_on_submit(): - print(f"{file_name} description: {describe_form.description.data}") - return +def describe_file(file_id): + distribusi_file = DistribusiFiles.query.filter_by(id=file_id).first() + describe_form = DescribeFilesForm(distribusi_file.id, distribusi_file.path) + print( + f"file: {describe_form.file_path} description: {describe_form.description.data}" + ) + print( + f"file: {describe_form.file_path} searchtags: {describe_form.searchtags.data}" + ) + print( + f"file: {describe_form.file_path} alttext: {describe_form.alttext.data}" + ) + distribusi_file_forms = get_distribusi_file_forms( + distribusi_file.distribusi + ) + return render_template( + "describe.html", + distribusi_file_forms=distribusi_file_forms, + ) + + +def get_distribusi_file_forms(distribusi_id): + distribusi_file_forms = {} + distribusi_files = DistribusiFiles.query.filter_by( + distribusi=distribusi_id + ).all() + for distribusi_file in distribusi_files: + describe_form = DescribeFilesForm( + distribusi_file.id, distribusi_file.path + ) + describe_form.description.data = distribusi_file.description + describe_form.alttext.data = distribusi_file.alttext + describe_form.searchtags.data = distribusi_file.tags + distribusi_file_forms[distribusi_file.id] = describe_form + return distribusi_file_forms diff --git a/verse/describer/forms/describe_file_form.py b/verse/describer/forms/describe_file_form.py deleted file mode 100644 index 0a97b3e..0000000 --- a/verse/describer/forms/describe_file_form.py +++ /dev/null @@ -1,29 +0,0 @@ -"""Describe File Form to describe files in the distribusi archive""" - -from flask_wtf import FlaskForm -from wtforms import StringField, SubmitField, validators -from wtforms.validators import Length - - -class DescribeFileForm(FlaskForm): - """DescribeFileForm selection form.""" - - alttext = StringField( - "Alt-text for this file:", - validators=[ - Length(3, 255), - ], - ) - searchtags = StringField( - "Add search tags, seperated by commas. No need for the '#' sign:", - validators=[ - Length(3, 500), - ], - ) - description = StringField( - "Description of this file:", - validators=[ - Length(3, 4096), - ], - ) - save = SubmitField("Save") diff --git a/verse/describer/forms/describe_files_form.py b/verse/describer/forms/describe_files_form.py new file mode 100644 index 0000000..f127751 --- /dev/null +++ b/verse/describer/forms/describe_files_form.py @@ -0,0 +1,47 @@ +"""Describe File Form to describe files in the distribusi archive""" + +from flask_wtf import FlaskForm +from wtforms import StringField, SubmitField, validators +from wtforms.validators import Length +from wtforms.widgets import TextArea + + +class DescribeFilesForm(FlaskForm): + """DescribeFileForm selection form.""" + + alttext = StringField( + "Alt-text for this file:", + validators=[ + Length(3, 255), + ], + ) + searchtags = StringField( + "Add search tags, seperated by commas. No need for the '#' sign:", + validators=[ + Length(3, 500), + ], + ) + description = StringField( + "Description of this file:", + validators=[ + Length(3, 4096), + ], + widget=TextArea(), + ) + save = SubmitField("Save") + + def __init__(self, id, file_path=None): + super(DescribeFilesForm, self).__init__() + self.id = id + self.file_path = file_path + self.alttext.id = f"alttext-{id}" + self.searchtags.id = f"searchtags-{id}" + self.description.id = f"description-{id}" + self.save.id = f"save-{id}" + + +# def StringFieldWithId(form_name, **kwargs): +# name = kwargs.get('name', 'Bar') +# return wtf.StringField(name, render_kw={ +# 'id': f'{}{}'.format(form_name, name.lower(), +# **kwargs.get('render_kw', {})}) diff --git a/verse/describer/templates/describe_files/describe.html b/verse/describer/templates/describe_files/describe.html index 6b30b14..5b3276e 100644 --- a/verse/describer/templates/describe_files/describe.html +++ b/verse/describer/templates/describe_files/describe.html @@ -27,10 +27,11 @@
- {% for file in distribusi_files %} -

{{file.path}}

-
+ {% for id, describe_form in distribusi_file_forms.items() %} + {{ describe_form.csrf_token }} +

{{describe_form.id}}

+

{{describe_form.file_path}}

{{ describe_form.description.label }} {{ describe_form.description }} diff --git a/verse/distribusikan/distribusiselector.py b/verse/distribusikan/distribusiselector.py index fef2c62..b035953 100644 --- a/verse/distribusikan/distribusiselector.py +++ b/verse/distribusikan/distribusiselector.py @@ -69,7 +69,7 @@ def SelectNewDistribusi(): def SelectDescribeDistribusi(distribusiname): return redirect( url_for( - "describer.describe_distribusi_files", + "describer.show_distribusi_files", distribusiname=distribusiname, ) ) diff --git a/verse/file_crawler.py b/verse/file_crawler.py index 0a86f79..d378f85 100644 --- a/verse/file_crawler.py +++ b/verse/file_crawler.py @@ -70,4 +70,4 @@ def add_distribusi_files(path): _distribusi_file_with_type(distribusi, full_path) -add_distribusi_files("2018-12-WttF-Mastodon-and-the-Fediverse") +add_distribusi_files("2018-02-16-Extratonality") diff --git a/verse/start.py b/verse/start.py index 0796441..5e9530c 100644 --- a/verse/start.py +++ b/verse/start.py @@ -14,7 +14,7 @@ from flask_login import current_user, login_required, logout_user from flask_wtf.csrf import CSRFError from adminpage import AdminPage -from app import create_app, login_manager +from app import db, create_app, login_manager from describer.describe_files import describer from distribusikan.distribusikan import distribusikan from distribusikan.distribusisinfo import DistribusisInfo @@ -112,7 +112,7 @@ def handle_csrf_error(e): @login_manager.user_loader def load_user(user_id): - return User.query.get(int(user_id)) + return db.session.get(User, int(user_id)) def is_adminuser():