def create_user(cls, user_dict: dict, current_user_id=None): """ This will create a new user, with new id and encrypted password. Import: You must use save() to storage it in the database. :param user_dict: A dictionary representation of the user. The dictionary must contain keys with the fields of UserModel table. :param current_user_id: (Optional) The current user id that is creating this user. :return: A new UserModel. """ user: UserModel = cls.from_dict(user_dict) user.id = app_utils.generate_id(16) user.password = app_utils.encrypt_password(user.password) # Add role relational field. roles_id = user_dict.get("roles") if not roles_id and len(roles_id) < 1: raise exceptions.RoleNotFound( "User must have at least one role assigned.") user.add_roles(roles_ids=roles_id, current_user_id=current_user_id) # Add right relational field. right_ids = user_dict.get("rights") if right_ids and len(right_ids) > 0: user.add_rights(right_ids=right_ids, current_user_id=current_user_id) return user
def create_equipments(cls, equipment_dict: dict): equipment: EquipmentModel = cls.from_dict(equipment_dict) equipment.id = app_utils.generate_id(16) if cls.find_by(name=equipment.name): raise CreateEntityError( "The instrument {} already exists in the database.".format( equipment.name)) return equipment.save()
def create_token(cls, user_id, expiry=None): """ Create an instance of TokenModel using only user_id. :param user_id: A user id. :param expiry: (Optional) An expire date stamp. :return: The instance of TokenModel. """ token_model = cls(expiry=expiry, user_id=user_id) token_model.token = app_utils.generate_token() token_model.id = app_utils.generate_id(16) return token_model
def create_station(cls, station_dict: dict): st: StationModel = cls.from_dict(station_dict) st.id = app_utils.generate_id(16) # validate creation st.creation_validation() if st.save(): # create default location LocationModel.create_location_at_station(st) return True return False
def create_channel(cls, channel_dict: dict): channel: ChannelModel = cls.from_dict(channel_dict) channel.id = app_utils.generate_id(16) # validate creation channel.creation_validation() # Add equipments relational field. equipments = [ EquipmentModel.from_dict(eq_dict) for eq_dict in channel_dict.get("equipments") ] channel.add_equipments(equipments=equipments) return channel.save()
def create_location(cls, location_dict: dict): """ Create a location from dict. :param location_dict: A dictionary with keys equal to LocationModel columns. :return: True if succeed, false otherwise. """ location: LocationModel = cls.from_dict(location_dict) location.id = app_utils.generate_id(16) # validate creation. location.creation_validation() return location.save()
def save_target_folder(cls, target_folder): try: active_folder = cls.get_active_folder() except ActiveFolderNotFound: active_folder = None if target_folder.id: safe_tf: TargetFolderModel = cls.find_by_id(target_folder.id) safe_tf << target_folder safe_tf.save() else: target_folder.id = app_utils.generate_id(16) target_folder.save() if active_folder and active_folder.id != target_folder.id and target_folder.active: active_folder.deactivate() return target_folder
def create_data(cls, **kwargs): """ This will create a new seismic data entity, with new id. Import: You must use save() to storage it in the database. :param kwargs: A dictionary contain the kwargs: id: str, filename: str, relative_path: str, target_folder_id: str, start_time: datetime, stop_time: datetime, channel_id: str :return: An instance of seismic data """ data: SeismicDataModel = cls.from_dict(kwargs) data.id = app_utils.generate_id(16) # Add file data relational field. At creation filename must be the id of transferred file table. transferred_file_id = data.filename data.add_file_data(transferred_file_id) return data
def create(cls, filename: str, station_id): """ This will create a new entity, with new id. Import: You must use save() to storage it in the database. :param filename: The file name. :param station_id: The station id the file will be linked to. :return: An instance of StationAttachedFileModel """ attached_dict = { "id": app_utils.generate_id(16), "filename": filename, "relative_path": "", "target_folder_id": TargetFolderModel.get_active_folder().id, "station_id": station_id } model: StationAttachedFileModel = cls.from_dict(attached_dict) model.relative_path = model.create_relative_path() return model