From 7cbce7e64e509a74f1ba2fb26408971429a5afd0 Mon Sep 17 00:00:00 2001 From: Pawan Verma Date: Tue, 12 Sep 2023 11:30:01 +0530 Subject: [PATCH] Remove OneToOne user, add unique_together user and opportunity on Opportunity Access --- ...6_alter_opportunityaccess_user_and_more.py | 24 +++++++++++++++++++ commcare_connect/opportunity/models.py | 5 +++- 2 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 commcare_connect/opportunity/migrations/0016_alter_opportunityaccess_user_and_more.py diff --git a/commcare_connect/opportunity/migrations/0016_alter_opportunityaccess_user_and_more.py b/commcare_connect/opportunity/migrations/0016_alter_opportunityaccess_user_and_more.py new file mode 100644 index 00000000..b62c473c --- /dev/null +++ b/commcare_connect/opportunity/migrations/0016_alter_opportunityaccess_user_and_more.py @@ -0,0 +1,24 @@ +# Generated by Django 4.2.3 on 2023-09-12 05:21 + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ("opportunity", "0015_remove_opportunityaccess_date_claimed"), + ] + + operations = [ + migrations.AlterField( + model_name="opportunityaccess", + name="user", + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL), + ), + migrations.AlterUniqueTogether( + name="opportunityaccess", + unique_together={("user", "opportunity")}, + ), + ] diff --git a/commcare_connect/opportunity/models.py b/commcare_connect/opportunity/models.py index 42dfddd9..460c77c6 100644 --- a/commcare_connect/opportunity/models.py +++ b/commcare_connect/opportunity/models.py @@ -134,7 +134,7 @@ class Assessment(XFormBaseModel): class OpportunityAccess(models.Model): - user = models.OneToOneField(User, on_delete=models.CASCADE) + user = models.ForeignKey(User, on_delete=models.CASCADE) opportunity = models.ForeignKey(Opportunity, on_delete=models.CASCADE) date_learn_started = models.DateTimeField(null=True) @@ -166,6 +166,9 @@ def last_visit_date(self): return + class Meta: + unique_together = ("user", "opportunity") + class VisitValidationStatus(models.TextChoices): pending = "pending", gettext("Pending")