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']
Example #2
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(simplejson.dumps({"error_key": "ERROR_REPORT_FILE_EXCEED_SIZE", "request":request.POST}),mimetype='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(simplejson.dumps({"error_key": "ERROR_REPORT_FILE_MISSING_DATA_REPORT_ID", "request":request.POST}),mimetype='application/json')
    if (data_file_content == None):
        return HttpResponseBadRequest(simplejson.dumps({"error_key": "ERROR_REPORT_FILE_MISSING_DATA_REPORT_FILE", "request":request.POST}),mimetype='application/json')

    try:
        #Retrieve the report
        reference_report = Report.objects.get(id=data_report_id)
    except Exception:
        return HttpResponseBadRequest(simplejson.dumps({"error_key": "ERROR_REPORT_FILE_NOT_FOUND", "request":request.POST}),mimetype='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.save()
    except Exception:
        return HttpResponseBadRequest(simplejson.dumps({"error_key": "ERROR_REPORT_FILE_PROBLEM_DATA", "request":request.POST}),mimetype='application/json')

    #Return the report ID
    return JsonHttpResponse({
        'report_photo': report_file.id
    })