def manage_storage(request, username): """ Handle clear workspace requests. """ # Users are not allowed to make changes to other users settings if request.user.username != username: messages.warning(request, "You are not allowed to change other users' settings.") return redirect('user:profile', username=request.user.username) apps = SingletonHarvester().apps user = request.user for app in apps: workspace = _get_user_workspace(app, user) app.current_use = _convert_storage_units('gb', workspace.get_size('gb')) codename = 'user_workspace_quota' rqh = WorkspaceQuotaHandler(user) current_use = _convert_storage_units(rqh.units, rqh.get_current_use()) quota = get_quota(user, codename) quota = _check_quota_helper(quota) context = {'apps': apps, 'context_user': request.user, 'current_use': current_use, 'quota': quota, } return render(request, 'tethys_portal/user/manage_storage.html', context)
def settings(request, username=None): """ Handle the settings view. Access to change settings are not publicly accessible """ # Get the user object from model request_user = request.user # Users are not allowed to make changes to other users settings if request_user.username != username: messages.warning( request, "You are not allowed to change other users' settings.") return redirect('user:profile', username=request_user.username) if request.method == 'POST' and 'user-settings-submit' in request.POST: # Create a form populated with request data form = UserSettingsForm(request.POST) if form.is_valid(): first_name = form.cleaned_data['first_name'] last_name = form.cleaned_data['last_name'] email = form.cleaned_data['email'] # Update the User Model request_user.first_name = first_name request_user.last_name = last_name request_user.email = email # Save changes request_user.save() # Redirect return redirect('user:profile', username=username) else: # Create a form populated with data from the instance user form = UserSettingsForm(instance=request_user) # Create template context object user_token, token_created = Token.objects.get_or_create(user=request_user) codename = 'user_workspace_quota' rqh = WorkspaceQuotaHandler(request_user) current_use = _convert_storage_units(rqh.units, rqh.get_current_use()) quota = get_quota(request_user, codename) quota = _check_quota_helper(quota) context = { 'form': form, 'context_user': request.user, 'user_token': user_token.key, 'current_use': current_use, 'quota': quota, } return render(request, 'tethys_portal/user/settings.html', context)
def manage_app_storage(self, app): codename = 'tethysapp_workspace_quota' rqh = WorkspaceQuotaHandler(app) current_use = _convert_storage_units(rqh.units, rqh.get_current_use()) quota = get_quota(app, codename) if quota['quota']: quota = _convert_storage_units(quota['units'], quota['quota']) else: quota = "∞" url = reverse('admin:clear_workspace', kwargs={'app_id': app.id}) return format_html(""" <span>{} of {}</span> <a id="clear-workspace" class="btn btn-danger btn-sm" href="{url}"> Clear Workspace</a> """.format(current_use, quota, url=url))
def profile(request, username=None): """ Handle the profile view. Profiles could potentially be publicly accessible. """ # The profile should display information about the user that is given in the url. # However, the template will hide certain information if the username is not the same # as the username of the user that is accessing the page. context_user = User.objects.get(username=username) user_token, token_created = Token.objects.get_or_create(user=context_user) codename = 'user_workspace_quota' rqh = WorkspaceQuotaHandler(context_user) current_use = _convert_storage_units(rqh.units, rqh.get_current_use()) quota = get_quota(context_user, codename) quota = _check_quota_helper(quota) context = { 'context_user': context_user, 'user_token': user_token.key, 'current_use': current_use, 'quota': quota, } return render(request, 'tethys_portal/user/profile.html', context)