def create(file, user): try: secure_name = secure_filename(file.filename) if not db.session.query(File).filter( File.name == secure_name, File.user_id == user.id).first(): new_data = File(name=secure_name) user.files.append(new_data) h_log = History(type_h=TypeH.Info, description='File ' + new_data.name + ' added') user.history.append(h_log) db.session.merge(user) db.session.commit() path = os.path.join('Project/Server/DATA/' + user.home_catalog, new_data.name) if not os.path.isfile(path): file.save(path) return new_data except Exception as e: db.session.rollback() logging.getLogger('error_logger').exception(e) raise DatabaseException() msg = 'File with this name already exists.' logging.getLogger('logger').warning(msg) raise UserException(msg)
def create(type_h, description, user): try: log_h = History(type_h=type_h, description=description) user.history.append(log_h) db.session.merge(user) db.session.commit() except Exception as e: db.session.rollback() logging.getLogger('error_logger').exception(e) raise DatabaseException()
def update(user, info=None): if user: try: if info: h_log = History(type_h=TypeH.Info, description=info) user.history.append(h_log) db.session.merge(user) db.session.commit() except Exception as e: db.session.rollback() logging.getLogger('error_logger').exception(e) raise DatabaseException() else: logging.getLogger('logger').warning('Update operation warning. User not found.') raise UserException()
def update(file, user, info=None): if file and user: try: db.session.merge(file) if info: h_log = History(type_h=TypeH.Info, description=info) user.history.append(h_log) db.session.merge(user) db.session.commit() except Exception as e: db.session.rollback() logging.getLogger('error_logger').exception(e) raise DatabaseException() else: logging.getLogger('logger').warning( 'Update data error. Data not found.') raise UserException('Data doesn\'t exists.')
def create(login, password): try: if not db.session.query(User).filter(User.login == login).first(): new_user = User(login=login, password=password) h_log = History(type_h=TypeH.Info, description='Account created') new_user.history.append(h_log) db.session.add(new_user) db.session.commit() path = 'Project/Server/DATA/' + new_user.home_catalog if not os.path.isdir(path): os.makedirs(path) return new_user except Exception as e: db.session.rollback() logging.getLogger('error_logger').exception(e) raise DatabaseException() msg = 'User with ' + login + ' already exists.' logging.getLogger('logger').warning(msg) raise UserException(msg)