mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-11-29 08:25:10 +00:00
sharing: Use privileged decorator for actions
Tests: - Initial setup works. - Empty Apache configuration file is created - Adding a share works all the information added is shown during editing. Configuration file is updated as expected. - List of shares is shown as expected. - When editing a share, information about share is shown correctly. Editing works are expected. - Removing a share works. - Trying to add share with a name that already exists throws a proper error message. Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org> Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
1 parent
6e7b31a3cf
commit
637e6b1198
5 files changed
+175
-248
No files matched your search
@@ -1,7 +1,5 @@
|
||||
# SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
"""
|
||||
Views for the sharing app.
|
||||
"""
|
||||
"""Views for the sharing app."""
|
||||
|
||||
from django.contrib import messages
|
||||
from django.contrib.messages.views import SuccessMessageMixin
|
||||
@@ -12,26 +10,28 @@ from django.utils.translation import gettext_lazy as _
|
||||
from django.views.decorators.http import require_POST
|
||||
from django.views.generic import FormView
|
||||
|
||||
from plinth.modules import sharing
|
||||
from plinth.views import AppView
|
||||
|
||||
from . import privileged
|
||||
from .forms import AddShareForm
|
||||
|
||||
|
||||
class SharingAppView(AppView):
|
||||
"""Sharing configuration page."""
|
||||
|
||||
app_id = 'sharing'
|
||||
template_name = 'sharing.html'
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
"""Return additional context for rendering the template."""
|
||||
context = super().get_context_data(**kwargs)
|
||||
context['shares'] = sharing.list_shares()
|
||||
context['shares'] = privileged.list_shares()
|
||||
return context
|
||||
|
||||
|
||||
class AddShareView(SuccessMessageMixin, FormView):
|
||||
"""View to add a new share."""
|
||||
|
||||
form_class = AddShareForm
|
||||
prefix = 'sharing'
|
||||
template_name = 'sharing_add_edit.html'
|
||||
@@ -52,6 +52,7 @@ class AddShareView(SuccessMessageMixin, FormView):
|
||||
|
||||
class EditShareView(SuccessMessageMixin, FormView):
|
||||
"""View to edit an existing share."""
|
||||
|
||||
form_class = AddShareForm
|
||||
prefix = 'sharing'
|
||||
template_name = 'sharing_add_edit.html'
|
||||
@@ -68,7 +69,7 @@ class EditShareView(SuccessMessageMixin, FormView):
|
||||
"""Load information about share being edited."""
|
||||
try:
|
||||
return [
|
||||
share for share in sharing.list_shares()
|
||||
share for share in privileged.list_shares()
|
||||
if share['name'] == self.kwargs['name']
|
||||
][0]
|
||||
except IndexError:
|
||||
@@ -77,20 +78,20 @@ class EditShareView(SuccessMessageMixin, FormView):
|
||||
def form_valid(self, form):
|
||||
"""Add the share on valid form submission."""
|
||||
if form.initial != form.cleaned_data:
|
||||
sharing.remove_share(form.initial['name'])
|
||||
privileged.remove(form.initial['name'])
|
||||
_add_share(form.cleaned_data)
|
||||
|
||||
return super().form_valid(form)
|
||||
|
||||
|
||||
def _add_share(form_data):
|
||||
sharing.add_share(form_data['name'], form_data['path'],
|
||||
form_data['groups'], form_data['is_public'])
|
||||
privileged.add(form_data['name'], form_data['path'], form_data['groups'],
|
||||
form_data['is_public'])
|
||||
|
||||
|
||||
@require_POST
|
||||
def remove(request, name):
|
||||
"""View to remove a share."""
|
||||
sharing.remove_share(name)
|
||||
privileged.remove(name)
|
||||
messages.success(request, _('Share deleted.'))
|
||||
return redirect(reverse_lazy('sharing:index'))
|
||||
Reference in new issue
Block a user