tidy ups and tweaks to suggest topics form #281

This commit is contained in:
rimu 2024-08-08 19:26:07 +12:00
parent 674dc4c910
commit 654c4fe42e
5 changed files with 56 additions and 52 deletions

View file

@ -39,6 +39,18 @@ def send_welcome_email(user, application_required):
html_body=render_template('email/welcome.html', user=user, application_required=application_required)) html_body=render_template('email/welcome.html', user=user, application_required=application_required))
def send_topic_suggestion(communities_for_topic, user, recipients, subject, topic_name):
send_email(subject,
sender=f'{g.site.name} <{current_app.config["MAIL_FROM"]}>',
recipients=recipients,
text_body=render_template('email/suggested_topic.txt', site_name=g.site.name,
current_user_name=user.user_name, topic_name=topic_name,
communities_for_topic=communities_for_topic),
html_body=render_template('email/suggested_topic.html', site_name=g.site.name,
current_user_name=user.user_name, topic_name=topic_name,
communities_for_topic=communities_for_topic,
domain=current_app.config['SERVER_NAME']))
@celery.task @celery.task
def send_async_email(subject, sender, recipients, text_body, html_body, reply_to): def send_async_email(subject, sender, recipients, text_body, html_body, reply_to):
if 'ngrok.app' in sender: # for local development if 'ngrok.app' in sender: # for local development

View file

@ -7,7 +7,7 @@
{% set active_child = 'list_topics' -%} {% set active_child = 'list_topics' -%}
{% block app_content -%} {% block app_content -%}
{% if len(topics) > 0 -%} {% if len(topics) > 0 -%}
{% macro render_topic(topic, depth) -%} {% macro render_topic(topic, depth) -%}
<li> <li>
{% if depth == 0 -%}<strong>{% endif -%} {% if depth == 0 -%}<strong>{% endif -%}
@ -30,9 +30,11 @@
{% endfor -%} {% endfor -%}
</ul> </ul>
</div> </div>
{% else -%} {% else -%}
<p>{{ _('There are no communities yet.') }}</p> <p>{{ _('There are no communities yet.') }}</p>
{% endif -%} {% endif -%}
<p><a href="/communities" class="btn btn-primary">{{ _('Explore communities') }}</a></p> <p><a href="/communities" class="btn btn-primary">{{ _('Explore communities') }}</a></p>
<p><a href="/suggest-topics" class="btn btn-primary">{{ _('Suggest A Topic') }}</a></p> {% if current_user.is_authenticated and current_user.trustworthy() -%}
<p><a href="/topics/new" class="btn btn-primary">{{ _('Suggest a topic') }}</a></p>
{% endif -%}
{% endblock -%} {% endblock -%}

View file

@ -11,7 +11,7 @@
<div class="col col-login mx-auto"> <div class="col col-login mx-auto">
<div class="card mt-5"> <div class="card mt-5">
<div class="card-body p-6"> <div class="card-body p-6">
<div class="card-title">{{ _('Suggest a topic!') }}</div> <div class="card-title">{{ _('Suggest a topic') }}</div>
<div class="card-body"> <div class="card-body">
{{ render_form(form) }} {{ render_form(form) }}
</div> </div>

View file

@ -1,19 +1,18 @@
from flask import request
from flask_login import current_user
from flask_wtf import FlaskForm from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField, TextAreaField, BooleanField, HiddenField, SelectField, FileField from wtforms import StringField, SubmitField, TextAreaField, BooleanField, HiddenField, SelectField, FileField
from wtforms.validators import ValidationError, DataRequired, Email, EqualTo, Length, Optional from wtforms.validators import ValidationError, DataRequired, Email, EqualTo, Length, Optional
from flask_babel import _, lazy_gettext as _l from flask_babel import _, lazy_gettext as _l
from app.utils import MultiCheckboxField from app.utils import MultiCheckboxField
from app import db
class ChooseTopicsForm(FlaskForm): class ChooseTopicsForm(FlaskForm):
chosen_topics = MultiCheckboxField(_l('Choose some topics you are interested in'), coerce=int) chosen_topics = MultiCheckboxField(_l('Choose some topics you are interested in'), coerce=int)
submit = SubmitField(_l('Choose')) submit = SubmitField(_l('Choose'))
class SuggestTopicsForm(FlaskForm): class SuggestTopicsForm(FlaskForm):
topic_name = TextAreaField(_l('New Topic Name'), validators=[DataRequired(), Length(min=1, max=100)], render_kw={'placeholder': 'New Topic name here...'}) topic_name = TextAreaField(_l('New topic name'), validators=[DataRequired(), Length(min=1, max=100)],
communities_for_topic = TextAreaField(_l('Suggested Communities'), validators=[DataRequired(), Length(min=1, max=5000)], render_kw={'placeholder': 'Comma seperated list of community suggestions'}) render_kw={'placeholder': _l('New topic name here...')})
communities_for_topic = TextAreaField(_l('Suggested communities'), validators=[Optional(), Length(max=5000)],
render_kw={'placeholder': _l('Comma seperated list of community suggestions')})
submit = SubmitField(_l('Submit')) submit = SubmitField(_l('Submit'))

View file

@ -16,7 +16,7 @@ from app.inoculation import inoculation
from app.models import Topic, Community, Post, utcnow, CommunityMember, CommunityJoinRequest, User, \ from app.models import Topic, Community, Post, utcnow, CommunityMember, CommunityJoinRequest, User, \
NotificationSubscription NotificationSubscription
from app.topic import bp from app.topic import bp
from app.email import send_email from app.email import send_email, send_topic_suggestion
from app import db, celery, cache from app import db, celery, cache
from app.topic.forms import ChooseTopicsForm, SuggestTopicsForm from app.topic.forms import ChooseTopicsForm, SuggestTopicsForm
from app.utils import render_template, user_filters_posts, moderating_communities, joined_communities, \ from app.utils import render_template, user_filters_posts, moderating_communities, joined_communities, \
@ -238,38 +238,29 @@ def topic_notification(topic_id: int):
return render_template('topic/_notification_toggle.html', topic=topic) return render_template('topic/_notification_toggle.html', topic=topic)
@bp.route('/suggest-topics', methods=['GET', 'POST'])
@bp.route('/topics/new', methods=['GET', 'POST'])
@login_required @login_required
def suggest_topics(): def suggest_topics():
form = SuggestTopicsForm() form = SuggestTopicsForm()
if current_user.created_recently() or current_user.reputation <= -10 or current_user.banned or not current_user.verified: if not current_user.trustworthy():
return redirect(url_for('topic.suggestion_denied')) return redirect(url_for('topic.suggestion_denied'))
if form.validate_on_submit(): if form.validate_on_submit():
subject = f'New Topic Suggestion from {g.site.name}' subject = f'New topic suggestion from {g.site.name}'
sender = f'{g.site.name} <{current_app.config["MAIL_FROM"]}>'
recipients = g.site.contact_email recipients = g.site.contact_email
topic_name = form.topic_name.data topic_name = form.topic_name.data
communities_for_topic = form.communities_for_topic.data communities_for_topic = form.communities_for_topic.data
send_email(subject, send_topic_suggestion(communities_for_topic, current_user, recipients, subject, topic_name)
sender, flash(_(f'Thank you for the topic suggestion, it has been sent to the site administrator(s).'))
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')) return redirect(url_for('main.list_topics'))
else: else:
return render_template('topic/suggest_topics.html', form=form, title=_('Suggest A Topic!"'), return render_template('topic/suggest_topics.html', form=form, title=_('Suggest a topic"'),
moderating_communities=moderating_communities(current_user.get_id()), moderating_communities=moderating_communities(current_user.get_id()),
joined_communities=joined_communities(current_user.get_id()), joined_communities=joined_communities(current_user.get_id()),
menu_topics=menu_topics(), menu_topics=menu_topics(),
site=g.site) site=g.site)
@bp.route('/topic/suggestion-denied', methods=['GET']) @bp.route('/topic/suggestion-denied', methods=['GET'])
@login_required @login_required
def suggestion_denied(): def suggestion_denied():