Skip to content

Commit

Permalink
apps/projects: adds custom migration
Browse files Browse the repository at this point in the history
  • Loading branch information
hklarner committed Jul 17, 2023
1 parent b70d550 commit 180e366
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 6 deletions.
2 changes: 1 addition & 1 deletion apps/projects/insights.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def create_insight(project: Project) -> ProjectInsight:
project=project,
comments=comments.count(),
ratings=sum(x.count() for x in rating_objects),
written_ideas=ideas.count() + map_ideas.count() + proposals.count(),
written_ideas=sum(x.count() for x in [ideas, map_ideas, proposals, topics]),
poll_answers=votes.count() + answers.count(),
live_questions=live_questions.count(),
)
Expand Down
37 changes: 37 additions & 0 deletions apps/projects/migrations/0007_auto_20230717_1054.py
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.

Copy link
@m4ra

m4ra Jul 17, 2023

Contributor

@hklarner can you squash this and the previous one? becasue this one only alters the fields that were created in the previous one.

This comment has been minimized.

Copy link
@hklarner

hklarner Jul 17, 2023

Author Contributor

Yes, I was thinking we clean the whole PR up at the end, i.e. create a single migration and a single commit.

89 changes: 89 additions & 0 deletions apps/projects/migrations/0008_initialize_insights.py
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),
]
10 changes: 5 additions & 5 deletions apps/projects/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,11 @@ class Meta:
class ProjectInsight(base.TimeStampedModel):
project = models.OneToOneField(to=Project, on_delete=models.CASCADE)
active_participants = models.ManyToManyField(to=User)
comments = models.PositiveIntegerField()
ratings = models.PositiveIntegerField()
written_ideas = models.PositiveIntegerField()
poll_answers = models.PositiveIntegerField()
live_questions = models.PositiveIntegerField()
comments = models.PositiveIntegerField(default=0)
ratings = models.PositiveIntegerField(default=0)
written_ideas = models.PositiveIntegerField(default=0)
poll_answers = models.PositiveIntegerField(default=0)
live_questions = models.PositiveIntegerField(default=0)
display = models.BooleanField(default=False)

def __str__(self):
Expand Down

0 comments on commit 180e366

Please sign in to comment.