]> git.parisson.com Git - teleforma.git/commitdiff
add seminar access rules through mixins
authoryomguy <yomguy@parisson.com>
Fri, 25 Jan 2013 17:04:59 +0000 (18:04 +0100)
committeryomguy <yomguy@parisson.com>
Fri, 25 Jan 2013 17:04:59 +0000 (18:04 +0100)
teleforma/templates/teleforma/inc/document_simple_list.html
teleforma/templates/teleforma/inc/document_step.html
teleforma/urls.py
teleforma/views/core.py
teleforma/views/pro.py

index 76ef7e93dbbb2b86a6d52e4d1acc84c6fa5a3876..b11cd279f680e5d6d358db52dd963cffc5fc1a04 100644 (file)
@@ -5,8 +5,8 @@
     <tbody>
         {% for doc in docs.all %}
             <tr>
-            <td {% if forloop.first %}class="border-top"{% endif %} width="95%">{% if doc.file %}<a href="{% url teleforma-document-view doc.id %}" target="_blank" title="{% trans "View" %}"><img src="{{ STATIC_URL }}/teleforma/images/application-msword.png" style="vertical-align:middle" alt="" />{% endif %} {{ doc.title }}{% if doc.file %}</a>{% endif %}</td>
-            <td {% if forloop.first %}class="border-top"{% endif %} width="5%" align="center">{% if doc.file %}<a href="{% url teleforma-document-download doc.id %}"><img src="{{ STATIC_URL }}teleforma/images/download.png" style="vertical-align:middle" alt="" title="{% trans "Download" %}" /></a>{% endif %}</td>
+            <td {% if forloop.first %}class="border-top"{% endif %} width="95%">{% if doc.file %}<a href="{% url teleforma-document-view seminar.id doc.id %}" target="_blank" title="{% trans "View" %}"><img src="{{ STATIC_URL }}/teleforma/images/application-msword.png" style="vertical-align:middle" alt="" />{% endif %} {{ doc.title }}{% if doc.file %}</a>{% endif %}</td>
+            <td {% if forloop.first %}class="border-top"{% endif %} width="5%" align="center">{% if doc.file %}<a href="{% url teleforma-document-download seminar.id doc.id %}"><img src="{{ STATIC_URL }}teleforma/images/download.png" style="vertical-align:middle" alt="" title="{% trans "Download" %}" /></a>{% endif %}</td>
             </tr>
         {% endfor %}
     </tbody>
index b97fccf089333836e9a0df137902b71ed5ab0a86..be4c6d8aaa14ee6b48b66ae548031ae38c4aed89 100644 (file)
@@ -5,7 +5,7 @@
     <tbody>
         {% for doc in docs.all %}
             <tr>
-            <td {% if forloop.first %}class="border-top"{% endif %} width="95%">{% if doc.file %}<a href="{% url teleforma-document-view doc.id %}" target="_blank" title="{% trans "View" %}"><img src="{{ STATIC_URL }}/teleforma/images/application-msword.png" style="vertical-align:middle" alt="" />{% endif %} {{ doc.title }}{% if doc.file %}</a>{% endif %}</td>
+            <td {% if forloop.first %}class="border-top"{% endif %} width="95%">{% if doc.file %}<a href="{% url teleforma-document-view seminar.id doc.id %}" target="_blank" title="{% trans "View" %}"><img src="{{ STATIC_URL }}/teleforma/images/application-msword.png" style="vertical-align:middle" alt="" />{% endif %} {{ doc.title }}{% if doc.file %}</a>{% endif %}</td>
             </tr>
         {% endfor %}
     </tbody>
index 85fe954c005c4f5eb80790201449324f61b647c0..596dc94c7821d47cc26ecebe60d6261a16f64c8a 100644 (file)
@@ -45,7 +45,7 @@ from jsonrpc import jsonrpc_site
 htdocs_forma = os.path.dirname(__file__) + '/static/teleforma/'
 user_export = UsersXLSExport()
 profile_view = ProfileView()
-document = DocumentView()
+document = SeminarDocumentView()
 media = MediaView()
 
 urlpatterns = patterns('',
@@ -67,7 +67,7 @@ urlpatterns = patterns('',
     url(r'^desk/seminars/(?P<pk>.*)/detail/$', SeminarView.as_view(), name="teleforma-seminar-detail"),
 
     url(r'^desk/medias/(?P<pk>.*)/detail/$', MediaView.as_view(), name="teleforma-media-detail"),
-    url(r'^desk/medias/(?P<pk>.*)/download/$', media.download, name="teleforma-media-download"),
+    url(r'^desk/medias/(?P<pk>.*)/download/$', media.download, name="teleforma-media-download"),
 
     url(r'^desk/seminars/(?P<id>.*)/media/(?P<pk>.*)/video/$',
             SeminarMediaView.as_view(template_name='teleforma/seminar_media_video.html'),
@@ -79,11 +79,9 @@ urlpatterns = patterns('',
             SeminarMediaPreviewView.as_view(),
             name="teleforma-media-preview-video"),
 
-    url(r'^desk/documents/(?P<pk>.*)/detail/$', DocumentView.as_view(),
-        name="teleforma-document-detail"),
-    url(r'^desk/documents/(?P<pk>.*)/download/$', document.download,
+    url(r'^desk/seminars/(?P<id>.*)/documents/(?P<pk>.*)/download/$', SeminarDocumentDownloadView.as_view(),
         name="teleforma-document-download"),
-    url(r'^desk/documents/(?P<pk>.*)/view/$', document.view,
+    url(r'^desk/seminars/(?P<id>.*)/documents/(?P<pk>.*)/view/$', SeminarDocumentView.as_view(),
         name="teleforma-document-view"),
 #    url(r'^desk/documents/(?P<pk>.*)/view/$', document_view, name="teleforma-document-view"),
 
index f4d3d9618d773d3e33b133305dbe8f0a9d68164b..742857c89484f70e9f32b6da6c111a5ffd42099d 100644 (file)
@@ -199,6 +199,16 @@ def get_periods(user):
     return periods
 
 
+class CourseAccessMixin(object):
+
+    def render_to_response(self, context):
+        course = context['course']
+        if not course in all_courses(self.request.user):
+            messages.warning(self.request, _("You do NOT have access to this resource and then have been redirected to your desk."))
+            return redirect('teleforma-desk')
+        return super(CourseAccessMixin, self).render_to_response(context)
+
+
 class CourseView(DetailView):
 
     model = Course
@@ -229,6 +239,7 @@ class CourseView(DetailView):
         return super(CourseView, self).dispatch(*args, **kwargs)
 
 
+
 class CoursesView(ListView):
 
     model = Course
@@ -348,35 +359,32 @@ class DocumentView(DetailView):
     def dispatch(self, *args, **kwargs):
         return super(DocumentView, self).dispatch(*args, **kwargs)
 
-    def download(self, request, pk):
-        document = Document.objects.get(id=pk)
-        courses = get_courses(request.user)
-        seminars = all_seminars(request)['all_seminars']
-        if get_seminar_doc_access(document, seminars):
-            document.readers.add(request.user)
-            fsock = open(document.file.path, 'r')
-            mimetype = mimetypes.guess_type(document.file.path)[0]
-            extension = mimetypes.guess_extension(mimetype)
-            response = HttpResponse(fsock, mimetype=mimetype)
-            response['Content-Disposition'] = "attachment; filename=%s%s" % \
+
+class DocumentDownloadView(DocumentView):
+
+    def render_to_response(self, context):
+        document = self.get_object()
+        document.readers.add(self.request.user)
+        fsock = open(document.file.path, 'r')
+        mimetype = mimetypes.guess_type(document.file.path)[0]
+        extension = mimetypes.guess_extension(mimetype)
+        response = HttpResponse(fsock, mimetype=mimetype)
+        response['Content-Disposition'] = "attachment; filename=%s%s" % \
                                              (document.title.encode('utf8'), extension)
-            return response
-        else:
-            return redirect('teleforma-document-detail', document.id)
+        return super(DocumentDownloadView, self).render_to_response(context)
 
-    def view(self, request, pk):
-        courses = get_courses(request.user)
-        seminars = all_seminars(request)['all_seminars']
-        document = Document.objects.get(id=pk)
-        if get_seminar_doc_access(document, seminars):
-            document.readers.add(request.user)
-            fsock = open(document.file.path, 'r')
-            mimetype = mimetypes.guess_type(document.file.path)[0]
-            extension = mimetypes.guess_extension(mimetype)
-            response = HttpResponse(fsock, mimetype=mimetype)
-            return response
-        else:
-            return redirect('teleforma-document-detail', document.id)
+
+class DocumentReadView(DocumentView):
+
+    def render_to_response(self, context):
+        courses = get_courses(self.request.user)
+        document = self.get_object()
+        document.readers.add(self.request.user)
+        fsock = open(document.file.path, 'r')
+        mimetype = mimetypes.guess_type(document.file.path)[0]
+        extension = mimetypes.guess_extension(mimetype)
+        response = HttpResponse(fsock, mimetype=mimetype)
+        return super(DocumentReadView, self).render_to_response(context)
 
 
 class ConferenceView(DetailView):
index 96d2cc7357c2f0845c603b237f41f9587f1b44dc..e2149daf3fd46311baf6fba1429a310dc02d257e 100644 (file)
@@ -223,16 +223,41 @@ class SeminarMediaView(SeminarAccessMixin, MediaView):
         set_revision(user, seminar)
         return context
 
-    def get_object(self, queryset=None):
-        return Media.objects.get(id=self.pk)
-
     @method_decorator(login_required)
     def dispatch(self, *args, **kwargs):
-        self.pk = kwargs.get('pk')
-        media = self.get_object()
         return super(SeminarMediaView, self).dispatch(*args, **kwargs)
 
 
+class SeminarDocumentView(SeminarAccessMixin, DocumentReadView):
+
+    def get_context_data(self, **kwargs):
+        context = super(SeminarDocumentView, self).get_context_data(**kwargs)
+        user = self.request.user
+        seminar = Seminar.objects.get(pk=self.kwargs['id'])
+        context['seminar'] = seminar
+        set_revision(user, seminar)
+        return context
+
+    @method_decorator(login_required)
+    def dispatch(self, *args, **kwargs):
+        return super(SeminarDocumentView, self).dispatch(*args, **kwargs)
+
+
+class SeminarDocumentDownloadView(SeminarAccessMixin, DocumentDownloadView):
+
+    def get_context_data(self, **kwargs):
+        context = super(SeminarDocumentDownloadView, self).get_context_data(**kwargs)
+        user = self.request.user
+        seminar = Seminar.objects.get(pk=self.kwargs['id'])
+        context['seminar'] = seminar
+        set_revision(user, seminar)
+        return context
+
+    @method_decorator(login_required)
+    def dispatch(self, *args, **kwargs):
+        return super(SeminarDocumentDownloadView, self).dispatch(*args, **kwargs)
+
+
 class SeminarMediaPreviewView(DetailView):
 
     model = Seminar
@@ -246,6 +271,7 @@ class SeminarMediaPreviewView(DetailView):
     def dispatch(self, *args, **kwargs):
         return super(SeminarMediaPreviewView, self).dispatch(*args, **kwargs)
 
+
 class AnswersView(ListView):
 
     model = Answer