|
15 | 15 | from django.core.validators import validate_unicode_slug |
16 | 16 | from django.core.validators import MaxValueValidator |
17 | 17 | from django.db import models |
| 18 | +from django.db.models.constraints import UniqueConstraint |
18 | 19 | from django.urls import reverse |
19 | 20 | from django.utils.functional import cached_property |
20 | 21 | from django.utils import timezone as tz_utils |
| 22 | +from django.db.models.functions import Lower |
| 23 | +from django.db.models import OuterRef, Exists, Q |
21 | 24 |
|
22 | | -from patchwork.fields import HashField |
| 25 | +from patchwork.fields import HashField, ColorField |
23 | 26 | from patchwork.hasher import hash_diff |
24 | 27 |
|
25 | 28 | if settings.ENABLE_REST_API: |
@@ -261,6 +264,61 @@ class Meta: |
261 | 264 | ordering = ['ordering'] |
262 | 265 |
|
263 | 266 |
|
| 267 | +class Label(models.Model): |
| 268 | + """Labels for patches. |
| 269 | +
|
| 270 | + Labels are arbitrary bits of metadata attached to a patch. They can |
| 271 | + be used to signify priority, category, or other similar information. |
| 272 | + They can also be used to filter patches and identify the ones most |
| 273 | + interesting to a given user. |
| 274 | + """ |
| 275 | + |
| 276 | + project = models.ForeignKey( |
| 277 | + Project, |
| 278 | + related_name='labels', |
| 279 | + related_query_name='label', |
| 280 | + null=True, |
| 281 | + blank=True, |
| 282 | + on_delete=models.CASCADE, |
| 283 | + help_text='The project this label is associated with. If unset, this ' |
| 284 | + 'label is global to the instance', |
| 285 | + ) |
| 286 | + |
| 287 | + name = models.CharField(max_length=25, help_text='The label value.') |
| 288 | + description = models.TextField( |
| 289 | + null=True, |
| 290 | + blank=True, |
| 291 | + max_length=1000, |
| 292 | + help_text='A description of what the label is and when it should be ' |
| 293 | + 'applied.', |
| 294 | + ) |
| 295 | + color = ColorField(help_text='The color code to use in the UI.') |
| 296 | + |
| 297 | + def __str__(self): |
| 298 | + return self.name |
| 299 | + |
| 300 | + @staticmethod |
| 301 | + def get_project_labels(project): |
| 302 | + project_label_names = Label.objects.filter( |
| 303 | + project=project |
| 304 | + ).values_list('name') |
| 305 | + return Label.objects.filter( |
| 306 | + models.Q(project=project) |
| 307 | + | ( |
| 308 | + models.Q(project__isnull=True) |
| 309 | + & ~models.Q(name__in=project_label_names) |
| 310 | + ) |
| 311 | + ) |
| 312 | + |
| 313 | + class Meta: |
| 314 | + ordering = ['name'] |
| 315 | + constraints = [ |
| 316 | + UniqueConstraint( |
| 317 | + 'project', Lower('name'), name='uniq_labels_project_id_name' |
| 318 | + ) |
| 319 | + ] |
| 320 | + |
| 321 | + |
264 | 322 | class Tag(models.Model): |
265 | 323 | name = models.CharField(max_length=20) |
266 | 324 | pattern = models.CharField( |
@@ -466,7 +524,18 @@ class Meta: |
466 | 524 | abstract = True |
467 | 525 |
|
468 | 526 |
|
| 527 | +class CoverLabel(models.Model): |
| 528 | + cover = models.ForeignKey('Cover', on_delete=models.CASCADE) |
| 529 | + label = models.ForeignKey('Label', on_delete=models.CASCADE) |
| 530 | + pk = models.CompositePrimaryKey("cover", "label") |
| 531 | + |
| 532 | + class Meta: |
| 533 | + unique_together = [('cover', 'label')] |
| 534 | + |
| 535 | + |
469 | 536 | class Cover(SubmissionMixin): |
| 537 | + labels = models.ManyToManyField(Label, through=CoverLabel) |
| 538 | + |
470 | 539 | def get_absolute_url(self): |
471 | 540 | return reverse( |
472 | 541 | 'cover-detail', |
@@ -500,11 +569,21 @@ class Meta: |
500 | 569 | ] |
501 | 570 |
|
502 | 571 |
|
| 572 | +class PatchLabel(models.Model): |
| 573 | + patch = models.ForeignKey('Patch', on_delete=models.CASCADE) |
| 574 | + label = models.ForeignKey('Label', on_delete=models.CASCADE) |
| 575 | + pk = models.CompositePrimaryKey("patch", "label") |
| 576 | + |
| 577 | + class Meta: |
| 578 | + unique_together = [('patch', 'label')] |
| 579 | + |
| 580 | + |
503 | 581 | class Patch(SubmissionMixin): |
504 | 582 | diff = models.TextField(null=True, blank=True) |
505 | 583 | commit_ref = models.CharField(max_length=255, null=True, blank=True) |
506 | 584 | pull_url = models.CharField(max_length=255, null=True, blank=True) |
507 | 585 | tags = models.ManyToManyField(Tag, through=PatchTag) |
| 586 | + labels = models.ManyToManyField(Label, through=PatchLabel) |
508 | 587 |
|
509 | 588 | # patchwork metadata |
510 | 589 |
|
|
0 commit comments