From db675ccf36a76047e55fa96ed3ddb07da732d709 Mon Sep 17 00:00:00 2001 From: aneuhmanh Date: Sat, 8 Mar 2025 02:01:18 +0200 Subject: [PATCH] comics captcha and view fix --- app.py | 24 ++++---- models.py | 9 ++- static/js/comic_upload.js | 15 +++++ templates/comic_edit.html | 4 +- templates/comic_upload.html | 51 ++++++---------- templates/view.html | 4 +- upload.py | 119 +++++++++++++++++++----------------- 7 files changed, 122 insertions(+), 104 deletions(-) create mode 100644 static/js/comic_upload.js diff --git a/app.py b/app.py index 9da9cbf..1134327 100644 --- a/app.py +++ b/app.py @@ -160,14 +160,11 @@ def view(content_type, id): content = Comic.query.get_or_404(id) comments = Comments.query.filter_by(comic_id=id).order_by(Comments.comment_date.desc()).all() - comic_pages_dir = os.path.join(app.config['UPLOAD_FOLDER']['comics'], content.comic_folder) + comic_pages = ComicPage.query.filter_by(comic_id=id).order_by(ComicPage.page_number).all() - if not os.path.exists(comic_pages_dir): + if not comic_pages: return render_template('error.html', message="Comic pages not found") - comic_pages = os.listdir(comic_pages_dir) - comic_pages = sorted(comic_pages) - all_comics = Comic.query.order_by(Comic.id).all() comic_ids = [comic.id for comic in all_comics] current_index = comic_ids.index(id) @@ -530,28 +527,32 @@ async def comic_edit(comic_id): if action == 'delete' and (page_id := request.form.get('page')): page = ComicPage.query.get(page_id) if page: - os.remove(page.file_path) + os.remove(page.file_path.replace('\\', '/')) db.session.delete(page) db.session.commit() + pages = ComicPage.query.filter_by(comic_id=comic.id).order_by(ComicPage.page_number).all() + for i, page in enumerate(pages, start=1): + page.page_number = i + db.session.commit() elif action == 'update' and (page_id := request.form.get('page')) and 'new_page' in request.files: new_page = request.files['new_page'] filename = f"{uuid.uuid4().hex}.webp" - file_path = os.path.join(cfp, filename) + file_path = os.path.join(cfp, filename).replace('\\', '/') webp_image = await convert_to_webp(new_page) async with aiofiles.open(file_path, 'wb') as f: await f.write(webp_image.read()) page = ComicPage.query.get(page_id) if page: - os.remove(page.file_path) + os.remove(page.file_path.replace('\\', '/')) page.file_path = file_path db.session.commit() elif action == 'add' and 'new_page' in request.files: new_page = request.files['new_page'] filename = f"{uuid.uuid4().hex}.webp" - file_path = os.path.join(cfp, filename) + file_path = os.path.join(cfp, filename).replace('\\', '/') webp_image = await convert_to_webp(new_page) async with aiofiles.open(file_path, 'wb') as f: await f.write(webp_image.read()) @@ -562,7 +563,8 @@ async def comic_edit(comic_id): return redirect(url_for('comic_edit', comic_id=comic.id)) - return render_template('comic_edit.html', comic=comic, comic_pages=comic.pages, form=form) + comic_pages = ComicPage.query.filter_by(comic_id=comic.id).order_by(ComicPage.page_number).all() + return render_template('comic_edit.html', comic=comic, comic_pages=comic_pages, form=form) def update_related_tables(old_username, new_username): @@ -862,4 +864,4 @@ def buy_item(item_id): if __name__ == '__main__': with app.app_context(): db.create_all() - app.run(debug=True) \ No newline at end of file + app.run(debug=False) \ No newline at end of file diff --git a/models.py b/models.py index b2b4dd8..0eccffe 100644 --- a/models.py +++ b/models.py @@ -29,7 +29,6 @@ from config import Config from flask_wtf import FlaskForm from wtforms import StringField, PasswordField, SubmitField, FileField, BooleanField from wtforms.validators import DataRequired -from flask_wtf import RecaptchaField from flask_wtf.file import FileAllowed db = SQLAlchemy() @@ -220,6 +219,14 @@ class UploadVideoForm(FlaskForm): submit = SubmitField('Upload') agree_with_rules = BooleanField('I agree with the publication rules', validators=[DataRequired()]) +class UploadComicForm(FlaskForm): + title = StringField('Comic Name', validators=[DataRequired()]) + thumbnail = FileField('Thumbnail', validators=[DataRequired()]) + tags = StringField('Tags (comma-separated)') + recaptcha = RecaptchaField() + agree_with_rules = BooleanField('I agree with the publication rules', validators=[DataRequired(message="You must agree with the publication rules.")]) + submit = SubmitField('Upload') + class EditTagsForm(FlaskForm): tags = StringField('Tags', validators=[DataRequired(), Length(max=100)], render_kw={"placeholder": "Enter tags"}) submit = SubmitField('Save') diff --git a/static/js/comic_upload.js b/static/js/comic_upload.js new file mode 100644 index 0000000..80b9ae5 --- /dev/null +++ b/static/js/comic_upload.js @@ -0,0 +1,15 @@ +let pageCount = 1; +const maxPages = 64; + +function addPage() { + if (pageCount >= maxPages) { + alert(`You can't add more than ${maxPages} pages!`); + return; + } + + pageCount++; + const newPage = document.createElement("div"); + newPage.classList.add("form-group"); + newPage.innerHTML = ``; + document.getElementById("pages").appendChild(newPage); +} \ No newline at end of file diff --git a/templates/comic_edit.html b/templates/comic_edit.html index 5025089..476db46 100644 --- a/templates/comic_edit.html +++ b/templates/comic_edit.html @@ -26,8 +26,8 @@