Skip to content

Commit

Permalink
Downloads: Enforce constraint that only one ReleaseFile per OS per Re…
Browse files Browse the repository at this point in the history
…lease has "Download button" enabled. (#2137)

* add fixtures for downloads/releases

* enforce constraint that only one release file per os has download_button value

This is by far the most common breakage during CPython releases... stop it from being possible

* Raise a ValidationError if attempting to breach constraint on release/os/download_button
  • Loading branch information
ewdurbin committed Sep 9, 2022
1 parent a886cf8 commit d6cb282
Show file tree
Hide file tree
Showing 3 changed files with 63,066 additions and 0 deletions.
17 changes: 17 additions & 0 deletions downloads/migrations/0008_auto_20220907_2102.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 2.2.24 on 2022-09-07 21:02

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('downloads', '0007_auto_20220809_1655'),
]

operations = [
migrations.AddConstraint(
model_name='releasefile',
constraint=models.UniqueConstraint(condition=models.Q(download_button=True), fields=('os', 'release'), name='only_one_download_per_os_per_release'),
),
]
14 changes: 14 additions & 0 deletions downloads/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from django.urls import reverse
from django.conf import settings
from django.core.exceptions import ValidationError
from django.db import models
from django.db.models.signals import post_save
from django.dispatch import receiver
Expand Down Expand Up @@ -332,7 +333,20 @@ class ReleaseFile(ContentManageable, NameSlugModel):
filesize = models.IntegerField(default=0)
download_button = models.BooleanField(default=False, help_text="Use for the supernav download button for this OS")

def validate_unique(self, exclude=None):
if self.download_button:
qs = ReleaseFile.objects.filter(release=self.release, os=self.os, download_button=True).exclude(pk=self.id)
if qs.count() > 0:
raise ValidationError("Only one Release File per OS can have \"Download button\" enabled")
super(ReleaseFile, self).validate_unique(exclude=exclude)

class Meta:
verbose_name = 'Release File'
verbose_name_plural = 'Release Files'
ordering = ('-release__is_published', 'release__name', 'os__name', 'name')

constraints = [
models.UniqueConstraint(fields=['os', 'release'],
condition=models.Q(download_button=True),
name="only_one_download_per_os_per_release"),
]

0 comments on commit d6cb282

Please sign in to comment.