From: Stephen Finucane Date: Thu, 12 Apr 2018 12:01:31 +0000 (+0100) Subject: models: Add a backreference for a user's bundles X-Git-Tag: v2.1.0-rc1~20 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=26f22a87cdd80018f1cbd2de3bfb822f9018ccba;p=thirdparty%2Fpatchwork.git models: Add a backreference for a user's bundles This is more intuitive. Signed-off-by: Stephen Finucane --- 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 f91b994c..54530212 100644 --- a/patchwork/models.py +++ b/patchwork/models.py @@ -778,7 +778,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..5aa63fba 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.all() 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..eccb5441 100644 --- a/patchwork/views/patch.py +++ b/patchwork/views/patch.py @@ -44,7 +44,7 @@ def patch_list(request, project_id): view_args={'project_id': project.linkname}) if is_authenticated(request.user): - context['bundles'] = Bundle.objects.filter(owner=request.user) + context['bundles'] = request.user.bundles.all() return render(request, 'patchwork/list.html', context) @@ -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.all() context['submission'] = patch context['patchform'] = form diff --git a/patchwork/views/user.py b/patchwork/views/user.py index 2a2d7046..289a1871 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.all(), 'profileform': form, }