<s> from __future__ import absolute_import <EOL> from django . http import HttpResponse <EOL> from django . template . response import TemplateResponse <EOL> from django . views . generic import TemplateView <EOL> from . utils import ( content_disposition_filename , render_pdf_from_template ) <EOL> class PDFResponse ( HttpResponse ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , content , status = <NUM_LIT:200> , content_type = None , <EOL> filename = None , show_content_in_browser = None , * args , ** kwargs ) : <EOL> if content_type is None : <EOL> content_type = '<STR_LIT>' <EOL> super ( PDFResponse , self ) . __init__ ( content = content , <EOL> status = status , <EOL> content_type = content_type ) <EOL> self . set_filename ( filename , show_content_in_browser ) <EOL> def set_filename ( self , filename , show_content_in_browser ) : <EOL> self . filename = filename <EOL> if filename : <EOL> fileheader = '<STR_LIT>' <EOL> if show_content_in_browser : <EOL> fileheader = '<STR_LIT>' <EOL> filename = content_disposition_filename ( filename ) <EOL> header_content = fileheader . format ( filename ) <EOL> self [ '<STR_LIT>' ] = header_content <EOL> else : <EOL> del self [ '<STR_LIT>' ] <EOL> class PDFTemplateResponse ( TemplateResponse , PDFResponse ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , request , template , context = None , <EOL> status = None , content_type = None , current_app = None , <EOL> filename = None , show_content_in_browser = None , <EOL> header_template = None , footer_template = None , <EOL> cmd_options = None , * args , ** kwargs ) : <EOL> super ( PDFTemplateResponse , self ) . __init__ ( request = request , <EOL> template = template , <EOL> context = context , <EOL> status = status , <EOL> content_type = content_type , <EOL> current_app = None , <EOL> * args , ** kwargs ) <EOL> self . set_filename ( filename , show_content_in_browser ) <EOL> self . header_template = header_template <EOL> self . footer_template = footer_template <EOL> if cmd_options is None : <EOL> cmd_options = { } <EOL> self . cmd_options = cmd_options <EOL> @ property <EOL> def rendered_content ( self ) : <EOL> """<STR_LIT>""" <EOL> cmd_options = self . cmd_options . copy ( ) <EOL> return render_pdf_from_template ( <EOL> self . resolve_template ( self . template_name ) , <EOL> self . resolve_template ( self . header_template ) , <EOL> self . resolve_template ( self . footer_template ) , <EOL> context = self . resolve_context ( self . context_data ) , <EOL> request = self . _request , <EOL> cmd_options = cmd_options <EOL> ) <EOL> class PDFTemplateView ( TemplateView ) : <EOL> """<STR_LIT>""" <EOL> filename = '<STR_LIT>' <EOL> show_content_in_browser = False <EOL> template_name = None <EOL> header_template = None <EOL> footer_template = None <EOL> response_class = PDFTemplateResponse <EOL> html_response_class = TemplateResponse <EOL> cmd_options = { <EOL> } <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( PDFTemplateView , self ) . __init__ ( * args , ** kwargs ) <EOL> self . cmd_options = self . cmd_options . copy ( ) <EOL> def get ( self , request , * args , ** kwargs ) : <EOL> response_class = self . response_class <EOL> try : <EOL> if request . GET . get ( '<STR_LIT>' , '<STR_LIT>' ) == '<STR_LIT:html>' : <EOL> self . response_class = self . html_response_class <EOL> return super ( PDFTemplateView , self ) . get ( request , <EOL> * args , ** kwargs ) <EOL> finally : <EOL> self . response_class = response_class <EOL> def get_filename ( self ) : <EOL> return self . filename <EOL> def get_cmd_options ( self ) : <EOL> return self . cmd_options <EOL> def render_to_response ( self , context , ** response_kwargs ) : <EOL> """<STR_LIT>""" <EOL> filename = response_kwargs . pop ( '<STR_LIT:filename>' , None ) <EOL> cmd_options = response_kwargs . pop ( '<STR_LIT>' , None ) <EOL> if issubclass ( self . response_class , PDFTemplateResponse ) : <EOL> if filename is None : <EOL> filename = self . get_filename ( ) <EOL> if cmd_options is None : <EOL> cmd_options = self . get_cmd_options ( ) <EOL> return super ( PDFTemplateView , self ) . render_to_response ( <EOL> context = context , filename = filename , <EOL> show_content_in_browser = self . show_content_in_browser , <EOL> header_template = self . header_template , <EOL> footer_template = self . footer_template , <EOL> cmd_options = cmd_options , <EOL> ** response_kwargs <EOL> ) <EOL> else : <EOL> return super ( PDFTemplateView , self ) . render_to_response ( <EOL> context = context , <EOL> ** response_kwargs <EOL> ) </s>
