Skip to content

Commit

Permalink
models: Add relationship fields to Series model
Browse files Browse the repository at this point in the history
Series can now be linked based on their dependency o precedence
Closes getpatchwork#506

Signed-off-by: andrepapoti <[email protected]>
  • Loading branch information
andrepapoti committed Dec 3, 2024
1 parent 8c7aed3 commit c678787
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Generated by Django 5.1.2 on 2024-11-21 16:12

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
('patchwork', '0047_add_database_indexes'),
]

operations = [
migrations.AddField(
model_name='series',
name='previous_series',
field=models.ManyToManyField(
related_name='subsequent_series', to='patchwork.series'
),
),
migrations.AddField(
model_name='series',
name='required_series',
field=models.ManyToManyField(
related_name='required_by_series', to='patchwork.series'
),
),
]
66 changes: 66 additions & 0 deletions patchwork/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -848,6 +848,14 @@ class Series(FilenameMixin, models.Model):
help_text='An optional name to associate with '
'the series, e.g. "John\'s PCI series".',
)
previous_series = models.ManyToManyField(
'self', symmetrical=False, related_name='subsequent_series'
)

required_series = models.ManyToManyField(
'self', symmetrical=False, related_name='required_by_series'
)

date = models.DateTimeField()
submitter = models.ForeignKey(Person, on_delete=models.CASCADE)
version = models.IntegerField(
Expand Down Expand Up @@ -934,6 +942,64 @@ def add_patch(self, patch, number):

return patch

def add_previous_series(self, series):
"""Add a series to previous_series, automatically adding self to its subsequent_series."""
if self.project_id != series.project_id:
raise ValueError(
'Previous series must belong to the same project.'
)
if self == series:
raise ValueError('A series cannot be linked to itself.')
self.previous_series.add(series)
series.subsequent_series.add(self)

def add_subsequent_series(self, series):
"""Add a series to subsequent_series, automatically adding self to its previous_series."""
if self.project_id != series.project_id:
raise ValueError(
'Subsequent series must belong to the same project.'
)
if self == series:
raise ValueError('A series cannot be linked to itself.')
self.subsequent_series.add(series)
series.previous_series.add(self)

def add_required_series(self, series):
"""Add a series to required_series, automatically adding self to its required_by_series."""
if self.project_id != series.project_id:
raise ValueError(
'Required series must belong to the same project.'
)
if self == series:
raise ValueError('A series cannot be linked to itself.')
self.required_series.add(series)
series.required_by_series.add(self)

def add_required_by_series(self, series):
"""Add a series to required_by_series, automatically adding self to its required_series."""
if self.project_id != series.project_id:
raise ValueError(
'Required by series must belong to the same project.'
)
if self == series:
raise ValueError('A series cannot be linked to itself.')
self.required_by_series.add(series)
series.required_series.add(self)

def is_editable(self, user):
if not user.is_authenticated:
return False

if user.is_superuser:
return True

try:
person = Person.objects.get(user=user)
except Exception:
return False

return person == self.submitter

def get_absolute_url(self):
# TODO(stephenfin): We really need a proper series view
return reverse(
Expand Down

0 comments on commit c678787

Please sign in to comment.