def get_context_data(self, **kwargs): """get contexts""" ret = {'is_success': True, 'error': ''} ctx = super(SendView, self).get_context_data(**kwargs) user = self.request.user institution_id = int(kwargs['institution_id']) if Institution.objects.filter(pk=institution_id).exists(): institution = Institution.objects.get(pk=institution_id) else: institution = get_dummy_institution() all_superusers_list = list(OSFUser.objects.filter(is_superuser=True).values_list('username', flat=True)) to_list = [user.username] cc_list = all_superusers_list if user.is_superuser: cc_list.remove(user.username) elif not user.is_staff: ret['is_success'] = False return ctx current_date = get_current_date() attachment_file_name = 'statistics' + current_date.strftime('%Y/%m/%d') + '.pdf' attachment_file_data = get_pdf_data(institution=institution) mail_data = { 'subject': '[[GakuNin RDM]] statistic information at ' + current_date.strftime('%Y/%m/%d'), 'content': 'statistic information of storage in ' + institution.name + ' at ' + current_date.strftime('%Y/%m/%d'), 'attach_file': attachment_file_name, 'attach_data': attachment_file_data } ret = send_email(to_list=to_list, cc_list=cc_list, data=mail_data, user=user) data = { 'ret': ret, 'mail_data': mail_data } ctx['data'] = data return ctx
def get(self, request, *args, **kwargs): """get contexts""" user = self.request.user if self.is_super_admin: self.object_list = self.get_queryset() ctx = self.get_context_data() return self.render_to_response(ctx) elif self.is_admin: institution = user.affiliated_institutions.first() if institution: return redirect(reverse('timestampadd:nodes', args=[institution.id])) else: institution = get_dummy_institution() return redirect(reverse('timestampadd:nodes', args=[institution.id]))
def get(self, request, *args, **kwargs): # simple authentication access_token = self.kwargs.get('access_token') if not simple_auth(access_token): response_hash = {'state': 'fail', 'error': 'access forbidden'} response_json = json.dumps(response_hash) response = HttpResponse(response_json, content_type='application/json') return response # user user = self.request.user self.cnt = 0 self.stat_list = [] current_date = get_current_date() try: # create session self.session = requests.Session() self.adapter = requests.adapters.HTTPAdapter(max_retries=WB_MAX_RETRY) # user crawling for user in self.get_users(): if user.affiliated_institutions.first(): institution = user.affiliated_institutions.first() else: institution = get_dummy_institution() cookie = self.get_cookie(user) for node in self.get_user_nodes(user): providers = node.get_addon_names() for guid in node.guids.all(): for provider in providers: self.count_list = [] path = '/' self.count_project_files(node_id=guid._id, provider=provider, path=path, cookies=cookie) if len(self.count_list) > 0: self.regist_database(node=node, guid=guid, owner=user, institution=institution, provider=provider, date_acquired=current_date, count_list=self.count_list) self.stat_list.append([institution.name, guid._id, provider]) response_json = json.dumps(self.stat_list) response = HttpResponse(response_json, content_type='application/json') # statistics mail send send_stat_mail(request) except Exception as err: response_hash = {'state': 'fail', 'error': str(err)} response_json = json.dumps(response_hash) response = HttpResponse(response_json, content_type='application/json') send_error_mail(err) finally: self.session.close() return response
def get(self, request, *args, **kwargs): """get contexts""" user = self.request.user # superuser: if self.is_super_admin: self.object_list = self.get_queryset() ctx = self.get_context_data() return self.render_to_response(ctx) # institution administraor: elif self.is_admin: institution = user.affiliated_institutions.first() if institution: return redirect( reverse('keymanagement:users', args=[institution.id])) else: institution = get_dummy_institution() return redirect( reverse('keymanagement:users', args=[institution.id]))
def get_context_data(self, **kwargs): """get contexts""" ctx = super(AddonListView, self).get_context_data(**kwargs) ctx['enable_force'] = admin_settings.ENABLE_FORCE_CHECK institution_id = int(kwargs['institution_id']) if Institution.objects.filter(pk=institution_id).exists(): institution = Institution.objects.get(pk=institution_id) else: institution = get_dummy_institution() ctx['institution'] = institution with app.test_request_context(): ctx['addon_settings'] = utils.get_addons_by_config_type( 'accounts', self.request.user) accounts_addons = [ addon for addon in website_settings.ADDONS_AVAILABLE if 'accounts' in addon.configs ] ctx.update({ 'addon_enabled_settings': [addon.short_name for addon in accounts_addons], 'addons_js': utils.collect_addon_js(accounts_addons), 'addon_capabilities': website_settings.ADDON_CAPABILITIES, 'addons_css': [] }) for addon in ctx['addon_settings']: addon_name = addon['addon_short_name'] rdm_addon_option = utils.get_rdm_addon_option( institution.id, addon_name) addon['option'] = {} addon['option'] = model_to_dict(rdm_addon_option) addon['option'][ 'external_accounts'] = rdm_addon_option.external_accounts.values( ) return ctx
def get(self, request, *args, **kwargs): """get contexts""" user = self.request.user # superuser: if self.is_super_admin: ctx = { 'institutions': Institution.objects.order_by('id').all(), 'logohost': admin_settings.OSF_URL, } return self.render_to_response(ctx) # institution administrator elif self.is_admin: institution = user.affiliated_institutions.first() if institution: return redirect(reverse('addons:addons', args=[institution.id])) else: institution = get_dummy_institution() return redirect(reverse('addons:addons', args=[institution.id]))
def create_csv(request, **kwargs): """download pdf""" user = request.user if not user.is_authenticated: raise PermissionDenied if not (user.is_superuser or user.is_staff): raise PermissionDenied institution_id = int(kwargs['institution_id']) if Institution.objects.filter(pk=institution_id).exists(): institution = Institution.objects.get(pk=institution_id) else: institution = get_dummy_institution() current_date = get_current_date() csv_data = get_all_statistic_data_csv(institution=institution) csv_file_name = 'statistics.all.' + current_date.strftime('%Y%m%d') + '.csv' response = HttpResponse(content_type='text/csv') response['Content-Disposition'] = 'attachment; filename=' + csv_file_name writer = csv.writer(response, quoting=csv.QUOTE_NONNUMERIC) writer.writerows(csv_data) return response
def get_context_data(self, **kwargs): """get contexts""" ctx = super(StatisticsView, self).get_context_data(**kwargs) user = self.request.user institution_id = int(kwargs['institution_id']) if Institution.objects.filter(pk=institution_id).exists(): institution = Institution.objects.get(pk=institution_id) else: institution = get_dummy_institution() if institution: ctx['institution'] = institution current_date = get_current_date() start_date = get_start_date(end_date=current_date) provider_data_array = get_provider_data_array(institution=institution, start_date=start_date, end_date=current_date) ctx['current_date'] = current_date ctx['user'] = user ctx['provider_data_array'] = provider_data_array digest = hashlib.sha512(SITE_KEY).hexdigest() ctx['token'] = digest.upper() return ctx
def create_pdf(request, is_pdf=True, **kwargs): """download pdf""" user = request.user if not user.is_authenticated: raise PermissionDenied if not (user.is_superuser or user.is_staff): raise PermissionDenied institution_id = int(kwargs['institution_id']) if Institution.objects.filter(pk=institution_id).exists(): institution = Institution.objects.get(pk=institution_id) else: institution = get_dummy_institution() current_date = get_current_date() start_date = get_start_date(end_date=current_date) provider_data_array = get_provider_data_array(institution=institution, start_date=start_date, end_date=current_date) template_name = 'rdm_statistics/statistics_report.html' # context data ctx = {} if institution: ctx['institution'] = institution ctx['current_date'] = current_date ctx['user'] = user ctx['provider_data_array'] = provider_data_array html_string = render_to_string(template_name, ctx) # if html if is_pdf: # if PDF try: converted_pdf = convert_to_pdf(html_string=html_string, file=False) pdf_file_name = 'statistics.' + current_date.strftime('%Y%m%d') + '.pdf' response = HttpResponse(converted_pdf, content_type='application/pdf') response['Content-Disposition'] = 'attachment; filename="' + pdf_file_name + '"' return response except OSError as e: response = HttpResponse(str(e), content_type='text/html', status=501) except Exception as e: response = HttpResponse(str(e), content_type='text/html', status=501) else: response = HttpResponse(html_string, content_type='text/html') return response
def get(self, request, *args, **kwargs): """get context data""" graph_type = self.kwargs.get('graph_type') provider = self.kwargs.get('provider') institution_id = int(self.kwargs.get('institution_id')) if Institution.objects.filter(pk=institution_id).exists(): institution = Institution.objects.get(pk=institution_id) else: institution = get_dummy_institution() # create provider data provider_data = self.__get_data(provider=provider, institution=institution) cols = ['left', 'height', 'type'] data = pd.DataFrame(index=[], columns=cols) statistics_data = provider_data.get_data(data_type=graph_type) left = statistics_data.label if statistics_data.data_type == 'ext': data = statistics_data.df else: size_df_sum = statistics_data.df.groupby('left', as_index=False).sum() size_sum_list = list(size_df_sum['height'].values.flatten()) data = pd.DataFrame({'left': left, 'height': size_sum_list, 'type': statistics_data.data_type}) fig = plt.figure(figsize=(STATISTICS_IMAGE_WIDTH, STATISTICS_IMAGE_HEIGHT)) sns.set_style(statistics_data.graphstyle) fig.patch.set_facecolor(statistics_data.background) ax = sns.pointplot(x='left', y='height', hue='type', data=data) ax.set_xticklabels(labels=statistics_data.label, rotation=20) ax.set_xlabel(xlabel=statistics_data.x_label) ax.set_ylabel(ylabel=statistics_data.y_label) ax.set_title(statistics_data.title + ' in ' + provider) ax.tick_params(labelsize=9) ax.yaxis.set_major_locator(ticker.MaxNLocator(integer=True)) plt.legend(loc='upper right', bbox_to_anchor=(1.1255555, 1), ncol=1, borderaxespad=1, shadow=True) response = HttpResponse(content_type='image/png') canvas = FigureCanvasAgg(fig) canvas.print_png(response) plt.close() return response