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/templates/list_topics.html b/app/templates/list_topics.html index c8abb7ee..6954176b 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 %} +
+
+
+
+
{{ _('Suggest a topic!') }}
+
+ {{ render_form(form) }} +
+
+
+
+
+{% 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 %} +
+
+
+
+
{{ _('Sorry') }}
+
+

{{ _('You have not been using PieFed long enough to be allowed to suggest Topics.') }}

+
+
+
+
+
+{% endblock %} \ No newline at end of file diff --git a/app/topic/forms.py b/app/topic/forms.py index e7a9ab2a..72ba5633 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': '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')) diff --git a/app/topic/routes.py b/app/topic/routes.py index fab41a44..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_ @@ -15,8 +16,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 +238,43 @@ 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(): + 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: + 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()