Пример #1
0
def create_project(request):
    dictionary = {
        'account_list': Account.objects.all(),
    }
    if request.method == 'GET':
        return render(request, 'accounts_and_projects/create_project.html',
                      dictionary)
    form = ProjectForm(request.POST)
    if not form.is_valid():
        dictionary['form'] = form
        return render(request, 'accounts_and_projects/create_project.html',
                      dictionary)
    project = form.save()
    account_history = MembershipHistory()
    account_history.authorizer = request.user
    account_history.action = MembershipHistory.Action.ADDED
    account_history.child_content_object = project
    account_history.parent_content_object = project.account
    account_history.save()
    project_history = ActivityHistory()
    project_history.authorizer = request.user
    project_history.action = project.active
    project_history.content_object = project
    project_history.save()
    return redirect('account', project.account.id)
Пример #2
0
def create_project(request):
    form = ProjectForm(request.POST or None)
    dictionary = {
        "account_list": Account.objects.all(),
        "user_list": User.objects.filter(is_active=True),
        "form": form
    }
    if request.method == "GET":
        return render(request, "accounts_and_projects/create_project.html",
                      dictionary)
    if not form.is_valid():
        return render(request, "accounts_and_projects/create_project.html",
                      dictionary)
    project = form.save()
    account_history = MembershipHistory()
    account_history.authorizer = request.user
    account_history.action = MembershipHistory.Action.ADDED
    account_history.child_content_object = project
    account_history.parent_content_object = project.account
    account_history.save()
    project_history = ActivityHistory()
    project_history.authorizer = request.user
    project_history.action = project.active
    project_history.content_object = project
    project_history.save()
    return redirect("project", project.id)
Пример #3
0
def record_active_state(request, obj, form, field_name, is_initial_creation):
	"""
	Record whether the account, project, or user is active when the active state is changed.
	"""
	if field_name in form.changed_data or is_initial_creation:
		activity_entry = ActivityHistory()
		activity_entry.authorizer = request.user
		activity_entry.action = getattr(obj, field_name)
		activity_entry.content_object = obj
		activity_entry.save()
Пример #4
0
def create_account(request):
	if request.method == 'GET':
		return render(request, 'accounts_and_projects/create_account.html')
	form = AccountForm(request.POST)
	if not form.is_valid():
		return render(request, 'accounts_and_projects/create_account.html', {'form': form})
	account = form.save()
	history = ActivityHistory()
	history.authorizer = request.user
	history.action = account.active
	history.content_object = account
	history.save()
	return redirect('account', account.id)
Пример #5
0
def toggle_active(request, kind, identifier):
	if kind == 'account':
		entity = get_object_or_404(Account, id=identifier)
	elif kind == 'project':
		entity = get_object_or_404(Project, id=identifier)
	else:
		return HttpResponseBadRequest('Invalid entity for active toggle request.')
	entity.active = not entity.active
	entity.save()
	history = ActivityHistory()
	history.authorizer = request.user
	history.action = entity.active
	history.content_object = entity
	history.save()
	return redirect(request.META.get('HTTP_REFERER', 'accounts_and_projects'))
Пример #6
0
def create_account(request):
    form = AccountForm(request.POST or None)
    dictionary = {"form": form}
    if request.method == "GET":
        return render(request, "accounts_and_projects/create_account.html",
                      dictionary)
    if not form.is_valid():
        return render(request, "accounts_and_projects/create_account.html",
                      dictionary)
    account = form.save()
    history = ActivityHistory()
    history.authorizer = request.user
    history.action = account.active
    history.content_object = account
    history.save()
    return redirect("account", account.id)
Пример #7
0
def deactivate(request, user_id):
    dictionary = {
        'user_to_deactivate':
        get_object_or_404(User, id=user_id),
        'reservations':
        Reservation.objects.filter(user=user_id,
                                   cancelled=False,
                                   missed=False,
                                   end__gt=timezone.now()),
        'staff_charges':
        StaffCharge.objects.filter(customer=user_id, end=None),
        'tool_usage':
        UsageEvent.objects.filter(user=user_id,
                                  end=None).prefetch_related('tool'),
    }
    user_to_deactivate = dictionary['user_to_deactivate']
    if request.method == 'GET':
        return render(request, 'users/safe_deactivation.html', dictionary)
    elif request.method == 'POST':
        if settings.IDENTITY_SERVICE['available']:
            parameters = {
                'username': user_to_deactivate.username,
                'domain': user_to_deactivate.domain,
            }
            try:
                result = requests.delete(settings.IDENTITY_SERVICE['url'],
                                         data=parameters,
                                         timeout=3)
                # If the delete succeeds, or the user is not found, then everything is ok.
                if result.status_code not in (HTTPStatus.OK,
                                              HTTPStatus.NOT_FOUND):
                    logger.error(
                        f'The identity service encountered a problem while attempting to delete a user. The HTTP error is {result.status_code}: {result.text}'
                    )
                    dictionary[
                        'warning'] = 'The user information was not modified because the identity service could not delete the corresponding domain account. The NEMO administrator has been notified to resolve the problem.'
                    return render(request, 'users/safe_deactivation.html',
                                  dictionary)
            except Exception as e:
                logger.error(
                    'There was a problem communicating with the identity service while attempting to delete a user. An exception was encountered: '
                    + type(e).__name__ + ' - ' + str(e))
                dictionary[
                    'warning'] = 'The user information was not modified because the identity service could not delete the corresponding domain account. The NEMO administrator has been notified to resolve the problem.'
                return render(request, 'users/safe_deactivation.html',
                              dictionary)

        if request.POST.get('cancel_reservations') == 'on':
            # Cancel all reservations that haven't ended
            for reservation in dictionary['reservations']:
                reservation.cancelled = True
                reservation.cancellation_time = timezone.now()
                reservation.cancelled_by = request.user
                reservation.save()
        if request.POST.get('disable_tools') == 'on':
            # End all current tool usage
            for usage_event in dictionary['tool_usage']:
                if usage_event.tool.interlock and not usage_event.tool.interlock.lock(
                ):
                    error_message = f"The interlock command for the {usage_event.tool} failed. The error message returned: {usage_event.tool.interlock.most_recent_reply}"
                    logger.error(error_message)
                usage_event.end = timezone.now()
                usage_event.save()
        if request.POST.get('force_area_logout') == 'on':
            area_access = user_to_deactivate.area_access_record()
            if area_access:
                area_access.end = timezone.now()
                area_access.save()
        if request.POST.get('end_staff_charges') == 'on':
            # End a staff charge that the user might be performing
            staff_charge = user_to_deactivate.get_staff_charge()
            if staff_charge:
                staff_charge.end = timezone.now()
                staff_charge.save()
                try:
                    area_access = AreaAccessRecord.objects.get(
                        staff_charge=staff_charge, end=None)
                    area_access.end = timezone.now()
                    area_access.save()
                except AreaAccessRecord.DoesNotExist:
                    pass
            # End all staff charges that are being performed for the user
            for staff_charge in dictionary['staff_charges']:
                staff_charge.end = timezone.now()
                staff_charge.save()
                try:
                    area_access = AreaAccessRecord.objects.get(
                        staff_charge=staff_charge, end=None)
                    area_access.end = timezone.now()
                    area_access.save()
                except AreaAccessRecord.DoesNotExist:
                    pass
        user_to_deactivate.is_active = False
        user_to_deactivate.save()
        activity_entry = ActivityHistory()
        activity_entry.authorizer = request.user
        activity_entry.action = ActivityHistory.Action.DEACTIVATED
        activity_entry.content_object = user_to_deactivate
        activity_entry.save()
        return redirect('users')