Example #1
0
    def test_get_quota_bad_entity(self, mock_rq):
        rq = mock.MagicMock()
        rq.active = True
        mock_rq.objects.get.return_value = rq

        with self.assertRaises(ValueError) as context:
            utilities.get_quota(mock.MagicMock(), 'codename')
        self.assertTrue(
            'Entity needs to be User or TethysApp' in str(context.exception))
Example #2
0
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)
Example #3
0
    def test_get_quota_rq_inactive(self, mock_rq):
        rq = mock.MagicMock()
        rq.active = False
        mock_rq.objects.get.return_value = rq

        ret = utilities.get_quota(mock.MagicMock(), 'codename')
        self.assertEquals(None, ret['quota'])
Example #4
0
    def test_get_quota_staff(self, mock_rq):
        rq = mock.MagicMock()
        rq.active = True
        mock_rq.objects.get.return_value = rq

        user = User()
        user.is_staff = True

        ret = utilities.get_quota(user, 'codename')
        self.assertEquals(None, ret['quota'])
Example #5
0
    def test_get_quota_impose_default(self, mock_rq, mock_aq):
        rq = mock.MagicMock()
        rq.active = True
        rq.default = 100
        mock_rq.objects.get.return_value = rq

        mock_aq.objects.get.side_effect = TethysAppQuota.DoesNotExist
        mock_aq.DoesNotExist = TethysAppQuota.DoesNotExist

        ret = utilities.get_quota(TethysApp(), 'codename')
        self.assertEquals(100, ret['quota'])
Example #6
0
    def test_get_quota_aq(self, mock_rq, mock_aq):
        rq = mock.MagicMock()
        rq.active = True
        mock_rq.objects.get.return_value = rq

        aq = mock.MagicMock()
        aq.value = 100
        mock_aq.objects.get.return_value = aq

        ret = utilities.get_quota(TethysApp(), 'codename')
        self.assertEquals(100, ret['quota'])
Example #7
0
File: user.py Project: rfun/tethys
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)
Example #8
0
    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))
Example #9
0
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)