# -*- coding:utf8 -*-
# Copyright (C) 2012 FinalsClub Foundation
+import os
+
+from django.conf import settings
from django.contrib.sites.models import Site
from django.http import HttpResponse
from django.views.generic import DetailView
from karmaworld.apps.notes.forms import NoteForm
+
+def is_pdf(self):
+ _path = self.object.note_file.name
+ _, _extension = os.path.splitext(_path)
+ if _extension.lower() == '.pdf':
+ return True
+ return False
+
+def is_ppt(self):
+ if self.object.file_type == 'ppt':
+ return True
+ return False
+
+
class NoteDetailView(DetailView):
""" Class-based view for the note html page """
model = Note
context_object_name = u"note" # name passed to template
def get_context_data(self, **kwargs):
- """ add the hostname to the PDF embed for PDF.js
- from the Sites admin database record """
- kwargs = {
- 'hostname': Site.objects.get_current()
- }
+ """ Generate custom context for the page rendering a Note
+ + if pdf, set the `pdf` flag
+ """
+ # not current using these
+ #kwargs['hostname'] = Site.objects.get_current()
+
+ kwargs['pdf'] = is_pdf(self)
+ kwargs['ppt'] = is_ppt(self)
+
return super(NoteDetailView, self).get_context_data(**kwargs)
self.object.year = form.cleaned_data['year']
# use *arg expansion to pass tags a list of tags
self.object.tags.add(*form.cleaned_data['tags'])
+ # User has submitted this form, so set the SHOW flag
+ self.object.draft = False
self.object.save()
return super(NoteSaveView, self).form_valid(form)
context_object_name = u"note"
model = Note
-class PDFView(TemplateView):
- """ A testing view to render a PDF """
+
+class PDFView(DetailView):
+ """ Render PDF files in an iframe based on ID"""
template_name = u'partial/pdfembed.html'
+ model = Note
+
+ def get_context_data(self, **kwargs):
+ """ Generate a path to the pdf file associated with this note
+ by generating a path to the MEDIA_URL by hand """
+
+ if is_ppt(self):
+ kwargs['pdf_path'] = "{0}{1}".format(settings.MEDIA_URL,
+ os.path.basename(self.object.pdf_file.name))
+ elif is_pdf(self):
+ kwargs['pdf_path'] = "{0}{1}".format(settings.MEDIA_URL,
+ os.path.basename(self.object.note_file.name))
+ return super(PDFView, self).get_context_data(**kwargs)