Patchwork [2/2] New Project method to check whether the given user has edit rights on it.

login
register
mail settings
Submitter Guilherme Salgado
Date April 12, 2011, 9:35 p.m.
Message ID <20110412213502.21112.87934.stgit@localhost6.localdomain6>
Download mbox | patch
Permalink /patch/90875/
State Accepted
Headers show

Comments

Guilherme Salgado - April 12, 2011, 9:35 p.m.
This is to replace the couple places where we duplicate this same check.
---
 apps/patchwork/models.py         |    7 ++++++-
 apps/patchwork/views/__init__.py |    3 +--
 2 files changed, 7 insertions(+), 3 deletions(-)
Jeremy Kerr - April 14, 2011, 7:13 a.m.
Hi Guilherme,

> This is to replace the couple places where we duplicate this same check.

Thanks, applied with one change:

> --- a/apps/patchwork/views/__init__.py
> +++ b/apps/patchwork/views/__init__.py
> @@ -44,8 +44,7 @@ def generic_list(request, project, view,
>          data = request.POST
>      user = request.user
>      properties_form = None
> -    if (user.is_authenticated()
> -        and project in user.get_profile().maintainer_projects.all()):
> +    if user.is_authenticated() and project.is_editable(user):
>          properties_form = MultiplePatchForm(project, data = data)
>  
>      if request.method == 'POST' and data.get('form') == 'patchlistform':

project.is_editable already checks for user.is_authenticated(), so I
dropped that check from here.

Cheers,


Jeremy

Patch

diff --git a/apps/patchwork/models.py b/apps/patchwork/models.py
index e4df2c5..7cfcd92 100644
--- a/apps/patchwork/models.py
+++ b/apps/patchwork/models.py
@@ -67,6 +67,11 @@  class Project(models.Model):
     def __unicode__(self):
         return self.name
 
+    def is_editable(self, user):
+        if not user.is_authenticated():
+            return False
+        return self in user.get_profile().maintainer_projects.all()
+
 class UserProfile(models.Model):
     user = models.ForeignKey(User, unique = True)
     primary_project = models.ForeignKey(Project, null = True, blank = True)
@@ -223,7 +228,7 @@  class Patch(models.Model):
         if self.submitter.user == user or self.delegate == user:
             return True
 
-        return self.project in user.get_profile().maintainer_projects.all()
+        return self.project.is_editable(user)
 
     def filename(self):
         fname_re = re.compile('[^-_A-Za-z0-9\.]+')
diff --git a/apps/patchwork/views/__init__.py b/apps/patchwork/views/__init__.py
index f66a2bf..fee2a32 100644
--- a/apps/patchwork/views/__init__.py
+++ b/apps/patchwork/views/__init__.py
@@ -44,8 +44,7 @@  def generic_list(request, project, view,
         data = request.POST
     user = request.user
     properties_form = None
-    if (user.is_authenticated()
-        and project in user.get_profile().maintainer_projects.all()):
+    if user.is_authenticated() and project.is_editable(user):
         properties_form = MultiplePatchForm(project, data = data)
 
     if request.method == 'POST' and data.get('form') == 'patchlistform':