2013-02-16 16:19:20 +01:00
|
|
|
|
#!/usr/bin/env python
|
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
"""
|
|
|
|
|
openslides.mediafile.views
|
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
|
|
Views for the mediafile app.
|
|
|
|
|
|
|
|
|
|
:copyright: 2011–2013 by OpenSlides team, see AUTHORS.
|
|
|
|
|
:license: GNU GPL, see LICENSE for more details.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
from django.core.urlresolvers import reverse
|
|
|
|
|
from django.utils.translation import ugettext as _
|
|
|
|
|
|
|
|
|
|
from openslides.utils.template import Tab
|
|
|
|
|
from openslides.utils.views import ListView, CreateView, UpdateView, DeleteView
|
|
|
|
|
|
|
|
|
|
from .models import Mediafile
|
|
|
|
|
from .forms import MediafileNormalUserCreateForm, MediafileUpdateForm
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class MediafileListView(ListView):
|
2013-03-19 00:51:52 +01:00
|
|
|
|
"""
|
|
|
|
|
View to see a table of all uploaded files.
|
|
|
|
|
"""
|
2013-02-16 16:19:20 +01:00
|
|
|
|
model = Mediafile
|
|
|
|
|
|
|
|
|
|
def has_permission(self, request, *args, **kwargs):
|
|
|
|
|
return (request.user.has_perm('mediafile.can_see') or
|
|
|
|
|
request.user.has_perm('mediafile.can_upload') or
|
|
|
|
|
request.user.has_perm('mediafile.can_manage'))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class MediafileCreateView(CreateView):
|
2013-03-19 00:51:52 +01:00
|
|
|
|
"""
|
|
|
|
|
View to upload a new file. A manager can also set the uploader, else
|
|
|
|
|
the request user is set as uploader.
|
2013-02-16 16:19:20 +01:00
|
|
|
|
"""
|
|
|
|
|
model = Mediafile
|
|
|
|
|
permission_required = 'mediafile.can_upload'
|
|
|
|
|
success_url_name = 'mediafile_list'
|
2013-09-25 12:53:44 +02:00
|
|
|
|
url_name_args = []
|
2013-02-16 16:19:20 +01:00
|
|
|
|
|
|
|
|
|
def get_form(self, form_class):
|
|
|
|
|
form_kwargs = self.get_form_kwargs()
|
|
|
|
|
if self.request.method == 'GET':
|
2013-03-18 21:50:50 +01:00
|
|
|
|
form_kwargs['initial'].update({'uploader': self.request.user.person_id})
|
2013-02-16 16:19:20 +01:00
|
|
|
|
if not self.request.user.has_perm('mediafile.can_manage'):
|
2013-03-19 00:51:52 +01:00
|
|
|
|
# Returns our own ModelForm from .forms
|
2013-02-16 16:19:20 +01:00
|
|
|
|
return MediafileNormalUserCreateForm(**form_kwargs)
|
|
|
|
|
else:
|
2013-03-19 00:51:52 +01:00
|
|
|
|
# Returns a ModelForm created by Django.
|
2013-02-16 16:19:20 +01:00
|
|
|
|
return form_class(**form_kwargs)
|
|
|
|
|
|
|
|
|
|
def manipulate_object(self, *args, **kwargs):
|
2013-03-19 00:51:52 +01:00
|
|
|
|
"""
|
|
|
|
|
Method to handle the uploader. If a user has manager permissions,
|
|
|
|
|
he has to set the uploader in the given form field. Then this
|
|
|
|
|
method only calls super. Else it sets the requesting user as uploader.
|
2013-03-18 21:50:50 +01:00
|
|
|
|
"""
|
2013-02-16 16:19:20 +01:00
|
|
|
|
if not self.request.user.has_perm('mediafile.can_manage'):
|
|
|
|
|
self.object.uploader = self.request.user
|
|
|
|
|
return super(MediafileCreateView, self).manipulate_object(*args, **kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class MediafileUpdateView(UpdateView):
|
2013-03-19 00:51:52 +01:00
|
|
|
|
"""
|
|
|
|
|
View to edit the entry of an uploaded file.
|
|
|
|
|
"""
|
2013-02-16 16:19:20 +01:00
|
|
|
|
model = Mediafile
|
|
|
|
|
permission_required = 'mediafile.can_manage'
|
|
|
|
|
form_class = MediafileUpdateForm
|
|
|
|
|
success_url_name = 'mediafile_list'
|
2013-09-25 12:53:44 +02:00
|
|
|
|
url_name_args = []
|
2013-02-16 16:19:20 +01:00
|
|
|
|
|
|
|
|
|
def get_form_kwargs(self, *args, **kwargs):
|
|
|
|
|
form_kwargs = super(MediafileUpdateView, self).get_form_kwargs(*args, **kwargs)
|
|
|
|
|
form_kwargs['initial'].update({'uploader': self.object.uploader.person_id})
|
|
|
|
|
return form_kwargs
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class MediafileDeleteView(DeleteView):
|
2013-03-19 00:51:52 +01:00
|
|
|
|
"""
|
|
|
|
|
View to delete the entry of an uploaded file and the file itself.
|
|
|
|
|
"""
|
2013-02-16 16:19:20 +01:00
|
|
|
|
model = Mediafile
|
|
|
|
|
permission_required = 'mediafile.can_manage'
|
|
|
|
|
success_url_name = 'mediafile_list'
|
|
|
|
|
|
2013-09-25 12:53:44 +02:00
|
|
|
|
def on_clicked_yes(self, *args, **kwargs):
|
2013-02-16 16:19:20 +01:00
|
|
|
|
"""Deletes the file in the filesystem, if user clicks "Yes"."""
|
|
|
|
|
self.object.mediafile.delete()
|
2013-09-25 12:53:44 +02:00
|
|
|
|
return super(MediafileDeleteView, self).on_clicked_yes(*args, **kwargs)
|
2013-02-16 16:19:20 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def register_tab(request):
|
2013-03-19 00:51:52 +01:00
|
|
|
|
"""
|
|
|
|
|
Inserts a new Tab to the views for files.
|
|
|
|
|
"""
|
2013-02-16 16:19:20 +01:00
|
|
|
|
selected = request.path.startswith('/mediafile/')
|
|
|
|
|
return Tab(
|
2013-05-14 20:20:41 +02:00
|
|
|
|
title=_('Files'),
|
2013-02-16 16:19:20 +01:00
|
|
|
|
app='mediafile', # TODO: Rename this to icon='mediafile' later
|
|
|
|
|
stylefile='styles/mediafile.css',
|
|
|
|
|
url=reverse('mediafile_list'),
|
|
|
|
|
permission=(request.user.has_perm('mediafile.can_see') or
|
|
|
|
|
request.user.has_perm('mediafile.can_upload') or
|
|
|
|
|
request.user.has_perm('mediafile.can_manage')),
|
|
|
|
|
selected=selected)
|