-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
apps/projects: adds custom migration
- Loading branch information
Showing
4 changed files
with
132 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# Generated by Django 3.2.19 on 2023-07-17 08:54 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("a4_candy_projects", "0006_auto_20230716_2352"), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name="projectinsight", | ||
name="comments", | ||
field=models.PositiveIntegerField(default=0), | ||
), | ||
migrations.AlterField( | ||
model_name="projectinsight", | ||
name="live_questions", | ||
field=models.PositiveIntegerField(default=0), | ||
), | ||
migrations.AlterField( | ||
model_name="projectinsight", | ||
name="poll_answers", | ||
field=models.PositiveIntegerField(default=0), | ||
), | ||
migrations.AlterField( | ||
model_name="projectinsight", | ||
name="ratings", | ||
field=models.PositiveIntegerField(default=0), | ||
), | ||
migrations.AlterField( | ||
model_name="projectinsight", | ||
name="written_ideas", | ||
field=models.PositiveIntegerField(default=0), | ||
), | ||
] | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
hklarner
Author
Contributor
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
# Generated by Django 3.2.19 on 2023-07-12 15:44 | ||
|
||
from collections import defaultdict | ||
from django.db import migrations | ||
from django.db.models import Q | ||
|
||
|
||
def initialize_insights(apps, schema_editor): | ||
Project = apps.get_model("a4projects", "Project") | ||
ProjectInsight = apps.get_model("a4_candy_projects", "ProjectInsight") | ||
|
||
Comment = apps.get_model("a4comments", "Comment") | ||
Answer = apps.get_model("a4polls", "Answer") | ||
Poll = apps.get_model("a4polls", "Poll") | ||
Vote = apps.get_model("a4polls", "Vote") | ||
Rating = apps.get_model("a4ratings", "Rating") | ||
Proposal = apps.get_model("a4_candy_budgeting", "Proposal") | ||
Idea = apps.get_model("a4_candy_ideas", "Idea") | ||
Like = apps.get_model("a4_candy_interactive_events", "Like") | ||
LiveQuestion = apps.get_model("a4_candy_interactive_events", "LiveQuestion") | ||
MapIdea = apps.get_model("a4_candy_mapideas", "MapIdea") | ||
Topic = apps.get_model("a4_candy_topicprio", "Topic") | ||
|
||
insights = defaultdict(ProjectInsight) | ||
user_ids = defaultdict(set) | ||
errors = defaultdict(list) | ||
|
||
for idea_model in [Idea, MapIdea, Proposal, Topic]: | ||
for x in idea_model.objects.prefetch_related("module"): | ||
project = x.module.project | ||
insights[project].written_ideas += 1 | ||
user_ids[project].add(x.creator.id) | ||
|
||
for live_question in LiveQuestion.objects.all(): | ||
insights[live_question.module.project].live_questions += 1 | ||
|
||
for answer in Answer.objects.all(): | ||
project = answer.question.poll.module.project | ||
insights[project].live_questions += 1 | ||
user_ids[project].add(answer.creator.id) | ||
|
||
for vote in Vote.objects.all(): | ||
project = vote.choice.question.poll.module.project | ||
insights[project].live_questions += 1 | ||
user_ids[project].add(vote.creator.id) | ||
|
||
for poll in Poll.objects.all(): | ||
project = poll.module.project | ||
insights[project].live_questions += 1 | ||
user_ids[project].add(poll.creator.id) | ||
|
||
for comment in Comment.objects.all(): | ||
project = comment.project | ||
insights[project].comments += 1 | ||
user_ids[project].add(comment.creator.id) | ||
|
||
for like in Like.objects.all(): | ||
project = like.livequestion.module.project | ||
insights[project].ratings += 1 | ||
|
||
for x in Rating.objects.all(): | ||
try: | ||
project = x.module.project | ||
except Exception as e: | ||
errors[str(e)].append(x) | ||
continue | ||
|
||
insights[project].ratings += 1 | ||
user_ids[project].add(x.creator.id) | ||
|
||
if errors: | ||
print() | ||
print("errors:") | ||
for key, objects in errors.items(): | ||
print(f"{key=}, {len(objects)=}, {objects[:2]=}") | ||
|
||
|
||
def do_nothing(apps, schema_editor): | ||
pass | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("a4_candy_projects", "0007_auto_20230717_1054"), | ||
] | ||
|
||
operations = [ | ||
migrations.RunPython(code=initialize_insights, reverse_code=do_nothing), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@hklarner can you squash this and the previous one? becasue this one only alters the fields that were created in the previous one.