Skip to content

Commit

Permalink
Refactoring of UpdateProjectHeadView.handle()
Browse files Browse the repository at this point in the history
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

Message-Id: <20180523191607.34465-1-shubhamjain7495@gmail.com>
  • Loading branch information
shubhamdotjain authored and Fam Zheng committed May 25, 2018
1 parent d5f6f35 commit 1d07e25
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 26 deletions.
31 changes: 31 additions & 0 deletions api/models.py
Expand Up @@ -171,6 +171,37 @@ def recognizes(self, m):
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)
Expand Down
29 changes: 3 additions & 26 deletions api/views.py
Expand Up @@ -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):
Expand Down

0 comments on commit 1d07e25

Please sign in to comment.