[Patchew-devel] [PATCH 7/9] rest: add support for PUT of result objects

Paolo Bonzini pbonzini at redhat.com
Wed Aug 22 09:38:33 UTC 2018


On 22/08/2018 10:52, Fam Zheng wrote:
> On Wed, 08/22 10:35, Paolo Bonzini wrote:
>> On 22/08/2018 08:14, Fam Zheng wrote:
>>>> +    @property
>>>> +    def result_renderer(self):
>>>> +        try:
>>>> +            found = re.match("^[^.]*", self.kwargs['name'])
>>> Maybe create a Result.mod field so this magic is not necessary? Otherwise the
>>> convention should be documented somewhere.
>>>
>>
>> Do you mean something like Result.renderer_from_name (a staticmethod)?
> 
> I mean can we somehow match the self.kwargs['name'] string back to DB record(s),
> where we can create a new field that "duplicates" the first part of the name
> (the part before the first "." as matched in this line)?

I think you don't have the Result object yet, but we can do something 
like this to avoid the code duplication:

diff --git a/api/models.py b/api/models.py
index eea924f..32e8b41 100644
--- a/api/models.py
+++ b/api/models.py
@@ -84,10 +84,14 @@ class Result(models.Model):
         emit_event("ResultUpdate", obj=self.obj,
                    old_status=old_status, result=self)
 
+    @staticmethod
+    def renderer_from_name(name):
+        found = re.match("^[^.]*", name)
+        return mod.get_module(found.group(0)) if found else None
+
     @property
     def renderer(self):
-        found = re.match("^[^.]*", self.name)
-        return mod.get_module(found.group(0)) if found else None
+        return Result.renderer_from_name(self.name)
 
     @property
     def obj(self):
diff --git a/api/rest.py b/api/rest.py
index 22d7c98..cc881af 100644
--- a/api/rest.py
+++ b/api/rest.py
@@ -536,11 +536,9 @@ class ResultsViewSet(mixins.ListModelMixin, mixins.RetrieveModelMixin,
 
     @property
     def result_renderer(self):
-        try:
-            found = re.match("^[^.]*", self.kwargs['name'])
-        except:
-            return None
-        return mod.get_module(found.group(0)) if found else None
+        if 'name' in self.kwargs:
+            return Result.renderer_from_name(self.kwargs['name'])
+        return None
 
     def get_serializer_context(self):
         context = super(ResultsViewSet, self).get_serializer_context()




More information about the Patchew-devel mailing list