8000 Downloads: Enforce constraint that only one ReleaseFile per OS per Re… · python/pythondotorg@d6cb282 · GitHub
[go: up one dir, main page]

Skip to content

Commit d6cb282

Browse files
authored
Downloads: Enforce constraint that only one ReleaseFile per OS per Release 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
1 parent a886cf8 commit d6cb282

File tree

3 files changed

+63066
-0
lines changed

3 files changed

+63066
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Generated by Django 2.2.24 on 2022-09-07 21:02
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
('downloads', '0007_auto_20220809_1655'),
10+
]
11+
12+
operations = [
13+
migrations.AddConstraint(
14+
model_name='releasefile',
15+
constraint=models.UniqueConstraint(condition=models.Q(download_button=True), fields=('os', 'release'), name='only_one_download_per_os_per_release'),
16+
),
17+
]

downloads/models.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

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

336+
def validate_unique(self, exclude=None):
337+
if self.download_button:
338+
qs = ReleaseFile.objects.filter(release=self.release, os=self.os, download_button=True).exclude(pk=self.id)
339+
if qs.count() > 0:
340+
raise ValidationError("Only one Release File per OS can have \"Download button\" enabled")
341+
super(ReleaseFile, self).validate_unique(exclude=exclude)
342+
335343
class Meta:
336344
verbose_name = 'Release File'
337345
verbose_name_plural = 'Release Files'
338346
ordering = ('-release__is_published', 'release__name', 'os__name', 'name')
347+
348+
constraints = [
349+
models.UniqueConstraint(fields=['os', 'release'],
350+
condition=models.Q(download_button=True),
351+
name="only_one_download_per_os_per_release"),
352+
]

0 commit comments

Comments
 (0)
0