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 @@
+
+
+
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 3e81c1f80..bd4e8293a 100644
--- a/coldfront/plugins/ifx/views.py
+++ b/coldfront/plugins/ifx/views.py
@@ -7,17 +7,22 @@
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 rest_framework.decorators import api_view, permission_classes
+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
+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
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
@@ -76,14 +81,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',])
@@ -252,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