<s> from io import BytesIO <EOL> from django . core . exceptions import PermissionDenied <EOL> from django . http import HttpResponse <EOL> from django . utils . translation import ugettext_lazy <EOL> from django . views import generic as django_views <EOL> from django . views . decorators . csrf import ensure_csrf_cookie <EOL> from reportlab . lib . units import cm <EOL> from reportlab . platypus import SimpleDocTemplate , Spacer <EOL> from rest_framework . response import Response <EOL> from rest_framework . views import APIView as _APIView <EOL> from . pdf import firstPage , laterPages <EOL> View = django_views . View <EOL> class SingleObjectMixin ( django_views . detail . SingleObjectMixin ) : <EOL> """<STR_LIT>""" <EOL> def dispatch ( self , * args , ** kwargs ) : <EOL> if not hasattr ( self , '<STR_LIT:object>' ) : <EOL> self . object = self . get_object ( ) <EOL> return super ( ) . dispatch ( * args , ** kwargs ) <EOL> def get_object ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> obj = self . _object <EOL> except AttributeError : <EOL> obj = super ( ) . get_object ( * args , ** kwargs ) <EOL> self . _object = obj <EOL> return obj <EOL> class CSRFMixin : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def as_view ( cls , * args , ** kwargs ) : <EOL> view = super ( ) . as_view ( * args , ** kwargs ) <EOL> return ensure_csrf_cookie ( view ) <EOL> class PDFView ( View ) : <EOL> """<STR_LIT>""" <EOL> filename = ugettext_lazy ( '<STR_LIT>' ) <EOL> top_space = <NUM_LIT:3> <EOL> document_title = None <EOL> required_permission = None <EOL> def check_permission ( self , request , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if self . required_permission is None : <EOL> return True <EOL> else : <EOL> return request . user . has_perm ( self . required_permission ) <EOL> def dispatch ( self , request , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if not self . check_permission ( request , * args , ** kwargs ) : <EOL> raise PermissionDenied <EOL> return super ( ) . dispatch ( request , * args , ** kwargs ) <EOL> def get_top_space ( self ) : <EOL> return self . top_space <EOL> def get_document_title ( self ) : <EOL> if self . document_title : <EOL> return str ( self . document_title ) <EOL> else : <EOL> return '<STR_LIT>' <EOL> def get_filename ( self ) : <EOL> return self . filename <EOL> def get_template ( self , buffer ) : <EOL> return SimpleDocTemplate ( buffer ) <EOL> def build_document ( self , pdf_document , story ) : <EOL> pdf_document . build ( <EOL> story , onFirstPage = firstPage , onLaterPages = laterPages ) <EOL> def render_to_response ( self , filename ) : <EOL> response = HttpResponse ( content_type = '<STR_LIT>' ) <EOL> filename = '<STR_LIT>' % self . get_filename ( ) <EOL> response [ '<STR_LIT>' ] = filename . encode ( '<STR_LIT:utf-8>' ) <EOL> buffer = BytesIO ( ) <EOL> pdf_document = self . get_template ( buffer ) <EOL> pdf_document . title = self . get_document_title ( ) <EOL> story = [ Spacer ( <NUM_LIT:1> , self . get_top_space ( ) * cm ) ] <EOL> self . append_to_pdf ( story ) <EOL> self . build_document ( pdf_document , story ) <EOL> pdf = buffer . getvalue ( ) <EOL> buffer . close ( ) <EOL> response . write ( pdf ) <EOL> return response <EOL> def get ( self , request , * args , ** kwargs ) : <EOL> return self . render_to_response ( self . get_filename ( ) ) <EOL> class APIView ( _APIView ) : <EOL> """<STR_LIT>""" <EOL> http_method_names = [ ] <EOL> """<STR_LIT>""" <EOL> def get_context_data ( self , ** context ) : <EOL> """<STR_LIT>""" <EOL> return context <EOL> def method_call ( self , request , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return Response ( self . get_context_data ( ) ) <EOL> get = post = put = patch = delete = head = options = trace = method_call <EOL> del method_call </s>
