[Patchew-devel] [PATCH] models: return None from MessageManager query methods

Paolo Bonzini posted 1 patch 14 weeks ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/patchew next tags/patchew/20190318111649.9106-1-pbonzini@redhat.com
api/models.py | 30 ++++++++++++++++++++++++------
1 file changed, 24 insertions(+), 6 deletions(-)

[Patchew-devel] [PATCH] models: return None from MessageManager query methods

Posted by Paolo Bonzini 14 weeks ago
Otherwise, we get 500 errors on series views such as
https://patchew.org/XYZ/abc@def/ or message views such as
https://patchew.org/XYZ/abc@def/ghi@jkl/.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 api/models.py | 30 ++++++++++++++++++++++++------
 1 file changed, 24 insertions(+), 6 deletions(-)

diff --git a/api/models.py b/api/models.py
index 7d1b609..41228df 100644
--- a/api/models.py
+++ b/api/models.py
@@ -329,25 +329,43 @@ class MessageManager(models.Manager):
 
     def project_messages(self, project):
         q = super(MessageManager, self).get_queryset()
-        if isinstance(project, str):
-            po = Project.objects.get(name=project)
-        elif isinstance(project, int):
-            po = Project.objects.get(id=project)
+        try:
+            if isinstance(project, str):
+                po = Project.objects.get(name=project)
+            elif isinstance(project, int):
+                po = Project.objects.get(id=project)
+        except Project.DoesNotExist:
+            return None
         q = q.filter(project=po) | q.filter(project__parent_project=po)
         return q
 
     def series_heads(self, project=None):
         if project:
             q = self.project_messages(project)
+            if not q:
+                return None
         else:
             q = super(MessageManager, self).get_queryset()
         return q.filter(is_series_head=True).prefetch_related('properties', 'project')
 
     def find_series(self, message_id, project_name=None):
-        return self.series_heads(project_name).filter(message_id=message_id).first()
+        heads = self.series_heads(project_name)
+        if not heads:
+            return None
+        try:
+            return heads.filter(message_id=message_id).first()
+        except Message.DoesNotExist:
+            return None
+
 
     def find_message(self, message_id, project_name):
-        return self.project_messages(project_name).filter(message_id=message_id).first()
+        messages = self.project_messages(project_name)
+        if not messages:
+            return None
+        try:
+            return messages.filter(message_id=message_id).first()
+        except Message.DoesNotExist:
+            return None
 
     def patches(self):
         return super(MessageManager, self).get_queryset().filter(is_patch=True)
-- 
2.20.1

_______________________________________________
Patchew-devel mailing list
Patchew-devel@redhat.com
https://www.redhat.com/mailman/listinfo/patchew-devel

Re: [Patchew-devel] [PATCH] models: return None from MessageManager query methods

Posted by Fam Zheng 14 weeks ago
On Mon, 03/18 12:16, Paolo Bonzini wrote:
> Otherwise, we get 500 errors on series views such as
> https://patchew.org/XYZ/abc@def/ or message views such as
> https://patchew.org/XYZ/abc@def/ghi@jkl/.
> 
> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
> ---
>  api/models.py | 30 ++++++++++++++++++++++++------
>  1 file changed, 24 insertions(+), 6 deletions(-)
> 
> diff --git a/api/models.py b/api/models.py
> index 7d1b609..41228df 100644
> --- a/api/models.py
> +++ b/api/models.py
> @@ -329,25 +329,43 @@ class MessageManager(models.Manager):
>  
>      def project_messages(self, project):
>          q = super(MessageManager, self).get_queryset()
> -        if isinstance(project, str):
> -            po = Project.objects.get(name=project)
> -        elif isinstance(project, int):
> -            po = Project.objects.get(id=project)
> +        try:
> +            if isinstance(project, str):
> +                po = Project.objects.get(name=project)
> +            elif isinstance(project, int):
> +                po = Project.objects.get(id=project)
> +        except Project.DoesNotExist:
> +            return None
>          q = q.filter(project=po) | q.filter(project__parent_project=po)
>          return q
>  
>      def series_heads(self, project=None):
>          if project:
>              q = self.project_messages(project)
> +            if not q:
> +                return None
>          else:
>              q = super(MessageManager, self).get_queryset()
>          return q.filter(is_series_head=True).prefetch_related('properties', 'project')
>  
>      def find_series(self, message_id, project_name=None):
> -        return self.series_heads(project_name).filter(message_id=message_id).first()
> +        heads = self.series_heads(project_name)
> +        if not heads:
> +            return None
> +        try:
> +            return heads.filter(message_id=message_id).first()
> +        except Message.DoesNotExist:
> +            return None
> +
>  
>      def find_message(self, message_id, project_name):
> -        return self.project_messages(project_name).filter(message_id=message_id).first()
> +        messages = self.project_messages(project_name)
> +        if not messages:
> +            return None
> +        try:
> +            return messages.filter(message_id=message_id).first()

For above two: does first() throw DoesNotExist at all? If not, all
except: are not necessary by using first().

Fam


> +        except Message.DoesNotExist:
> +            return None
>  
>      def patches(self):
>          return super(MessageManager, self).get_queryset().filter(is_patch=True)

_______________________________________________
Patchew-devel mailing list
Patchew-devel@redhat.com
https://www.redhat.com/mailman/listinfo/patchew-devel

Re: [Patchew-devel] [PATCH] models: return None from MessageManager query methods

Posted by Paolo Bonzini 14 weeks ago
On 19/03/19 06:28, Fam Zheng wrote:
>>      def find_series(self, message_id, project_name=None):
>> -        return self.series_heads(project_name).filter(message_id=message_id).first()
>> +        heads = self.series_heads(project_name)
>> +        if not heads:
>> +            return None
>> +        try:
>> +            return heads.filter(message_id=message_id).first()
>> +        except Message.DoesNotExist:
>> +            return None
>> +
>>  
>>      def find_message(self, message_id, project_name):
>> -        return self.project_messages(project_name).filter(message_id=message_id).first()
>> +        messages = self.project_messages(project_name)
>> +        if not messages:
>> +            return None
>> +        try:
>> +            return messages.filter(message_id=message_id).first()
> 
> For above two: does first() throw DoesNotExist at all? If not, all
> except: are not necessary by using first().

Good point.  We still need  to check for None, but no try/except is
necessary.

Paolo

_______________________________________________
Patchew-devel mailing list
Patchew-devel@redhat.com
https://www.redhat.com/mailman/listinfo/patchew-devel