1
1
import json
2
+ from dateutil .parser import isoparse
2
3
3
4
from django .contrib import messages
4
5
from django .contrib .auth .mixins import PermissionRequiredMixin
15
16
from admin .base .forms import ImportFileForm
16
17
from admin .institutions .forms import InstitutionForm , InstitutionalMetricsAdminRegisterForm
17
18
from 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
18
22
19
23
20
24
class InstitutionList (PermissionRequiredMixin , ListView ):
@@ -129,6 +133,38 @@ def get(self, request, *args, **kwargs):
129
133
return response
130
134
131
135
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
+
132
168
class CreateInstitution (PermissionRequiredMixin , CreateView ):
133
169
permission_required = 'osf.change_institution'
134
170
raise_exception = True
@@ -141,6 +177,18 @@ def get_context_data(self, *args, **kwargs):
141
177
kwargs ['import_form' ] = ImportFileForm ()
142
178
return super ().get_context_data (* args , ** kwargs )
143
179
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
+
144
192
145
193
class InstitutionNodeList (PermissionRequiredMixin , ListView ):
146
194
template_name = 'institutions/node_list.html'
0 commit comments