Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/fix/proposal-creation' into test
Browse files Browse the repository at this point in the history
  • Loading branch information
frjo committed Nov 20, 2024
2 parents c06d852 + e6656d6 commit d22fdd3
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 25 deletions.
5 changes: 3 additions & 2 deletions hypha/apply/funds/models/submissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ def transition_id(target, phase):
class AddTransitions(models.base.ModelBase):
def __new__(cls, name, bases, attrs, **kwargs):
for workflow in WORKFLOWS.values():
for phase, data in workflow.items():
for phase_name, data in workflow.items():
for transition_name, action in data.transitions.items():
method_name = transition_id(transition_name, data)
permission_name = method_name + "_permission"
Expand All @@ -305,10 +305,11 @@ def __new__(cls, name, bases, attrs, **kwargs):
# Wrap with transition decorator
transition_func = transition(
attrs["status"],
source=phase,
source=phase_name,
target=transition_name,
permission=permission_func,
conditions=conditions,
custom=action.get("custom", {}),
)(transition_state)

# Attach to new class
Expand Down
51 changes: 31 additions & 20 deletions hypha/apply/funds/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1260,13 +1260,6 @@ class BaseSubmissionEditView(UpdateView):

model = ApplicationSubmission

@property
def transitions(self):
transitions = self.object.get_available_user_status_transitions(
self.request.user
)
return {transition.name: transition for transition in transitions}

def render_preview(self, request: HttpRequest, form: BaseModelForm) -> HttpResponse:
"""Gets a rendered preview of a form
Expand Down Expand Up @@ -1327,6 +1320,25 @@ def get_object_fund_current_round(self):
return assigned_fund.open_round
return False

def get_on_submit_transition(self, user):
"""Gets the transition that should be triggered when a form is submitted.
Checks all available status transitions for the current user and returns the first
one that has trigger_on_submit=True in its custom settings.
Returns:
dict: The transition configuration dictionary with trigger_on_submit=True,
or None if no matching transition is found.
"""
return next(
(
t
for t in self.object.get_available_user_status_transitions(user)
if t.custom.get("trigger_on_submit", False)
),
None,
)

def form_valid(self, form: BaseModelForm) -> HttpResponse:
"""Handle the form returned from a `SubmissionEditView`.
Expand Down Expand Up @@ -1379,19 +1391,18 @@ def form_valid(self, form: BaseModelForm) -> HttpResponse:
related=revision,
)

action = set(self.request.POST.keys()) & set(self.transitions.keys())
try:
transition = self.transitions[action.pop()]
except KeyError:
pass
else:
self.object.perform_transition(
transition.target,
self.request.user,
request=self.request,
notify=not (revision or submitting_proposal)
or self.object.status == DRAFT_STATE, # Use the other notification
)
if "submit" in self.request.POST:
if transition := self.get_on_submit_transition(self.request.user):
notify = (
not (revision or submitting_proposal)
or self.object.status == DRAFT_STATE,
)
self.object.perform_transition(
transition.target,
self.request.user,
request=self.request,
notify=notify, # Use the other notification
)

# Required for django-file-form: delete temporary files for the new files
# uploaded while edit.
Expand Down
35 changes: 32 additions & 3 deletions hypha/apply/funds/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ def __init__(
transition["permissions"] = action.get(
"permissions", default_permissions
)
if "custom" in action:
transition["custom"] = action["custom"]

self.transitions[transition_target] = transition

def __str__(self):
Expand Down Expand Up @@ -251,6 +254,7 @@ def make_permissions(edit=None, review=None, view=None):
"display": _("Submit"),
"permissions": {UserPermissions.APPLICANT},
"method": "create_revision",
"custom": {"trigger_on_submit": True},
},
},
"display": _("Draft"),
Expand Down Expand Up @@ -284,6 +288,7 @@ def make_permissions(edit=None, review=None, view=None):
UserPermissions.ADMIN,
},
"method": "create_revision",
"custom": {"trigger_on_submit": True},
},
"determination": _("Ready For Determination"),
"almost": _("Accept but additional info required"),
Expand Down Expand Up @@ -334,6 +339,7 @@ def make_permissions(edit=None, review=None, view=None):
UserPermissions.ADMIN,
},
"method": "create_revision",
"custom": {"trigger_on_submit": True},
},
"determination": _("Ready For Determination"),
"almost": _("Accept but additional info required"),
Expand Down Expand Up @@ -518,6 +524,7 @@ def make_permissions(edit=None, review=None, view=None):
"display": _("Submit"),
"permissions": {UserPermissions.APPLICANT},
"method": "create_revision",
"custom": {"trigger_on_submit": True},
},
},
"display": _("Draft"),
Expand Down Expand Up @@ -549,6 +556,7 @@ def make_permissions(edit=None, review=None, view=None):
UserPermissions.ADMIN,
},
"method": "create_revision",
"custom": {"trigger_on_submit": True},
},
},
"display": _("More information required"),
Expand Down Expand Up @@ -594,6 +602,7 @@ def make_permissions(edit=None, review=None, view=None):
UserPermissions.ADMIN,
},
"method": "create_revision",
"custom": {"trigger_on_submit": True},
},
},
"display": _("More information required"),
Expand Down Expand Up @@ -637,6 +646,7 @@ def make_permissions(edit=None, review=None, view=None):
UserPermissions.ADMIN,
},
"method": "create_revision",
"custom": {"trigger_on_submit": True},
},
},
"display": _("More information required"),
Expand Down Expand Up @@ -694,6 +704,7 @@ def make_permissions(edit=None, review=None, view=None):
"display": _("Submit"),
"permissions": {UserPermissions.APPLICANT},
"method": "create_revision",
"custom": {"trigger_on_submit": True},
},
},
"display": _("Draft"),
Expand Down Expand Up @@ -727,6 +738,7 @@ def make_permissions(edit=None, review=None, view=None):
UserPermissions.ADMIN,
},
"method": "create_revision",
"custom": {"trigger_on_submit": True},
},
},
"display": _("More information required"),
Expand Down Expand Up @@ -796,6 +808,7 @@ def make_permissions(edit=None, review=None, view=None):
UserPermissions.ADMIN,
},
"method": "create_revision",
"custom": {"trigger_on_submit": True},
},
},
"display": _("More information required"),
Expand Down Expand Up @@ -839,6 +852,7 @@ def make_permissions(edit=None, review=None, view=None):
UserPermissions.ADMIN,
},
"method": "create_revision",
"custom": {"trigger_on_submit": True},
},
},
"display": _("More information required"),
Expand Down Expand Up @@ -896,6 +910,7 @@ def make_permissions(edit=None, review=None, view=None):
"display": _("Submit"),
"permissions": {UserPermissions.APPLICANT},
"method": "create_revision",
"custom": {"trigger_on_submit": True},
},
},
"display": _("Draft"),
Expand Down Expand Up @@ -928,6 +943,7 @@ def make_permissions(edit=None, review=None, view=None):
UserPermissions.ADMIN,
},
"method": "create_revision",
"custom": {"trigger_on_submit": True},
},
"concept_rejected": _("Dismiss"),
"invited_to_proposal": _("Invite to Proposal"),
Expand Down Expand Up @@ -977,6 +993,7 @@ def make_permissions(edit=None, review=None, view=None):
UserPermissions.ADMIN,
},
"method": "create_revision",
"custom": {"trigger_on_submit": True},
},
"invited_to_proposal": _("Invite to Proposal"),
},
Expand Down Expand Up @@ -1029,6 +1046,7 @@ def make_permissions(edit=None, review=None, view=None):
"display": _("Submit"),
"permissions": {UserPermissions.APPLICANT},
"method": "create_revision",
"custom": {"trigger_on_submit": True},
},
"external_review": _("Open External Review"),
"proposal_determination": _("Ready For Final Determination"),
Expand Down Expand Up @@ -1063,6 +1081,7 @@ def make_permissions(edit=None, review=None, view=None):
UserPermissions.ADMIN,
},
"method": "create_revision",
"custom": {"trigger_on_submit": True},
},
"external_review": _("Open External Review"),
"proposal_determination": _("Ready For Final Determination"),
Expand Down Expand Up @@ -1111,6 +1130,7 @@ def make_permissions(edit=None, review=None, view=None):
UserPermissions.ADMIN,
},
"method": "create_revision",
"custom": {"trigger_on_submit": True},
},
"external_review": _("Open External Review"),
},
Expand Down Expand Up @@ -1155,6 +1175,7 @@ def make_permissions(edit=None, review=None, view=None):
UserPermissions.ADMIN,
},
"method": "create_revision",
"custom": {"trigger_on_submit": True},
},
},
"display": _("More information required"),
Expand Down Expand Up @@ -1201,12 +1222,20 @@ def make_permissions(edit=None, review=None, view=None):


def unpack_phases(phases):
for step, step_data in enumerate(phases):
for name, phase_data in step_data.items():
yield step, name, phase_data
"""Unpack a list of phases into a generator of step, name, phase_data."""
return (
(step, name, phase_data)
for step, step_data in enumerate(phases)
for name, phase_data in step_data.items()
)


def phase_data(phases):
"""Convert a list of phases into a dictionary of Phase objects.
Adds the step number to the phase data. The step number is the index of the
phase in the list of phases.
"""
return {
phase_name: Phase(phase_name, step=step, **phase_data)
for step, phase_name, phase_data in unpack_phases(phases)
Expand Down

0 comments on commit d22fdd3

Please sign in to comment.