Skip to content

Commit

Permalink
Rename learn_app and deliver_app, add validation for not allowing sam…
Browse files Browse the repository at this point in the history
…e app
  • Loading branch information
pxwxnvermx committed Jun 29, 2023
1 parent 9391580 commit 5f77551
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions commcare_connect/opportunity/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ class Meta:
model = Opportunity
fields = ["name", "description"]

learn_app_select = forms.ChoiceField()
deliver_app_select = forms.ChoiceField()
learn_app = forms.ChoiceField()
deliver_app = forms.ChoiceField()

def __init__(self, *args, **kwargs):
self.applications = kwargs.pop("applications", [])
Expand All @@ -25,20 +25,26 @@ def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

choices = [(app["id"], app["name"]) for app in self.applications]
self.fields["learn_app_select"] = forms.ChoiceField(choices=choices)
self.fields["deliver_app_select"] = forms.ChoiceField(choices=choices)
self.fields["learn_app"] = forms.ChoiceField(choices=choices)
self.fields["deliver_app"] = forms.ChoiceField(choices=choices)

def clean(self):
cleaned_data = super().clean()
if cleaned_data["learn_app"] == cleaned_data["deliver_app"]:
self.add_error("learn_app", "Learn app and Deliver app cannot be same")
self.add_error("deliver_app", "Learn app and Deliver app cannot be same")

def save(self, commit=True):
for app in self.applications:
if app["id"] == self.cleaned_data["learn_app_select"]:
if app["id"] == self.cleaned_data["learn_app"]:
self.instance.learn_app, _ = CommCareApp.objects.get_or_create(
cc_app_id=app["id"],
name=app["name"],
cc_domain=app["domain"],
defaults={"created_by": self.user.email, "modified_by": self.user.email},
)

if app["id"] == self.cleaned_data["deliver_app_select"]:
if app["id"] == self.cleaned_data["deliver_app"]:
self.instance.deliver_app, _ = CommCareApp.objects.get_or_create(
cc_app_id=app["id"],
name=app["name"],
Expand Down

0 comments on commit 5f77551

Please sign in to comment.