Ejemplo n.º 1
0
def create_report_photo(request):
    '''This method is used to create citizens reports. Validation included.'''
    #Test the submit content size (max 2MB)

    if (int(request.META.get('CONTENT_LENGTH')) > 15000000):
        return HttpResponseBadRequest(json.dumps({"error_key": "ERROR_REPORT_FILE_EXCEED_SIZE", "request":request.POST}),content_type='application/json')

    data_report_id = request.POST.get('report_id')

    # Check if the photo is base64 encoded
    if request.POST.get('base64', False):
        image_data_base64 = request.POST.get('report_file')

        # Extract content and metadata from base64 info
        metadata, file_base64 = image_data_base64.split(';base64,')
        metadata = metadata.replace("data:", "")

        # Extract name and type from metadata
        data_file_type, data_file_name = metadata.split(',')

        # Set (meta)data to file object
        data_file_content = ContentFile(file_base64.decode('base64'), name=data_file_name)
        data_file_content.content_type = data_file_type
    else:
        data_file_content = request.FILES.get('report_file')

    #Verify that everything has been posted to create a citizen report.
    if (data_report_id == None):
        return HttpResponseBadRequest(json.dumps({"error_key": "ERROR_REPORT_FILE_MISSING_DATA_REPORT_ID", "request":request.POST}),content_type='application/json')
    if (data_file_content == None):
        return HttpResponseBadRequest(json.dumps({"error_key": "ERROR_REPORT_FILE_MISSING_DATA_REPORT_FILE", "request":request.POST}),content_type='application/json')

    try:
        #Retrieve the report
        reference_report = Report.objects.get(id=data_report_id)
    except Exception:
        return HttpResponseBadRequest(json.dumps({"error_key": "ERROR_REPORT_FILE_NOT_FOUND", "request":request.POST}),content_type='application/json')

    report_file = ReportFile()
    try:
        report_file.title  = "Mobile Upload"
        report_file.file   = data_file_content
        report_file.report = reference_report
        report_file.file_creation_date = datetime.now()

        # Set same creator than report creator
        report_file.created_by = reference_report.citizen or reference_report.created_by

        #Save given data
        report_file.is_new_report = True
        report_file.save()
    except Exception:
        return HttpResponseBadRequest(json.dumps({"error_key": "ERROR_REPORT_FILE_PROBLEM_DATA", "request":request.POST}),content_type='application/json')

    #Return the report ID
    return JsonHttpResponse({
        'report_photo': report_file.id
    })
Ejemplo n.º 2
0
	def saveFiles(cls, session, report):
		if 'files' in session:
			for f in session['files']:
				ftype = ReportFile.PDF
				if str(f['file']).endswith("pdf"):
					ftype = ReportFile.PDF
				if str(f['file']).endswith("doc"):
					ftype = ReportFile.WORD
				if str(f['file']).endswith("png") or str(f['file']).endswith("jpg") or str(f['file']).endswith("jpeg"):
					ftype = ReportFile.IMAGE
				if str(f['file']).endswith("xls"):
					ftype = ReportFile.EXCEL
				c = ReportFile(title=f['title'], file=f['file'], file_creation_date = f['file_creation_date'], file_type=ftype, report=report)
                                #if no content the user the filename as description
                                if (report_file.text == None):
                                    report_file.text = str(report_file.file.name)
                                c.created_by = report.created_by
				c.save()
			del session['files']
Ejemplo n.º 3
0
    def set_pictures(self, report, row):
        # Generate path and name
        now = datetime.datetime.now()
        picture_name = row[self.PAVE_PICTURE_IDX]
        thumbnail_name = picture_name[:-3] + "thumbnail.JPG"

        path = "files/{}/{}/{}/".format(now.year, now.month, report.id)
        file_path = path + picture_name

        # Move pictures to expected path
        self._move_to_media(picture_name, thumbnail_name, path)

        # Create FMS attachment
        report_file = ReportFile(file_type=ReportFile.IMAGE,
                                 file_creation_date=now,
                                 title=self._get_pave_id(row),
                                 report=report,
                                 file=file_path,
                                 image=file_path,
                                 created_by=self._get_pave_user())
        report_file.bypassNotification = True
        report_file.save()
Ejemplo n.º 4
0
def create_report_photo(request):
    '''This method is used to create citizens reports. Validation included.'''
    #Test the submit content size (max 2MB)

    if (int(request.META.get('CONTENT_LENGTH')) > 15000000):
        return HttpResponseBadRequest(json.dumps({"error_key": "ERROR_REPORT_FILE_EXCEED_SIZE", "request":request.POST}),content_type='application/json')

    data_report_id = request.POST.get('report_id')
    data_file_content = request.FILES.get('report_file')

    #Verify that everything has been posted to create a citizen report.
    if (data_report_id == None):
        return HttpResponseBadRequest(json.dumps({"error_key": "ERROR_REPORT_FILE_MISSING_DATA_REPORT_ID", "request":request.POST}),content_type='application/json')
    if (data_file_content == None):
        return HttpResponseBadRequest(json.dumps({"error_key": "ERROR_REPORT_FILE_MISSING_DATA_REPORT_FILE", "request":request.POST}),content_type='application/json')

    try:
        #Retrieve the report
        reference_report = Report.objects.get(id=data_report_id)
    except Exception:
        return HttpResponseBadRequest(json.dumps({"error_key": "ERROR_REPORT_FILE_NOT_FOUND", "request":request.POST}),content_type='application/json')

    report_file = ReportFile()
    try:
        report_file.title  = "Mobile Upload"
        report_file.file   = data_file_content
        report_file.report = reference_report
        report_file.file_creation_date = datetime.now()

        # Set same creator than report creator
        report_file.created_by = reference_report.citizen or reference_report.created_by

        #Save given data
        report_file.is_new_report = True
        report_file.save()
    except Exception:
        return HttpResponseBadRequest(json.dumps({"error_key": "ERROR_REPORT_FILE_PROBLEM_DATA", "request":request.POST}),content_type='application/json')

    #Return the report ID
    return JsonHttpResponse({
        'report_photo': report_file.id
    })
Ejemplo n.º 5
0
    def testReportFileType(self):
        new_report = Report(
            status=Report.CREATED,
            secondary_category=self.secondary_category,
            category=self.category,
            description="Just a test",
            postalcode=1000,
            address="my address",
            point=dict_to_point({"x": "149776", "y": "170105"}),
            address_number="6h",
            created_by=self.manager_etterbeek,
        )
        new_report.save()

        reportFile = ReportFile(file_type=ReportFile.PDF, report=new_report)
        self.assertTrue(reportFile.is_pdf())
        self.assertFalse(reportFile.is_word())
        self.assertFalse(reportFile.is_excel())
        self.assertFalse(reportFile.is_image())
        self.assertTrue(reportFile.is_document())

        reportFile = ReportFile(file_type=ReportFile.WORD, report=new_report)
        self.assertFalse(reportFile.is_pdf())
        self.assertTrue(reportFile.is_word())
        self.assertFalse(reportFile.is_excel())
        self.assertFalse(reportFile.is_image())
        self.assertTrue(reportFile.is_document())

        reportFile = ReportFile(file_type=ReportFile.EXCEL, report=new_report)
        self.assertFalse(reportFile.is_pdf())
        self.assertFalse(reportFile.is_word())
        self.assertTrue(reportFile.is_excel())
        self.assertFalse(reportFile.is_image())
        self.assertTrue(reportFile.is_document())

        reportFile = ReportFile(file_type=ReportFile.IMAGE, report=new_report)
        self.assertFalse(reportFile.is_pdf())
        self.assertFalse(reportFile.is_word())
        self.assertFalse(reportFile.is_excel())
        self.assertTrue(reportFile.is_image())
        self.assertFalse(reportFile.is_document())
Ejemplo n.º 6
0
def create_report_photo(request):
    '''This method is used to create citizens reports. Validation included.'''
    #Test the submit content size (max 2MB)

    if (int(request.META.get('CONTENT_LENGTH')) > 15000000):
        return HttpResponseBadRequest(json.dumps({
            "error_key": "ERROR_REPORT_FILE_EXCEED_SIZE",
            "request": request.POST
        }),
                                      content_type='application/json')

    data_report_id = request.POST.get('report_id')

    # Check if the photo is base64 encoded
    if request.POST.get('base64', False):
        image_data_base64 = request.POST.get('report_file')

        # Extract content and metadata from base64 info
        metadata, file_base64 = image_data_base64.split(';base64,')
        metadata = metadata.replace("data:", "")

        # Extract name and type from metadata
        data_file_type, data_file_name = metadata.split(',')

        # Set (meta)data to file object
        data_file_content = ContentFile(file_base64.decode('base64'),
                                        name=data_file_name)
        data_file_content.content_type = data_file_type
    else:
        data_file_content = request.FILES.get('report_file')

    #Verify that everything has been posted to create a citizen report.
    if (data_report_id == None):
        return HttpResponseBadRequest(json.dumps({
            "error_key": "ERROR_REPORT_FILE_MISSING_DATA_REPORT_ID",
            "request": request.POST
        }),
                                      content_type='application/json')
    if (data_file_content == None):
        return HttpResponseBadRequest(json.dumps({
            "error_key": "ERROR_REPORT_FILE_MISSING_DATA_REPORT_FILE",
            "request": request.POST
        }),
                                      content_type='application/json')

    try:
        #Retrieve the report
        reference_report = Report.objects.get(id=data_report_id)
    except Exception:
        return HttpResponseBadRequest(json.dumps({
            "error_key": "ERROR_REPORT_FILE_NOT_FOUND",
            "request": request.POST
        }),
                                      content_type='application/json')

    report_file = ReportFile()
    try:
        report_file.title = "Mobile Upload"
        report_file.file = data_file_content
        report_file.report = reference_report
        report_file.file_creation_date = datetime.now()

        # Set same creator than report creator
        report_file.created_by = reference_report.citizen or reference_report.created_by

        #Save given data
        report_file.is_new_report = True
        report_file.save()
    except Exception:
        return HttpResponseBadRequest(json.dumps({
            "error_key": "ERROR_REPORT_FILE_PROBLEM_DATA",
            "request": request.POST
        }),
                                      content_type='application/json')

    #Return the report ID
    return JsonHttpResponse({'report_photo': report_file.id})
Ejemplo n.º 7
0
    def testReportFileType(self):
        new_report = Report(
            status=Report.CREATED,
            secondary_category=self.secondary_category,
            category=self.category,
            description='Just a test',
            postalcode=1000,
            address='my address',
            point=dict_to_point({"x": '149776', "y": '170105'}),
            address_number='6h',
            created_by=self.manager_etterbeek
        )
        new_report.save()

        reportFile = ReportFile(file_type=ReportFile.PDF, report=new_report)
        self.assertTrue(reportFile.is_pdf())
        self.assertFalse(reportFile.is_word())
        self.assertFalse(reportFile.is_excel())
        self.assertFalse(reportFile.is_image())
        self.assertTrue(reportFile.is_document())

        reportFile = ReportFile(file_type=ReportFile.WORD, report=new_report)
        self.assertFalse(reportFile.is_pdf())
        self.assertTrue(reportFile.is_word())
        self.assertFalse(reportFile.is_excel())
        self.assertFalse(reportFile.is_image())
        self.assertTrue(reportFile.is_document())

        reportFile = ReportFile(file_type=ReportFile.EXCEL, report=new_report)
        self.assertFalse(reportFile.is_pdf())
        self.assertFalse(reportFile.is_word())
        self.assertTrue(reportFile.is_excel())
        self.assertFalse(reportFile.is_image())
        self.assertTrue(reportFile.is_document())

        reportFile = ReportFile(file_type=ReportFile.IMAGE, report=new_report)
        self.assertFalse(reportFile.is_pdf())
        self.assertFalse(reportFile.is_word())
        self.assertFalse(reportFile.is_excel())
        self.assertTrue(reportFile.is_image())
        self.assertFalse(reportFile.is_document())