Add Passage model

This commit is contained in:
Yohann D'ANELLO 2021-01-14 14:22:45 +01:00
parent 4d83664c0d
commit 7a6aaa3f58
Signed by: ynerant
GPG Key ID: 3A75C55819C8CF85
2 changed files with 101 additions and 7 deletions

View File

@ -0,0 +1,43 @@
# Generated by Django 3.0.11 on 2021-01-14 12:13
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('participation', '0008_auto_20210113_1700'),
]
operations = [
migrations.CreateModel(
name='Passage',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('place', models.CharField(default='Non indiqué', help_text='Where the solution is presented?', max_length=255, verbose_name='place')),
('defender', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='+', to='participation.Participation', verbose_name='defender')),
('opponent', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='+', to='participation.Participation', verbose_name='opponent')),
('pool', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='passages', to='participation.Pool', verbose_name='pool')),
('reporter', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='+', to='participation.Participation', verbose_name='reporter')),
],
options={
'verbose_name': 'passage',
'verbose_name_plural': 'passages',
},
),
migrations.AddField(
model_name='synthesis',
name='passage',
field=models.ForeignKey(default=None, on_delete=django.db.models.deletion.CASCADE, related_name='syntheses', to='participation.Passage', verbose_name='passage'),
preserve_default=False,
),
migrations.AlterUniqueTogether(
name='synthesis',
unique_together={('participation', 'passage', 'type')},
),
migrations.RemoveField(
model_name='synthesis',
name='pool',
),
]

View File

@ -5,6 +5,7 @@ import os
from address.models import AddressField
from django.conf import settings
from django.core.exceptions import ValidationError
from django.core.validators import RegexValidator
from django.db import models
from django.db.models import Index
@ -324,6 +325,59 @@ class Pool(models.Model):
verbose_name_plural = _("pools")
class Passage(models.Model):
pool = models.ForeignKey(
Pool,
on_delete=models.CASCADE,
verbose_name=_("pool"),
related_name="passages",
)
place = models.CharField(
verbose_name=_("place"),
max_length=255,
help_text=_("Where the solution is presented?"),
default="Non indiqué",
)
defender = models.ForeignKey(
Participation,
on_delete=models.PROTECT,
verbose_name=_("defender"),
related_name="+",
)
opponent = models.ForeignKey(
Participation,
on_delete=models.PROTECT,
verbose_name=_("opponent"),
related_name="+",
)
reporter = models.ForeignKey(
Participation,
on_delete=models.PROTECT,
verbose_name=_("reporter"),
related_name="+",
)
def clean(self):
if self.defender not in self.pool.participations.all():
raise ValidationError(_("Team {trigram} is not registered in the pool.")
.format(trigram=self.defender.team.trigram))
if self.opponent not in self.pool.participations.all():
raise ValidationError(_("Team {trigram} is not registered in the pool.")
.format(trigram=self.opponent.team.trigram))
if self.reporter not in self.pool.participations.all():
raise ValidationError(_("Team {trigram} is not registered in the pool.")
.format(trigram=self.reporter.team.trigram))
return super().clean()
class Meta:
verbose_name = _("passage")
verbose_name_plural = _("passages")
def get_solution_filename(instance, filename):
return f"solutions/{instance.participation.team.trigram}_{instance.problem}" \
+ ("final" if instance.final_solution else "")
@ -378,11 +432,11 @@ class Synthesis(models.Model):
verbose_name=_("participation"),
)
pool = models.ForeignKey(
Pool,
passage = models.ForeignKey(
Passage,
on_delete=models.CASCADE,
related_name="syntheses",
verbose_name=_("pool"),
verbose_name=_("passage"),
)
type = models.PositiveSmallIntegerField(
@ -400,10 +454,7 @@ class Synthesis(models.Model):
default="",
)
def __str__(self):
return repr(self)
class Meta:
verbose_name = _("synthesis")
verbose_name_plural = _("syntheses")
unique_together = (('participation', 'pool', 'type', ), )
unique_together = (('participation', 'passage', 'type', ), )