diff --git a/opentech/apply/funds/admin.py b/opentech/apply/funds/admin.py index b3da7925fbff5995afb963a9ea316c7d95549922..3dda27b90a6836c095ac1ed7969592adb9fc9c1b 100644 --- a/opentech/apply/funds/admin.py +++ b/opentech/apply/funds/admin.py @@ -1,3 +1,5 @@ +from django.urls import reverse +from django.utils.safestring import mark_safe from wagtail.contrib.modeladmin.helpers import PermissionHelper from wagtail.contrib.modeladmin.options import ModelAdmin, ModelAdminGroup @@ -16,7 +18,6 @@ from opentech.apply.categories.admin import CategoryAdmin, MetaCategoryAdmin class BaseRoundAdmin(ModelAdmin): choose_parent_view_class = RoundFundChooserView choose_parent_template_name = 'funds/admin/parent_chooser.html' - list_display = ('title', 'fund', 'start_date', 'end_date') button_helper_class = ButtonsWithPreview def fund(self, obj): @@ -26,6 +27,38 @@ class BaseRoundAdmin(ModelAdmin): class RoundAdmin(BaseRoundAdmin): model = Round menu_icon = 'repeat' + list_display = ('title', 'fund', 'start_date', 'end_date', 'sealed', 'applications', 'review_forms') + + def applications(self, obj): + def build_urls(applications): + for application in applications: + url = reverse('funds_applicationform_modeladmin_edit', args=[application.id]) + yield f'<a href="{url}">{application}</a>' + + urls = list(build_urls(obj.forms.all())) + + if not urls: + return + + return mark_safe('<br />'.join(urls)) + + def fund(self, obj): + url = self.url_helper.get_action_url('edit', obj.fund.id) + url_tag = f'<a href="{url}">{obj.fund}</a>' + return mark_safe(url_tag) + + def review_forms(self, obj): + def build_urls(review_forms): + for review_form in review_forms: + url = reverse('funds_round_modeladmin_edit', args=[review_form.id]) + yield f'<a href="{url}">{review_form}</a>' + + urls = list(build_urls(obj.review_forms.all())) + + if not urls: + return + + return mark_safe('<br />'.join(urls)) class ScreeningStatusPermissionHelper(PermissionHelper): @@ -54,6 +87,7 @@ class SealedRoundAdmin(BaseRoundAdmin): model = SealedRound menu_icon = 'locked' menu_label = 'Sealed Rounds' + list_display = ('title', 'fund', 'start_date', 'end_date') class FundAdmin(ModelAdmin): diff --git a/opentech/apply/funds/tests/test_admin_views.py b/opentech/apply/funds/tests/test_admin_views.py index 3469b0448bbab7eb7bdf37c197628fa22d2ee2d4..9bfc4cc438ddc05d359141bb8d6f88a77abe4483 100644 --- a/opentech/apply/funds/tests/test_admin_views.py +++ b/opentech/apply/funds/tests/test_admin_views.py @@ -1,9 +1,13 @@ +from django.contrib.auth.models import Group from django.test import TestCase from django.urls import reverse +from wagtail.tests.utils import WagtailTestUtils -from opentech.apply.users.tests.factories import SuperUserFactory from opentech.apply.home.factories import ApplyHomePageFactory +from opentech.apply.users.groups import STAFF_GROUP_NAME +from opentech.apply.users.tests.factories import SuperUserFactory +from .factories.models import RoundFactory from .test_admin_form import form_data @@ -42,3 +46,40 @@ class TestFundCreationView(TestCase): fund = self.create_page(2, same_forms=True) self.assertEqual(fund.forms.count(), 2) self.assertEqual(fund.review_forms.count(), 2) + + +class TestRoundIndexView(WagtailTestUtils, TestCase): + def setUp(self): + user = self.create_test_user() + self.client.force_login(user) + + group, _ = Group.objects.get_or_create(name=STAFF_GROUP_NAME) + group.user_set.add(user) + + self.round = RoundFactory() + + def test_application_links(self): + response = self.client.get('/admin/funds/round/', follow=True) + + application_links = [ + f'<a href="/admin/funds/applicationform/edit/{app.id}/">{app}</a>' + for app in self.round.forms.all() + ] + applications_cell = f'<td class="field-applications">{"".join(application_links)}</td>' + self.assertContains(response, applications_cell, html=True) + + def test_number_of_rounds(self): + response = self.client.get('/admin/funds/round/', follow=True) + + self.assertEqual(response.status_code, 200) + self.assertEqual(response.context_data["result_count"], 1) + + def test_review_form_links(self): + response = self.client.get('/admin/funds/round/', follow=True) + + review_form_links = [ + f'<a href="/admin/funds/round/edit/{review_form.id}/">{review_form}</a>' + for review_form in self.round.review_forms.all() + ] + review_form_cell = f'<td class="field-review_forms">{"".join(review_form_links)}</td>' + self.assertContains(response, review_form_cell, html=True)