Skip to content

Commit

Permalink
feat(vote): Create constraint unique_cpf_per_year on model candidature
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiomario committed Oct 8, 2024
1 parent ad0ec16 commit ecad1eb
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 26 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Generated by Django 4.2 on 2024-10-08 14:40

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('candidature', '0022_alter_candidature_ballot_name_and_more'),
]

operations = [
migrations.CreateModel(
name='ElectionResult',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('status', models.CharField(choices=[('eleita', 'Eleita/o'), ('segundo_turno', 'Segundo Turno'), ('nao_eleito', 'Não Eleita/o')], max_length=20, verbose_name='Status da eleição')),
],
),
migrations.AddField(
model_name='candidature',
name='election_year',
field=models.PositiveIntegerField(default=2024, verbose_name='Ano da eleição'),
),
migrations.AddConstraint(
model_name='candidature',
constraint=models.UniqueConstraint(fields=('cpf', 'election_year'), name='unique_cpf_per_year'),
),
migrations.AddField(
model_name='electionresult',
name='candidature',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='election_results', to='candidature.candidature'),
),
]
10 changes: 7 additions & 3 deletions app/org_eleicoes/votepeloclima/candidature/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class Candidature(models.Model):
proposes = models.JSONField(blank=True, verbose_name="Propostas")
appointments = models.JSONField(blank=True, verbose_name="Compromissos")

election_year = models.PositiveIntegerField(default=2024, verbose_name="Ano da eleição")

# friendly url by ballot_name
slug = models.SlugField(max_length=100, unique=True, blank=True, null=True)

Expand All @@ -48,6 +50,9 @@ class Candidature(models.Model):
class Meta:
verbose_name = "Candidatura"
ordering = ["-updated_at"]
constraints = [
models.UniqueConstraint(fields=['cpf', 'election_year'], name='unique_cpf_per_year')
]

@property
def status(self):
Expand Down Expand Up @@ -105,8 +110,8 @@ def get_proposes_items(self):
return proposes_list

@property
def get_election_result(self, year):
return self.election_results.filter(year=year).first()
def get_election_result(self):
return self.election_results.first()


def save(self, *args, **kwargs):
Expand All @@ -117,7 +122,6 @@ def save(self, *args, **kwargs):

class ElectionResult(models.Model):
candidature = models.ForeignKey('Candidature', on_delete=models.CASCADE, related_name='election_results')
year = models.PositiveIntegerField(verbose_name="Ano da eleição")
status = models.CharField(max_length=20, choices=ElectionStatus.choices, verbose_name="Status da eleição")


Expand Down

0 comments on commit ecad1eb

Please sign in to comment.