From 99e3ff039171b30b5388051b75cfec55a92e5641 Mon Sep 17 00:00:00 2001 From: Alan Roberts Date: Tue, 6 Aug 2024 16:25:27 -0400 Subject: [PATCH 1/4] Adding a Suggest Topics page --- app/templates/list_topics.html | 1 + app/templates/topic/suggest_topics.html | 22 +++++++++++++++ app/templates/topic/suggestion_denied.html | 22 +++++++++++++++ app/topic/forms.py | 5 ++++ app/topic/routes.py | 32 +++++++++++++++++++++- 5 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 app/templates/topic/suggest_topics.html create mode 100644 app/templates/topic/suggestion_denied.html diff --git a/app/templates/list_topics.html b/app/templates/list_topics.html index c8abb7ee..e63f796f 100644 --- a/app/templates/list_topics.html +++ b/app/templates/list_topics.html @@ -34,4 +34,5 @@

{{ _('There are no communities yet.') }}

{% endif -%}

{{ _('Explore communities') }}

+

{{ _('Suggest A Topic') }}

{% endblock -%} diff --git a/app/templates/topic/suggest_topics.html b/app/templates/topic/suggest_topics.html new file mode 100644 index 00000000..ac3e54a4 --- /dev/null +++ b/app/templates/topic/suggest_topics.html @@ -0,0 +1,22 @@ +{% if theme() and file_exists('app/templates/themes/' + theme() + '/base.html') %} + {% extends 'themes/' + theme() + '/base.html' %} +{% else %} + {% extends "base.html" %} +{% endif %} %} +{% set active_child = 'suggest_topics' %} +{% from 'bootstrap/form.html' import render_form %} + +{% block app_content %} +
+ +
+{% endblock %} \ No newline at end of file diff --git a/app/templates/topic/suggestion_denied.html b/app/templates/topic/suggestion_denied.html new file mode 100644 index 00000000..4f9313d0 --- /dev/null +++ b/app/templates/topic/suggestion_denied.html @@ -0,0 +1,22 @@ +{% if theme() and file_exists('app/templates/themes/' + theme() + '/base.html') %} + {% extends 'themes/' + theme() + '/base.html' %} +{% else %} + {% extends "base.html" %} +{% endif %} %} +{% from 'bootstrap/form.html' import render_form %} +{% set active_child = 'suggest_topics' %} + +{% block app_content %} +
+ +
+{% endblock %} \ No newline at end of file diff --git a/app/topic/forms.py b/app/topic/forms.py index e7a9ab2a..7a5a19c8 100644 --- a/app/topic/forms.py +++ b/app/topic/forms.py @@ -12,3 +12,8 @@ from app import db class ChooseTopicsForm(FlaskForm): chosen_topics = MultiCheckboxField(_l('Choose some topics you are interested in'), coerce=int) submit = SubmitField(_l('Choose')) + +class SuggestTopicsForm(FlaskForm): + topic_name = TextAreaField(_l('New Topic Name'), validators=[DataRequired(), Length(min=1, max=100)], render_kw={'placeholder': 'Type a reply here...'}) + communities_for_topic = TextAreaField(_l('Suggested Communities'), validators=[DataRequired(), Length(min=1, max=5000)], render_kw={'placeholder': 'Comma seperated list of community suggestions'}) + submit = SubmitField(_l('Submit')) diff --git a/app/topic/routes.py b/app/topic/routes.py index fab41a44..1d24a40c 100644 --- a/app/topic/routes.py +++ b/app/topic/routes.py @@ -15,8 +15,9 @@ from app.inoculation import inoculation from app.models import Topic, Community, Post, utcnow, CommunityMember, CommunityJoinRequest, User, \ NotificationSubscription from app.topic import bp +from app.email import send_email from app import db, celery, cache -from app.topic.forms import ChooseTopicsForm +from app.topic.forms import ChooseTopicsForm, SuggestTopicsForm from app.utils import render_template, user_filters_posts, moderating_communities, joined_communities, \ community_membership, blocked_domains, validation_required, mimetype_from_url, blocked_instances, \ communities_banned_from, blocked_users, menu_topics @@ -236,6 +237,35 @@ def topic_notification(topic_id: int): return render_template('topic/_notification_toggle.html', topic=topic) +@bp.route('/suggest-topics', methods=['GET', 'POST']) +@login_required +def suggest_topics(): + form = SuggestTopicsForm() + if current_user.created_recently() or current_user.reputation <= -10 or current_user.banned or not current_user.verified: + return redirect(url_for('topic.suggestion_denied')) + if form.validate_on_submit(): + sub = f'New Topic Suggestion from {g.site.name}' + send = f'{g.site.name} <{current_app.config["MAIL_FROM"]}>' + recip = g.site.contact_email + tn = form.topic_name.data + cft = form.communities_for_topic.data + text_body = f'{current_user.user_name} suggested the new Topic "{tn}", containing the communities: {cft}' + html_body = f'

{current_user.user_name} suggested the new Topic "{tn}", containing the communities: {cft}

' + send_email(sub, send, recip, text_body=text_body, html_body=html_body) + flash(_(f'Thank you for the Topic Suggestion! Your suggestion has been sent to the site administrator(s)')) + return redirect(url_for('main.list_topics')) + else: + return render_template('topic/suggest_topics.html', form=form, title=_('Suggest A Topic!"'), + moderating_communities=moderating_communities(current_user.get_id()), + joined_communities=joined_communities(current_user.get_id()), + menu_topics=menu_topics(), + site=g.site) + +@bp.route('/topic/suggestion-denied', methods=['GET']) +@login_required +def suggestion_denied(): + return render_template('topic/suggestion_denied.html') + def topics_for_form(): topics = Topic.query.filter_by(parent_id=None).order_by(Topic.name).all() From f4dfe0a9b2765f70add609ed16a8f162797d1768 Mon Sep 17 00:00:00 2001 From: Alan Roberts Date: Tue, 6 Aug 2024 16:26:53 -0400 Subject: [PATCH 2/4] move to hyphen rather than underscore for href --- app/templates/list_topics.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/templates/list_topics.html b/app/templates/list_topics.html index e63f796f..6954176b 100644 --- a/app/templates/list_topics.html +++ b/app/templates/list_topics.html @@ -34,5 +34,5 @@

{{ _('There are no communities yet.') }}

{% endif -%}

{{ _('Explore communities') }}

-

{{ _('Suggest A Topic') }}

+

{{ _('Suggest A Topic') }}

{% endblock -%} From f4cbac34ba97baa5188536afe787027eada094be Mon Sep 17 00:00:00 2001 From: Alan Roberts Date: Wed, 7 Aug 2024 10:21:00 -0400 Subject: [PATCH 3/4] converting to using rendered templates --- app/templates/email/suggested_topic.html | 15 ++++++++++++++ app/templates/email/suggested_topic.txt | 8 ++++++++ app/topic/routes.py | 25 ++++++++++++++++-------- 3 files changed, 40 insertions(+), 8 deletions(-) create mode 100644 app/templates/email/suggested_topic.html create mode 100644 app/templates/email/suggested_topic.txt diff --git a/app/templates/email/suggested_topic.html b/app/templates/email/suggested_topic.html new file mode 100644 index 00000000..cce382aa --- /dev/null +++ b/app/templates/email/suggested_topic.html @@ -0,0 +1,15 @@ +

+

Hi {{ site_name }} Admin,

+

The user {{ current_user_name }} has suggested the following new Topic:

+ + + + + + + + + +
Topic Name: {{ topic_name }}
Communities: {{ communities_for_topic }}
+ +

Thank you for your time and attention.

\ No newline at end of file diff --git a/app/templates/email/suggested_topic.txt b/app/templates/email/suggested_topic.txt new file mode 100644 index 00000000..aaaa46d1 --- /dev/null +++ b/app/templates/email/suggested_topic.txt @@ -0,0 +1,8 @@ +Hi {{ site_name }} Admin! + +The user {{ current_user_name }} has suggested the following new Topic: + +Topic Name: {{ topic_name }} +Communities: {{ communities_for_topic }} + +Thank you for your time and attention. \ No newline at end of file diff --git a/app/topic/routes.py b/app/topic/routes.py index 1d24a40c..7d215fb8 100644 --- a/app/topic/routes.py +++ b/app/topic/routes.py @@ -4,6 +4,7 @@ from random import randint from feedgen.feed import FeedGenerator from flask import request, flash, json, url_for, current_app, redirect, abort, make_response, g +from flask import render_template as flask_render_template from flask_login import login_required, current_user from flask_babel import _ from sqlalchemy import text, desc, or_ @@ -244,14 +245,22 @@ def suggest_topics(): if current_user.created_recently() or current_user.reputation <= -10 or current_user.banned or not current_user.verified: return redirect(url_for('topic.suggestion_denied')) if form.validate_on_submit(): - sub = f'New Topic Suggestion from {g.site.name}' - send = f'{g.site.name} <{current_app.config["MAIL_FROM"]}>' - recip = g.site.contact_email - tn = form.topic_name.data - cft = form.communities_for_topic.data - text_body = f'{current_user.user_name} suggested the new Topic "{tn}", containing the communities: {cft}' - html_body = f'

{current_user.user_name} suggested the new Topic "{tn}", containing the communities: {cft}

' - send_email(sub, send, recip, text_body=text_body, html_body=html_body) + subject = f'New Topic Suggestion from {g.site.name}' + sender = f'{g.site.name} <{current_app.config["MAIL_FROM"]}>' + recipients = g.site.contact_email + topic_name = form.topic_name.data + communities_for_topic = form.communities_for_topic.data + send_email(subject, + sender, + recipients, + text_body=flask_render_template('email/suggested_topic.txt', site_name=g.site.name, + current_user_name=current_user.user_name, topic_name=topic_name, + communities_for_topic=communities_for_topic), + html_body=flask_render_template('email/suggested_topic.html', site_name=g.site.name, + current_user_name=current_user.user_name, topic_name=topic_name, + communities_for_topic=communities_for_topic, + domain=current_app.config['SERVER_NAME']) + ) flash(_(f'Thank you for the Topic Suggestion! Your suggestion has been sent to the site administrator(s)')) return redirect(url_for('main.list_topics')) else: From 0961d3e5dd56a6d10c0bdb63d1a8993e7dfdb37d Mon Sep 17 00:00:00 2001 From: Alan Roberts Date: Wed, 7 Aug 2024 12:21:00 -0400 Subject: [PATCH 4/4] better topic name placeholder text --- app/topic/forms.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/topic/forms.py b/app/topic/forms.py index 7a5a19c8..72ba5633 100644 --- a/app/topic/forms.py +++ b/app/topic/forms.py @@ -14,6 +14,6 @@ class ChooseTopicsForm(FlaskForm): submit = SubmitField(_l('Choose')) class SuggestTopicsForm(FlaskForm): - topic_name = TextAreaField(_l('New Topic Name'), validators=[DataRequired(), Length(min=1, max=100)], render_kw={'placeholder': 'Type a reply here...'}) + topic_name = TextAreaField(_l('New Topic Name'), validators=[DataRequired(), Length(min=1, max=100)], render_kw={'placeholder': 'New Topic name here...'}) communities_for_topic = TextAreaField(_l('Suggested Communities'), validators=[DataRequired(), Length(min=1, max=5000)], render_kw={'placeholder': 'Comma seperated list of community suggestions'}) submit = SubmitField(_l('Submit'))