mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-11-29 08:25:10 +00:00
sharing: Finish implementation
Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org> Reviewed-by: Joseph Nuthalapati <njoseph@thoughtworks.com>
This commit is contained in:
1 parent
a42aed78f1
commit
ebabb2f8aa
10 files changed
+446
-154
No files matched your search
@@ -1,5 +1,5 @@
|
||||
#
|
||||
# This file is part of Plinth.
|
||||
# This file is part of FreedomBox.
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Affero General Public License as
|
||||
@@ -14,94 +14,46 @@
|
||||
# You should have received a copy of the GNU Affero General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
#
|
||||
|
||||
"""
|
||||
Plinth module to configure sharing.
|
||||
FreedomBox app to configure sharing.
|
||||
"""
|
||||
import os
|
||||
|
||||
from django import forms
|
||||
from django.template.response import TemplateResponse
|
||||
from django.urls import reverse_lazy
|
||||
from django.utils.translation import ugettext_lazy as _, ugettext_lazy
|
||||
import json
|
||||
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from plinth import actions
|
||||
from plinth.menu import main_menu
|
||||
from plinth.modules.users import groups
|
||||
|
||||
version = 1
|
||||
|
||||
name = _('Sharing')
|
||||
|
||||
short_description = _('File Sharing')
|
||||
|
||||
description = [
|
||||
_('Sharing allows you to share your content over web with a group of '
|
||||
'users. Add the content you would like to share in the sharing app.'),
|
||||
|
||||
_('Sharing app will be available from <a href="/plinth/apps/add_share">'
|
||||
'/sharing</a> path on the web server.'),
|
||||
_('Sharing allows you to share your content over web with chosen groups'
|
||||
'of users. Add the content you would like to share in the sharing app.'),
|
||||
]
|
||||
|
||||
subsubmenu = [{'url': reverse_lazy('sharing:about'),
|
||||
'text': ugettext_lazy('About')},
|
||||
{'url': reverse_lazy('sharing:add_share'),
|
||||
'text': ugettext_lazy('Add share')}]
|
||||
|
||||
|
||||
def init():
|
||||
"""Initialize the module."""
|
||||
menu = main_menu.get('apps')
|
||||
menu.add_urlname(name, 'glyphicon-share', 'sharing:about')
|
||||
menu.add_urlname(name, 'glyphicon-share', 'sharing:index')
|
||||
|
||||
|
||||
def index(request):
|
||||
return TemplateResponse(request, 'about.html',
|
||||
{'title': name,
|
||||
'description': description,
|
||||
'subsubmenu': subsubmenu})
|
||||
def list_shares():
|
||||
"""Return a list of shares."""
|
||||
output = actions.superuser_run('sharing', ['list'])
|
||||
return json.loads(output)['shares']
|
||||
|
||||
|
||||
# TODO: handle the error case
|
||||
def add_path_to_share(url, path, user_group):
|
||||
if os.path.exists(path):
|
||||
actions.superuser_run('sharing', options=['add', url, path, user_group])
|
||||
else:
|
||||
pass
|
||||
def add_share(name, path, groups):
|
||||
"""Add a new share by called the action script."""
|
||||
actions.superuser_run(
|
||||
'sharing',
|
||||
['add', '--name', name, '--path', path, '--groups'] + groups)
|
||||
|
||||
|
||||
def share(request):
|
||||
if request.method == 'POST':
|
||||
form = AddShareForm(request.POST)
|
||||
|
||||
if form.is_valid():
|
||||
path = form.cleaned_data['share_path']
|
||||
user_group = form.cleaned_data['user_group']
|
||||
share_url = 'share_' + path.split("/")[len(path.split("/")) - 1]
|
||||
add_path_to_share(share_url, path, user_group)
|
||||
|
||||
form = AddShareForm()
|
||||
|
||||
else:
|
||||
form = AddShareForm()
|
||||
|
||||
return TemplateResponse(request, 'share.html',
|
||||
{'title': name,
|
||||
'subsubmenu': subsubmenu,
|
||||
'form': form})
|
||||
|
||||
|
||||
class AddShareForm(forms.Form):
|
||||
share_path = forms.CharField(
|
||||
label=_('Add path'),
|
||||
help_text=_('Add the path to the folder you want to share'))
|
||||
|
||||
user_group = forms.ChoiceField(
|
||||
required=False,
|
||||
choices=groups,
|
||||
label=_('User-group'),
|
||||
initial=None)
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(forms.Form, self).__init__(*args, **kwargs)
|
||||
return
|
||||
def remove_share(name):
|
||||
"""Remove a share by calling the action script."""
|
||||
actions.superuser_run('sharing', ['remove', '--name', name])
|
||||
@@ -0,0 +1,64 @@
|
||||
#
|
||||
# This file is part of FreedomBox.
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Affero General Public License as
|
||||
# published by the Free Software Foundation, either version 3 of the
|
||||
# License, or (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Affero General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Affero General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
#
|
||||
"""
|
||||
Django forms for sharing app.
|
||||
"""
|
||||
|
||||
from django import forms
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from plinth.modules.users.forms import get_group_choices
|
||||
from plinth.modules import sharing
|
||||
|
||||
|
||||
class AddShareForm(forms.Form):
|
||||
"""Form to add a new share."""
|
||||
|
||||
name = forms.RegexField(
|
||||
label=_('Name of the share'), strip=True, regex=r'^[a-z0-9]+$',
|
||||
help_text=_(
|
||||
'A lowercase alpha-numeric string that uniquely identifies a '
|
||||
'share. Example: <em>media</em>.'))
|
||||
|
||||
path = forms.CharField(
|
||||
label=_('Path to share'), strip=True, help_text=_(
|
||||
'Disk path to a folder on this server that you intend to share.'))
|
||||
|
||||
groups = forms.MultipleChoiceField(
|
||||
widget=forms.CheckboxSelectMultiple,
|
||||
label=_('User groups who can read the files in the share'),
|
||||
help_text=_(
|
||||
'Users who have these permissions will also be able to read the '
|
||||
'files in the share.'))
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
"""Initialize the form with extra request argument."""
|
||||
super().__init__(*args, **kwargs)
|
||||
self.fields['groups'].choices = get_group_choices()
|
||||
|
||||
def clean_name(self):
|
||||
"""Check if the name is valid."""
|
||||
name = self.cleaned_data['name']
|
||||
if 'name' in self.initial and name == self.initial['name']:
|
||||
return name
|
||||
|
||||
if any((share for share in sharing.list_shares()
|
||||
if name == share['name'])):
|
||||
raise ValidationError(_('A share with this name already exists.'))
|
||||
|
||||
return name
|
||||
@@ -1,25 +0,0 @@
|
||||
{% extends "simple_service.html" %}
|
||||
{% comment %}
|
||||
#
|
||||
# This file is part of Plinth.
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Affero General Public License as
|
||||
# published by the Free Software Foundation, either version 3 of the
|
||||
# License, or (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Affero General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Affero General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
#
|
||||
{% endcomment %}
|
||||
|
||||
{% load i18n %}
|
||||
|
||||
{% block configuration %}
|
||||
|
||||
{% endblock %}
|
||||
@@ -0,0 +1,83 @@
|
||||
{% extends "base.html" %}
|
||||
{% comment %}
|
||||
#
|
||||
# This file is part of FreedomBox.
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Affero General Public License as
|
||||
# published by the Free Software Foundation, either version 3 of the
|
||||
# License, or (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Affero General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Affero General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
#
|
||||
{% endcomment %}
|
||||
|
||||
{% load i18n %}
|
||||
|
||||
{% block content %}
|
||||
|
||||
<h2>{{ title }}</h2>
|
||||
|
||||
{% for paragraph in description %}
|
||||
<p>{{ paragraph|safe }}</p>
|
||||
{% endfor %}
|
||||
|
||||
<p>
|
||||
<a title="{% trans 'Add new share' %}"
|
||||
role="button" class="btn btn-primary"
|
||||
href="{% url 'sharing:add' %}">
|
||||
{% trans 'Add new share' %}
|
||||
</a>
|
||||
</p>
|
||||
|
||||
{% if not shares %}
|
||||
<p>{% trans 'No shares currently configured.' %}</p>
|
||||
{% else %}
|
||||
<table class="table table-bordered table-condensed table-striped" id="shares-list">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>{% trans "Share Name" %}</th>
|
||||
<th>{% trans "Disk Path" %}</th>
|
||||
<th>{% trans "Shared Over" %}</th>
|
||||
<th>{% trans "To Groups" %}</th>
|
||||
<th></th>
|
||||
</tr>
|
||||
</thead>
|
||||
|
||||
<tbody>
|
||||
{% for share in shares %}
|
||||
<tr id="share-{{ share.name }}" class="share">
|
||||
<td class="share-name">{{ share.name }}</td>
|
||||
<td class="share-path">{{ share.path }}</td>
|
||||
<td class="share-url">
|
||||
<a href="{{ share.url }}"
|
||||
title="{{ share.url}}">
|
||||
{{ share.url }}
|
||||
</a>
|
||||
</td>
|
||||
<td class="share-groups">{{ share.groups|join:", " }}</td>
|
||||
<td class="share-operations">
|
||||
<form class="form form-inline" method="post"
|
||||
action="{% url 'sharing:remove' share.name %}">
|
||||
{% csrf_token %}
|
||||
<button class="share-remove btn btn-sm btn-default glyphicon glyphicon-trash pull-right"
|
||||
type="submit"></button>
|
||||
</form>
|
||||
<a class="share-edit btn btn-sm btn-default pull-right"
|
||||
href="{% url 'sharing:edit' share.name %}">
|
||||
<span class="glyphicon glyphicon-edit" aria-hidden="true"></span>
|
||||
</a>
|
||||
</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
{% endif %}
|
||||
|
||||
{% endblock %}
|
||||
+17
-10
@@ -1,7 +1,7 @@
|
||||
{% extends "simple_service.html" %}
|
||||
{% extends "base.html" %}
|
||||
{% comment %}
|
||||
#
|
||||
# This file is part of Plinth.
|
||||
# This file is part of FreedomBox.
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Affero General Public License as
|
||||
@@ -21,16 +21,23 @@
|
||||
{% load bootstrap %}
|
||||
{% load i18n %}
|
||||
|
||||
{% block configuration %}
|
||||
{% block content %}
|
||||
|
||||
<form class="form" method="post">
|
||||
{% csrf_token %}
|
||||
<h3>{{ title }}</h3>
|
||||
|
||||
{{ form|bootstrap }}
|
||||
<form class="form" method="post">
|
||||
{% csrf_token %}
|
||||
|
||||
<input type="submit" class="btn btn-primary"
|
||||
value="{% trans "Submit" %}"/>
|
||||
</form>
|
||||
{{ form|bootstrap }}
|
||||
|
||||
<input type="submit" class="btn btn-primary"
|
||||
value="{% trans "Submit" %}"/>
|
||||
</form>
|
||||
|
||||
{% endblock %}
|
||||
{% endblock %}
|
||||
|
||||
{% block page_js %}
|
||||
<script>
|
||||
$('#id_sharing-name').focus();
|
||||
</script>
|
||||
{% endblock %}
|
||||
@@ -1,5 +1,5 @@
|
||||
#
|
||||
# This file is part of Plinth.
|
||||
# This file is part of FreedomBox.
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Affero General Public License as
|
||||
@@ -14,16 +14,18 @@
|
||||
# You should have received a copy of the GNU Affero General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
#
|
||||
|
||||
"""
|
||||
URLs for the sharing module.
|
||||
URLs for the sharing app.
|
||||
"""
|
||||
|
||||
from django.conf.urls import url
|
||||
|
||||
from . import index, share
|
||||
from .views import AddShareView, EditShareView, IndexView, remove
|
||||
|
||||
urlpatterns = [
|
||||
url(r'^apps/sharing/$', index, name='about'),
|
||||
url(r'^apps/sharing/add_share$', share, name='add_share'),
|
||||
]
|
||||
url(r'^apps/sharing/$', IndexView.as_view(), name='index'),
|
||||
url(r'^apps/sharing/add/$', AddShareView.as_view(), name='add'),
|
||||
url(r'^apps/sharing/(?P<name>[a-z0-9]+)/edit/$', EditShareView.as_view(),
|
||||
name='edit'),
|
||||
url(r'^apps/sharing/(?P<name>[a-z0-9]+)/remove/$', remove, name='remove'),
|
||||
]
|
||||
@@ -0,0 +1,109 @@
|
||||
#
|
||||
# This file is part of FreedomBox.
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Affero General Public License as
|
||||
# published by the Free Software Foundation, either version 3 of the
|
||||
# License, or (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Affero General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Affero General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
#
|
||||
"""
|
||||
Views for the sharing app.
|
||||
"""
|
||||
|
||||
from django.contrib import messages
|
||||
from django.contrib.messages.views import SuccessMessageMixin
|
||||
from django.http import Http404
|
||||
from django.shortcuts import redirect
|
||||
from django.urls import reverse_lazy
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
from django.views.decorators.http import require_POST
|
||||
from django.views.generic import FormView, TemplateView
|
||||
|
||||
from plinth.modules import sharing
|
||||
|
||||
from .forms import AddShareForm
|
||||
|
||||
|
||||
class IndexView(TemplateView):
|
||||
"""View to show list of shares."""
|
||||
template_name = 'sharing.html'
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
"""Return additional context for rendering the template."""
|
||||
context = super().get_context_data(**kwargs)
|
||||
context['title'] = sharing.name
|
||||
context['description'] = sharing.description
|
||||
context['shares'] = sharing.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'
|
||||
success_url = reverse_lazy('sharing:index')
|
||||
success_message = _('Share added.')
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
"""Return additional context for rendering the template."""
|
||||
context = super().get_context_data(**kwargs)
|
||||
context['title'] = _('Add Share')
|
||||
return context
|
||||
|
||||
def form_valid(self, form):
|
||||
"""Add the share on valid form submission."""
|
||||
sharing.add_share(form.cleaned_data['name'], form.cleaned_data['path'],
|
||||
form.cleaned_data['groups'])
|
||||
return super().form_valid(form)
|
||||
|
||||
|
||||
class EditShareView(SuccessMessageMixin, FormView):
|
||||
"""View to edit an existing share."""
|
||||
form_class = AddShareForm
|
||||
prefix = 'sharing'
|
||||
template_name = 'sharing_add_edit.html'
|
||||
success_url = reverse_lazy('sharing:index')
|
||||
success_message = _('Share edited.')
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
"""Return additional context for rendering the template."""
|
||||
context = super().get_context_data(**kwargs)
|
||||
context['title'] = _('Edit Share')
|
||||
return context
|
||||
|
||||
def get_initial(self):
|
||||
"""Load information about share being edited."""
|
||||
try:
|
||||
return [
|
||||
share for share in sharing.list_shares()
|
||||
if share['name'] == self.kwargs['name']
|
||||
][0]
|
||||
except IndexError:
|
||||
raise Http404
|
||||
|
||||
def form_valid(self, form):
|
||||
"""Add the share on valid form submission."""
|
||||
if form.initial != form.cleaned_data:
|
||||
sharing.remove_share(form.initial['name'])
|
||||
sharing.add_share(form.cleaned_data['name'],
|
||||
form.cleaned_data['path'],
|
||||
form.cleaned_data['groups'])
|
||||
|
||||
return super().form_valid(form)
|
||||
|
||||
|
||||
@require_POST
|
||||
def remove(request, name):
|
||||
"""View to remove a share."""
|
||||
sharing.remove_share(name)
|
||||
messages.success(request, _('Share deleted.'))
|
||||
return redirect(reverse_lazy('sharing:index'))
|
||||
Reference in new issue
Block a user