Skip to content

Commit

Permalink
ratings: fix wrong creation dates
Browse files Browse the repository at this point in the history
  • Loading branch information
rr- committed Nov 15, 2024
1 parent 459aa2a commit d89ac9a
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions backend/trcustoms/ratings/migrations/0002_auto_20241115_1135.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,31 @@
# Generated by Django 4.2.3 on 2024-11-15 11:35

from contextlib import contextmanager

from django.db import migrations


@contextmanager
def suppress_auto_now(model, field_names):
fields_state = {}
for field_name in field_names:
field = model._meta.get_field(field_name)
fields_state[field] = {
"auto_now": field.auto_now,
"auto_now_add": field.auto_now_add,
}

for field in fields_state:
field.auto_now = False
field.auto_now_add = False
try:
yield
finally:
for field, state in fields_state.items():
field.auto_now = state["auto_now"]
field.auto_now_add = state["auto_now_add"]


def forward_func(apps, schema_editor):
RatingClass = apps.get_model("common", "RatingClass")
ReviewTemplateQuestion = apps.get_model(
Expand Down Expand Up @@ -71,15 +94,16 @@ def forward_func(apps, schema_editor):

# Bulk create Ratings
print("Creating reviews")
Rating.objects.bulk_create(rating_instances)
with suppress_auto_now(Rating, ["created", "last_updated"]):
Rating.objects.bulk_create(rating_instances)

# Set many-to-many relations for answers
print("Mapping answers")
through_model = Rating.answers.through
m2m_relations = []
for review in Review.objects.all():
for review in Review.objects.prefetch_related("answers").iterator():
new_rating_id = review.id
for answer in review.answers.all():
for answer in review.answers.iterator():
m2m_relations.append(
through_model(
rating_id=new_rating_id,
Expand Down

0 comments on commit d89ac9a

Please sign in to comment.