[Patchew-devel] [PATCH v2] Refactoring of UpdateProjectHeadView.handle()

Shubham Jain shubhamjain7495 at gmail.com
Wed May 23 19:16:07 UTC 2018


moved and refactored the UpdateProjectHeadView.handle() into method of api.models.Project so that it can be re-used in rest conversion of update-project-head
---
 api/models.py | 31 +++++++++++++++++++++++++++++++
 api/views.py  | 29 +++--------------------------
 2 files changed, 34 insertions(+), 26 deletions(-)

diff --git a/api/models.py b/api/models.py
index d602cb7..1a51a6d 100644
--- a/api/models.py
+++ b/api/models.py
@@ -171,6 +171,37 @@ class Project(models.Model):
     def get_subprojects(self):
         return Project.objects.filter(parent_project=self)
 
+    def get_project_head(self):
+        return self.get_property("git.head")
+
+    def set_project_head(self, new_head):
+        self.set_property("git.head", new_head)
+
+    project_head = property(get_project_head,set_project_head)
+
+    def series_update(self, message_ids):
+        updated_series = []
+        for msgid in message_ids:
+            if msgid.startswith("<") and msgid.endswith(">"):
+                msgid = msgid[1:-1]
+            mo = Message.objects.filter(project=self, message_id=msgid,
+                                            is_merged=False).first()
+            if not mo:
+                continue
+            mo.is_merged = True
+            mo.save()
+            s = mo.get_series_head()
+            if s:
+                updated_series.append(s)
+        for s in updated_series:
+            for p in series.get_patches():
+                if not p.is_merged:
+                    break
+            else:
+                series.is_merged = True
+                series.save()
+        return len(updated_series)
+
 class ProjectProperty(models.Model):
     project = models.ForeignKey('Project', on_delete=models.CASCADE)
     name = models.CharField(max_length=1024, db_index=True)
diff --git a/api/views.py b/api/views.py
index c27cd5a..7a37481 100644
--- a/api/views.py
+++ b/api/views.py
@@ -120,34 +120,11 @@ class UpdateProjectHeadView(APILoginRequiredView):
 
     def handle(self, request, project, old_head, new_head, message_ids):
         po = Project.objects.get(name=project)
-        old_head_0 = po.get_property("git.head")
+        old_head_0 = po.project_head
         if old_head_0 and old_head_0 != old_head:
             raise Exception("wrong old head")
-        ret = 0
-        updated_series = []
-        for msgid in message_ids:
-            if msgid.startswith("<") and msgid.endswith(">"):
-                msgid = msgid[1:-1]
-            mo = Message.objects.filter(project=po, message_id=msgid,
-                                        is_merged=False).first()
-            if not mo:
-                continue
-            ret += 1
-            mo.is_merged = True
-            mo.save()
-            s = mo.get_series_head()
-            if s:
-                updated_series.append(s)
-        for s in updated_series:
-            merged = True
-            for p in s.get_patches():
-                if not p.is_merged:
-                    merged = False
-                    break
-            if merged:
-                s.is_merged = True
-                s.save()
-        po.set_property("git.head", new_head)
+        ret = po.series_update(message_ids)
+        po.project_head = new_head
         return ret
 
 class SetPropertyView(APILoginRequiredView):
-- 
2.14.3 (Apple Git-98)




More information about the Patchew-devel mailing list