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

Stephen Finucane stephenfinucane at hotmail.com
Tue Sep 13 07:53:27 AEST 2016


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

Signed-off-by: Stephen Finucane <stephenfinucane at hotmail.com>
---
 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 5043c9a..3ab90e1 100644
--- a/patchwork/models.py
+++ b/patchwork/models.py
@@ -48,7 +48,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):
@@ -134,6 +134,7 @@ 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 = list(filter(
@@ -141,17 +142,19 @@ class UserProfile(models.Model):
             return ' '.join(names)
         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:
@@ -165,7 +168,7 @@ class UserProfile(models.Model):
         return qs
 
     def __str__(self):
-        return self.name()
+        return self.name
 
 
 def _user_saved_callback(sender, created, instance, **kwargs):
@@ -278,6 +281,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 ''
@@ -437,6 +441,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 15695d6..008c629 100644
--- a/patchwork/views/__init__.py
+++ b/patchwork/views/__init__.py
@@ -366,10 +366,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 3346568..b48cf14 100644
--- a/patchwork/views/patch.py
+++ b/patchwork/views/patch.py
@@ -113,7 +113,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
 
 
@@ -126,7 +126,7 @@ 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 dfbfde8..0eba67b 100644
--- a/patchwork/views/user.py
+++ b/patchwork/views/user.py
@@ -87,7 +87,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 edb7555..ca85a0e 100644
--- a/patchwork/views/xmlrpc.py
+++ b/patchwork/views/xmlrpc.py
@@ -268,7 +268,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