diff --git a/backend/api/grants/mutations.py b/backend/api/grants/mutations.py index 5178179046..435403c170 100644 --- a/backend/api/grants/mutations.py +++ b/backend/api/grants/mutations.py @@ -90,6 +90,7 @@ def validate(self, conference: Conference, user: User) -> GrantErrors: "python_usage", "been_to_other_events", "why", + "grant_type", ) for field in non_empty_fields: @@ -110,7 +111,7 @@ class SendGrantInput(BaseGrantInput): age_group: AgeGroup gender: str occupation: Occupation - grant_type: GrantType + grant_type: list[GrantType] python_usage: str been_to_other_events: str community_contribution: str @@ -149,7 +150,7 @@ class UpdateGrantInput(BaseGrantInput): age_group: AgeGroup gender: str occupation: Occupation - grant_type: GrantType + grant_type: list[GrantType] python_usage: str been_to_other_events: str community_contribution: str diff --git a/backend/api/grants/types.py b/backend/api/grants/types.py index 2a399d9ecb..742028d53a 100644 --- a/backend/api/grants/types.py +++ b/backend/api/grants/types.py @@ -22,7 +22,7 @@ class Grant: age_group: Optional[AgeGroup] gender: str occupation: Occupation - grant_type: GrantType + grant_type: list[GrantType] python_usage: str community_contribution: str been_to_other_events: str @@ -46,7 +46,7 @@ def from_model(cls, grant: GrantModel) -> Grant: age_group=AgeGroup(grant.age_group) if grant.age_group else None, gender=grant.gender, occupation=Occupation(grant.occupation), - grant_type=GrantType(grant.grant_type), + grant_type=[GrantType(g) for g in grant.grant_type], python_usage=grant.python_usage, community_contribution=grant.community_contribution, been_to_other_events=grant.been_to_other_events, diff --git a/backend/grants/migrations/0023_alter_grant_grant_type.py b/backend/grants/migrations/0023_alter_grant_grant_type.py new file mode 100644 index 0000000000..d57511884b --- /dev/null +++ b/backend/grants/migrations/0023_alter_grant_grant_type.py @@ -0,0 +1,50 @@ +# Generated by Django 5.1.1 on 2024-12-01 17:59 + +from django.db import migrations, models +import json + +def forwards_func(apps, schema_editor): + Grant = apps.get_model('grants', 'Grant') + for grant in Grant.objects.all(): + old_value = grant.grant_type + # Convert the old string value into a list + grant.grant_type_json = [old_value] if old_value else [] + grant.save(update_fields=['grant_type_json']) + +def reverse_func(apps, schema_editor): + Grant = apps.get_model('grants', 'Grant') + for grant in Grant.objects.all(): + value_list = grant.grant_type + # Convert the list back to a single string + if value_list: + grant.grant_type = value_list[0] + else: + grant.grant_type = '' + grant.save(update_fields=['grant_type']) + +class Migration(migrations.Migration): + dependencies = [ + ("grants", "0022_grant_departure_city_grant_nationality_and_more"), + ] + + operations = [ + # Step 1: Add a temporary JSONField + migrations.AddField( + model_name='grant', + name='grant_type_json', + field=models.JSONField(default=list, verbose_name="grant type"), + ), + # Step 2: Backfill data into the temporary field + migrations.RunPython(forwards_func, reverse_func), + # Step 3: Remove the old field + migrations.RemoveField( + model_name='grant', + name='grant_type', + ), + # Step 4: Rename the temporary field to grant_type + migrations.RenameField( + model_name='grant', + old_name='grant_type_json', + new_name='grant_type', + ), + ] diff --git a/backend/grants/models.py b/backend/grants/models.py index 2d3a5ddc16..ef4304c745 100644 --- a/backend/grants/models.py +++ b/backend/grants/models.py @@ -97,9 +97,7 @@ class ApprovedType(models.TextChoices): ) # Your Grant Section - grant_type = models.CharField( - _("grant type"), choices=GrantType.choices, max_length=10 - ) + grant_type = models.JSONField(_("grant type"), default=list) departure_country = models.CharField( _("Departure Country"), max_length=100, diff --git a/backend/grants/tests/factories.py b/backend/grants/tests/factories.py index f03a67f053..bd716665f2 100644 --- a/backend/grants/tests/factories.py +++ b/backend/grants/tests/factories.py @@ -8,6 +8,7 @@ from countries import countries from participants.tests.factories import ParticipantFactory from participants.models import Participant +import random class GrantFactory(DjangoModelFactory): @@ -22,7 +23,12 @@ class Meta: age_group = factory.fuzzy.FuzzyChoice(Grant.AgeGroup) gender = factory.fuzzy.FuzzyChoice([gender[0] for gender in GENDERS]) occupation = factory.fuzzy.FuzzyChoice(Grant.Occupation) - grant_type = factory.fuzzy.FuzzyChoice(Grant.GrantType) + grant_type = factory.LazyFunction( + lambda: random.sample( + [choice[0] for choice in Grant.GrantType.choices], + k=random.randint(1, len(Grant.GrantType.choices)), + ) + ) python_usage = factory.Faker("text") been_to_other_events = factory.Faker("text") diff --git a/backend/reviews/templates/grant-review.html b/backend/reviews/templates/grant-review.html index dee1a6f1be..09c8840c26 100644 --- a/backend/reviews/templates/grant-review.html +++ b/backend/reviews/templates/grant-review.html @@ -112,7 +112,11 @@