Repository: UTNkar/moore

Issue:
Hide disapproved/rejected applications from the info button
<!-- Do you want to ask a question? Are you looking for support? The system administrator can help you: admin@utn.se -->

### Description

_Don't remember if I already mentioned this:

When not approving a application in the application system it should be deleted immediately, since we shouldn't save old applications._

The new group leaders should not be able to see who they competed against or who applied in earlier years.

<!-- Please select the appropriate "topic category"/blue and "issue type"/yellow label -->



Discussion:
This can be solved by hiding all non-appointed applications once everyone has been appointed.

Resolving patch:
diff --git a/website/involvement/cron.py b/website/involvement/cron.py
--- a/website/involvement/cron.py
+++ b/website/involvement/cron.py
@@ -67,3 +67,15 @@ def send_extension_emails():
             subject, body, None, [pos.role.election_email]
         )
         email_message.send()
+
+
+@kronos.register('0 3 * * *')  # At 03:00.
+def remove_old_applications():
+    old_applications = Application.objects.filter(
+        position__recruitment_end__lte=date.today() - timedelta(days=730)
+    ).exclude(
+        status='appointed'
+    )
+
+    for app in old_applications:
+        app.delete()
diff --git a/website/involvement/views.py b/website/involvement/views.py
--- a/website/involvement/views.py
+++ b/website/involvement/views.py
@@ -10,9 +10,11 @@
 from django.urls import reverse
 from django.utils.translation import ugettext_lazy as _
 from rules.contrib.views import permission_required
+from wagtail.contrib.modeladmin.views import CreateView, EditView, InspectView
 
 from involvement import forms
-from involvement.models import Position, Team, Application
+from involvement.models import Position, Team, Application, official_of, Role
+from involvement.rules import is_admin
 
 
 def open_positions(request, context):
@@ -258,3 +260,66 @@ def admin_extend_deadline(request, key):
     return HttpResponseRedirect(
         reverse('involvement_position_modeladmin_index')
     )
+
+
+class PositionCreateView(CreateView):
+    def get_form(self, form_class=None):
+        form = super(PositionCreateView, self).get_form(form_class=form_class)
+        queryset = form.fields['role'].queryset
+        queryset = queryset.filter(
+            archived=False
+        )
+        if not is_admin(self.request.user):
+            teams = official_of(self.request.user)
+            queryset = queryset.filter(
+                team__in=teams
+            )
+        form.fields['role'].queryset = queryset
+        return form
+
+
+class PositionEditView(EditView):
+    def get_form(self, form_class=None):
+        form = super(PositionEditView, self).get_form(form_class=form_class)
+        queryset = form.fields['role'].queryset
+        init = Role.objects.get(pk=form.initial['role'])
+        if not init.archived:
+            queryset = queryset.filter(
+                archived=False
+            )
+        if not is_admin(self.request.user):
+            teams = official_of(self.request.user)
+            queryset = queryset.filter(
+                team__in=teams
+            )
+        form.fields['role'].queryset = queryset
+        return form
+
+
+class PositionInspectView(InspectView):
+    def get_context_data(self, **kwargs):
+        context = super(PositionInspectView, self).get_context_data(**kwargs)
+        applicants = self.instance.applications.all()
+        if self.instance.current_action() == 'done':
+            context['applicants'] = applicants.filter(status='appointed')
+        else:
+            context['applicants'] = applicants.exclude(
+                status__in=['disapproved', 'turned_down']
+            )
+        return context
+
+
+class RoleCreateView(CreateView):
+    def get_form(self, form_class=None):
+        form = super(RoleCreateView, self).get_form(form_class=form_class)
+        if not is_admin(self.request.user):
+            form.fields['team'].queryset = official_of(self.request.user)
+        return form
+
+
+class RoleEditView(EditView):
+    def get_form(self, form_class=None):
+        form = super(RoleEditView, self).get_form(form_class=form_class)
+        if not is_admin(self.request.user):
+            form.fields['team'].queryset = official_of(self.request.user)
+        return form
diff --git a/website/involvement/wagtail_hooks.py b/website/involvement/wagtail_hooks.py
--- a/website/involvement/wagtail_hooks.py
+++ b/website/involvement/wagtail_hooks.py
@@ -9,11 +9,11 @@
 from wagtail.contrib.modeladmin.helpers import ButtonHelper
 from wagtail.contrib.modeladmin.options import ModelAdmin, ModelAdminGroup, \
     modeladmin_register
-from wagtail.contrib.modeladmin.views import CreateView, EditView
 
 from involvement.models import Team, Role, Position, Application, \
     official_of, member_of
 from involvement.rules import is_admin, approve_state, appoint_state
+from involvement import views
 from utils.permissions import RulesPermissionHelper
 
 
@@ -39,22 +39,6 @@ def get_queryset(self, request):
             return qs
 
 
-class RoleCreateView(CreateView):
-    def get_form(self, form_class=None):
-        form = super(RoleCreateView, self).get_form(form_class=form_class)
-        if not is_admin(self.request.user):
-            form.fields['team'].queryset = official_of(self.request.user)
-        return form
-
-
-class RoleEditView(EditView):
-    def get_form(self, form_class=None):
-        form = super(RoleEditView, self).get_form(form_class=form_class)
-        if not is_admin(self.request.user):
-            form.fields['team'].queryset = official_of(self.request.user)
-        return form
-
-
 class RoleAdmin(ModelAdmin):
     model = Role
     menu_label = _('Roles')
@@ -67,8 +51,8 @@ class RoleAdmin(ModelAdmin):
     # https://code.djangoproject.com/ticket/8851#no1
     list_filter = ('team', 'archived')
     permission_helper_class = RulesPermissionHelper
-    create_view_class = RoleCreateView
-    edit_view_class = RoleEditView
+    create_view_class = views.RoleCreateView
+    edit_view_class = views.RoleEditView
 
     def get_queryset(self, request):
         if is_admin(request.user):
@@ -196,40 +180,6 @@ def get_buttons_for_obj(self, obj, exclude=None, classnames_add=None,
         return btns
 
 
-class PositionCreateView(CreateView):
-    def get_form(self, form_class=None):
-        form = super(PositionCreateView, self).get_form(form_class=form_class)
-        queryset = form.fields['role'].queryset
-        queryset = queryset.filter(
-            archived=False
-        )
-        if not is_admin(self.request.user):
-            teams = official_of(self.request.user)
-            queryset = queryset.filter(
-                team__in=teams
-            )
-        form.fields['role'].queryset = queryset
-        return form
-
-
-class PositionEditView(EditView):
-    def get_form(self, form_class=None):
-        form = super(PositionEditView, self).get_form(form_class=form_class)
-        queryset = form.fields['role'].queryset
-        init = Role.objects.get(pk=form.initial['role'])
-        if not init.archived:
-            queryset = queryset.filter(
-                archived=False
-            )
-        if not is_admin(self.request.user):
-            teams = official_of(self.request.user)
-            queryset = queryset.filter(
-                team__in=teams
-            )
-        form.fields['role'].queryset = queryset
-        return form
-
-
 class PositionAdmin(ModelAdmin):
     model = Position
     menu_label = _('Positions')
@@ -243,8 +193,9 @@ class PositionAdmin(ModelAdmin):
     inspect_view_enabled = True
     permission_helper_class = PositionPermissionHelper
     button_helper_class = PositionButtonHelper
-    create_view_class = PositionCreateView
-    edit_view_class = PositionEditView
+    create_view_class = views.PositionCreateView
+    edit_view_class = views.PositionEditView
+    inspect_view_class = views.PositionInspectView
 
     def get_queryset(self, request):
         if is_admin(request.user):


Test patch: