Merge pull request 'Adding a "Suggest A Topic" page.' (#281) from JollyDevelopment/pyfedi:jollydev/adding-suggest-topic-option into main

Reviewed-on: https://codeberg.org/rimu/pyfedi/pulls/281
This commit is contained in:
rimu 2024-08-08 07:09:50 +00:00
commit 674dc4c910
7 changed files with 113 additions and 1 deletions

View file

@ -0,0 +1,15 @@
<p style="margin-bottom: 0;"><a href="https://{{ domain }}/"><img src="https://{{ domain }}/static/images/logo2.png" style="max-width: 100%;" width="50" height="50"></a></p>
<p>Hi {{ site_name }} Admin,</p>
<p>The user {{ current_user_name }} has suggested the following new Topic:</p>
<table>
<tr>
<td>Topic Name: </td>
<td>{{ topic_name }}</td>
</tr>
<tr>
<td>Communities: </td>
<td>{{ communities_for_topic }}</td>
</tr>
</table>
<p>Thank you for your time and attention.</p>

View file

@ -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.

View file

@ -34,4 +34,5 @@
<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>
{% endblock -%}

View file

@ -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 %}
<div class="row">
<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-body">
{{ render_form(form) }}
</div>
</div>
</div>
</div>
</div>
{% endblock %}

View file

@ -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 %}
<div class="row">
<div class="col col-login mx-auto">
<div class="card mt-5">
<div class="card-body p-6">
<div class="card-title">{{ _('Sorry') }}</div>
<div class="card-body">
<p>{{ _('You have not been using PieFed long enough to be allowed to suggest Topics.') }}</p>
</div>
</div>
</div>
</div>
</div>
{% endblock %}

View file

@ -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'))

View file

@ -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()