def add(db_session, data, username): logger.info(LogMsg.START, username) schema_validate(data, ADD_SCHEMA_PATH) logger.debug(LogMsg.SCHEMA_CHECKED) following_id = data.get('following_id') user = check_user(username, db_session) if user is None: raise Http_error(400, Message.INVALID_USER) if user.person_id is None: logger.error(LogMsg.USER_HAS_NO_PERSON, username) raise Http_error(400, Message.Invalid_persons) validate_person(user.person_id, db_session) validate_person(following_id, db_session) logger.debug(LogMsg.PERSON_EXISTS) if following_id == user.person_id: logger.error(LogMsg.FOLLOW_SELF_DENIED) raise Http_error(400, Message.FOLLOW_DENIED) logger.debug(LogMsg.FOLLOW_CHECK, data) follow = get(user.person_id, following_id, db_session) if follow is not None: logger.error(LogMsg.FOLLOW_EXISTS, data) raise Http_error(409, Message.ALREADY_FOLLOWS) model_instance = Follow() model_instance.id = str(uuid4()) model_instance.creation_date = Now() model_instance.creator = username model_instance.version = 1 model_instance.tags = data.get('tags') model_instance.following_id = following_id model_instance.follower_id = user.person_id db_session.add(model_instance) logger.debug(LogMsg.FOLLOW_ADD, follow_to_dict(model_instance)) logger.info(LogMsg.END) return model_instance
def re_add_users_following_before_delete(instance): # First, let's see if this page existed before. if instance.versions.all().count() > 2: # Now let's get the version before it was deleted before_delete = instance.versions.all()[2] past_follows = Follow.versions.filter(target_page__id=before_delete.id) # Now, re-follow. for follow in follows_before_cascade(past_follows): f = Follow(user=follow.user, target_page=instance) # For versioning purposes, let's keep the same pk # we had before delete. f.id = follow.id # We set TYPE_REVERTED here because it's slightly # more accurate than the (default) TYPE_ADDED. f._history_type = TYPE_REVERTED f.save()