Skip to content

Commit bc3625d

Browse files
andrepapotivictor-accarini
authored andcommitted
models: Add relationship fields to Series model
Series can now be linked based on their dependency o precedence Closes getpatchwork#506 Signed-off-by: andrepapoti <[email protected]>
1 parent 16d23ea commit bc3625d

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Generated by Django 5.1.2 on 2024-11-21 16:12
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
dependencies = [
8+
('patchwork', '0047_add_database_indexes'),
9+
]
10+
11+
operations = [
12+
migrations.AddField(
13+
model_name='series',
14+
name='previous_series',
15+
field=models.ManyToManyField(
16+
related_name='subsequent_series', to='patchwork.series'
17+
),
18+
),
19+
migrations.AddField(
20+
model_name='series',
21+
name='required_series',
22+
field=models.ManyToManyField(
23+
related_name='required_by_series', to='patchwork.series'
24+
),
25+
),
26+
]

patchwork/models.py

+66
Original file line numberDiff line numberDiff line change
@@ -848,6 +848,14 @@ class Series(FilenameMixin, models.Model):
848848
help_text='An optional name to associate with '
849849
'the series, e.g. "John\'s PCI series".',
850850
)
851+
previous_series = models.ManyToManyField(
852+
'self', symmetrical=False, related_name='subsequent_series'
853+
)
854+
855+
required_series = models.ManyToManyField(
856+
'self', symmetrical=False, related_name='required_by_series'
857+
)
858+
851859
date = models.DateTimeField()
852860
submitter = models.ForeignKey(Person, on_delete=models.CASCADE)
853861
version = models.IntegerField(
@@ -934,6 +942,64 @@ def add_patch(self, patch, number):
934942

935943
return patch
936944

945+
def add_previous_series(self, series):
946+
"""Add a series to previous_series, automatically adding self to its subsequent_series."""
947+
if self.project_id != series.project_id:
948+
raise ValueError(
949+
'Previous series must belong to the same project.'
950+
)
951+
if self == series:
952+
raise ValueError('A series cannot be linked to itself.')
953+
self.previous_series.add(series)
954+
series.subsequent_series.add(self)
955+
956+
def add_subsequent_series(self, series):
957+
"""Add a series to subsequent_series, automatically adding self to its previous_series."""
958+
if self.project_id != series.project_id:
959+
raise ValueError(
960+
'Subsequent series must belong to the same project.'
961+
)
962+
if self == series:
963+
raise ValueError('A series cannot be linked to itself.')
964+
self.subsequent_series.add(series)
965+
series.previous_series.add(self)
966+
967+
def add_required_series(self, series):
968+
"""Add a series to required_series, automatically adding self to its required_by_series."""
969+
if self.project_id != series.project_id:
970+
raise ValueError(
971+
'Required series must belong to the same project.'
972+
)
973+
if self == series:
974+
raise ValueError('A series cannot be linked to itself.')
975+
self.required_series.add(series)
976+
series.required_by_series.add(self)
977+
978+
def add_required_by_series(self, series):
979+
"""Add a series to required_by_series, automatically adding self to its required_series."""
980+
if self.project_id != series.project_id:
981+
raise ValueError(
982+
'Required by series must belong to the same project.'
983+
)
984+
if self == series:
985+
raise ValueError('A series cannot be linked to itself.')
986+
self.required_by_series.add(series)
987+
series.required_series.add(self)
988+
989+
def is_editable(self, user):
990+
if not user.is_authenticated:
991+
return False
992+
993+
if user.is_superuser:
994+
return True
995+
996+
try:
997+
person = Person.objects.get(user=user)
998+
except Exception:
999+
return False
1000+
1001+
return person == self.submitter
1002+
9371003
def get_absolute_url(self):
9381004
# TODO(stephenfin): We really need a proper series view
9391005
return reverse(

0 commit comments

Comments
 (0)