示例#1
0
文件: api.py 项目: tawazz/ledger
    def get(self, request, format=None):
        try:
            http_status = status.HTTP_200_OK
            #parse and validate data
            report = None
            data = {
                "start": request.GET.get('start'),
                "end": request.GET.get('end'),
                "banked_start": request.GET.get('banked_start', None),
                "banked_end": request.GET.get('banked_end', None),
                "system": request.GET.get('system'),
                "items": request.GET.get('items', False),
                "region": request.GET.get('region'),
                "district": request.GET.get('district')
            }
            serializer = ReportSerializer(data=data)
            serializer.is_valid(raise_exception=True)
            filename = 'report-{}-{}'.format(
                str(serializer.validated_data['start']),
                str(serializer.validated_data['end']))
            # Generate Report
            if serializer.validated_data['items']:
                report = generate_items_csv(
                    systemid_check(serializer.validated_data['system']),
                    serializer.validated_data['start'],
                    serializer.validated_data['end'],
                    serializer.validated_data['banked_start'],
                    serializer.validated_data['banked_end'],
                    district=serializer.validated_data['district'])
            else:
                report = generate_trans_csv(
                    systemid_check(serializer.validated_data['system']),
                    serializer.validated_data['start'],
                    serializer.validated_data['end'],
                    district=serializer.validated_data['district'])
            if report:
                response = HttpResponse(FileWrapper(report),
                                        content_type='text/csv')
                response[
                    'Content-Disposition'] = 'attachment; filename={}.csv'.format(
                        filename)
                return response
            else:
                raise serializers.ValidationError('No report was generated.')
        except serializers.ValidationError:
            raise
        except Exception as e:
            traceback.print_exc()
            raise serializers.ValidationError(str(e))


#######################################################
#                                                     #
#                    /REPORTS                         #
#                                                     #
#######################################################
示例#2
0
    def get(self,request,format=None):
        try:
            http_status = status.HTTP_200_OK
            #parse and validate data
            report = None
            data = {
                "start":request.GET.get('start'),
                "end":request.GET.get('end'),
                "banked_start":request.GET.get('banked_start',None),
                "banked_end":request.GET.get('banked_end',None),
                "system":request.GET.get('system'),
                "items": request.GET.get('items', False),
                "region": request.GET.get('region'),
                "district": request.GET.get('district')
            }
            serializer = ReportSerializer(data=data)
            serializer.is_valid(raise_exception=True)
            filename = 'report-{}-{}'.format(str(serializer.validated_data['start']),str(serializer.validated_data['end']))
            # Generate Report
            if serializer.validated_data['items']:
                report = generate_items_csv(systemid_check(serializer.validated_data['system']),
                                            serializer.validated_data['start'],
                                            serializer.validated_data['end'],
                                            serializer.validated_data['banked_start'],
                                            serializer.validated_data['banked_end'])
            else:
                report = generate_trans_csv(systemid_check(serializer.validated_data['system'])
                                            ,serializer.validated_data['start'],
                                            serializer.validated_data['end'],
                                            district = serializer.validated_data['district'])
            if report:
                response = HttpResponse(FileWrapper(report), content_type='text/csv')
                response['Content-Disposition'] = 'attachment; filename={}.csv'.format(filename)
                return response
            else:
                raise serializers.ValidationError('No report was generated.')
        except serializers.ValidationError:
            raise
        except Exception as e:
            traceback.print_exc()
            raise serializers.ValidationError(str(e))
#######################################################
#                                                     #
#                    /REPORTS                         #
#                                                     #
#######################################################