11import json
2+ from dateutil .parser import isoparse
23
34from django .contrib import messages
45from django .contrib .auth .mixins import PermissionRequiredMixin
1516from admin .base .forms import ImportFileForm
1617from admin .institutions .forms import InstitutionForm , InstitutionalMetricsAdminRegisterForm
1718from osf .models import Institution , Node , OSFUser
19+ from osf .metrics .utils import YearMonth
20+ from osf .metrics .reporters import AllMonthlyReporters
21+ from osf .management .commands .monthly_reporters_go import monthly_reporter_do
1822
1923
2024class InstitutionList (PermissionRequiredMixin , ListView ):
@@ -129,6 +133,38 @@ def get(self, request, *args, **kwargs):
129133 return response
130134
131135
136+ class InstitutionMonthlyReporterDo (PermissionRequiredMixin , View ):
137+ permission_required = 'osf.view_institution'
138+ raise_exception = True
139+
140+ def post (self , request , * args , ** kwargs ):
141+ institution_id = self .kwargs .get ('institution_id' )
142+ try :
143+ institution = Institution .objects .get_all_institutions ().get (id = institution_id )
144+ except Institution .DoesNotExist :
145+ raise Http404 (f"Institution with id { institution_id } is not found or deactivated." )
146+
147+ monthly_report_date = request .POST .get ('monthly_report_date' , None )
148+ if monthly_report_date :
149+ try :
150+ monthly_report_date = isoparse (monthly_report_date ).date ()
151+ except ValueError as exc :
152+ messages .error (request , str (exc ))
153+ return redirect ('institutions:detail' , institution_id = institution .id )
154+ else :
155+ messages .error (request , 'Report date cannot be none.' )
156+ return redirect ('institutions:detail' , institution_id = institution .id )
157+
158+ monthly_reporter_do .apply_async (kwargs = {
159+ 'yearmonth' : str (YearMonth .from_date (monthly_report_date )),
160+ 'reporter_key' : request .POST .get ('monthly_reporter' , None ),
161+ 'report_kwargs' : {'institution_pk' : institution .id },
162+ })
163+
164+ messages .success (request , 'Monthly reporter successfully went.' )
165+ return redirect ('institutions:detail' , institution_id = institution .id )
166+
167+
132168class CreateInstitution (PermissionRequiredMixin , CreateView ):
133169 permission_required = 'osf.change_institution'
134170 raise_exception = True
@@ -141,6 +177,18 @@ def get_context_data(self, *args, **kwargs):
141177 kwargs ['import_form' ] = ImportFileForm ()
142178 return super ().get_context_data (* args , ** kwargs )
143179
180+ def form_valid (self , form ):
181+ response = super ().form_valid (form )
182+
183+ # Make a report after Institution is created
184+ monthly_reporter_do .apply_async (kwargs = {
185+ 'yearmonth' : str (YearMonth .from_date (self .object .created )),
186+ 'reporter_key' : AllMonthlyReporters .INSTITUTIONAL_SUMMARY .name ,
187+ 'report_kwargs' : {'institution_pk' : self .object .id },
188+ })
189+
190+ return response
191+
144192
145193class InstitutionNodeList (PermissionRequiredMixin , ListView ):
146194 template_name = 'institutions/node_list.html'
0 commit comments