[PATCH v4 1/7] models: Convert functions to properties

Stephen Finucane stephen at that.guru
Sun Oct 9 06:09:02 AEDT 2016


A number of models contain functions that are, semantically speaking,
actually properties. Mark them as such.

Signed-off-by: Stephen Finucane <stephen at that.guru>
Reviewed-by: Andy Doan <andy.doan at linaro.org>
---
 patchwork/models.py         | 15 ++++++++++-----
 patchwork/views/__init__.py |  4 ++--
 patchwork/views/patch.py    |  4 ++--
 patchwork/views/user.py     |  2 +-
 patchwork/views/xmlrpc.py   |  2 +-
 5 files changed, 16 insertions(+), 11 deletions(-)

diff --git a/patchwork/models.py b/patchwork/models.py
index 217de51..28e9861 100644
--- a/patchwork/models.py
+++ b/patchwork/models.py
@@ -47,7 +47,7 @@ class Person(models.Model):
                              on_delete=models.SET_NULL)
 
     def link_to_user(self, user):
-        self.name = user.profile.name()
+        self.name = user.profile.name
         self.user = user
 
     def __str__(self):
@@ -133,23 +133,26 @@ class UserProfile(models.Model):
         default=100, null=False, blank=False,
         help_text='Number of items to display per page')
 
+    @property
     def name(self):
         if self.user.first_name or self.user.last_name:
             names = [self.user.first_name, self.user.last_name]
             return ' '.join([x for x in names if x])
         return self.user.username
 
+    @property
     def contributor_projects(self):
         submitters = Person.objects.filter(user=self.user)
         return Project.objects.filter(id__in=Submission.objects.filter(
             submitter__in=submitters).values('project_id').query)
 
-    def sync_person(self):
-        pass
-
+    @property
     def n_todo_patches(self):
         return self.todo_patches().count()
 
+    def sync_person(self):
+        pass
+
     def todo_patches(self, project=None):
         # filter on project, if necessary
         if project:
@@ -163,7 +166,7 @@ class UserProfile(models.Model):
         return qs
 
     def __str__(self):
-        return self.name()
+        return self.name
 
 
 def _user_saved_callback(sender, created, instance, **kwargs):
@@ -276,6 +279,7 @@ class EmailMixin(models.Model):
         r'^(Tested|Reviewed|Acked|Signed-off|Nacked|Reported)-by: .*$',
         re.M | re.I)
 
+    @property
     def patch_responses(self):
         if not self.content:
             return ''
@@ -435,6 +439,7 @@ class Patch(Submission):
 
         return self.project.is_editable(user)
 
+    @property
     def filename(self):
         fname_re = re.compile(r'[^-_A-Za-z0-9\.]+')
         str = fname_re.sub('-', self.name)
diff --git a/patchwork/views/__init__.py b/patchwork/views/__init__.py
index df30f51..8b5e881 100644
--- a/patchwork/views/__init__.py
+++ b/patchwork/views/__init__.py
@@ -368,10 +368,10 @@ def patch_to_mbox(patch):
         postscript = ''
 
     # TODO(stephenfin): Make this use the tags infrastructure
-    body += patch.patch_responses()
+    body += patch.patch_responses
 
     for comment in Comment.objects.filter(submission=patch):
-        body += comment.patch_responses()
+        body += comment.patch_responses
 
     if postscript:
         body += '---\n' + postscript + '\n'
diff --git a/patchwork/views/patch.py b/patchwork/views/patch.py
index 244abe2..caabdd7 100644
--- a/patchwork/views/patch.py
+++ b/patchwork/views/patch.py
@@ -120,7 +120,7 @@ def content(request, patch_id):
     response = HttpResponse(content_type="text/x-patch")
     response.write(patch.diff)
     response['Content-Disposition'] = 'attachment; filename=' + \
-        patch.filename().replace(';', '').replace('\n', '')
+        patch.filename.replace(';', '').replace('\n', '')
     return response
 
 
@@ -133,5 +133,5 @@ def mbox(request, patch_id):
     else:
         response.write(patch_to_mbox(patch).as_string(True))
     response['Content-Disposition'] = 'attachment; filename=' + \
-        patch.filename().replace(';', '').replace('\n', '')
+        patch.filename.replace(';', '').replace('\n', '')
     return response
diff --git a/patchwork/views/user.py b/patchwork/views/user.py
index 691a4f9..74dbbe7 100644
--- a/patchwork/views/user.py
+++ b/patchwork/views/user.py
@@ -92,7 +92,7 @@ def register_confirm(request, conf):
         person = Person.objects.get(email__iexact=conf.user.email)
     except Person.DoesNotExist:
         person = Person(email=conf.user.email,
-                        name=conf.user.profile.name())
+                        name=conf.user.profile.name)
     person.user = conf.user
     person.save()
 
diff --git a/patchwork/views/xmlrpc.py b/patchwork/views/xmlrpc.py
index cfb80d3..538c945 100644
--- a/patchwork/views/xmlrpc.py
+++ b/patchwork/views/xmlrpc.py
@@ -273,7 +273,7 @@ def patch_to_dict(obj):
     return {
         'id': obj.id,
         'date': six.text_type(obj.date).encode('utf-8'),
-        'filename': obj.filename(),
+        'filename': obj.filename,
         'msgid': obj.msgid,
         'name': obj.name,
         'project': six.text_type(obj.project).encode('utf-8'),
-- 
2.7.4



More information about the Patchwork mailing list