From patchwork Sun Oct 9 22:38:56 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stephen Finucane X-Patchwork-Id: 680106 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.ozlabs.org (lists.ozlabs.org [103.22.144.68]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3ssdTq20qgz9ryv for ; Mon, 10 Oct 2016 09:39:11 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=fail reason="key not found in DNS" (0-bit key; unprotected) header.d=that.guru header.i=@that.guru header.b=pJJm2GMx; dkim-atps=neutral Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) by lists.ozlabs.org (Postfix) with ESMTP id 3ssdTq0wGvzDsTW for ; Mon, 10 Oct 2016 09:39:11 +1100 (AEDT) Authentication-Results: lists.ozlabs.org; dkim=fail reason="key not found in DNS" (0-bit key; unprotected) header.d=that.guru header.i=@that.guru header.b=pJJm2GMx; dkim-atps=neutral X-Original-To: patchwork@lists.ozlabs.org Delivered-To: patchwork@lists.ozlabs.org Received: from brown.birch.relay.mailchannels.net (brown.birch.relay.mailchannels.net [23.83.209.23]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 3ssdTl1v5MzDrPL for ; Mon, 10 Oct 2016 09:39:06 +1100 (AEDT) Authentication-Results: lists.ozlabs.org; dkim=fail reason="key not found in DNS" (0-bit key; unprotected) header.d=that.guru header.i=@that.guru header.b=pJJm2GMx; dkim-atps=neutral X-Sender-Id: mxroute|x-authuser|stephen@that.guru Received: from relay.mailchannels.net (localhost [127.0.0.1]) by relay.mailchannels.net (Postfix) with ESMTP id B3081A0AC8 for ; Sun, 9 Oct 2016 22:39:03 +0000 (UTC) Received: from one.mxroute.com (ip-10-107-69-155.us-west-2.compute.internal [10.107.69.155]) by relay.mailchannels.net (Postfix) with ESMTPA id 7BA8FA0AD0 for ; Sun, 9 Oct 2016 22:39:02 +0000 (UTC) X-Sender-Id: mxroute|x-authuser|stephen@that.guru Received: from one.mxroute.com ([UNAVAILABLE]. [10.102.194.57]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384) by 0.0.0.0:2500 (trex/5.7.8); Sun, 09 Oct 2016 22:39:03 +0000 X-MC-Relay: Neutral X-MailChannels-SenderId: mxroute|x-authuser|stephen@that.guru X-MailChannels-Auth-Id: mxroute X-MC-Loop-Signature: 1476052742723:2588755951 X-MC-Ingress-Time: 1476052742723 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=that.guru; s=default; h=Message-Id:Date:Subject:To:From:Sender:Reply-To:Cc:MIME-Version :Content-Type:Content-Transfer-Encoding:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: In-Reply-To:References:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=bxoC5n2qduuOHf+cH4BkwFX8g3yA4Ynh7IILBkUvkrw=; b=pJJm2GMx570XSzNjZYlUZwnzwF gWOvU7wCog2+NBF/L5HENULG18plKXNtjH+eUnQ6sXMwmDFEBIMPtgv8IQfBprg+KRR7BoBsAXSgi xqWRNHXqDmlww/BcOSK7Ke3Bp0h6FVOM0E1KnpwyuIm8VqV45ALnKY1E+WjpFiWlXHfAR+TDJ540e PeeBmDOSFBfiMZA7nZAPadvZCqrN2NXoCja2i4Oji9tx+Jck35aov1sGejLXq9T3vcPfy1I438VIh sWWSrsb3/nexmp/CXO3dUsLujuX8kd5H+q1BZaXfSxaR4Eo1GsDl5q1AbOvO72WQiRygJq7SZ45RD YpqfTPFw==; From: Stephen Finucane To: patchwork@lists.ozlabs.org Subject: [PATCH] models: Add a backreference for a user's bundles Date: Sun, 9 Oct 2016 23:38:56 +0100 Message-Id: <1476052736-28261-1-git-send-email-stephen@that.guru> X-Mailer: git-send-email 2.7.4 X-AuthUser: stephen@that.guru X-BeenThere: patchwork@lists.ozlabs.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Patchwork development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: patchwork-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org Sender: "Patchwork" This is more intuitive. Signed-off-by: Stephen Finucane --- .../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 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, }