def generate_thumbnail(self): thumbnail_path = self.get_thumbnail_path() thumbnail_name = generate_filename(self.original_name) thumbnail_path = os.path.join(thumbnail_path, thumbnail_name) image_file = Image.open(self.get_file_path(True)) image_file.thumbnail(settings.THUMBS_SIZE, Image.ANTIALIAS) image_file.save(thumbnail_path, image_file.format) self.thumbnail = thumbnail_name
def post(self, request, type_upload): response_data = {'status': 'ko', 'message': 'Error in parameters'} file_uploaded = request.FILES['file'] if not WALL_VAL.match(file_uploaded.name): response_data = { 'status': 'ko', 'message': 'The name of the file is not correct' } elif file_uploaded.content_type not in settings.SUPPORTED_FILES: response_data = { 'status': 'ko', 'message': 'You cannot upload this kind of file' } elif file_uploaded.size * 1.0 / 1048576 > settings.FILE_SIZE: response_data = { 'status': 'ko', 'message': 'You cannot upload a file bigger than %d MB' % settings.FILE_SIZE } elif type_upload == 'profile': entity = request.POST['entity'] project_id = None if 'project' in request.POST: project_id = request.POST['project'] if entity in ('project', 'user'): new_filename = generate_filename(file_uploaded.name) if entity == 'project': obj = get_object_or_404(Project, id=project_id) obj.image_name = new_filename obj.save() elif entity == 'user': request.user.profile.picture_name = new_filename request.user.profile.save() upload_file(file_uploaded, entity, new_filename) response_data = { 'status': 'ok', 'message': 'File uploaded correctly' } elif type_upload == 'document': relative_path = upload_file(file_uploaded, 'documents', with_subpath=True) response_data = { 'status': 'ok', 'message': 'File uploaded correctly', 'url': relative_path } return HttpResponse(json.dumps(response_data), mimetype='application/json')
def post(self, request, type_upload): response_data = {"status": "ko", "message": "Error in parameters"} file_uploaded = request.FILES["file"] if not WALL_VAL.match(file_uploaded.name): response_data = {"status": "ko", "message": "The name of the file is not correct"} elif file_uploaded.content_type not in settings.SUPPORTED_FILES: response_data = {"status": "ko", "message": "You cannot upload this kind of file"} elif file_uploaded.size * 1.0 / 1048576 > settings.FILE_SIZE: response_data = { "status": "ko", "message": "You cannot upload a file bigger than %d MB" % settings.FILE_SIZE, } elif type_upload == "profile": entity = request.POST["entity"] project_id = None if "project" in request.POST: project_id = request.POST["project"] if entity in ("project", "user"): new_filename = generate_filename(file_uploaded.name) if entity == "project": obj = get_object_or_404(Project, id=project_id) obj.image_name = new_filename obj.save() elif entity == "user": request.user.profile.picture_name = new_filename request.user.profile.save() upload_file(file_uploaded, entity, new_filename) response_data = {"status": "ok", "message": "File uploaded correctly"} elif type_upload == "document": relative_path = upload_file(file_uploaded, "documents", with_subpath=True) response_data = {"status": "ok", "message": "File uploaded correctly", "url": relative_path} return HttpResponse(json.dumps(response_data), mimetype="application/json")