Skip to content

Commit 9809b33

Browse files
stephenfinfstachura
authored andcommitted
models: Add submission labels
Labels are arbitrary bits of metadata attached to a submission. They can be used to signify priority, category, or other similar information. They can also be used to filter patches and identify the ones most interesting to a given user. Labels can be associated with a project to ensure that one project can use a totally different set of labels to another and to, in the future, allow a project administrator to use their own labels. However, they can also be global, which is useful for things that would be common across multiple projects such as "RFC".
1 parent afeb357 commit 9809b33

4 files changed

Lines changed: 257 additions & 1 deletion

File tree

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<django-objects version="1.0">
3+
<object model="patchwork.label" pk="1">
4+
<field name="project" rel="ManyToOneRel" to="patchwork.project"><None></None></field>
5+
<field name="name" type="CharField">RFC</field>
6+
<field name="description" type="TextField">"RFC" stands for "Request for Comment", which tells maintainers that they should review your patch thoroughly and provide feedback. RFC is typically used when sending feature patches for the first time, or anytime the patch is more than just a simple bug fix.</field>
7+
<field name="color" type="PositiveIntegerField">#EDEDED</field>
8+
</object>
9+
</django-objects>
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
# Generated by Django 6.0.7 on 2026-11-01 11:48
2+
3+
import django.db.models.deletion
4+
import django.db.models.functions.text
5+
import patchwork.fields
6+
from django.db import migrations, models
7+
8+
9+
class Migration(migrations.Migration):
10+
dependencies = [
11+
('patchwork', '0053_series_ordering_indexes'),
12+
]
13+
14+
operations = [
15+
migrations.CreateModel(
16+
name='Label',
17+
fields=[
18+
(
19+
'id',
20+
models.AutoField(
21+
auto_created=True,
22+
primary_key=True,
23+
serialize=False,
24+
verbose_name='ID',
25+
),
26+
),
27+
(
28+
'name',
29+
models.CharField(
30+
help_text='The label value.', max_length=25
31+
),
32+
),
33+
(
34+
'description',
35+
models.TextField(
36+
blank=True,
37+
help_text='A description of what the label is '
38+
'and when it should be applied.',
39+
max_length=1000,
40+
null=True,
41+
),
42+
),
43+
(
44+
'color',
45+
patchwork.fields.ColorField(
46+
help_text='The color code to use in the UI.'
47+
),
48+
),
49+
(
50+
'project',
51+
models.ForeignKey(
52+
blank=True,
53+
help_text='The project this label is associated with. '
54+
'If unset, this label is global to the instance',
55+
null=True,
56+
on_delete=django.db.models.deletion.CASCADE,
57+
related_name='labels',
58+
related_query_name='label',
59+
to='patchwork.project',
60+
),
61+
),
62+
],
63+
options={
64+
'ordering': ['name'],
65+
},
66+
),
67+
migrations.CreateModel(
68+
name='CoverLabel',
69+
fields=[
70+
(
71+
'pk',
72+
models.CompositePrimaryKey(
73+
'cover',
74+
'label',
75+
blank=True,
76+
editable=False,
77+
primary_key=True,
78+
serialize=False,
79+
),
80+
),
81+
(
82+
'cover',
83+
models.ForeignKey(
84+
on_delete=django.db.models.deletion.CASCADE,
85+
to='patchwork.cover',
86+
),
87+
),
88+
(
89+
'label',
90+
models.ForeignKey(
91+
on_delete=django.db.models.deletion.CASCADE,
92+
to='patchwork.label',
93+
),
94+
),
95+
],
96+
),
97+
migrations.AddField(
98+
model_name='cover',
99+
name='labels',
100+
field=models.ManyToManyField(
101+
through='patchwork.CoverLabel', to='patchwork.label'
102+
),
103+
),
104+
migrations.CreateModel(
105+
name='PatchLabel',
106+
fields=[
107+
(
108+
'pk',
109+
models.CompositePrimaryKey(
110+
'patch',
111+
'label',
112+
blank=True,
113+
editable=False,
114+
primary_key=True,
115+
serialize=False,
116+
),
117+
),
118+
(
119+
'label',
120+
models.ForeignKey(
121+
on_delete=django.db.models.deletion.CASCADE,
122+
to='patchwork.label',
123+
),
124+
),
125+
(
126+
'patch',
127+
models.ForeignKey(
128+
on_delete=django.db.models.deletion.CASCADE,
129+
to='patchwork.patch',
130+
),
131+
),
132+
],
133+
),
134+
migrations.AddField(
135+
model_name='patch',
136+
name='labels',
137+
field=models.ManyToManyField(
138+
through='patchwork.PatchLabel', to='patchwork.label'
139+
),
140+
),
141+
migrations.AddConstraint(
142+
model_name='label',
143+
constraint=models.UniqueConstraint(
144+
models.F('project'),
145+
django.db.models.functions.text.Lower('name'),
146+
name='uniq_labels_project_id_name',
147+
),
148+
),
149+
migrations.AlterUniqueTogether(
150+
name='coverlabel',
151+
unique_together={('cover', 'label')},
152+
),
153+
migrations.AlterUniqueTogether(
154+
name='patchlabel',
155+
unique_together={('patch', 'label')},
156+
),
157+
]

‎patchwork/models.py‎

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,14 @@
1515
from django.core.validators import validate_unicode_slug
1616
from django.core.validators import MaxValueValidator
1717
from django.db import models
18+
from django.db.models.constraints import UniqueConstraint
1819
from django.urls import reverse
1920
from django.utils.functional import cached_property
2021
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
2124

22-
from patchwork.fields import HashField
25+
from patchwork.fields import HashField, ColorField
2326
from patchwork.hasher import hash_diff
2427

2528
if settings.ENABLE_REST_API:
@@ -261,6 +264,61 @@ class Meta:
261264
ordering = ['ordering']
262265

263266

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+
264322
class Tag(models.Model):
265323
name = models.CharField(max_length=20)
266324
pattern = models.CharField(
@@ -466,7 +524,18 @@ class Meta:
466524
abstract = True
467525

468526

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+
469536
class Cover(SubmissionMixin):
537+
labels = models.ManyToManyField(Label, through=CoverLabel)
538+
470539
def get_absolute_url(self):
471540
return reverse(
472541
'cover-detail',
@@ -500,11 +569,21 @@ class Meta:
500569
]
501570

502571

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+
503581
class Patch(SubmissionMixin):
504582
diff = models.TextField(null=True, blank=True)
505583
commit_ref = models.CharField(max_length=255, null=True, blank=True)
506584
pull_url = models.CharField(max_length=255, null=True, blank=True)
507585
tags = models.ManyToManyField(Tag, through=PatchTag)
586+
labels = models.ManyToManyField(Label, through=PatchLabel)
508587

509588
# patchwork metadata
510589

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
features:
3+
- |
4+
Add support for patch labels. Labels are a way to organize and prioritize
5+
submissions. You can apply labels to patches and cover letters to signify
6+
priority, category, or any other marker you wish.
7+
8+
Labels can be either global or tied to the project they are created in.
9+
Labels can have an optional description attached, which will provide a
10+
little insight into the purpose of the label. Labels are completely
11+
customizable and the labels available will vary by instance.

0 commit comments

Comments
 (0)