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
|
2013-10-04 11:38:39 +02:00
|
|
|
|
from django.http import HttpResponse
|
2013-02-16 16:19:20 +01:00
|
|
|
|
from django.utils.translation import ugettext as _
|
|
|
|
|
|
2013-10-04 11:38:39 +02:00
|
|
|
|
from openslides.config.api import config
|
|
|
|
|
from openslides.projector.api import get_active_slide
|
|
|
|
|
from openslides.projector.projector import Widget
|
2013-02-16 16:19:20 +01:00
|
|
|
|
from openslides.utils.template import Tab
|
2013-10-04 11:38:39 +02:00
|
|
|
|
from openslides.utils.tornado_webserver import ProjectorSocketHandler
|
|
|
|
|
from openslides.utils.views import (AjaxView, CreateView, DeleteView, RedirectView, ListView,
|
|
|
|
|
UpdateView)
|
2013-02-16 16:19:20 +01:00
|
|
|
|
|
|
|
|
|
from .forms import MediafileNormalUserCreateForm, MediafileUpdateForm
|
2013-09-25 10:01:01 +02:00
|
|
|
|
from .models import Mediafile
|
2013-02-16 16:19:20 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
2013-10-04 11:38:39 +02:00
|
|
|
|
class PdfNavBaseView(AjaxView):
|
|
|
|
|
"""
|
|
|
|
|
BaseView for the Pdf Ajax Navigation.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
def get_ajax_context(self, *args, **kwargs):
|
|
|
|
|
return {'current_page': self.active_slide['page_num']}
|
|
|
|
|
|
|
|
|
|
def load_other_page(self, active_slide):
|
|
|
|
|
"""
|
|
|
|
|
Tell connected clients to load an other pdf page.
|
|
|
|
|
"""
|
|
|
|
|
config['projector_active_slide'] = active_slide
|
|
|
|
|
ProjectorSocketHandler.send_updates(
|
|
|
|
|
{'calls': {'load_pdf_page': active_slide['page_num']}})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class PdfNextView(PdfNavBaseView):
|
|
|
|
|
"""
|
|
|
|
|
Activate the next Page of a pdf and return the number of the current page.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
|
"""
|
|
|
|
|
Increment the page number by 1.
|
|
|
|
|
|
|
|
|
|
If the page number is set in the active slide, we are the value is
|
|
|
|
|
incremented by 1. Otherwise, it is the first page and it is set to 2.
|
|
|
|
|
"""
|
|
|
|
|
self.active_slide = get_active_slide()
|
|
|
|
|
if self.active_slide['callback'] == 'mediafile':
|
|
|
|
|
if 'page_num' not in self.active_slide:
|
|
|
|
|
self.active_slide['page_num'] = 2
|
|
|
|
|
else:
|
|
|
|
|
self.active_slide['page_num'] += 1
|
|
|
|
|
self.load_other_page(self.active_slide)
|
|
|
|
|
response = super(PdfNextView, self).get(self, request, *args, **kwargs)
|
|
|
|
|
else:
|
|
|
|
|
# no Mediafile is active and the JavaScript should not do anything.
|
|
|
|
|
response = HttpResponse()
|
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class PdfPreviousView(PdfNavBaseView):
|
|
|
|
|
"""
|
|
|
|
|
Activate the previous Page of a pdf and return the number of the current page.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
|
"""
|
|
|
|
|
Decrement the page number by 1.
|
|
|
|
|
|
|
|
|
|
If the page number is set and it is greater than 1, it is decremented
|
|
|
|
|
by 1. Otherwise, it is the first page and nothing happens.
|
|
|
|
|
"""
|
|
|
|
|
self.active_slide = get_active_slide()
|
|
|
|
|
response = None
|
|
|
|
|
if self.active_slide['callback'] == 'mediafile':
|
|
|
|
|
if 'page_num' in self.active_slide and self.active_slide['page_num'] > 1:
|
|
|
|
|
self.active_slide['page_num'] -= 1
|
|
|
|
|
self.load_other_page(self.active_slide)
|
|
|
|
|
response = super(PdfPreviousView, self).get(self, request, *args, **kwargs)
|
|
|
|
|
if not response:
|
|
|
|
|
response = HttpResponse()
|
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class PdfGoToPageView(PdfNavBaseView):
|
|
|
|
|
"""
|
|
|
|
|
Activate the page set in the textfield.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
|
target_page = int(request.GET.get('page_num'))
|
|
|
|
|
self.active_slide = get_active_slide()
|
|
|
|
|
if target_page:
|
|
|
|
|
self.active_slide['page_num'] = target_page
|
|
|
|
|
self.load_other_page(self.active_slide)
|
|
|
|
|
response = super(PdfGoToPageView, self).get(self, request, *args, **kwargs)
|
|
|
|
|
else:
|
|
|
|
|
response = HttpResponse()
|
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class PdfToggleFullscreenView(RedirectView):
|
|
|
|
|
"""
|
|
|
|
|
Toggle fullscreen mode for pdf presentations.
|
|
|
|
|
"""
|
|
|
|
|
allow_ajax = True
|
|
|
|
|
url_name = 'dashboard'
|
|
|
|
|
|
|
|
|
|
def get_ajax_context(self, *args, **kwargs):
|
|
|
|
|
config['pdf_fullscreen'] = not config['pdf_fullscreen']
|
|
|
|
|
active_slide = get_active_slide()
|
|
|
|
|
if active_slide['callback'] == 'mediafile':
|
|
|
|
|
ProjectorSocketHandler.send_updates(
|
|
|
|
|
{'calls': {'toggle_fullscreen': config['pdf_fullscreen']}})
|
|
|
|
|
return {'fullscreen': config['pdf_fullscreen']}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_widgets(request):
|
|
|
|
|
"""
|
|
|
|
|
Return the widgets of the projector app
|
|
|
|
|
"""
|
|
|
|
|
widgets = []
|
|
|
|
|
|
|
|
|
|
# PDF-Presentation widget
|
|
|
|
|
pdfs = Mediafile.objects.filter(
|
|
|
|
|
filetype__in=Mediafile.PRESENTABLE_FILE_TYPES,
|
|
|
|
|
is_presentable=True
|
|
|
|
|
)
|
|
|
|
|
current_page = get_active_slide().get('page_num', 1)
|
|
|
|
|
widgets.append(Widget(
|
|
|
|
|
request,
|
|
|
|
|
name='presentations',
|
|
|
|
|
display_name=_('Presentations'),
|
|
|
|
|
template='mediafile/pdfs_widget.html',
|
|
|
|
|
context={'pdfs': pdfs, 'current_page': current_page,
|
|
|
|
|
'pdf_fullscreen': config['pdf_fullscreen']},
|
|
|
|
|
permission_required='projector.can_manage_projector',
|
|
|
|
|
default_column=1,
|
|
|
|
|
default_weight=75))
|
|
|
|
|
|
|
|
|
|
return widgets
|
|
|
|
|
|
|
|
|
|
|
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)
|