예제 #1
0
 def get_all(user_id):
     try:
         return db.session.query(History).filter(
             History.user_id == user_id).all()
     except Exception as e:
         logging.getLogger('error_logger').exception(e)
         raise DatabaseException()
예제 #2
0
    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)
예제 #3
0
 def get(history_id):
     try:
         return db.session.query(History).filter(
             History.id == history_id).first()
     except Exception as e:
         logging.getLogger('error_logger').exception(e)
         raise DatabaseException()
예제 #4
0
 def delete(history):
     try:
         for log in history:
             db.session.delete(log)
         db.session.commit()
     except Exception as e:
         logging.getLogger('error_logger').exception(e)
         raise DatabaseException()
예제 #5
0
 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()
예제 #6
0
 def read(name, user_id):
     try:
         file = db.session.query(File).filter(
             File.name == name, File.user_id == user_id).first()
     except Exception as e:
         logging.getLogger('error_logger').exception(e)
         raise DatabaseException()
     if file:
         return file
     else:
         logging.getLogger('logger').warning('File not found.')
         raise UserException()
예제 #7
0
 def read(user_id, type_h=None):
     try:
         if type_h:
             return db.session.query(History).filter(
                 History.type_h == type_h,
                 History.user_id == user_id).all()
         else:
             return db.session.query(History).filter(
                 History.user_id == user_id).all()
     except Exception as e:
         logging.getLogger('error_logger').exception(e)
         raise DatabaseException()
예제 #8
0
 def delete(users):
     try:
         for user in users:
             db.session.delete(user)
         db.session.commit()
         for user in users:
             path = 'Project/Client/static/DATA/' + user.home_catalog
             if os.path.isdir(path):
                 shutil.rmtree(path)
     except Exception as e:
         db.session.rollback()
         logging.getLogger('error_logger').exception(e)
         raise DatabaseException()
예제 #9
0
 def delete(files, home_catalog):
     try:
         for dt in files:
             db.session.delete(dt)
         db.session.commit()
         for dt in files:
             path = os.path.join('Project/Server/DATA/' + home_catalog,
                                 dt.name)
             if os.path.isfile(path):
                 os.remove(path)
     except Exception as e:
         db.session.rollback()
         logging.getLogger('error_logger').exception(e)
         raise DatabaseException()
예제 #10
0
 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()
예제 #11
0
 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.')
예제 #12
0
 def read(login, password):
     try:
         user = db.session.query(User).filter(User.login == login).first()
     except Exception as e:
         logging.getLogger('error_logger').exception(e)
         raise DatabaseException()
     if user:
         hash_password = hashlib.sha3_512(password.encode('utf-8') + user.salt.encode('utf-8')).hexdigest()
         if user.hashed_password == hash_password:
             logging.getLogger('logger').info('User ' + user.login + ' was found.')
             return user
         else:
             logging.getLogger('logger').warning('Wrong password for ' + user.login + ' user.')
             raise UserException('Wrong username or password.')
     else:
         logging.getLogger('logger').warning('Cannot find user ' + login + '.')
         raise UserException('Wrong username or password.')
예제 #13
0
    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)
예제 #14
0
 def get(user_id):
     try:
         return db.session.query(User).filter(User.id == user_id).first()
     except Exception as e:
         logging.getLogger('error_logger').exception(e)
         raise DatabaseException()
예제 #15
0
 def get_all():
     try:
         return db.session.query(User).all()
     except Exception as e:
         logging.getLogger('error_logger').exception(e)
         raise DatabaseException()
예제 #16
0
 def get(data_id):
     try:
         return db.session.query(File).filter(File.id == data_id).first()
     except Exception as e:
         logging.getLogger('error_logger').exception(e)
         raise DatabaseException()