[PATCH] models: Add a backreference for a user's bundles

Stephen Finucane stephen at that.guru
Thu Apr 12 22:01:31 AEST 2018


This is more intuitive.

Signed-off-by: Stephen Finucane <stephen at that.guru>
---
 .../migrations/0026_add_user_bundles_backref.py    | 22 ++++++++++++++++++++++
 patchwork/models.py                                |  4 +++-
 patchwork/views/bundle.py                          |  4 ++--
 patchwork/views/patch.py                           |  2 +-
 patchwork/views/user.py                            |  3 +--
 5 files changed, 29 insertions(+), 6 deletions(-)
 create mode 100644 patchwork/migrations/0026_add_user_bundles_backref.py

diff --git a/patchwork/migrations/0026_add_user_bundles_backref.py b/patchwork/migrations/0026_add_user_bundles_backref.py
new file mode 100644
index 00000000..e3dbf805
--- /dev/null
+++ b/patchwork/migrations/0026_add_user_bundles_backref.py
@@ -0,0 +1,22 @@
+# -*- coding: utf-8 -*-
+# Generated by Django 1.11.10 on 2018-04-12 11:59
+from __future__ import unicode_literals
+
+from django.conf import settings
+from django.db import migrations, models
+import django.db.models.deletion
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('patchwork', '0025_add_regex_validators'),
+    ]
+
+    operations = [
+        migrations.AlterField(
+            model_name='bundle',
+            name='owner',
+            field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='bundles', related_query_name='bundle', to=settings.AUTH_USER_MODEL),
+        ),
+    ]
diff --git a/patchwork/models.py b/patchwork/models.py
index 67c2d3a2..38f0f753 100644
--- a/patchwork/models.py
+++ b/patchwork/models.py
@@ -781,7 +781,9 @@ class SeriesReference(models.Model):
 
 
 class Bundle(models.Model):
-    owner = models.ForeignKey(User, on_delete=models.CASCADE)
+    owner = models.ForeignKey(User, on_delete=models.CASCADE,
+                              related_name='bundles',
+                              related_query_name='bundle')
     project = models.ForeignKey(Project, on_delete=models.CASCADE)
     name = models.CharField(max_length=50, null=False, blank=False)
     patches = models.ManyToManyField(Patch, through='BundlePatch')
diff --git a/patchwork/views/bundle.py b/patchwork/views/bundle.py
index 2d18571d..d795608f 100644
--- a/patchwork/views/bundle.py
+++ b/patchwork/views/bundle.py
@@ -71,10 +71,10 @@ def bundle_list(request, project_id=None):
                 bundle.delete()
 
     if project_id is None:
-        bundles = Bundle.objects.filter(owner=request.user)
+        bundles = request.user.bundles
     else:
         project = get_object_or_404(Project, linkname=project_id)
-        bundles = Bundle.objects.filter(owner=request.user, project=project)
+        bundles = request.user.bundles.filter(project=project)
 
     for bundle in bundles:
         bundle.delete_form = DeleteBundleForm(auto_id=False,
diff --git a/patchwork/views/patch.py b/patchwork/views/patch.py
index 7e962e72..6ff3ef27 100644
--- a/patchwork/views/patch.py
+++ b/patchwork/views/patch.py
@@ -112,7 +112,7 @@ def patch_detail(request, patch_id):
                 messages.success(request, 'Patch updated')
 
     if is_authenticated(request.user):
-        context['bundles'] = Bundle.objects.filter(owner=request.user)
+        context['bundles'] = request.user.bundles
 
     context['submission'] = patch
     context['patchform'] = form
diff --git a/patchwork/views/user.py b/patchwork/views/user.py
index 2a2d7046..d6ab4253 100644
--- a/patchwork/views/user.py
+++ b/patchwork/views/user.py
@@ -111,9 +111,8 @@ def profile(request):
     else:
         form = UserProfileForm(instance=request.user.profile)
 
-    # TODO(stephenfin): Add a related_name for User->Bundle
     context = {
-        'bundles': Bundle.objects.filter(owner=request.user),
+        'bundles': request.user.bundles,
         'profileform': form,
     }
 
-- 
2.14.3



More information about the Patchwork mailing list