def create_field(label, space_id, type, **kwargs): space = Space.get_by_id(space_id) if not space: raise InvalidUsage.project_not_found() if 'file' in kwargs and kwargs['file'] is not None: uploaded_file = kwargs.pop('file') if uploaded_file.filename == '': raise InvalidUsage.no_files_found() filename_original, file_extension = os.path.splitext( secure_filename(uploaded_file.filename)) check_file_extension_for_type(type, file_extension) filename = '{}{}'.format(uuid.uuid4().hex, file_extension) uploaded_file.save(os.path.join(app.root_path, app.config['UPLOAD_FOLDER'], filename)) file_url = '/uploads/{}'.format(filename) file_size = os.path.getsize(os.path.join(app.root_path, app.config['UPLOAD_FOLDER'], filename)) file_object = File(filename=filename, url=file_url, filemime=uploaded_file.mimetype, filename_original=uploaded_file.filename, filesize=file_size) file_object.save() field = None try: if type == 'position': field = Position(label=label, space=space, author=current_user.profile, **kwargs) elif type == 'text': field = Text(label=label, space=space, author=current_user.profile, ** kwargs) elif type == 'number': field = Number(label=label, space=space, author=current_user.profile, **kwargs) elif type == 'audio': field = Audio(label=label, space=space, author=current_user.profile, file=file_object, ** kwargs) elif type == 'video': field = Video(label=label, space=space, author=current_user.profile, file=file_object, ** kwargs) elif type == 'videosphere': field = VideoSphere(label=label, space=space, author=current_user.profile, file=file_object, ** kwargs) elif type == 'image': field = Image(label=label, space=space, author=current_user.profile, file=file_object, ** kwargs) elif type == 'photosphere': field = PhotoSphere(label=label, space=space, author=current_user.profile, file=file_object, ** kwargs) elif type == 'link': field = Link(label=label, space=space, author=current_user.profile, **kwargs) elif type == 'color': field = Color(label=label, space=space, author=current_user.profile, **kwargs) else: raise Exception(("Field type - {} not found").format(type)) return if 'parent_id' in kwargs: parent_field = Field.get_by_id(kwargs['parent_id']) if parent_field: field.parent = parent_field field.save() except IntegrityError: db.session.rollback() raise InvalidUsage.no_files_found() except Exception as e: print(e) raise InvalidUsage.field_error() return field
def create_field(label, project_name, type, **kwargs): project = Project.query.filter_by(slug=project_name).first() if not project: raise InvalidUsage.project_not_found() field = None try: if type == 'position': field = Position(label=label, project=project, author=project.author, **kwargs) elif type == 'text': field = Text(label=label, project=project, author=project.author, **kwargs) elif type == 'number': field = Number(label=label, project=project, author=project.author, **kwargs) elif type == 'audio': field = Audio(label=label, project=project, author=project.author, **kwargs) elif type == 'video': field = Video(label=label, project=project, author=project.author, **kwargs) elif type == 'videosphere': field = VideoSphere(label=label, project=project, author=project.author, **kwargs) elif type == 'image': field = Image(label=label, project=project, author=project.author, **kwargs) elif type == 'photosphere': field = PhotoSphere(label=label, project=project, author=project.author, **kwargs) else: raise Exception(("Field type - {} not found").format(type)) return if 'parent_id' in kwargs: parent_field = Field.get_by_id(kwargs['parent_id']) if parent_field: field.parent = parent_field field.save() except IntegrityError: db.session.rollback() raise InvalidUsage.no_files_found() except Exception as e: print(e) raise InvalidUsage.field_error() return field