|
19 | 19 | from django.utils.functional import cached_property |
20 | 20 | from django.utils import timezone as tz_utils |
21 | 21 |
|
22 | | -from patchwork.fields import HashField |
| 22 | +from patchwork.fields import HashField, ColorField |
23 | 23 | from patchwork.hasher import hash_diff |
24 | 24 |
|
25 | 25 | if settings.ENABLE_REST_API: |
@@ -261,6 +261,56 @@ class Meta: |
261 | 261 | ordering = ['ordering'] |
262 | 262 |
|
263 | 263 |
|
| 264 | +class Label(models.Model): |
| 265 | + """Labels for patches. |
| 266 | +
|
| 267 | + Labels are arbitrary bits of metadata attached to a patch. They can |
| 268 | + be used to signify priority, category, or other similar information. |
| 269 | + They can also be used to filter patches and identify the ones most |
| 270 | + interesting to a given user. |
| 271 | + """ |
| 272 | + |
| 273 | + project = models.ForeignKey( |
| 274 | + Project, |
| 275 | + related_name='labels', |
| 276 | + related_query_name='label', |
| 277 | + null=True, |
| 278 | + blank=True, |
| 279 | + on_delete=models.CASCADE, |
| 280 | + help_text='The project this label is associated with. If unset, this ' |
| 281 | + 'label is global to the instance', |
| 282 | + ) |
| 283 | + |
| 284 | + name = models.CharField(max_length=25, help_text='The label value.') |
| 285 | + description = models.TextField( |
| 286 | + null=True, |
| 287 | + blank=True, |
| 288 | + help_text='A description of what the label is and when it should be ' |
| 289 | + 'applied.', |
| 290 | + ) |
| 291 | + color = ColorField(help_text='The color code to use in the UI.') |
| 292 | + |
| 293 | + def __str__(self): |
| 294 | + return self.name |
| 295 | + |
| 296 | + @staticmethod |
| 297 | + def get_project_labels(project): |
| 298 | + result = {} |
| 299 | + project_labels = Label.objects.filter( |
| 300 | + models.Q(project=project) | models.Q(project__isnull=True) |
| 301 | + ).all() |
| 302 | + |
| 303 | + for label in project_labels: |
| 304 | + if label.name not in result or label.project is not None: |
| 305 | + result[label.name] = label |
| 306 | + |
| 307 | + return list(result.values()) |
| 308 | + |
| 309 | + class Meta: |
| 310 | + ordering = ['name'] |
| 311 | + unique_together = [('project', 'name')] |
| 312 | + |
| 313 | + |
264 | 314 | class Tag(models.Model): |
265 | 315 | name = models.CharField(max_length=20) |
266 | 316 | pattern = models.CharField( |
@@ -441,6 +491,7 @@ class SubmissionMixin(FilenameMixin, EmailMixin, models.Model): |
441 | 491 | # submission metadata |
442 | 492 |
|
443 | 493 | name = models.CharField(max_length=255) |
| 494 | + labels = models.ManyToManyField(Label) |
444 | 495 |
|
445 | 496 | @cached_property |
446 | 497 | def list_archive_url(self): |
|
0 commit comments