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))
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
def send_async_email(subject, sender, recipients, text_body, html_body, reply_to):
if 'ngrok.app' in sender: # for local development

View file

@ -7,32 +7,34 @@
{% set active_child = 'list_topics' -%}
{% block app_content -%}
{% if len(topics) > 0 -%}
{% macro render_topic(topic, depth) -%}
<li>
{% if depth == 0 -%}<strong>{% endif -%}
<a href="/topic/{{ topic['topic'].path() }}">{{ topic['topic'].name }}</a>
{% if depth == 0 -%}</strong>{% endif -%}
{% if topic['children'] -%}
<ul>
{% for topic in topic['children'] -%}
{{ render_topic(topic, depth + 1)|safe }}
{% if len(topics) > 0 -%}
{% macro render_topic(topic, depth) -%}
<li>
{% if depth == 0 -%}<strong>{% endif -%}
<a href="/topic/{{ topic['topic'].path() }}">{{ topic['topic'].name }}</a>
{% if depth == 0 -%}</strong>{% endif -%}
{% if topic['children'] -%}
<ul>
{% for topic in topic['children'] -%}
{{ render_topic(topic, depth + 1)|safe }}
{% endfor -%}
</ul>
{% endif -%}
</li>
{% endmacro -%}
<h1>{{ _('Choose a topic') }}</h1>
<div class="table-responsive-md mt-4">
<ul class="topics_list">
{% for topic in topics -%}
{{ render_topic(topic, 0)|safe }}
{% endfor -%}
</ul>
{% endif -%}
</li>
{% endmacro -%}
<h1>{{ _('Choose a topic') }}</h1>
<div class="table-responsive-md mt-4">
<ul class="topics_list">
{% for topic in topics -%}
{{ render_topic(topic, 0)|safe }}
{% endfor -%}
</ul>
</div>
{% else -%}
<p>{{ _('There are no communities yet.') }}</p>
{% endif -%}
<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>
</ul>
</div>
{% else -%}
<p>{{ _('There are no communities yet.') }}</p>
{% endif -%}
<p><a href="/communities" class="btn btn-primary">{{ _('Explore communities') }}</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 -%}

View file

@ -11,7 +11,7 @@
<div class="col col-login mx-auto">
<div class="card mt-5">
<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">
{{ render_form(form) }}
</div>

View file

@ -1,19 +1,18 @@
from flask import request
from flask_login import current_user
from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField, TextAreaField, BooleanField, HiddenField, SelectField, FileField
from wtforms.validators import ValidationError, DataRequired, Email, EqualTo, Length, Optional
from flask_babel import _, lazy_gettext as _l
from app.utils import MultiCheckboxField
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'})
topic_name = TextAreaField(_l('New topic name'), validators=[DataRequired(), Length(min=1, max=100)],
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'))

View file

@ -16,7 +16,7 @@ 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.email import send_email, send_topic_suggestion
from app import db, celery, cache
from app.topic.forms import ChooseTopicsForm, SuggestTopicsForm
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)
@bp.route('/suggest-topics', methods=['GET', 'POST'])
@bp.route('/topics/new', 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:
if not current_user.trustworthy():
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"]}>'
subject = f'New topic suggestion from {g.site.name}'
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)'))
send_topic_suggestion(communities_for_topic, current_user, recipients, subject, topic_name)
flash(_(f'Thank you for the topic suggestion, it 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!"'),
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():