Repository: UTNkar/moore

Issue:
Application drafts in limbo
<!-- Do you want to ask a question? Are you looking for support? The system administrator can help you: admin@utn.se -->

### Prerequisites

* [x] Put an X between the brackets on this line if you have done all of the
following:
    * Reproduced the problem with clear cache.
    * (If running the application locally:) Made sure your running the newest version on the development branch
    * Checked that your issue isn't already filed: https://github.com/UTNkar/moore/issues

### Description

The problem has not been reproduced but it's the kind of problem that probably will occur any way.

When a user starts an application draft and forgets to submit it the person can not be appointed to the position with the overturn function. If the application for a position has been saved as draft when the application period ends the application becomes stuck in limbo. The group administrator can before the application period has ended see that there is a draft for the person. When the application period ends the draft is not visible among the submitted applications (very resonable). The problem occurs when the administrator wants to appoint that person anyway with the overturn function then an error message occurs saying. "You can not appoint this person since an application has been submitted". This should not be the case, a draft application should not be seen as a submitted application by the overturn function. The user can not see or delete the draft application after the application period has ended.

Quick fix, an application system administrator can access the applications and set the status to submitted. 

### Steps to Reproduce

1. Apply to an position and save the application as draft
2. Wait for the application time to end
3. Go to appoint for the given position
4. Try overturn with the user who saved the application as draft
5. Error message occurs 

<!-- Please select the appropriate "topic category"/blue and "issue type"/yellow label -->


Discussion:
**Temporary Workaround:**
1. Find the draft in the applications.
2. Change the application status to "Appointed"

**Fixing**
Thanks for the great bug report! I think I might know what is going on. I'll see about it as soon as possible!

Resolving patch:
diff --git a/website/involvement/forms.py b/website/involvement/forms.py
--- a/website/involvement/forms.py
+++ b/website/involvement/forms.py
@@ -107,6 +107,8 @@ def clean_overturn(self):
                     )
                 elif self.position.applications.filter(
                     applicant__username=u
+                ).exclude(
+                    status='draft'
                 ).exists():
                     raise forms.ValidationError(
                         _('User %(user)s already applied for this position '
@@ -144,8 +146,11 @@ def save(self):
             user = get_user_model().objects.get(
                     username=user
             )
-            Application.objects.create(
+            appl, created = Application.objects.get_or_create(
                 position=self.position,
                 applicant=user,
-                status='appointed',
+                defaults={'status': 'appointed'}
             )
+            if not created:
+                appl.status = 'appointed'
+                appl.save()


Test patch: