got forms and buttons to work

This commit is contained in:
Alan Roberts 2024-08-09 10:57:46 -04:00
parent 94a06829d8
commit e28ffb3d40
3 changed files with 83 additions and 11 deletions

View file

@ -9,4 +9,7 @@ from app import db
class AddTestCommunities(FlaskForm):
submit = SubmitField(_l('Populate Communities for Testing'))
communities_submit = SubmitField(_l('Populate Communities for Testing'))
class AddTestTopics(FlaskForm):
topics_submit = SubmitField(_l('Populate Topics for Testing'))

View file

@ -5,21 +5,20 @@ from sqlalchemy import desc, or_, and_, text
from app.activitypub.signature import RsaKeys
from app import db, celery, cache
from app.dev.forms import AddTestCommunities
# from app.chat.forms import AddReply, ReportConversationForm
# from app.chat.util import send_message
from app.models import Site, User, Community, CommunityMember, Language
# from app.user.forms import ReportUserForm
from app.dev.forms import AddTestCommunities, AddTestTopics
from app.models import Site, User, Community, CommunityMember, Language, Topic
from app.utils import render_template, community_membership, moderating_communities, joined_communities, menu_topics, markdown_to_html
from app.dev import bp
import random
# use this to populate communities in the database
@bp.route('/dev/tools', methods=['GET', 'POST'])
@login_required
def populate_communities():
form = AddTestCommunities()
if form.validate_on_submit():
def tools():
communities_form = AddTestCommunities()
topics_form = AddTestTopics()
if communities_form.communities_submit.data and communities_form.validate():
# do a for loop for a range up to 30 or so
# build the community title from that and then submit it
# to the db
@ -64,5 +63,65 @@ def populate_communities():
cache.delete_memoized(joined_communities, current_user.id)
cache.delete_memoized(moderating_communities, current_user.id)
return redirect(url_for('main.list_communities'))
elif topics_form.topics_submit.data and topics_form.validate():
# get the list of communities in the db
communities = Community.query.filter_by(banned=False)
# pick 10 random communities from the communities list
rand_communities = []
for c in range(10):
rand_communities.append(random.choice(communities.all()))
# get those community_ids
# rand_community_ids = []
# for c in rand_communities:
# rand_community_ids.append(c.id)
# generate new topics
# for n in range(10):
# # generate strings for name, machine_name, and default to 0 communities
# loop_num = "{:02d}".format(n)
# name = "dev_Topic_" + loop_num
# machine_name = "dev-topic-" + loop_num
# num_communities = 0
# # make the topic
# topic = Topic(name=name, machine_name=machine_name, num_communities=num_communities)
# # add the new topic to the db
# db.session.add(topic)
# db.session.commit()
# # refresh the topic menu
# cache.delete_memoized(menu_topics)
# get the list of topics in the db
topics = Topic.query.filter_by()
# pick 10 random topics from the topics list
rand_topics = []
for t in range(10):
rand_topics.append(random.choice(topics.all()))
# get those topic_ids
rand_topic_ids = []
for t in rand_topics:
rand_topic_ids.append(t.id)
# loop 10 times
# get the community
# add the topic_id to the community
# save the db
# update the num_communities for the topic
# save the db again
for i in range(10):
community = rand_communities[i]
community.topic_id = rand_topic_ids[i]
db.session.commit()
community.topic.num_communities = community.topic.communities.count()
db.session.commit()
# flash(_(f'rand_topic_ids: {rand_topic_ids}'))
# return redirect(url_for('dev.tools'))
return redirect(url_for('main.list_topics'))
else:
return render_template('dev/tools.html', form=form)
return render_template('dev/tools.html', communities_form=communities_form, topics_form=topics_form)

View file

@ -12,7 +12,17 @@
<div class="card-body p-6">
<div class="card-title">{{ _('Add Communities to DB for Testing') }}</div>
<div class="card-body">
{{ render_form(form) }}
{{ render_form(communities_form) }}
</div>
</div>
</div>
</div>
<div class="col col-login mx-auto">
<div class="card mt-5">
<div class="card-body p-6">
<div class="card-title">{{ _('Add Topics to DB for Testing') }}</div>
<div class="card-body">
{{ render_form(topics_form) }}
</div>
</div>
</div>