From 76f6a6c2db948c4864b10e374484b4e1ff6962fb Mon Sep 17 00:00:00 2001 From: Aaron Kitzmiller Date: Thu, 8 Aug 2024 13:39:36 -0400 Subject: [PATCH 1/2] Fix auth --- coldfront/plugins/ifx/views.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/coldfront/plugins/ifx/views.py b/coldfront/plugins/ifx/views.py index 3e81c1f80..8b8fc2041 100644 --- a/coldfront/plugins/ifx/views.py +++ b/coldfront/plugins/ifx/views.py @@ -11,9 +11,10 @@ from django.db import connection from django.conf import settings from django.core.exceptions import PermissionDenied -from rest_framework.decorators import api_view, permission_classes +from rest_framework.decorators import api_view, permission_classes, authentication_classes from rest_framework.response import Response from rest_framework import status +from rest_framework.authentication import SessionAuthentication, BasicAuthentication, TokenAuthentication from ifxreport.views import run_report as ifxreport_run_report from ifxbilling import models as ifxbilling_models from ifxbilling.calculator import getClassFromName @@ -76,14 +77,15 @@ def billing_records(request): raise PermissionDenied return render(request, 'plugins/ifx/billing_records.html') -@login_required +@api_view(['GET',]) +@authentication_classes([TokenAuthentication, SessionAuthentication, BasicAuthentication]) def get_billing_record_list(request): ''' Get billing record list ''' if not request.user.is_superuser: raise PermissionDenied - return ifxbilling_get_billing_record_list(request) + return ifxbilling_get_billing_record_list(request._request) @login_required @api_view(['POST',]) From 68b03eb4f809c6bf0de94c0cde7f4d7e706bb899 Mon Sep 17 00:00:00 2001 From: Aaron Kitzmiller Date: Mon, 19 Aug 2024 15:42:32 -0400 Subject: [PATCH 2/2] Update user account view setup, using DjangoQ async task that emails user when done Updated ifxbilling, ifxec with Account.ifxacct, Facility.ifxfac and new sync process using FacilityCodes --- .../plugins/ifx/calculate_billing_month.html | 21 ++++++ coldfront/plugins/ifx/urls.py | 3 +- coldfront/plugins/ifx/views.py | 69 +++++++++++++++++++ fiine.client | 2 +- ifxbilling | 2 +- ifxec | 2 +- 6 files changed, 95 insertions(+), 4 deletions(-) diff --git a/coldfront/plugins/ifx/templates/plugins/ifx/calculate_billing_month.html b/coldfront/plugins/ifx/templates/plugins/ifx/calculate_billing_month.html index d4da06cd3..de19046f4 100644 --- a/coldfront/plugins/ifx/templates/plugins/ifx/calculate_billing_month.html +++ b/coldfront/plugins/ifx/templates/plugins/ifx/calculate_billing_month.html @@ -107,6 +107,21 @@ function () { productUsagesTable.ajax.reload() } ) }) + $("#updateUserAccounts").click((ev) => { + ev.preventDefault() + $.ajax({ + url: '/ifx/api/update-user-accounts/', + method: 'POST', + headers: {'X-CSRFToken': '{{ csrf_token }}'}, + data: '', + dataType: "json", + error: function (jqXHR, status, error) { + alert(status + ' ' + error) + }, + }).success( + alert('Update started. You will receive an email when the update is complete.') + ) + }) }) })(jQuery) @@ -130,6 +145,12 @@
+
+
+ {% csrf_token %} + +
+
diff --git a/coldfront/plugins/ifx/urls.py b/coldfront/plugins/ifx/urls.py index c827f3bc8..82e2300cf 100644 --- a/coldfront/plugins/ifx/urls.py +++ b/coldfront/plugins/ifx/urls.py @@ -3,7 +3,7 @@ from ifxbilling.views import unauthorized as unauthorized_api from ifxuser.views import get_org_names from coldfront.plugins.ifx.viewsets import ColdfrontBillingRecordViewSet, ColdfrontReportRunViewSet, ColdfrontProductUsageViewSet -from coldfront.plugins.ifx.views import get_billing_record_list, unauthorized, report_runs, run_report, calculate_billing_month, billing_month, get_product_usages, billing_records, send_billing_record_review_notification +from coldfront.plugins.ifx.views import update_user_accounts_view, get_billing_record_list, unauthorized, report_runs, run_report, calculate_billing_month, billing_month, get_product_usages, billing_records, send_billing_record_review_notification router = routers.DefaultRouter() router.register(r'billing-records', ColdfrontBillingRecordViewSet, 'billing-record') @@ -18,6 +18,7 @@ path('api/get-org-names/', get_org_names, name='get-org-names'), path('api/get-product-usages/', get_product_usages, name='get-product-usages'), path('api/send-billing-record-review-notification///', send_billing_record_review_notification, name='send-billing-record-review-notification'), + path('api/update-user-accounts/', update_user_accounts_view, name='update-user-accounts'), path('api/', include(router.urls)), path('unauthorized/', unauthorized, name='unauthorized'), path('report-runs/', report_runs, name='report-runs'), diff --git a/coldfront/plugins/ifx/views.py b/coldfront/plugins/ifx/views.py index 8b8fc2041..bd4e8293a 100644 --- a/coldfront/plugins/ifx/views.py +++ b/coldfront/plugins/ifx/views.py @@ -7,10 +7,12 @@ import json from django.shortcuts import render, redirect from django.contrib.auth.decorators import login_required +from django.contrib.auth import get_user_model from django.utils import timezone from django.db import connection from django.conf import settings from django.core.exceptions import PermissionDenied +from django_q.tasks import async_task from rest_framework.decorators import api_view, permission_classes, authentication_classes from rest_framework.response import Response from rest_framework import status @@ -19,6 +21,8 @@ from ifxbilling import models as ifxbilling_models from ifxbilling.calculator import getClassFromName from ifxbilling.views import get_billing_record_list as ifxbilling_get_billing_record_list +from ifxbilling.fiine import update_user_accounts +from ifxmail.client import send from ifxuser import models as ifxuser_models from coldfront.plugins.ifx.calculator import NewColdfrontBillingCalculator from coldfront.plugins.ifx.permissions import AdminPermissions @@ -254,3 +258,68 @@ def send_billing_record_review_notification(request, year, month): except Exception as e: logger.exception(e) return Response(data={ 'error': f'Billing record summary failed {str(e)}' }, status=status.HTTP_500_INTERNAL_SERVER_ERROR) + +def update_user_accounts_and_notify(user_queryset, email): + ''' + Update user accounts and notify the user by sending an email with ifxmail.client send() + ''' + successes = 0 + errors = [] + + for user in user_queryset: + try: + update_user_accounts(user) + successes += 1 + except Exception as e: + logger.exception(e) + errors.append(f'Error updating {user}: {e}') + + + fromstr = 'rchelp@rc.fas.harvard.edu' + tostr = email + message = f'{successes} user accounts updated successfully.' + if errors: + errorstr = '\n'.join(errors) + message += f'Errors: {errorstr}' + subject = "Update of user accounts" + + try: + send( + to=tostr, + fromaddr=fromstr, + message=message, + subject=subject + ) + except Exception as e: + logger.exception(e) + raise Exception(f'Error sending email to {tostr} from {fromstr} with message {message} and subject {subject}: {e}.') from e + print('User accounts updated and email sent to %s', tostr) + + +@api_view(('POST',)) +def update_user_accounts_view(request): + ''' + Take a list of ifxids and update data from fiine. Body should be of the form: + { + 'ifxids': [ + 'IFXID0001', + 'IFXID0002', + ] + } + If no data is specified, all accounts will be updated + ''' + logger.error('Updating user accounts in view') + data = request.data + + if not data.keys(): + queryset = get_user_model().objects.filter(ifxid__isnull=False) + else: + queryset = get_user_model().objects.filter(ifxid__in=data['ifxids']) + + async_task( + 'coldfront.plugins.ifx.views.update_user_accounts_and_notify', + queryset, + request.user.email + ) + + return Response('OK') diff --git a/fiine.client b/fiine.client index e6e60518a..c1eac23df 160000 --- a/fiine.client +++ b/fiine.client @@ -1 +1 @@ -Subproject commit e6e60518a41e9fb4231ceaf42fc2ca5bf86f674a +Subproject commit c1eac23df0f218e512ea8dc207b78baef33deba4 diff --git a/ifxbilling b/ifxbilling index 57a550848..3a1cc1199 160000 --- a/ifxbilling +++ b/ifxbilling @@ -1 +1 @@ -Subproject commit 57a550848c19920b1c7cbc1cfe24807712f01aed +Subproject commit 3a1cc1199b2f3c4da6fe31e998fce0e3ff5486ba diff --git a/ifxec b/ifxec index 3d472b760..677e37ec1 160000 --- a/ifxec +++ b/ifxec @@ -1 +1 @@ -Subproject commit 3d472b7601488938fed772b145a37ef6fbd20d94 +Subproject commit 677e37ec1f538e4c97211438df802601dcfe4081