Beispiel #1
0
def query_user_search_by_pattern(**kw):
    """
    search user by pattern (re.Pattern)
    :param kw: keyword arguments
        :key "user_name": (optional) single keyword of username to be searched
        :key "user_email": (optional) single keyword of email to be searched
        :key "user_first_name": (optional) single keyword of first name to be
        searched
        :key "user_last_name": (optional) single keyword of last name to be
        searched
        :key "user_phone": (optional) single keyword of phone to be searched
        :key "user_street1": (optional) single keyword of street1 to be
        searched
        :key "user_street2": (optional) single keyword of street2 to be
        searched
        :key "user_city": (optional) single keyword of city to be searched
        :key "user_state": (optional) single keyword of state to be searched
        :key "user_country": (optional) single keyword of country to be
        searched
        :key "user_zip": (optional) single keyword of zip to be searched
        :key "user_status": (optional) single keyword of status to be searched
    \nAt least one key must be provided
    :return: array of searching results (User Model)
    """
    # Check input param
    if len(kw) == 0:
        raise MongoError(ErrorCode.MONGODB_EMPTY_PARAM)

    for arg in kw:
        if type(kw[arg]) != re.Pattern:
            raise MongoError(ErrorCode.MONGODB_RE_PATTERN_EXPECTED)

    if 'pattern_name' in kw:
        return User.objects(user_name=kw['pattern_name'])
    elif 'pattern_email' in kw:
        return User.objects(user_email=kw['pattern_email'])
    elif 'pattern_first_name' in kw:
        return User.objects(
            user_detail__user_first_name=kw['pattern_first_name'])
    elif 'pattern_last_name' in kw:
        return User.objects(
            user_detail__user_last_name=kw['pattern_last_name'])
    elif 'pattern_phone' in kw:
        return User.objects(user_detail__user_phone=kw['pattern_phone'])
    elif 'pattern_street1' in kw:
        return User.objects(user_detail__user_street1=kw['pattern_street1'])
    elif 'pattern_street2' in kw:
        return User.objects(user_detail__user_street2=kw['pattern_street2'])
    elif 'pattern_city' in kw:
        return User.objects(user_detail__user_city=kw['pattern_city'])
    elif 'pattern_state' in kw:
        return User.objects(user_detail__user_state=kw['pattern_state'])
    elif 'pattern_country' in kw:
        return User.objects(user_detail__user_country=kw['pattern_country'])
    elif 'pattern_zip' in kw:
        return User.objects(user_detail__user_zip=kw['pattern_zip'])
    elif 'pattern_status' in kw:
        return User.objects(user_status=kw['pattern_status'])

    raise MongoError(ErrorCode.MONGODB_INVALID_SEARCH_PARAM)
Beispiel #2
0
def query_user_add_follow(follower_id: str, following_id: str):
    """
    :param follower_id: follower user id
    :param following_id: uploader user_id
    :return: 1 if succeeded
    """
    if type(follower_id) != str or type(following_id) != str:
        raise MongoError(ErrorCode.MONGODB_STR_EXPECTED)

    follower = query_user_get_by_id(follower_id)
    following = query_user_get_by_id(following_id)

    if len(follower) == 0:
        raise MongoError(ErrorCode.MONGODB_FOLLOWER_NOT_FOUND)

    if len(following) == 0:
        raise MongoError(ErrorCode.MONGODB_FOLLOWED_NOT_FOUND)

    if following_id in follower[0].user_following and follower_id in \
            following[0].user_follower:
        raise MongoError(ErrorCode.MONGODB_FOLLOW_REL_EXISTS)

    User.objects(id=follower_id).update(
        add_to_set__user_following=following_id)
    User.objects(id=following_id).update(
        add_to_set__user_follower=follower_id)

    return 1
Beispiel #3
0
def query_video_cnt_incr_by_one(video_id: str, video_cnt: str):
    """
    This is for incrementing total number of
    views/comments/likes/dislikes/stars/shares
    :param video_id: video's unique id
    :param video_cnt: can choose from (view/comment/like/dislike/star/share)
    :return: 1 if succeeded
    """

    if len(query_video_get_by_video_id(video_id)) == 0:
        raise MongoError(ErrorCode.MONGODB_VIDEO_NOT_FOUND)

    if video_cnt == 'view' or video_cnt == 'views' or video_cnt == \
            'video_view':
        Video.objects(id=video_id).update(inc__video_view=1)
    elif video_cnt == 'comment' or video_cnt == 'comments' or video_cnt == \
            'video_comment':
        Video.objects(id=video_id).update(inc__video_comment=1)
    elif video_cnt == 'like' or video_cnt == 'likes' or video_cnt == \
            'video_like':
        Video.objects(id=video_id).update(inc__video_like=1)
    elif video_cnt == 'dislike' or video_cnt == 'dislikes' or video_cnt == \
            'video_dislike':
        Video.objects(id=video_id).update(inc__video_dislike=1)
    elif video_cnt == 'star' or video_cnt == 'stars' or video_cnt == \
            'video_star':
        Video.objects(id=video_id).update(inc__video_star=1)
    elif video_cnt == 'share' or video_cnt == 'shares' or video_cnt == \
            'video_share':
        Video.objects(id=video_id).update(inc__video_share=1)
    else:
        raise MongoError(ErrorCode.MONGODB_INVALID_VIDEO_CNT_PARAM)

    return 1
Beispiel #4
0
def query_user_add_login(user_id: str, ip="0.0.0.0", time=get_time_now_utc()):
    """
    :param user_id: user's unique id
    :param ip: user's login ip address
    :param time: user's login time (utc, optional), default: current system
    time (utc)
    :return: 1 if succeeded
    """
    if type(user_id) != str or type(ip) != str or type(
            time) != datetime.datetime:
        raise MongoError(ErrorCode.MONGODB_STR_EXPECTED)
    users = query_user_get_by_id(user_id)
    if len(users) == 0:
        raise MongoError(ErrorCode.MONGODB_USER_NOT_FOUND)
    # only keep 10 login info
    login_history = users[0].user_login
    oldest = login_history[0]
    latest = login_history[-1]
    # TODO: update latest 10 login info method, some bugs for current version
    if len(login_history) >= 10:
        # Delete oldest history
        clean = UserLogin(
            user_login_ip=oldest.user_login_ip,
            user_login_time=oldest.user_login_time)
        print(User.objects(id=user_id).update_one(
            pull__user_login__user_login_time=clean.user_login_time))
    if time == latest:
        User.objects(id=user_id).update(
            pull__user_login__user_login_time=latest)
    new_login = {'user_login_ip': ip, 'user_login_time': time}
    User.objects(id=user_id).update(
        add_to_set__user_login=[new_login])
    return 1
Beispiel #5
0
def query_video_op_create(user_id: str, video_id: str,
                          init_time=get_time_now_utc()):
    """
    Create video operations

    :param user_id: user's unique id
    :param video_id: video's unique id
    :param init_time: op creation time (optional, default utc now)
    :return VideoOp model if succeeded, -1 if no such user, -2 if no such
    video, -3 if VideoOp exists
    """
    if len(query_user_get_by_id(user_id)) == 0:
        raise MongoError(ErrorCode.MONGODB_USER_NOT_FOUND)

    if len(query_video_get_by_video_id(video_id)) == 0:
        raise MongoError(ErrorCode.MONGODB_VIDEO_NOT_FOUND)

    if len(query_video_op_get_by_user_video(user_id, video_id)) > 0:
        raise MongoError(ErrorCode.MONGODB_VIDEO_OP_EXISTS)

    video_op = VideoOp(user_id=user_id, video_id=video_id, process=0,
                       comment="", like=False,
                       dislike=False, star=False, process_date=init_time,
                       comment_date=init_time,
                       like_date=init_time, dislike_date=init_time,
                       star_date=init_time)

    return video_op.save()
Beispiel #6
0
def query_user_create(user_name: str, user_email: str, user_password: str,
                      user_ip="0.0.0.0"):
    """
    :param user_name: user's unique nickname
    :param user_email: user's unique email
    :param user_password: user's password
    :param user_ip: user's ip address (default 0.0.0.0)
    :return: user object if succeeded
    """
    if type(user_name) != str or type(user_email) != str or type(
            user_password) != str:
        raise MongoError(ErrorCode.MONGODB_STR_EXPECTED)

    if len(query_user_get_by_name(user_name)) > 0:
        raise MongoError(ErrorCode.MONGODB_USER_NAME_TAKEN)

    elif len(query_user_get_by_email(user_email)) > 0:
        raise MongoError(ErrorCode.MONGODB_USER_EMAIL_TAKEN)

    login = [
        UserLogin(user_login_ip=user_ip, user_login_time=get_time_now_utc())]

    # EmbeddedDocument must be included when creating
    # user_detail, user_reg_date
    user = User(user_name=user_name, user_email=user_email,
                user_password=user_password,
                user_detail=UserDetail(), user_status="private",
                user_thumbnail="",
                user_reg_date=get_time_now_utc(), user_login=login,
                user_following=[], user_follower=[])

    return user.save()
Beispiel #7
0
def query_video_search_by_pattern(**kw):
    """
    Search video by pattern
    :param kw: keyword arguments
        :key "pattern_title": (```str```, optional) search title pattern
        :key "pattern_description": (```str```, optional) search description
        pattern
    \nAt least one key must be provided
    :return: array of searching results (Video Model)
    """
    # Check input param
    if len(kw) == 0:
        raise MongoError(ErrorCode.MONGODB_EMPTY_PARAM)

    for arg in kw:
        if type(kw[arg]) != re.Pattern:
            raise MongoError(ErrorCode.MONGODB_RE_PATTERN_EXPECTED)

    if 'pattern_title' in kw:
        return Video.objects(video_title=kw['pattern_title'])
    elif 'pattern_channel' in kw:
        return Video.objects(video_channel=kw['pattern_channel'])
    elif 'pattern_description' in kw:
        return Video.objects(video_description=kw['pattern_description'])

    raise MongoError(ErrorCode.MONGODB_INVALID_SEARCH_PARAM)
Beispiel #8
0
def query_video_cnt_decr_by_one(video_id: str, video_cnt: str):
    """
    This is for decrementing total number of
    views/comments/likes/dislikes/stars/shares.

    :param video_id: video's unique id
    :param video_cnt: can choose from (view/comment/like/dislike/star/share)
    :return: 1 if succeeded, 0 if cnt < 0
    """

    videos = query_video_get_by_video_id(video_id)
    if len(videos) == 0:
        raise MongoError(ErrorCode.MONGODB_VIDEO_NOT_FOUND)

    if video_cnt not in VALID_VIDEO_CNT:
        raise MongoError(ErrorCode.MONGODB_INVALID_VIDEO_CNT_PARAM)

    video = videos[0].to_dict()

    if video_cnt == 'view' or video_cnt == 'views' or video_cnt == \
            'video_view':
        if video['video_view'] <= 0:
            return 0
            # raise MongoError(ErrorCode.MONGODB_VIDEO_CNT_ZERO)
        Video.objects(id=video_id).update(dec__video_view=1)
    if video_cnt == 'comment' or video_cnt == 'comments' or video_cnt == \
            'video_comment':
        if video['video_comment'] <= 0:
            return 0
            # raise MongoError(ErrorCode.MONGODB_VIDEO_CNT_ZERO)
        Video.objects(id=video_id).update(dec__video_comment=1)
    if video_cnt == 'like' or video_cnt == 'likes' or video_cnt == \
            'video_like':
        if video['video_like'] <= 0:
            return 0
            # raise MongoError(ErrorCode.MONGODB_VIDEO_CNT_ZERO)
        Video.objects(id=video_id).update(dec__video_like=1)
    if video_cnt == 'dislike' or video_cnt == 'dislikes' or video_cnt == \
            'video_dislike':
        if video['video_dislike'] <= 0:
            return 0
            # raise MongoError(ErrorCode.MONGODB_VIDEO_CNT_ZERO)
        Video.objects(id=video_id).update(dec__video_dislike=1)
    if video_cnt == 'star' or video_cnt == 'stars' or video_cnt == \
            'video_star':
        if video['video_star'] <= 0:
            return 0
            # raise MongoError(ErrorCode.MONGODB_VIDEO_CNT_ZERO)
        Video.objects(id=video_id).update(dec__video_star=1)
    if video_cnt == 'share' or video_cnt == 'shares' or video_cnt == \
            'video_share':
        if video['video_share'] <= 0:
            return 0
            # raise MongoError(ErrorCode.MONGODB_VIDEO_CNT_ZERO)
        Video.objects(id=video_id).update(dec__video_share=1)

    return 1
Beispiel #9
0
def query_user_update_details(**kw):
    """
    Update user details
    :param kw:
        :key "user_id": user unique id, required
        :key "user_first_name": (```str```, optional) new user first name
        :key "user_last_name": (```str```, optional) new user last name
        :key "user_phone": (```str```, optional) new user phone
        :key "user_street1": (```str```, optional) new user street1
        :key "user_street2": (```str```, optional) new user street2
        :key "user_city": (```str```, optional) new user city
        :key "user_state": (```str```, optional) new user state
        :key "user_country": (```str```, optional) new user country
        :key "user_zip": (```str```, optional) new user zip
    \nAt least one key must be provided
    :return: 1 if succeeded
    """
    if 'user_id' not in kw:
        raise MongoError(ErrorCode.MONGODB_MISSING_USER_ID)
    for arg in kw:
        if type(kw[arg]) != str:
            raise MongoError(ErrorCode.MONGODB_STR_EXPECTED)

    users = query_user_get_by_id(kw['user_id'])
    if len(users) == 0:
        raise MongoError(ErrorCode.MONGODB_USER_NOT_FOUND)

    id = kw['user_id']

    if 'user_first_name' in kw:
        User.objects(id=id).update(
            set__user_detail__user_first_name=kw['user_first_name'])
    if 'user_last_name' in kw:
        User.objects(id=id).update(
            set__user_detail__user_last_name=kw['user_last_name'])
    if 'user_phone' in kw:
        User.objects(id=id).update(
            set__user_detail__user_phone=kw['user_phone'])
    if 'user_street1' in kw:
        User.objects(id=id).update(
            set__user_detail__user_street1=kw['user_street1'])
    if 'user_street2' in kw:
        User.objects(id=id).update(
            set__user_detail__user_street2=kw['user_street2'])
    if 'user_city' in kw:
        User.objects(id=id).update(
            set__user_detail__user_city=kw['user_city'])
    if 'user_state' in kw:
        User.objects(id=id).update(
            set__user_detail__user_state=kw['user_state'])
    if 'user_country' in kw:
        User.objects(id=id).update(
            set__user_detail__user_country=kw['user_country'])
    if 'user_zip' in kw:
        User.objects(id=id).update(set__user_detail__user_zip=kw['user_zip'])

    return 1
Beispiel #10
0
def query_user_delete_by_name(user_name: str, silent=False):
    """
    :param user_name: user's name
    :param silent: delete user regardless of existence
    :return: 1 if succeeded
    """
    if type(user_name) != str:
        raise MongoError(ErrorCode.MONGODB_STR_EXPECTED)

    users = query_user_get_by_name(user_name)
    if len(users) == 0 and silent is False:
        raise MongoError(ErrorCode.MONGODB_USER_NOT_FOUND)

    return User.objects(user_name=user_name).delete()
Beispiel #11
0
def query_user_update_thumbnail(user_id: str, user_thumbnail: str):
    """
    :param user_id: user's id
    :param user_thumbnail: thumbnail URI
    :return: array of user Model
    """
    if type(user_id) != str or type(user_thumbnail) != str:
        raise MongoError(ErrorCode.MONGODB_STR_EXPECTED)

    users = query_user_get_by_id(user_id)
    if len(users) == 0:
        raise MongoError(ErrorCode.MONGODB_USER_NOT_FOUND)

    User.objects(id=user_id).update(
        user_thumbnail=user_thumbnail)
Beispiel #12
0
def query_video_search_by_aggregate(aggr: dict or list):
    """
    :param aggr: dict or list of searching param
    :return: array of searching results in dict
    """
    if type(aggr) != list and type(aggr) != dict:
        raise MongoError(ErrorCode.MONGODB_LIST_EXPECTED)
    return list(Video.objects.aggregate(aggr))
Beispiel #13
0
def query_user_update_status(user_id: str, user_status: str):
    """
    :param user_id: user's unique id
    :param user_status: user's new status
    :return: array of User Model
    """
    if type(user_id) != str or type(user_status) != str:
        raise MongoError(ErrorCode.MONGODB_STR_EXPECTED)

    if len(query_user_get_by_id(user_id)) == 0:
        raise MongoError(ErrorCode.MONGODB_USER_NOT_FOUND)

    if user_status not in VALID_USER_STATUS:
        raise MongoError(ErrorCode.MONGODB_USER_INVALID_STATUS)

    return User.objects(id=user_id).update(
        user_status=user_status)
Beispiel #14
0
def query_user_get_by_id(user_id: str):
    """
    :param user_id: user id
    :return: an array of such User (len == 0 or 1), len == 0 if no such
    user_id, len == 1 if found
    """
    if type(user_id) != str:
        raise MongoError(ErrorCode.MONGODB_STR_EXPECTED)

    return User.objects(id=user_id)
Beispiel #15
0
def query_user_get_by_name(user_name: str):
    """
    :param user_name: user name
    :return: an array of such User, len == 0 if no such user_name, len == 1
    if found
    """
    if type(user_name) != str:
        raise MongoError(ErrorCode.MONGODB_STR_EXPECTED)

    return User.objects(user_name=user_name)
Beispiel #16
0
def query_user_update_password(user_id: str, user_password: str):
    """
    :param user_id: user's id
    :param user_password: user's password
    :return: array of User Model
    """
    if type(user_id) != str or type(user_password) != str:
        raise MongoError(ErrorCode.MONGODB_STR_EXPECTED)

    users = query_user_get_by_id(user_id)
    if len(users) == 0:
        raise MongoError(ErrorCode.MONGODB_USER_NOT_FOUND)

    old_password = users[0].user_password
    if util_hash_encode(user_password) == old_password:
        raise MongoError(ErrorCode.MONGODB_UPDATE_SAME_PASS)

    return User.objects(id=user_id).update(
        user_password=util_hash_encode(user_password))
Beispiel #17
0
    def test_mongo_error(self):
        # Raise normal MongoError successfully
        with self.assertRaises(MongoError) as e:
            raise MongoError(ErrorCode.MONGODB_USER_NOT_FOUND)
        self.assertEqual(e.exception.error_code,
                         ErrorCode.MONGODB_USER_NOT_FOUND)

        # Wrong MongoError Param
        with self.assertRaises(MongoError) as e:
            MongoError(4000)
        self.assertEqual(e.exception.error_code, ErrorCode.ERR_INCORRECT_CODE)
        self.assertEqual(e.__str__(), e.__str__())

        # Get code & msg
        with self.assertRaises(MongoError) as e:
            raise MongoError(ErrorCode.MONGODB_CONNECTION_FAILURE)
        self.assertEqual(e.exception.get_code(),
                         ErrorCode.MONGODB_CONNECTION_FAILURE.get_code())
        self.assertEqual(e.exception.get_msg(),
                         ErrorCode.MONGODB_CONNECTION_FAILURE.get_msg())
Beispiel #18
0
def query_video_op_search_comment_by_pattern(comment):
    """
    Search video op by comment pattern
    :param comment: comment pattern
    :return: result array of VideoOp Model
    """
    # Check input param
    if type(comment) != re.Pattern:
        raise MongoError(ErrorCode.MONGODB_RE_PATTERN_EXPECTED)

    return VideoOp.objects(comment=comment)
Beispiel #19
0
def query_video_delete(video_id: str, silent=False):
    """
    :param video_id: video's unique id
    :param silent: delete video regardless of existence
    :return: 1 if succeeded, -1 if no such video
    """
    videos = query_video_get_by_video_id(video_id)
    if len(videos) == 0 and silent is False:
        raise MongoError(ErrorCode.MONGODB_VIDEO_NOT_FOUND)

    return Video.objects(id=video_id).delete()
Beispiel #20
0
def query_video_op_delete(op_id: str, silent=False):
    """
    :param op_id: op's unique id
    :param silent: delete video op regardless of existence
    :return: 1 if succeeded, -1 if no such video
    """
    if len(query_video_op_get_by_op_id(op_id)) == 0 and silent is False:
        # No such video op
        raise MongoError(ErrorCode.MONGODB_VIDEO_OP_NOT_FOUND)

    return VideoOp.objects(id=op_id).delete()
Beispiel #21
0
def query_user_update_name(user_id: str, user_name: str):
    """
    :param user_id: user's id
    :param user_name: user's name
    :return: array of User Model
    """
    if type(user_id) != str or type(user_name) != str:
        raise MongoError(ErrorCode.MONGODB_STR_EXPECTED)

    users = query_user_get_by_id(user_id)
    if len(users) == 0:
        raise MongoError(ErrorCode.MONGODB_USER_NOT_FOUND)

    old_name = users[0].user_name
    if user_name == old_name:
        raise MongoError(ErrorCode.MONGODB_UPDATE_SAME_NAME)

    if len(query_user_get_by_name(user_name)) > 0:
        raise MongoError(ErrorCode.MONGODB_USER_NAME_TAKEN)

    return User.objects(id=user_id).update(user_name=user_name)
Beispiel #22
0
def query_video_search_by_contains(**kw):
    """
    Search video by i-contains (ignore case)
    :param kw: keyword arguments
        :key "video_id": (```str```, optional) video's unique id
        :key "user_id": (```str```, optional) user's unique id
        :key "video_title": (```str```, optional) string of video title to be
        searched
        :key "video_channel": (```str```, optional) channel of videos
        :key "video_category": (```str```, optional) category of videos
    \nAt least one key must be provided
    :return: array of searching results (Video Model)
    """
    # Check input param
    if len(kw) == 0:
        raise MongoError(ErrorCode.MONGODB_EMPTY_PARAM)

    for arg in kw:
        if type(kw[arg]) != str:
            raise MongoError(ErrorCode.MONGODB_STR_EXPECTED)

    if 'video_id' in kw:
        return query_video_get_by_video_id(kw['video_id'])
    elif 'user_id' in kw:
        return query_video_get_by_user_id(kw['user_id'])
    elif 'video_title' in kw:
        return Video.objects.filter(video_title__icontains=kw['video_title'])
    elif 'video_channel' in kw:
        return Video.objects.filter(
            video_channel__icontains=kw['video_channel'])
    elif 'video_category' in kw:
        return Video.objects.filter(
            video_category__icontains=kw['video_category'])
    elif 'video_tag' in kw:
        return Video.objects.filter(video_tag__icontains=kw['video_tag'])
    elif 'video_description' in kw:
        return Video.objects.filter(
            video_description__icontains=kw['video_description'])

    raise MongoError(ErrorCode.MONGODB_INVALID_SEARCH_PARAM)
Beispiel #23
0
def query_video_op_update_comment(op_id: str, comment: str,
                                  comment_date=get_time_now_utc()):
    """
    :param: op_id, video op unique id
    :param: comment, video comment
    :param: comment_date (optional, default utc now)
    :return: 1 if succeeded, -1 if no such video_op
    """
    if len(query_video_op_get_by_op_id(op_id)) == 0:
        # No such video op
        raise MongoError(ErrorCode.MONGODB_VIDEO_OP_NOT_FOUND)

    return VideoOp.objects(id=op_id) \
        .update(comment=comment, comment_date=comment_date)
Beispiel #24
0
def query_video_op_update_process(op_id: str, process: int,
                                  process_date=get_time_now_utc()):
    """
    :param: op_id, video op unique id
    :param: process, video watching process
    :param: process_date (optional, default utc now)
    :return: 1 if succeeded, -1 if no such video_op
    """
    if len(query_video_op_get_by_op_id(op_id)) == 0:
        # No such video op
        raise MongoError(ErrorCode.MONGODB_VIDEO_OP_NOT_FOUND)

    return VideoOp.objects(id=op_id) \
        .update(process=process, process_date=process_date)
Beispiel #25
0
def query_video_op_update_star(op_id: str, star: bool,
                               star_date=get_time_now_utc()):
    """
    :param: op_id, video op unique id
    :param: star, video star (boolean)
    :param: star_date (optional, default utc now)
    :return: 1 if succeeded, -1 if no such video_op
    """
    if len(query_video_op_get_by_op_id(op_id)) == 0:
        # No such video op
        raise MongoError(ErrorCode.MONGODB_VIDEO_OP_NOT_FOUND)

    return VideoOp.objects(id=op_id)\
        .update(star=star, star_date=star_date)
Beispiel #26
0
def query_user_delete_follow(follower_id: str, following_id: str):
    """
    :param follower_id: follower user id
    :param following_id: uploader user_id
    :return: 1 if succeeded
    """

    if type(follower_id) != str or type(following_id) != str:
        raise MongoError(ErrorCode.MONGODB_STR_EXPECTED)

    follower = query_user_get_by_id(follower_id)
    following = query_user_get_by_id(following_id)

    if len(follower) == 0:
        raise MongoError(ErrorCode.MONGODB_FOLLOWER_NOT_FOUND)
    if len(following) == 0:
        raise MongoError(ErrorCode.MONGODB_FOLLOWED_NOT_FOUND)

    User.objects(id=follower_id).update(
        pull__user_following=following_id)
    User.objects(id=following_id).update(
        pull__user_follower=follower_id)

    return 1
Beispiel #27
0
def query_video_create(user_id: str):
    """
    Create Video
    :param user_id: (required) user's unique id
    :return: video uuid
    """
    if len(query_user_get_by_id(user_id)) == 0:
        raise MongoError(ErrorCode.MONGODB_USER_NOT_FOUND)

    # Construct Video Model
    video = Video(user_id=user_id,
                  video_title="",
                  video_raw_content="",
                  video_raw_status="pending",
                  video_status="public",
                  video_raw_size=0,
                  video_duration=0,
                  video_channel="",
                  video_tag=[],
                  video_category=[],
                  video_description="",
                  video_language="",
                  video_view=0,
                  video_comment=0,
                  video_like=0,
                  video_dislike=0,
                  video_star=0,
                  video_share=0,
                  video_thumbnail="",
                  video_upload_date=get_time_now_utc(),
                  video_uri=VideoURI())
    video.save()

    aws_uri = conf.AWS_STREAMING_FOLDER + str(video.id) + '_'
    aws_low = aws_uri + conf.AWS_STREAMING_LOW + conf.AWS_STREAMING_FORMAT
    aws_mid = aws_uri + conf.AWS_STREAMING_MID + conf.AWS_STREAMING_FORMAT
    aws_high = aws_uri + conf.AWS_STREAMING_HIGH + conf.AWS_STREAMING_FORMAT

    aws_thumbnail = conf.AWS_THUMBNAIL_FOLDER + \
        str(video.id) + conf.AWS_THUMBNAIL_FORMAT

    Video.objects(id=video.id).update(video_uri__video_uri_low=aws_low)
    Video.objects(id=video.id).update(video_uri__video_uri_mid=aws_mid)
    Video.objects(id=video.id).update(video_uri__video_uri_high=aws_high)
    Video.objects(id=video.id).update(video_thumbnail=aws_thumbnail)
    return str(video.id)
Beispiel #28
0
def query_user_search_by_contains(**kw):
    """
    Search user by i-contains (ignore case)
    :param kw: keyword arguments
        :key "user_name": (optional) single keyword of username to be searched
        :key "user_email": (optional) single keyword of email to be searched
        :key "user_first_name": (optional) single keyword of first name to be
        searched
        :key "user_last_name": (optional) single keyword of last name to be
        searched
        :key "user_phone": (optional) single keyword of phone to be searched
        :key "user_street1": (optional) single keyword of street1 to be
        searched
        :key "user_street2": (optional) single keyword of street2 to be
        searched
        :key "user_city": (optional) single keyword of city to be searched
        :key "user_state": (optional) single keyword of state to be searched
        :key "user_country": (optional) single keyword of country to be
        searched
        :key "user_zip": (optional) single keyword of zip to be searched
        :key "user_status": (optional) single keyword of status to be searched
        :key "user_reg_date": (optional) single keyword of reg date to be
        searched
    \nAt least one key must be provided
    :return: array of searching results (User Model)
    """
    if len(kw) == 0:
        raise MongoError(ErrorCode.MONGODB_EMPTY_PARAM)

    for arg in kw:
        if type(kw[arg]) != str:
            raise MongoError(ErrorCode.MONGODB_STR_EXPECTED)

    if 'user_id' in kw:
        return query_user_get_by_id(kw['user_id'])
    elif 'user_name' in kw:
        return User.objects.filter(user_name__icontains=kw['user_name'])
    elif 'user_email' in kw:
        return User.objects.filter(user_email__icontains=kw['user_email'])
    elif 'user_first_name' in kw:
        return User.objects.filter(
            user_detail__user_first_name__icontains=kw['user_first_name'])
    elif 'user_last_name' in kw:
        return User.objects.filter(
            user_detail__user_last_name__icontains=kw['user_last_name'])
    elif 'user_phone' in kw:
        return User.objects.filter(
            user_detail__user_phone__icontains=kw['user_phone'])
    elif 'user_street1' in kw:
        return User.objects.filter(
            user_detail__user_street1__icontains=kw['user_street1'])
    elif 'user_street2' in kw:
        return User.objects.filter(
            user_detail__user_street2__icontains=kw['user_street2'])
    elif 'user_city' in kw:
        return User.objects.filter(
            user_detail__user_city__icontains=kw['user_city'])
    elif 'user_state' in kw:
        return User.objects.filter(
            user_detail__user_state__icontains=kw['user_state'])
    elif 'user_country' in kw:
        return User.objects.filter(
            user_detail__user_country__icontains=kw['user_country'])
    elif 'user_zip' in kw:
        return User.objects.filter(
            user_detail__user_zip__icontains=kw['user_zip'])
    elif 'user_status' in kw:
        return User.objects.filter(user_status__icontains=kw['user_status'])

    raise MongoError(ErrorCode.MONGODB_INVALID_SEARCH_PARAM)
Beispiel #29
0
def query_video_update(video_id: str, **kw):
    """
    Update Video Info
    :param video_id: video's unique id
    :param kw:
        :key "video_title": (```str```, optional) new title
        :key "video_raw_content": (```str```, optional) new URI of raw video
        data (in temp space, to be transcode)
        :key "video_raw_status": (```str```, optional) new status of raw
        video data, default
        :key "video_raw_size": (```float```, optional) new size of raw video
        data
        :key "video_duration": (```int```, optional) duration of video in
        second
        :key "video_channel": (```str```, optional) channel of video (default
        self-made)
        :key "video_tag": (```list```, optional) array of new tags
        :key "video_category": (```list```, optional) array of new categories
        :key "video_description": (```str```, optional) new description
        :key "video_language": (```str```, optional) new language
        :key "video_status": (```str```, optional) new status, default: public
        :key "video_thumbnail": (```str```, optional) new thumbnail uri
        :key "video_uri_low": (```str```, optional) new final uri (480p low
        resolution)
        :key "video_uri_mid": (```str```, optional) new final uri (720p mid
        resolution)
        :key "video_uri_high": (```str```, optional) new final uri (1080p
        high resolution)
    \nAt least one key must be provided
    :return: 1 if succeeded
    """

    if len(query_video_get_by_video_id(video_id)) == 0:
        raise MongoError(ErrorCode.MONGODB_VIDEO_NOT_FOUND)

    if 'video_title' in kw and kw['video_title'] != "":
        Video.objects(id=video_id).update(video_title=kw['video_title'])
    if 'video_raw_content' in kw:
        Video.objects(id=video_id).update(
            video_raw_content=kw['video_raw_content'])
    if 'video_raw_status' in kw:
        if kw['video_raw_status'] not in VALID_VIDEO_RAW_STATUS:
            raise MongoError(ErrorCode.MONGODB_VIDEO_INVALID_STATUS)
        Video.objects(id=video_id).update(
            video_raw_status=kw['video_raw_status'])
    if 'video_raw_size' in kw:
        Video.objects(id=video_id).update(video_raw_size=kw['video_raw_size'])
    if 'video_duration' in kw:
        Video.objects(id=video_id).update(video_duration=kw['video_duration'])
    if 'video_channel' in kw:
        Video.objects(id=video_id).update(video_channel=kw['video_channel'])
    if 'video_tag' in kw:
        if type(kw['video_tag']) is not list:
            raise MongoError(ErrorCode.MONGODB_LIST_EXPECTED)
        Video.objects(id=video_id).update(video_tag=kw['video_tag'])
    if 'video_category' in kw:
        if type(kw['video_category']) is not list:
            raise MongoError(ErrorCode.MONGODB_LIST_EXPECTED)
        Video.objects(id=video_id).update(video_category=kw['video_category'])
    if 'video_description' in kw:
        Video.objects(id=video_id).update(
            video_description=kw['video_description'])
    if 'video_language' in kw:
        Video.objects(id=video_id).update(video_language=kw['video_language'])
    if 'video_status' in kw:
        if kw['video_status'] not in VALID_VIDEO_STATUS:
            raise MongoError(ErrorCode.MONGODB_VIDEO_INVALID_STATUS)
        Video.objects(id=video_id).update(video_status=kw['video_status'])
    if 'video_thumbnail' in kw:
        Video.objects(id=video_id).update(
            video_thumbnail=kw['video_thumbnail'])
    if 'video_uri_low' in kw:
        Video.objects(id=video_id).update(
            video_uri__video_uri_low=kw['video_uri_low'])
    if 'video_uri_mid' in kw:
        Video.objects(id=video_id).update(
            video_uri__video_uri_mid=kw['video_uri_mid'])
    if 'video_uri_high' in kw:
        Video.objects(id=video_id).update(
            video_uri__video_uri_high=kw['video_uri_high'])

    return 1