diff --git a/app/dev/forms.py b/app/dev/forms.py index 7fb9bedb..055e8b05 100644 --- a/app/dev/forms.py +++ b/app/dev/forms.py @@ -9,4 +9,7 @@ from app import db class AddTestCommunities(FlaskForm): - submit = SubmitField(_l('Populate Communities for Testing')) \ No newline at end of file + communities_submit = SubmitField(_l('Populate Communities for Testing')) + +class AddTestTopics(FlaskForm): + topics_submit = SubmitField(_l('Populate Topics for Testing')) diff --git a/app/dev/routes.py b/app/dev/routes.py index 28d7205b..1f1a220b 100644 --- a/app/dev/routes.py +++ b/app/dev/routes.py @@ -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) diff --git a/app/templates/dev/tools.html b/app/templates/dev/tools.html index 1a2b8c40..8f7e2d37 100644 --- a/app/templates/dev/tools.html +++ b/app/templates/dev/tools.html @@ -12,7 +12,17 @@
{{ _('Add Communities to DB for Testing') }}
- {{ render_form(form) }} + {{ render_form(communities_form) }} +
+
+ + +
+
+
+
{{ _('Add Topics to DB for Testing') }}
+
+ {{ render_form(topics_form) }}