<s> import datetime <EOL> import re <EOL> import copy <EOL> import json <EOL> from django . contrib . contenttypes . models import ContentType <EOL> from django . conf import settings <EOL> from django . core . mail import send_mail , EmailMultiAlternatives <EOL> from django . core . files . base import ContentFile <EOL> from django . contrib . admin . views . decorators import staff_member_required <EOL> from django . contrib . auth import get_user_model <EOL> from django . template . loader import get_template <EOL> from django . template import Context <EOL> from django . http import HttpResponse <EOL> from django . shortcuts import redirect , get_object_or_404 <EOL> from django . utils . decorators import method_decorator <EOL> from django . views . generic import TemplateView , View <EOL> from six import string_types <EOL> from . utils import duplicate <EOL> from . models import Report <EOL> from report_utils . mixins import DataExportMixin , generate_filename <EOL> User = get_user_model ( ) <EOL> class ReportSPAView ( TemplateView ) : <EOL> template_name = "<STR_LIT>" <EOL> def get_context_data ( self , ** kwargs ) : <EOL> context = super ( ReportSPAView , self ) . get_context_data ( ** kwargs ) <EOL> context [ '<STR_LIT>' ] = getattr ( <EOL> settings , '<STR_LIT>' , False <EOL> ) <EOL> return context <EOL> def fieldset_string_to_field ( fieldset_dict , model ) : <EOL> if isinstance ( fieldset_dict [ '<STR_LIT>' ] , tuple ) : <EOL> fieldset_dict [ '<STR_LIT>' ] = list ( fieldset_dict [ '<STR_LIT>' ] ) <EOL> i = <NUM_LIT:0> <EOL> for dict_field in fieldset_dict [ '<STR_LIT>' ] : <EOL> if isinstance ( dict_field , string_types ) : <EOL> fieldset_dict [ '<STR_LIT>' ] [ i ] = model . _meta . get_field_by_name ( <EOL> dict_field ) [ <NUM_LIT:0> ] <EOL> elif isinstance ( dict_field , list ) or isinstance ( dict_field , tuple ) : <EOL> dict_field [ <NUM_LIT:1> ] [ '<STR_LIT>' ] = True <EOL> fieldset_string_to_field ( dict_field [ <NUM_LIT:1> ] , model ) <EOL> i += <NUM_LIT:1> <EOL> def get_fieldsets ( model ) : <EOL> """<STR_LIT>""" <EOL> fieldsets = getattr ( model , '<STR_LIT>' , None ) <EOL> if fieldsets : <EOL> for fieldset_name , fieldset_dict in model . report_builder_fieldsets : <EOL> fieldset_string_to_field ( fieldset_dict , model ) <EOL> return fieldsets <EOL> def email_report ( report_url , user ) : <EOL> if ( ( getattr ( settings , '<STR_LIT>' , False ) or <EOL> getattr ( settings , '<STR_LIT>' , False ) ) and <EOL> getattr ( settings , '<STR_LIT>' , False ) ) : <EOL> if get_template ( '<STR_LIT>' ) : <EOL> email_template = get_template ( '<STR_LIT>' ) <EOL> msg = EmailMultiAlternatives ( <EOL> getattr ( settings , '<STR_LIT>' , False ) or <EOL> '<STR_LIT>' , <EOL> report_url , <EOL> getattr ( settings , '<STR_LIT>' ) , <EOL> [ user . email ] , <EOL> ) <EOL> htmlParameters = { <EOL> '<STR_LIT:name>' : user . first_name or user . username , <EOL> '<STR_LIT>' : report_url , <EOL> } <EOL> msg . attach_alternative ( <EOL> email_template . render ( Context ( htmlParameters ) ) , <EOL> "<STR_LIT>" <EOL> ) <EOL> msg . send ( ) <EOL> else : <EOL> send_mail ( <EOL> getattr ( settings , '<STR_LIT>' , False ) or <EOL> '<STR_LIT>' , <EOL> str ( report_url ) , <EOL> getattr ( settings , '<STR_LIT>' ) , <EOL> [ user . email ] , <EOL> fail_silently = True , <EOL> ) <EOL> class DownloadFileView ( DataExportMixin , View ) : <EOL> @ method_decorator ( staff_member_required ) <EOL> def dispatch ( self , * args , ** kwargs ) : <EOL> return super ( DownloadFileView , self ) . dispatch ( * args , ** kwargs ) <EOL> def process_report ( self , report_id , user_id , <EOL> file_type , to_response , queryset = None ) : <EOL> report = get_object_or_404 ( Report , pk = report_id ) <EOL> user = User . objects . get ( pk = user_id ) <EOL> if not queryset : <EOL> queryset = report . get_query ( ) <EOL> display_fields = report . get_good_display_fields ( ) <EOL> objects_list , message = self . report_to_list ( <EOL> queryset , <EOL> display_fields , <EOL> user , <EOL> preview = False , ) <EOL> title = re . sub ( r'<STR_LIT>' , '<STR_LIT>' , report . name ) [ : <NUM_LIT:30> ] <EOL> header = [ ] <EOL> widths = [ ] <EOL> for field in display_fields : <EOL> header . append ( field . name ) <EOL> widths . append ( field . width ) <EOL> if to_response : <EOL> if file_type == '<STR_LIT>' : <EOL> return self . list_to_csv_response ( <EOL> objects_list , title , header , widths ) <EOL> else : <EOL> return self . list_to_xlsx_response ( <EOL> objects_list , title , header , widths ) <EOL> else : <EOL> self . async_report_save ( report , objects_list , <EOL> title , header , widths , user , file_type ) <EOL> def async_report_save ( self , report , objects_list , <EOL> title , header , widths , user , file_type ) : <EOL> if file_type == '<STR_LIT>' : <EOL> csv_file = self . list_to_csv_file ( objects_list , title , <EOL> header , widths ) <EOL> title = generate_filename ( title , '<STR_LIT>' ) <EOL> report . report_file . save ( title , ContentFile ( csv_file . getvalue ( ) ) ) <EOL> else : <EOL> xlsx_file = self . list_to_xlsx_file ( objects_list , title , <EOL> header , widths ) <EOL> title = generate_filename ( title , '<STR_LIT>' ) <EOL> report . report_file . save ( title , ContentFile ( xlsx_file . getvalue ( ) ) ) <EOL> report . report_file_creation = datetime . datetime . today ( ) <EOL> report . save ( ) <EOL> if getattr ( settings , '<STR_LIT>' , False ) : <EOL> if user . email : <EOL> email_report ( report . report_file . url , user ) <EOL> def get ( self , request , * args , ** kwargs ) : <EOL> report_id = kwargs [ '<STR_LIT>' ] <EOL> file_type = kwargs . get ( '<STR_LIT>' ) <EOL> if getattr ( settings , '<STR_LIT>' , False ) : <EOL> from . tasks import report_builder_file_async_report_save <EOL> report_task = report_builder_file_async_report_save . delay ( <EOL> report_id , request . user . pk , file_type ) <EOL> task_id = report_task . task_id <EOL> return HttpResponse ( <EOL> json . dumps ( { '<STR_LIT>' : task_id } ) , <EOL> content_type = "<STR_LIT:application/json>" ) <EOL> else : <EOL> return self . process_report ( <EOL> report_id , request . user . pk , file_type , to_response = True ) <EOL> @ staff_member_required <EOL> def ajax_add_star ( request , pk ) : <EOL> """<STR_LIT>""" <EOL> report = get_object_or_404 ( Report , pk = pk ) <EOL> user = request . user <EOL> if user in report . starred . all ( ) : <EOL> added = False <EOL> report . starred . remove ( request . user ) <EOL> else : <EOL> added = True <EOL> report . starred . add ( request . user ) <EOL> return HttpResponse ( added ) <EOL> @ staff_member_required <EOL> def create_copy ( request , pk ) : <EOL> """<STR_LIT>""" <EOL> report = get_object_or_404 ( Report , pk = pk ) <EOL> new_report = duplicate ( report , changes = ( <EOL> ( '<STR_LIT:name>' , '<STR_LIT>' . format ( report . name ) ) , <EOL> ( '<STR_LIT>' , request . user ) , <EOL> ( '<STR_LIT>' , request . user ) , <EOL> ) ) <EOL> for display in report . displayfield_set . all ( ) : <EOL> new_display = copy . copy ( display ) <EOL> new_display . pk = None <EOL> new_display . report = new_report <EOL> new_display . save ( ) <EOL> for report_filter in report . filterfield_set . all ( ) : <EOL> new_filter = copy . copy ( report_filter ) <EOL> new_filter . pk = None <EOL> new_filter . report = new_report <EOL> new_filter . save ( ) <EOL> return redirect ( new_report ) <EOL> class ExportToReport ( DownloadFileView , TemplateView ) : <EOL> """<STR_LIT>""" <EOL> template_name = "<STR_LIT>" <EOL> def get_context_data ( self , ** kwargs ) : <EOL> ctx = super ( ExportToReport , self ) . get_context_data ( ** kwargs ) <EOL> ctx [ '<STR_LIT>' ] = self . request . GET . get ( '<STR_LIT>' , '<STR_LIT:/>' ) <EOL> ct = ContentType . objects . get_for_id ( self . request . GET [ '<STR_LIT>' ] ) <EOL> ids = self . request . GET [ '<STR_LIT>' ] . split ( '<STR_LIT:U+002C>' ) <EOL> ctx [ '<STR_LIT>' ] = "<STR_LIT:U+002C>" . join ( map ( str , ids ) ) <EOL> ctx [ '<STR_LIT>' ] = ct . id <EOL> ctx [ '<STR_LIT>' ] = len ( ids ) <EOL> ctx [ '<STR_LIT>' ] = Report . objects . filter ( <EOL> root_model = ct ) . order_by ( '<STR_LIT>' ) <EOL> ctx [ '<STR_LIT>' ] = ct . model_class ( ) . _meta . verbose_name <EOL> return ctx <EOL> def get ( self , request , * args , ** kwargs ) : <EOL> if '<STR_LIT>' in request . GET : <EOL> ct = ContentType . objects . get_for_id ( request . GET [ '<STR_LIT>' ] ) <EOL> ids = self . request . GET [ '<STR_LIT>' ] . split ( '<STR_LIT:U+002C>' ) <EOL> report = get_object_or_404 ( Report , pk = request . GET [ '<STR_LIT>' ] ) <EOL> queryset = ct . model_class ( ) . objects . filter ( pk__in = ids ) <EOL> return self . process_report ( <EOL> report . id , request . user . pk , <EOL> to_response = True , <EOL> queryset = queryset , <EOL> file_type = "<STR_LIT>" , <EOL> ) <EOL> context = self . get_context_data ( ** kwargs ) <EOL> return self . render_to_response ( context ) <EOL> @ staff_member_required <EOL> def check_status ( request , pk , task_id ) : <EOL> """<STR_LIT>""" <EOL> from celery . result import AsyncResult <EOL> res = AsyncResult ( task_id ) <EOL> link = '<STR_LIT>' <EOL> if res . state == '<STR_LIT>' : <EOL> report = get_object_or_404 ( Report , pk = pk ) <EOL> link = report . report_file . url <EOL> return HttpResponse ( <EOL> json . dumps ( { <EOL> '<STR_LIT:state>' : res . state , <EOL> '<STR_LIT>' : link , <EOL> '<STR_LIT:email>' : getattr ( <EOL> settings , <EOL> '<STR_LIT>' , <EOL> False <EOL> ) <EOL> } ) , <EOL> content_type = "<STR_LIT:application/json>" ) </s>
