models: Add a backreference for a user's bundles

Message ID 1476052736-28261-1-git-send-email-stephen@that.guru
State Deferred
Headers show

Commit Message

Stephen Finucane Oct. 9, 2016, 10:38 p.m.
This is more intuitive.

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

Patch

diff --git a/patchwork/migrations/0014_add_user_bundles_backref.py b/patchwork/migrations/0014_add_user_bundles_backref.py
new file mode 100644
index 0000000..4acc5b2
--- /dev/null
+++ b/patchwork/migrations/0014_add_user_bundles_backref.py
@@ -0,0 +1,20 @@ 
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals
+
+from django.db import migrations, models
+from django.conf import settings
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('patchwork', '0013_slug_check_context'),
+    ]
+
+    operations = [
+        migrations.AlterField(
+            model_name='bundle',
+            name='owner',
+            field=models.ForeignKey(related_query_name=b'bundle', related_name='bundles', to=settings.AUTH_USER_MODEL),
+        ),
+    ]
diff --git a/patchwork/models.py b/patchwork/models.py
index 217de51..27d8343 100644
--- a/patchwork/models.py
+++ b/patchwork/models.py
@@ -565,7 +565,8 @@  class Comment(EmailMixin, models.Model):
 
 
 class Bundle(models.Model):
-    owner = models.ForeignKey(User)
+    owner = models.ForeignKey(User, related_name='bundles',
+                              related_query_name='bundle')
     project = models.ForeignKey(Project)
     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 e717429..1b3f0e5 100644
--- a/patchwork/views/bundle.py
+++ b/patchwork/views/bundle.py
@@ -108,10 +108,10 @@  def bundles(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 244abe2..ccaf31b 100644
--- a/patchwork/views/patch.py
+++ b/patchwork/views/patch.py
@@ -98,7 +98,7 @@  def patch(request, patch_id):
                 messages.success(request, 'Patch updated')
 
     if request.user.is_authenticated():
-        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 691a4f9..ba84112 100644
--- a/patchwork/views/user.py
+++ b/patchwork/views/user.py
@@ -109,10 +109,9 @@  def profile(request):
     else:
         form = UserProfileForm(instance=request.user.profile)
 
-    # TODO(stephenfin): Add a related_name for User->Bundle
     context = {
         'project': request.user.profile.primary_project,
-        'bundles': Bundle.objects.filter(owner=request.user),
+        'bundles': request.user.bundles,
         'profileform': form,
     }