def add_existing_checkpoint_to_user(user_obj, user_checkpoint_obj): """ Duplicates a record of an instance of <<UserCheckpoint>> and gives it to the provided <<User>>. Also recursively duplicates corresponding <<UserCheckpointOptions>> and gives it to the duplicated instance of <<UserCheckpoint>> """ ucp = get_user_checkpoint_attr(user_obj, user_checkpoint_obj.checkpoint) if not ucp is None: return ucp from db import UserCheckpoint, UserCheckpointOptions, db #duplicate UserCheckpoint duplicated_ucp = UserCheckpoint() duplicated_ucp.user_id = user_obj.id duplicated_ucp.checkpoint_id = user_checkpoint_obj.checkpoint_id duplicated_ucp.date_added = datetime.datetime.now() db.session.add(duplicated_ucp) #duplicate UserCheckpointOptions options = UserCheckpointOptions.query.filter_by(user_checkpoint_id=user_checkpoint_obj.id) for opt in options: duplicated_opt = UserCheckpointOptions() duplicated_opt.user_checkpoint_id = duplicated_ucp.id duplicated_opt.name = opt.name duplicated_opt.value = opt.value db.session.add(duplicated_opt) db.session.commit() return duplicated_ucp
def add_checkpoint_to_user(user_obj, checkpoint_obj): """ Adds a record to the db table to reflect an addition of an NEW Checkpoint to a user's repertoir of Checkpoints """ ucp = get_user_checkpoint_attr(user_obj, checkpoint_obj) if not ucp is None: return ucp from db import UserCheckpoint, db user_checkpoint = UserCheckpoint() user_checkpoint.user_id = user_obj.id user_checkpoint.checkpoint_id = checkpoint_obj.id user_checkpoint.date_added = datetime.datetime.now() db.session.add(user_checkpoint) db.session.commit() return user_checkpoint