From b06976c608b01d54c529b382e4dc7a8f62779749 Mon Sep 17 00:00:00 2001 From: Boluwatife Popoola Date: Tue, 5 Nov 2024 12:43:55 +0100 Subject: [PATCH] sort round by chain, add constraint to round --- grantpicks/api.py | 10 ++++++ .../0004_alter_round_on_chain_id_and_more.py | 36 +++++++++++++++++++ grantpicks/models.py | 9 +++-- 3 files changed, 52 insertions(+), 3 deletions(-) create mode 100644 grantpicks/migrations/0004_alter_round_on_chain_id_and_more.py diff --git a/grantpicks/api.py b/grantpicks/api.py index d4c301d..73c40ea 100644 --- a/grantpicks/api.py +++ b/grantpicks/api.py @@ -57,6 +57,12 @@ class RoundsListAPI(APIView, CustomSizePageNumberPagination): location=OpenApiParameter.QUERY, description="Sort by field, e.g., deployed_at, vault_total_deposits", ), + OpenApiParameter( + "chain", + str, + OpenApiParameter.QUERY, + description="Filter projects by chain", + ), *pagination_parameters, ], responses={ @@ -79,6 +85,10 @@ class RoundsListAPI(APIView, CustomSizePageNumberPagination): @method_decorator(cache_page(60 * 1)) def get(self, request: Request, *args, **kwargs): rounds = Round.objects.all() + chain_param = request.query_params.get("chain") + if chain_param: + # chain = Chain.objects.get(name=chain_param) + rounds = rounds.filter(chain_id=chain_param) sort = request.query_params.get("sort", None) if sort == "deployed_at": rounds = rounds.order_by( diff --git a/grantpicks/migrations/0004_alter_round_on_chain_id_and_more.py b/grantpicks/migrations/0004_alter_round_on_chain_id_and_more.py new file mode 100644 index 0000000..a06f140 --- /dev/null +++ b/grantpicks/migrations/0004_alter_round_on_chain_id_and_more.py @@ -0,0 +1,36 @@ +# Generated by Django 5.0.6 on 2024-11-05 10:41 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("chains", "0003_add_stellar_chain"), + ("grantpicks", "0003_alter_votepair_project"), + ] + + operations = [ + migrations.AlterField( + model_name="round", + name="on_chain_id", + field=models.IntegerField( + help_text="Round ID in contract", verbose_name="contract round ID" + ), + ), + migrations.AlterField( + model_name="rounddeposit", + name="on_chain_id", + field=models.IntegerField( + help_text="Deposit ID in contract", verbose_name="contract deposit ID" + ), + ), + migrations.AlterUniqueTogether( + name="round", + unique_together={("chain", "on_chain_id")}, + ), + migrations.AlterUniqueTogether( + name="rounddeposit", + unique_together={("round", "on_chain_id")}, + ), + ] diff --git a/grantpicks/models.py b/grantpicks/models.py index 9fdd073..28ceaee 100644 --- a/grantpicks/models.py +++ b/grantpicks/models.py @@ -101,7 +101,6 @@ class Round(models.Model): on_chain_id = models.IntegerField( _("contract round ID"), null=False, - unique=True, help_text=_("Round ID in contract"), ) chain = models.ForeignKey( @@ -326,6 +325,9 @@ class Round(models.Model): help_text=_("Round complete date."), ) + class Meta: + unique_together = ('chain', 'on_chain_id') + @@ -357,7 +359,6 @@ def save(self, *args, **kwargs): super().save(*args, **kwargs) - class RoundDeposit(models.Model): id = models.AutoField( _("deposit id"), @@ -367,7 +368,6 @@ class RoundDeposit(models.Model): on_chain_id = models.IntegerField( _("contract deposit ID"), null=False, - unique=True, help_text=_("Deposit ID in contract"), ) round = models.ForeignKey( @@ -436,6 +436,9 @@ class RoundDeposit(models.Model): help_text=_("Transaction hash."), ) + class Meta: + unique_together = ('round', 'on_chain_id') + class Vote(models.Model): round = models.ForeignKey(Round, on_delete=models.CASCADE, related_name='votes') voter = models.ForeignKey(Account, on_delete=models.CASCADE, related_name='votes')