Ejemplo n.º 1
0
 def get(self, *args, **kwargs):
     self.devices = []
     self.venture_id = self.request.GET.get('venture')
     self.export_all = self.request.GET.get('export-all', '') == 'csv'
     self.task_in_progress = False
     self.form = ReportVentureCost(
         initial={'venture': self.venture_id},
     )
     if self.request.GET.get('invalidate-cache', '') == 'true':
         self.invalidate_data(venture_id=self.venture_id)
         return HttpResponseRedirect(
             _get_after_report_invalidation_link(
                 self.request,
                 'invalidate-cache',
             ),
         )
     if self.venture_id:
         try:
             venture = Venture.objects.get(id=self.venture_id)
         except Venture.DoesNotExist:
             venture = None
     if any((
         self.venture_id and venture,
         not self.venture_id and self.export_all,
     )):
         self.devices = self.get_data(venture_id=self.venture_id)
         if self.devices is None:
             self.task_in_progress = True
             messages.info(
                 self.request,
                 "Report processing in progress. Please wait...",
             )
         if all((
             not self.task_in_progress,
             self.request.GET.get('export') == 'csv'
         )):
             filename = 'report_devices_prices_per_venture-%s-%s.csv' % (
                 venture.symbol, datetime.date.today(),
             )
             return make_csv_response(
                 data=self.get_csv_data(self.devices),
                 filename=filename,
             )
         if not self.task_in_progress and self.export_all:
             filename = 'report_devices_prices_per_venture-all-%s.csv' % (
                 datetime.date.today()
             )
             return make_csv_response(
                 data=self.get_csv_data(self.devices),
                 filename=filename,
             )
     return super(ReportDevicePricesPerVenture, self).get(*args, **kwargs)
Ejemplo n.º 2
0
 def get(self, *args, **kwargs):
     get = self.request.GET
     if get:
         self.form = self.Form(get)
         self.got_query = True
     else:
         self.form = self.Form()
     if self.form.is_valid():
         if 'clear' in get:
             self.progress = 0
             self.got_query = False
             self._clear_cache(**self.form.cleaned_data)
             messages.success(
                 self.request, "Cache cleared for this report.",
             )
         else:
             self.progress, self.header, self.data = self._get_cached(
                 **self.form.cleaned_data
             )
             if get.get('format', '').lower() == 'csv':
                 if self.progress == 100:
                     return make_csv_response(
                         itertools.chain([self.header], self.data),
                         '{}.csv'.format(self.section),
                     )
                 else:
                     messages.warning(
                         self.request,
                         "Please wait for the report "
                         "to finish calculating.",
                     )
     return super(Report, self).get(*args, **kwargs)
Ejemplo n.º 3
0
 def get(self, *args, **kwargs):
     get = self.request.GET
     if get:
         self.form = self.Form(get)
         self.got_query = True
     else:
         self.form = self.Form()
     if self.form.is_valid():
         if 'clear' in get:
             self.progress = 0
             self.got_query = False
             self._clear_cache(**self.form.cleaned_data)
             messages.success(
                 self.request,
                 "Cache cleared for this report.",
             )
         else:
             self.progress, self.header, self.data = self._get_cached(
                 **self.form.cleaned_data)
             if get.get('format', '').lower() == 'csv':
                 if self.progress == 100:
                     return make_csv_response(
                         itertools.chain([self.header], self.data),
                         '{}.csv'.format(self.section),
                     )
                 else:
                     messages.warning(
                         self.request,
                         "Please wait for the report "
                         "to finish calculating.",
                     )
     return super(Report, self).get(*args, **kwargs)
Ejemplo n.º 4
0
 def get(self, *args, **kwargs):
     self.devices = []
     self.venture_id = self.request.GET.get('venture')
     self.export_all = self.request.GET.get('export-all', '') == 'csv'
     self.task_in_progress = False
     self.form = ReportVentureCost(initial={'venture': self.venture_id}, )
     if self.request.GET.get('invalidate-cache', '') == 'true':
         self.invalidate_data(venture_id=self.venture_id)
         return HttpResponseRedirect(
             _get_after_report_invalidation_link(
                 self.request,
                 'invalidate-cache',
             ), )
     if self.venture_id:
         try:
             venture = Venture.objects.get(id=self.venture_id)
         except Venture.DoesNotExist:
             venture = None
     if any((
             self.venture_id and venture,
             not self.venture_id and self.export_all,
     )):
         self.devices = self.get_data(venture_id=self.venture_id)
         if self.devices is None:
             self.task_in_progress = True
             messages.info(
                 self.request,
                 "Report processing in progress. Please wait...",
             )
         if all((not self.task_in_progress,
                 self.request.GET.get('export') == 'csv')):
             filename = 'report_devices_prices_per_venture-%s-%s.csv' % (
                 venture.symbol,
                 datetime.date.today(),
             )
             return make_csv_response(
                 data=self.get_csv_data(self.devices),
                 filename=filename,
             )
         if not self.task_in_progress and self.export_all:
             filename = 'report_devices_prices_per_venture-all-%s.csv' % (
                 datetime.date.today())
             return make_csv_response(
                 data=self.get_csv_data(self.devices),
                 filename=filename,
             )
     return super(ReportDevicePricesPerVenture, self).get(*args, **kwargs)
Ejemplo n.º 5
0
    def get(self, *args, **kwargs):
        """
        If statement exist then set header and data of current statement.
        Generate csv from current statement.
        """
        self.init_args()
        if self.statement:
            self.header = json.loads(self.statement.header)
            self.data = json.loads(self.statement.data)

        if self.request.GET.get('format', '').lower() == 'csv':
            self.header = format_csv_header(self.header)
            return make_csv_response(
                itertools.chain(self.header, self.data),
                '{}.csv'.format(self.section),
            )
        return super(Statements, self).get(*args, **kwargs)
Ejemplo n.º 6
0
    def get(self, *args, **kwargs):
        """
        If statement exist then set header and data of current statement.
        Generate csv from current statement.
        """
        self.init_args()
        if self.statement:
            self.header = json.loads(self.statement.header)
            self.data = json.loads(self.statement.data)

        if self.request.GET.get('format', '').lower() == 'csv':
            self.header = format_csv_header(self.header)
            return make_csv_response(
                itertools.chain(self.header, self.data),
                '{}.csv'.format(self.section),
            )
        return super(Statements, self).get(*args, **kwargs)
Ejemplo n.º 7
0
    def get(self, *args, **kwargs):
        get = self.request.GET
        if get:
            self.form = self.Form(get)
            self.got_query = True
        else:
            self.form = self.Form(initial=self.initial)
        if self.form.is_valid():
            if 'clear' in get:
                self.progress = 0
                self.got_query = False
                self._clear_cache(**self.form.cleaned_data)
                messages.success(
                    self.request, "Cache cleared for this report.",
                )
            else:
                self.progress, result = self.run_on_worker(
                    **self.form.cleaned_data
                )
                if result:
                    self.header, self.data = result
                    self._format_header()

                self.progress = round(self.progress, 0)
                if self.progress == 100:
                    if get.get('format', '').lower() == 'csv':
                        self.header = format_csv_header(self.header)
                        return make_csv_response(
                            itertools.chain(self.header, self.data),
                            '{}.csv'.format(self.section),
                        )
                else:
                    messages.warning(
                        self.request,
                        _("Please wait for the report "
                          "to finish calculating."),
                    )
        return super(BaseReport, self).get(*args, **kwargs)
Ejemplo n.º 8
0
 def make_csv_response(self, data):
     return csvutil.make_csv_response(
         data=data, filename=self.csv_file_name)
Ejemplo n.º 9
0
    def get(self, *args, **kwargs):
        self.perm_edit = False

        request = self.request.GET
        csv_conf = {
            'name': 'report_devices',
            'url': None,
        }
        # Filtering of the cross
        self.form_choice = DevicesChoiceReportForm(request)
        queries = {Q()}
        headers = ['Name']
        dep = self.request.GET.get('deprecation')
        no_dep = self.request.GET.get('no_deprecation')
        no_mar = self.request.GET.get('no_margin')
        no_sup = self.request.GET.get('no_support')
        no_pur = self.request.GET.get('no_purchase')
        no_ven = self.request.GET.get('no_venture')
        no_rol = self.request.GET.get('no_role')
        no_par = self.request.GET.get('no_parent')
        rows = []
        if dep:
            headers.append('Depreciation date')
            queries.update({Q(deprecation_date__lte=datetime.date.today())})
        if no_dep:
            headers.append('No depreciation date')
            queries.update({Q(deprecation_date=None)})
        if no_mar:
            headers.append('No depreciation kind')
            queries.update({Q(deprecation_kind=None)})
        if no_sup:
            headers.append('No support')
            queries.update({Q(support_expiration_date=None)})
        if no_pur:
            headers.append('No purchase')
            queries.update({Q(purchase_date=None)})
        if no_ven:
            headers.append('No venture')
            queries.update({Q(venture=None)})
        if no_rol:
            headers.append('No venture role')
            queries.update({Q(venture_role=None)})
        if no_par:
            headers.append('No parent')
            queries.update({Q(parent=None) | Q(p_deleted=True)})
        if len(queries) > 1:
            devices = Device.objects.filter(*queries)
            for dev in devices:
                row = []
                row.append(self.get_name(dev.name, dev.id))
                if dep:
                    row.append(dev.deprecation_date)
                if no_dep:
                    row.append(dev.deprecation_date)
                if no_mar:
                    row.append(dev.deprecation_kind)
                if no_sup:
                    row.append(dev.support_expiration_date)
                if no_pur:
                    row.append(dev.purchase_date)
                if no_ven:
                    row.append(dev.venture)
                if no_rol:
                    row.append(dev.venture_role)
                rows.append(row)
        # Filtering of the range
        # Support range
        s_start = self.request.GET.get('s_start', None)
        s_end = self.request.GET.get('s_end', None)
        if s_start and s_end:
            self.form_support_range = SupportRangeReportForm(request)
            if self.form_support_range.is_valid():
                headers = ('Name', 'Support expiration date')
                rows.extend([(
                    self.get_name(dev.name, dev.id),
                    dev.support_expiration_date,
                ) for dev in Device.objects.filter(
                    support_expiration_date__gte=s_start,
                    support_expiration_date__lte=s_end,
                )])
        else:
            self.form_support_range = SupportRangeReportForm(
                initial={
                    's_start': datetime.date.today() -
                    datetime.timedelta(days=30),
                    's_end': datetime.date.today(),
                })
        # Deprecation range
        d_start = self.request.GET.get('d_start', None)
        d_end = self.request.GET.get('d_end', None)
        if d_start and d_end:
            self.form_deprecation_range = DeprecationRangeReportForm(request)
            if self.form_deprecation_range.is_valid():
                headers = ('Name', 'Depreciation date')
                rows.extend([(
                    self.get_name(dev.name, dev.id),
                    dev.deprecation_date,
                ) for dev in Device.objects.filter(
                    deprecation_date__gte=d_start,
                    deprecation_date__lte=d_end,
                )])
        else:
            self.form_deprecation_range = DeprecationRangeReportForm(
                initial={
                    'd_start': datetime.date.today() -
                    datetime.timedelta(days=30),
                    'd_end': datetime.date.today(),
                })
        # Warranty range
        w_start = self.request.GET.get('w_start', None)
        w_end = self.request.GET.get('w_end', None)
        if w_start and w_end:
            self.form_warranty_range = WarrantyRangeReportForm(request)
            if self.form_warranty_range.is_valid():
                headers = ('Name', 'Warranty expiration date')
                rows.extend([(
                    self.get_name(dev.name, dev.id),
                    dev.warranty_expiration_date,
                ) for dev in Device.objects.filter(
                    warranty_expiration_date__gte=w_start,
                    warranty_expiration_date__lte=w_end,
                )])
        else:
            self.form_warranty_range = WarrantyRangeReportForm(
                initial={
                    'w_start': datetime.date.today() -
                    datetime.timedelta(days=30),
                    'w_end': datetime.date.today(),
                })
        # Show devices active or / and deleted
        self.device_list = ReportDeviceListForm(request)
        all_devices = request.get('show_all_devices')
        all_deleted_devices = request.get('show_all_deleted_devices')
        if all_devices or all_deleted_devices:
            show_devices = None
            if all_devices and all_deleted_devices:
                show_devices = Device.admin_objects.all()
                csv_conf = {
                    'title':
                    'All devices (active and deleted)',
                    'name':
                    'report_all_devices',
                    'url':
                    '?show_all_devices=on&show_all_deleted_devices=on&export=csv',
                }
            elif all_devices:
                show_devices = Device.objects.all()
                csv_conf = {
                    'title': 'All active devices',
                    'name': 'report_all_active_devices',
                    'url': '?show_all_devices=on&export=csv',
                }
            elif all_deleted_devices:
                show_devices = Device.admin_objects.filter(deleted=True)
                csv_conf = {
                    'title': 'All deleted devices',
                    'name': 'report_deleted_devices',
                    'url': '?show_all_deleted_devices=on&export=csv',
                }
            headers = [
                'Device',
                'Model',
                'SN',
                'Barcode',
                'Auto price',
                'Venture',
                'Role',
                'Remarks',
                'Verified',
                'Deleted',
            ]
            for dev in show_devices:
                rows.append([
                    dev.name,
                    dev.model,
                    dev.sn,
                    dev.barcode,
                    dev.cached_price,
                    dev.venture,
                    dev.role,
                    dev.remarks,
                    dev.verified,
                    dev.deleted,
                ])
        if request.get('export') == 'csv':
            rows.insert(0, headers)
            return make_csv_response(
                data=rows,
                filename=csv_conf.get('name'),
            )
        self.headers = headers
        self.rows = rows
        self.csv_url = csv_conf.get('url')
        self.title = csv_conf.get('title')
        return super(ReportDevices, self).get(*args, **kwargs)
Ejemplo n.º 10
0
 def get(self, *args, **kwargs):
     self.venture_data = []
     self.task_in_progress = False
     profile = self.request.user.get_profile()
     if 'start' in self.request.GET:
         self.form = DateRangeForm(self.request.GET)
     else:
         self.form = DateRangeForm(
             initial={
                 'start': datetime.date.today() -
                 datetime.timedelta(days=30),
                 'end': datetime.date.today(),
             })
     self.extra_types = list(
         VentureExtraCostType.objects.annotate(
             cost_count=db.Count('ventureextracost')).filter(
                 cost_count__gt=0).order_by('name'))
     if self.form.is_valid():
         self.ventures = profile.perm_ventures(
             Perm.read_device_info_reports).filter(
                 db.Q(parent=None) | db.Q(parent__parent=None),
                 show_in_ralph=True).values_list(
                     'id',
                     flat=True,
                 ).order_by('path')
         start = self.form.cleaned_data['start']
         end = self.form.cleaned_data['end']
         if self.request.GET.get('invalidate-cache') == 'true':
             self.invalidate_data(
                 start,
                 end,
                 self.ventures,
                 self.extra_types,
             )
             return HttpResponseRedirect(
                 _get_after_report_invalidation_link(
                     self.request,
                     'invalidate-cache',
                 ), )
         self.venture_data = self.get_data(
             start,
             end,
             self.ventures,
             self.extra_types,
         )
         if self.venture_data is None:
             self.task_in_progress = True
             messages.info(
                 self.request,
                 "Report processing in progress. Please wait...",
             )
     else:
         self.ventures = Venture.objects.none()
         self.venture_data = []
     if (self.request.GET.get('export') == 'csv'
             and self.venture_data is not None):
         return make_csv_response(
             data=self.export_csv(self.venture_data, self.extra_types),
             filename='ReportVentures.csv',
         )
     return super(ReportVentures, self).get(*args, **kwargs)
Ejemplo n.º 11
0
 def do_csv_export(self, queryset):
     return csvutil.make_csv_response(
             data=self.get_csv_data(queryset),
             filename=self.csv_file_name,
         )
Ejemplo n.º 12
0
 def get_response(self, request, result):
     return csvutil.make_csv_response(data=result, filename=self.filename)
Ejemplo n.º 13
0
 def make_csv_response(self, data):
     return csvutil.make_csv_response(data=data,
                                      filename=self.csv_file_name)
Ejemplo n.º 14
0
 def get(self, *args, **kwargs):
     self.venture_data = []
     self.task_in_progress = False
     profile = self.request.user.get_profile()
     if 'start' in self.request.GET:
         self.form = DateRangeForm(self.request.GET)
     else:
         self.form = DateRangeForm(initial={
             'start': datetime.date.today() - datetime.timedelta(days=30),
             'end': datetime.date.today(),
         })
     self.extra_types = list(VentureExtraCostType.objects.annotate(
         cost_count=db.Count('ventureextracost')
     ).filter(cost_count__gt=0).order_by('name'))
     if self.form.is_valid():
         self.ventures = profile.perm_ventures(
             Perm.read_device_info_reports
         ).filter(
             db.Q(parent=None) | db.Q(parent__parent=None),
             show_in_ralph=True
         ).values_list(
             'id',
             flat=True,
         ).order_by('path')
         start = self.form.cleaned_data['start']
         end = self.form.cleaned_data['end']
         if self.request.GET.get('invalidate-cache') == 'true':
             self.invalidate_data(
                 start,
                 end,
                 self.ventures,
                 self.extra_types,
             )
             return HttpResponseRedirect(
                 _get_after_report_invalidation_link(
                     self.request,
                     'invalidate-cache',
                 ),
             )
         self.venture_data = self.get_data(
             start,
             end,
             self.ventures,
             self.extra_types,
         )
         if self.venture_data is None:
             self.task_in_progress = True
             messages.info(
                 self.request,
                 "Report processing in progress. Please wait...",
             )
     else:
         self.ventures = Venture.objects.none()
         self.venture_data = []
     if (self.request.GET.get('export') == 'csv' and
         self.venture_data is not None):
         return make_csv_response(
             data=self.export_csv(self.venture_data, self.extra_types),
             filename='ReportVentures.csv',
         )
     return super(ReportVentures, self).get(*args, **kwargs)
Ejemplo n.º 15
0
 def do_csv_export(self, queryset):
     return csvutil.make_csv_response(data=self.get_csv_data(queryset), filename=self.csv_file_name)
Ejemplo n.º 16
0
 def get_response(self, request, result):
     return csvutil.make_csv_response(data=result, filename=self.filename)
Ejemplo n.º 17
0
    def get(self, *args, **kwargs):
        self.perm_edit = False

        request = self.request.GET
        csv_conf = {
            'name': 'report_devices',
            'url': None,
        }
        # Filtering of the cross
        self.form_choice = DevicesChoiceReportForm(request)
        queries = {Q()}
        headers = ['Name']
        dep = self.request.GET.get('deprecation')
        no_dep = self.request.GET.get('no_deprecation')
        no_mar = self.request.GET.get('no_margin')
        no_sup = self.request.GET.get('no_support')
        no_pur = self.request.GET.get('no_purchase')
        no_ven = self.request.GET.get('no_venture')
        no_rol = self.request.GET.get('no_role')
        no_par = self.request.GET.get('no_parent')
        rows = []
        if dep:
            headers.append('Depreciation date')
            queries.update({Q(deprecation_date__lte=datetime.date.today())})
        if no_dep:
            headers.append('No depreciation date')
            queries.update({Q(deprecation_date=None)})
        if no_mar:
            headers.append('No depreciation kind')
            queries.update({Q(deprecation_kind=None)})
        if no_sup:
            headers.append('No support')
            queries.update({Q(support_expiration_date=None)})
        if no_pur:
            headers.append('No purchase')
            queries.update({Q(purchase_date=None)})
        if no_ven:
            headers.append('No venture')
            queries.update({Q(venture=None)})
        if no_rol:
            headers.append('No venture role')
            queries.update({Q(venture_role=None)})
        if no_par:
            headers.append('No parent')
            queries.update({Q(parent=None) | Q(p_deleted=True)})
        if len(queries) > 1:
            devices = Device.objects.filter(*queries)
            for dev in devices:
                row = []
                row.append(self.get_name(dev.name, dev.id))
                if dep:
                    row.append(dev.deprecation_date)
                if no_dep:
                    row.append(dev.deprecation_date)
                if no_mar:
                    row.append(dev.deprecation_kind)
                if no_sup:
                    row.append(dev.support_expiration_date)
                if no_pur:
                    row.append(dev.purchase_date)
                if no_ven:
                    row.append(dev.venture)
                if no_rol:
                    row.append(dev.venture_role)
                rows.append(row)
        # Filtering of the range
        # Support range
        s_start = self.request.GET.get('s_start', None)
        s_end = self.request.GET.get('s_end', None)
        if s_start and s_end:
            self.form_support_range = SupportRangeReportForm(request)
            if self.form_support_range.is_valid():
                headers = ('Name', 'Support expiration date')
                rows.extend([
                    (
                        self.get_name(dev.name, dev.id),
                        dev.support_expiration_date,
                    ) for dev in Device.objects.filter(
                        support_expiration_date__gte=s_start,
                        support_expiration_date__lte=s_end,
                    )
                ])
        else:
            self.form_support_range = SupportRangeReportForm(initial={
                's_start': datetime.date.today() - datetime.timedelta(days=30),
                's_end': datetime.date.today(),
            })
        # Deprecation range
        d_start = self.request.GET.get('d_start', None)
        d_end = self.request.GET.get('d_end', None)
        if d_start and d_end:
            self.form_deprecation_range = DeprecationRangeReportForm(request)
            if self.form_deprecation_range.is_valid():
                headers = ('Name', 'Depreciation date')
                rows.extend([
                    (
                        self.get_name(dev.name, dev.id),
                        dev.deprecation_date,
                    ) for dev in Device.objects.filter(
                        deprecation_date__gte=d_start,
                        deprecation_date__lte=d_end,
                    )
                ])
        else:
            self.form_deprecation_range = DeprecationRangeReportForm(initial={
                'd_start': datetime.date.today() - datetime.timedelta(days=30),
                'd_end': datetime.date.today(),
            })
        # Warranty range
        w_start = self.request.GET.get('w_start', None)
        w_end = self.request.GET.get('w_end', None)
        if w_start and w_end:
            self.form_warranty_range = WarrantyRangeReportForm(request)
            if self.form_warranty_range.is_valid():
                headers = ('Name', 'Warranty expiration date')
                rows.extend([
                    (
                        self.get_name(dev.name, dev.id),
                        dev.warranty_expiration_date,
                    ) for dev in Device.objects.filter(
                        warranty_expiration_date__gte=w_start,
                        warranty_expiration_date__lte=w_end,
                    )
                ])
        else:
            self.form_warranty_range = WarrantyRangeReportForm(initial={
                'w_start': datetime.date.today() - datetime.timedelta(days=30),
                'w_end': datetime.date.today(),
            })
        # Show devices active or / and deleted
        self.device_list = ReportDeviceListForm(request)
        all_devices = request.get('show_all_devices')
        all_deleted_devices = request.get('show_all_deleted_devices')
        if all_devices or all_deleted_devices:
            show_devices = None
            if all_devices and all_deleted_devices:
                show_devices = Device.admin_objects.all()
                csv_conf = {
                    'title': 'All devices (active and deleted)',
                    'name': 'report_all_devices',
                    'url': '?show_all_devices=on&show_all_deleted_devices=on&export=csv',
                    }
            elif all_devices:
                show_devices = Device.objects.all()
                csv_conf = {
                    'title': 'All active devices',
                    'name': 'report_all_active_devices',
                    'url': '?show_all_devices=on&export=csv',
                    }
            elif all_deleted_devices:
                show_devices = Device.admin_objects.filter(deleted=True)
                csv_conf = {
                    'title': 'All deleted devices',
                    'name': 'report_deleted_devices',
                    'url': '?show_all_deleted_devices=on&export=csv',
                    }
            headers = [
                'Device', 'Model', 'SN', 'Barcode', 'Auto price', 'Venture',
                'Role', 'Remarks', 'Verified', 'Deleted',
            ]
            for dev in show_devices:
                rows.append([
                    dev.name,
                    dev.model,
                    dev.sn,
                    dev.barcode,
                    dev.cached_price,
                    dev.venture,
                    dev.role,
                    dev.remarks,
                    dev.verified,
                    dev.deleted,
                ])
        if request.get('export') == 'csv':
            rows.insert(0, headers)
            return make_csv_response(
                data=rows,
                filename=csv_conf.get('name'),
            )
        self.headers = headers
        self.rows = rows
        self.csv_url = csv_conf.get('url')
        self.title = csv_conf.get('title')
        return super(ReportDevices, self).get(*args, **kwargs)