Skip to content
Snippets Groups Projects
Unverified Commit 8cf7b0f1 authored by Fredrik Jonsson's avatar Fredrik Jonsson Committed by GitHub
Browse files

Merge pull request #482 from OpenTechFund/bugfix/make-rounds-less-likely-clash

Bugfix/make rounds less likely clash
parents 9a66ec75 2ecbedca
No related branches found
No related tags found
No related merge requests found
...@@ -144,13 +144,6 @@ class RoundBase(WorkflowStreamForm, SubmittableStreamForm): # type: ignore ...@@ -144,13 +144,6 @@ class RoundBase(WorkflowStreamForm, SubmittableStreamForm): # type: ignore
ObjectList(SubmittableStreamForm.promote_panels, heading='Promote'), ObjectList(SubmittableStreamForm.promote_panels, heading='Promote'),
]) ])
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# We attached the parent page as part of the before_create_hook
if hasattr(self, 'parent_page'):
self.workflow_name = self.parent_page.workflow_name
self.reviewers = self.parent_page.reviewers.all()
def get_template(self, request, *args, **kwargs): def get_template(self, request, *args, **kwargs):
# Make sure all children use the shared template # Make sure all children use the shared template
return 'funds/round.html' return 'funds/round.html'
...@@ -170,8 +163,9 @@ class RoundBase(WorkflowStreamForm, SubmittableStreamForm): # type: ignore ...@@ -170,8 +163,9 @@ class RoundBase(WorkflowStreamForm, SubmittableStreamForm): # type: ignore
def save(self, *args, **kwargs): def save(self, *args, **kwargs):
is_new = not self.id is_new = not self.id
if is_new and hasattr(self, 'parent_page'): if is_new and hasattr(self, 'parent_page'):
# Ensure that the workflow hasn't changed parent_page = self.parent_page[self.__class__][self.title]
self.workflow_name = self.parent_page.workflow_name self.workflow_name = parent_page.workflow_name
self.reviewers = parent_page.reviewers.all()
super().save(*args, **kwargs) super().save(*args, **kwargs)
...@@ -181,7 +175,7 @@ class RoundBase(WorkflowStreamForm, SubmittableStreamForm): # type: ignore ...@@ -181,7 +175,7 @@ class RoundBase(WorkflowStreamForm, SubmittableStreamForm): # type: ignore
self._copy_forms('review_forms') self._copy_forms('review_forms')
def _copy_forms(self, field): def _copy_forms(self, field):
for form in getattr(self.parent_page, field).all(): for form in getattr(self.get_parent().specific, field).all():
new_form = self._meta.get_field(field).related_model new_form = self._meta.get_field(field).related_model
self._copy_form(form, new_form) self._copy_form(form, new_form)
...@@ -220,10 +214,10 @@ class RoundBase(WorkflowStreamForm, SubmittableStreamForm): # type: ignore ...@@ -220,10 +214,10 @@ class RoundBase(WorkflowStreamForm, SubmittableStreamForm): # type: ignore
Q(end_date__gte=self.start_date) Q(end_date__gte=self.start_date)
) )
if hasattr(self, 'parent_page'): if not self.id and hasattr(self, 'parent_page'):
# Check if the create hook has added the parent page, we aren't an object yet. # Check if the create hook has added the parent page, we aren't an object yet.
# Ensures we can access related objects during the clean phase instead of save. # Ensures we can access related objects during the clean phase instead of save.
base_query = RoundBase.objects.child_of(self.parent_page) base_query = RoundBase.objects.child_of(self.parent_page[self.__class__][self.title])
else: else:
# don't need parent page, we are an actual object now. # don't need parent page, we are an actual object now.
base_query = RoundBase.objects.sibling_of(self) base_query = RoundBase.objects.sibling_of(self)
......
...@@ -138,7 +138,7 @@ class TestRoundModelDates(TestCase): ...@@ -138,7 +138,7 @@ class TestRoundModelDates(TestCase):
new_round = RoundFactory.build(start_date=overlapping_start, end_date=None) new_round = RoundFactory.build(start_date=overlapping_start, end_date=None)
# we add on the parent page which gets included from a pre_create_hook # we add on the parent page which gets included from a pre_create_hook
new_round.parent_page = self.fund new_round.parent_page = {new_round.__class__: {new_round.title: self.fund}}
with self.assertRaises(ValidationError): with self.assertRaises(ValidationError):
new_round.clean() new_round.clean()
...@@ -153,7 +153,7 @@ class TestRoundModelWorkflowAndForms(TestCase): ...@@ -153,7 +153,7 @@ class TestRoundModelWorkflowAndForms(TestCase):
self.round = RoundFactory.build(lead=lead, parent=None) self.round = RoundFactory.build(lead=lead, parent=None)
# Assign parent_page like the init does # Assign parent_page like the init does
self.round.parent_page = self.fund self.round.parent_page = {self.round.__class__: {self.round.title: self.fund}}
self.fund.add_child(instance=self.round) self.fund.add_child(instance=self.round)
def test_workflow_is_copied_to_new_rounds(self): def test_workflow_is_copied_to_new_rounds(self):
......
...@@ -10,6 +10,8 @@ modeladmin_register(ApplyAdminGroup) ...@@ -10,6 +10,8 @@ modeladmin_register(ApplyAdminGroup)
@hooks.register('before_create_page') @hooks.register('before_create_page')
def before_create_page(request, parent_page, page_class): def before_create_page(request, parent_page, page_class):
if issubclass(page_class, RoundBase): if issubclass(page_class, RoundBase) and request.POST:
page_class.parent_page = parent_page if not hasattr(page_class, 'parent_page'):
page_class.parent_page = {}
page_class.parent_page.setdefault(page_class, {})[request.POST['title']] = parent_page
return page_class return page_class
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment