[RFC 1/3] models: Convert functions to properties

Andy Doan andy.doan at linaro.org
Sat Apr 9 05:50:06 AEST 2016


On 04/01/2016 11:14 AM, Stephen Finucane wrote:
> A number of models contain functions that are, semantically speaking,
> actually properties. Mark them as such.
>
> This also includes a fix for a missing python_2_unicode_compatible
> decorator

I'm not sure I see where @python_2_unicode_compatible was added? 
Otherwise it looks fine.

> Signed-off-by: Stephen Finucane <stephen.finucane at intel.com>
> ---
>   patchwork/models.py         | 15 ++++++++++-----
>   patchwork/views/__init__.py |  2 +-
>   patchwork/views/patch.py    |  4 ++--
>   patchwork/views/user.py     |  2 +-
>   patchwork/views/xmlrpc.py   |  2 +-
>   5 files changed, 15 insertions(+), 10 deletions(-)
>
> diff --git a/patchwork/models.py b/patchwork/models.py
> index 4d454c7..9ad2bcb 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 ''
> @@ -379,6 +383,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 ddddf63..c1fb8ff 100644
> --- a/patchwork/views/__init__.py
> +++ b/patchwork/views/__init__.py
> @@ -366,7 +366,7 @@ def patch_to_mbox(patch):
>       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 1f48959..b65ae18 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'),
>



More information about the Patchwork mailing list