Beispiel #1
0
def service_video_comments(**kw):

    kw['service'] = 'video'
    kw = util_pattern_format_param(**kw)

    # keyword check and formatting
    if 'video_id' not in kw:
        raise ServiceError(ErrorCode.SERVICE_MISSING_PARAM)

    if not is_valid_id(kw["video_id"]):
        raise ServiceError(ErrorCode.SERVICE_INVALID_ID_OBJ)

    # perform db operations and get result
    search_mongo = query_video_op_get_by_video_id(kw["video_id"])
    if len(search_mongo) == 0:
        return []

    search_result = util_serializer_mongo_results_to_array(search_mongo)

    comments_result = []
    for each in search_result:
        if each["comment"] != "":
            user_obj = query_user_get_by_id(each["user_id"])[0].to_dict()
            comments_result.append({
                "video_id": each["video_id"],
                "user_id": each["user_id"],
                "user_name": user_obj["user_name"],
                "user_thumbnail": user_obj["user_thumbnail"],
                "comment": each["comment"],
                "comment_date": str(each["comment_date"])
            })

    return comments_result
Beispiel #2
0
def service_user_get_process(user_id):

    # user_id check
    if not is_valid_id(user_id):
        raise ServiceError(ErrorCode.SERVICE_INVALID_ID_OBJ)

    if len(query_user_get_by_id(user_id)) <= 0:
        raise ServiceError(ErrorCode.SERVICE_USER_NOT_FOUND)

    # perform db operations and get result
    search_mongo = query_video_op_get_by_user_id(user_id)
    if len(search_mongo) == 0:
        return []

    search_result = util_serializer_mongo_results_to_array(search_mongo)

    process_result = []
    for each in search_result:
        process_result.append({
            "video_id": each["video_id"],
            "user_id": each["user_id"],
            "process": str(each["process"]),
            "process_date": str(each["process_date"])
        })

    return process_result
Beispiel #3
0
def service_video_delete(**kw):

    kw['service'] = 'video'
    kw = util_pattern_format_param(**kw)

    # keyword check and formatting
    if 'video_id' not in kw:
        raise ServiceError(ErrorCode.SERVICE_MISSING_PARAM)

    if not is_valid_id(kw['video_id']):
        raise ServiceError(ErrorCode.SERVICE_INVALID_ID_OBJ)

    # delete by setting status
    if 'method' in kw and kw['method'] == 'status':
        res = query_video_update(kw['video_id'], video_status='deleted')
    # delete by removing from database
    else:
        res = query_video_delete(kw['video_id'], silent=True)

    # delete all op in this video immediately
    ops = query_video_op_get_by_video_id(kw['video_id'])
    for op in ops:
        opid = op.to_dict()['video_op_id']
        query_video_op_delete(opid, silent=True)
    return res
Beispiel #4
0
def service_user_get_user(user_id):

    # user_id check
    if not is_valid_id(user_id):
        raise ServiceError(ErrorCode.SERVICE_INVALID_ID_OBJ)

    users = query_user_get_by_id(user_id)
    if len(users) == 0:
        raise ServiceError(ErrorCode.SERVICE_USER_NOT_FOUND)

    user_array = util_serializer_mongo_results_to_array(users)
    return user_array[0]
Beispiel #5
0
def service_video_get_by_user(**kw):

    kw['service'] = 'video'
    kw = util_pattern_format_param(**kw)
    # keyword check and formatting
    if 'user_id' not in kw:
        raise ServiceError(ErrorCode.SERVICE_MISSING_PARAM)
    if not is_valid_id(kw["user_id"]):
        raise ServiceError(ErrorCode.SERVICE_INVALID_ID_OBJ)
    videos = query_video_get_by_user_id(kw['user_id'])
    if len(videos) == 0:
        return []
    video_array = util_serializer_mongo_results_to_array(videos)
    return video_array
Beispiel #6
0
def service_user_reg(**kw):
    """
    Register user

    :param kw: keyword
    :keyword:
        :key user_name: (required) str
        :key user_email: (required) str
        :key user_password: (required) str
        :key user_ip: (optional) str
    :return user model:
    """
    # user_name: str, user_email: str, user_password: str, user_ip = "0.0.0.0"
    # service_user_reg(conf, user_name="t", user_email="k",
    # user_password="******")

    kw['service'] = 'user'
    kw = util_pattern_format_param(**kw)
    if 'user_name' not in kw or 'user_email' not in kw \
            or 'user_password' not in kw:
        raise ServiceError(ErrorCode.SERVICE_MISSING_PARAM)

    query_user_create(kw['user_name'], kw['user_email'],
                      util_hash_encode(kw['user_password']))

    return query_user_get_by_name(kw['user_name'])[0].to_dict()
Beispiel #7
0
def service_search_user_by_pattern(**kw):
    if 'user_name' in kw:
        return query_user_search_by_pattern(pattern_name=kw['user_name'])
    elif 'user_email' in kw:
        return query_user_search_by_pattern(pattern_email=kw['user_email'])
    elif 'user_first_name' in kw:
        return query_user_search_by_pattern(
            pattern_first_name=kw['user_first_name'])
    elif 'user_last_name' in kw:
        return query_user_search_by_pattern(
            pattern_last_name=kw['user_last_name'])
    elif 'user_phone' in kw:
        return query_user_search_by_pattern(pattern_phone=kw['user_phone'])
    elif 'user_street1' in kw:
        return query_user_search_by_pattern(pattern_street1=kw['user_street1'])
    elif 'user_street2' in kw:
        return query_user_search_by_pattern(pattern_street2=kw['user_street2'])
    elif 'user_city' in kw:
        return query_user_search_by_pattern(pattern_city=kw['user_city'])
    elif 'user_state' in kw:
        return query_user_search_by_pattern(pattern_state=kw['user_state'])
    elif 'user_country' in kw:
        return query_user_search_by_pattern(pattern_country=kw['user_country'])
    elif 'user_zip' in kw:
        return query_user_search_by_pattern(pattern_zip=kw['user_zip'])
    elif 'user_status' in kw:
        return query_user_search_by_pattern(pattern_status=kw['user_status'])

    raise ServiceError(ErrorCode.SERVICE_PATTERN_SEARCH_NOT_SUPPORT)
Beispiel #8
0
def service_search_user(**kw):

    kw['service'] = 'user'
    kw = util_pattern_format_param(**kw)

    # Search configs
    if 'slice' in kw and kw['slice'] is True:
        raise ServiceError(ErrorCode.SERVICE_PARAM_SLICE_NOT_SUPPORT)
    if 'ignore_case' not in kw:
        kw['ignore_case'] = conf.SEARCH_IGNORE_CASE
    if 'exact' not in kw:
        kw['exact'] = conf.SEARCH_EXACT

    # TODO: add typo allowance, etc.

    # Search
    # TODO: Support aggregation pipeline, etc.
    if kw['ignore_case'] is False or kw['exact'] is True \
            or 'pattern' in kw and kw['pattern'] is True:
        kw = util_pattern_build(**kw)
        res_search = service_search_user_by_pattern(**kw)
    elif 'aggregate' in kw and kw['aggregate'] is True:
        res_search = service_search_user_by_aggregation(**kw)
        return res_search
    else:
        res_search = service_search_user_by_contains(**kw)

    return util_serializer_mongo_results_to_array(res_search)
Beispiel #9
0
def service_user_close(**kw):

    kw['service'] = 'user'
    kw = util_pattern_format_param(**kw)
    if 'user_id' not in kw:
        raise ServiceError(ErrorCode.SERVICE_MISSING_USER_ID)

    videos = query_video_get_by_user_id(kw['user_id'])
    ops = query_video_op_get_by_user_id(kw['user_id'])

    # delete by setting status
    if 'method' in kw and kw['method'] == 'status':
        res = query_user_update_status(kw['user_id'], 'closed')
        for video in videos:
            service_video_delete(video_id=video.to_dict()['video_id'],
                                 method='status')

    # delete by removing from databse
    else:
        res = query_user_delete_by_id(kw['user_id'])
        for video in videos:
            service_video_delete(video_id=video.to_dict()['video_id'])

    # delete all op created by this user immediately
    for op in ops:
        query_video_op_delete(op.to_dict()['video_op_id'], silent=True)

    return res
Beispiel #10
0
def service_auth_video_modify(token, video_id):
    videos = query_video_get_by_video_id(video_id)
    if len(videos) == 0:
        raise ServiceError(ErrorCode.SERVICE_VIDEO_NOT_FOUND)
    video = videos[0].to_dict()
    if video['user_id'] == token:
        return True
    return False
Beispiel #11
0
def service_auth_user_get(token, user_id):
    users = query_user_get_by_id(user_id)
    if len(users) == 0:
        raise ServiceError(ErrorCode.SERVICE_USER_NOT_FOUND)
    user = users[0].to_dict()
    if user['user_status'] == 'public' or token == user_id:
        return True
    return False
Beispiel #12
0
def service_video_upload(user_id: str):

    if not is_valid_id(user_id):
        raise ServiceError(ErrorCode.SERVICE_INVALID_ID_OBJ)

    # perform db operations and get result
    vid = query_video_create(user_id)
    return vid
Beispiel #13
0
def service_user_login(**kw):

    kw['service'] = 'user'
    kw = util_pattern_format_param(**kw)
    if 'user_name' in kw and 'user_password' in kw:
        users = query_user_get_by_name(kw['user_name'])
        if len(users) == 0:
            raise ServiceError(ErrorCode.SERVICE_USER_NOT_FOUND)
        user = users[0]
        if user.to_dict()["user_status"] == "closed":
            raise ServiceError(ErrorCode.SERVICE_USER_CLOSED)
        if util_hash_encode(kw['user_password']) != user.user_password:
            raise ServiceError(ErrorCode.SERVICE_USER_PASS_WRONG)
    elif 'user_email' in kw and 'user_password' in kw:
        users = query_user_get_by_email(kw['user_email'])
        if len(users) == 0:
            raise ServiceError(ErrorCode.SERVICE_USER_NOT_FOUND)
        user = users[0]
        if user.to_dict()["user_status"] == "closed":
            raise ServiceError(ErrorCode.SERVICE_USER_CLOSED)
        if util_hash_encode(kw['user_password']) != user.user_password:
            raise ServiceError(ErrorCode.SERVICE_USER_PASS_WRONG)
    elif 'user' in kw and 'user_password' in kw:
        user_names = query_user_get_by_name(kw['user'])
        user_emails = query_user_get_by_email(kw['user'])
        if len(user_emails) == 0 and len(user_names) == 0:
            raise ServiceError(ErrorCode.SERVICE_USER_NOT_FOUND)
        elif len(user_emails) != 0:
            user = user_emails[0]
        elif len(user_names) != 0:
            user = user_names[0]
        if user.to_dict()["user_status"] == "closed":
            raise ServiceError(ErrorCode.SERVICE_USER_CLOSED)
        if util_hash_encode(kw['user_password']) != user.user_password:
            raise ServiceError(ErrorCode.SERVICE_USER_PASS_WRONG)
    else:
        raise ServiceError(ErrorCode.SERVICE_MISSING_PARAM)

    uid = user.to_dict()['user_id']
    if 'ip' in kw:
        query_user_add_login(uid, ip=kw['ip'])
    return query_user_get_by_id(uid)[0].to_dict()
Beispiel #14
0
    def test_service_error(self):
        # Raise normal ServiceError successfully
        with self.assertRaises(ServiceError) as e:
            raise ServiceError(ErrorCode.SERVICE_MISSING_PARAM)
        self.assertEqual(e.exception.error_code,
                         ErrorCode.SERVICE_MISSING_PARAM)

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

        # Get code & msg
        with self.assertRaises(ServiceError) as e:
            raise ServiceError(ErrorCode.SERVICE_MISSING_PARAM)
        self.assertEqual(e.exception.get_code(),
                         ErrorCode.SERVICE_MISSING_PARAM.get_code())
        self.assertEqual(e.exception.get_msg(),
                         ErrorCode.SERVICE_MISSING_PARAM.get_msg())
Beispiel #15
0
def service_search_video_by_pattern(**kw):
    if 'video_title' in kw:
        return query_video_search_by_pattern(pattern_title=kw['video_title'])
    elif 'video_channel' in kw:
        return query_video_search_by_pattern(
            pattern_channel=kw['video_channel'])
    elif 'video_description' in kw:
        return query_video_search_by_pattern(
            pattern_description=kw['video_description'])

    raise ServiceError(ErrorCode.SERVICE_PATTERN_SEARCH_NOT_SUPPORT)
Beispiel #16
0
def service_auth_video_op_post(token, user_id, video_id):
    videos = query_video_get_by_video_id(video_id)
    if len(videos) == 0:
        raise ServiceError(ErrorCode.SERVICE_VIDEO_NOT_FOUND)
    video = videos[0].to_dict()
    if video['video_status'] != 'public' and \
       video['user_id'] != token:
        return False
    if token != user_id:
        return False
    return True
Beispiel #17
0
def service_video_info(**kw):

    kw['service'] = 'video'
    kw = util_pattern_format_param(**kw)
    # keyword check and formatting
    if 'video_id' not in kw:
        raise ServiceError(ErrorCode.SERVICE_MISSING_PARAM)

    if not is_valid_id(kw["video_id"]):
        raise ServiceError(ErrorCode.SERVICE_INVALID_ID_OBJ)
    # perform db operations and get result
    video = query_video_get_by_video_id(kw["video_id"])
    if len(video) == 0:
        raise ServiceError(ErrorCode.SERVICE_VIDEO_NOT_FOUND)

    res = video[0].to_dict()
    user_id = res["user_id"]
    user_obj = query_user_get_by_id(user_id)[0].to_dict()
    res["user_name"] = user_obj["user_name"]
    res["user_thumbnail"] = user_obj["user_thumbnail"]
    return res
Beispiel #18
0
def service_video_update(**kw):

    kw['service'] = 'video'
    kw = util_pattern_format_param(**kw)

    # keyword check and formatting
    if 'video_id' not in kw:
        raise ServiceError(ErrorCode.SERVICE_MISSING_PARAM)

    if not is_valid_id(kw["video_id"]):
        raise ServiceError(ErrorCode.SERVICE_INVALID_ID_OBJ)

    if 'video_status' in kw and kw['video_status'] not in VALID_VIDEO_STATUS:
        raise ServiceError(ErrorCode.SERVICE_VIDEO_INVALID_STATUS)

    if 'video_raw_status' in kw and \
       kw['video_raw_status'] not in VALID_VIDEO_RAW_STATUS:
        raise ServiceError(ErrorCode.SERVICE_VIDEO_INVALID_STATUS)

    query_video_update(**kw)

    return query_video_get_by_video_id(kw["video_id"])
Beispiel #19
0
def service_auth_video_get(token, video_id):
    videos = query_video_get_by_video_id(video_id)
    if len(videos) == 0:
        raise ServiceError(ErrorCode.SERVICE_VIDEO_NOT_FOUND)
    video = videos[0].to_dict()
    user = video['user_id']
    status = video['video_status']
    raw = video['video_raw_status']
    if status != 'public' and user != token:
        return False
    if raw != 'streaming' and user != token:
        return False
    return True
Beispiel #20
0
def service_user_update_info(**kw):

    kw['service'] = 'user'
    kw = util_pattern_format_param(**kw)
    if 'user_id' not in kw:
        raise ServiceError(ErrorCode.SERVICE_MISSING_USER_ID)
    if 'user_status' in kw:
        query_user_update_status(kw['user_id'], kw['user_status'])
    if 'user_name' in kw:
        query_user_update_name(kw['user_id'], kw['user_name'])
    if 'user_email' in kw:
        query_user_update_email(kw['user_id'], kw['user_email'])
    if 'user_password' in kw:
        query_user_update_password(kw['user_id'], kw['user_password'])
    if 'user_thumbnail' in kw:
        query_user_update_thumbnail(kw['user_id'], kw['user_thumbnail'])
    query_user_update_details(**kw)
    return query_user_get_by_id(kw['user_id'])[0].to_dict()
Beispiel #21
0
def service_search_video_by_contains(**kw):
    """
    Currently support searching 'id', 'title', 'channel', 'category', 'tag'
    :param kw:
    :return:
    """
    if 'video_id' in kw:
        return query_video_search_by_contains(video_id=kw['video_id'])
    elif 'video_title' in kw:
        return query_video_search_by_contains(video_title=kw['video_title'])
    elif 'video_channel' in kw:
        return query_video_search_by_contains(
            video_channel=kw['video_channel'])
    elif 'video_category' in kw:
        return query_video_search_by_contains(
            video_category=kw['video_category'])
    elif 'video_tag' in kw:
        return query_video_search_by_contains(video_tag=kw['video_tag'])
    elif 'video_description' in kw:
        return query_video_search_by_contains(
            video_description=kw['video_description'])

    raise ServiceError(ErrorCode.SERVICE_INVALID_SEARCH_PARAM)
Beispiel #22
0
def service_search_user_by_contains(**kw):
    """
    choose one attribute to search one keyword, case sensitive
    :param user_name: (optional) single keyword of username to be searched
    :param user_email: (optional) single keyword of email to be searched
    :return: array of searching results
    """
    if 'user_id' in kw:
        return query_user_search_by_contains(user_id=kw['user_id'])
    elif 'user_name' in kw:
        return query_user_search_by_contains(user_name=kw['user_name'])
    elif 'user_email' in kw:
        return query_user_search_by_contains(user_email=kw['user_email'])
    elif 'user_first_name' in kw:
        return query_user_search_by_contains(
            user_first_name=kw['user_first_name'])
    elif 'user_last_name' in kw:
        return query_user_search_by_contains(
            user_last_name=kw['user_last_name'])
    elif 'user_phone' in kw:
        return query_user_search_by_contains(user_phone=kw['user_phone'])
    elif 'user_street1' in kw:
        return query_user_search_by_contains(user_street1=kw['user_street1'])
    elif 'user_street2' in kw:
        return query_user_search_by_contains(user_street2=kw['user_street2'])
    elif 'user_city' in kw:
        return query_user_search_by_contains(user_city=kw['user_city'])
    elif 'user_state' in kw:
        return query_user_search_by_contains(user_state=kw['user_state'])
    elif 'user_country' in kw:
        return query_user_search_by_contains(user_country=kw['user_country'])
    elif 'user_zip' in kw:
        return query_user_search_by_contains(user_zip=kw['user_zip'])
    elif 'user_status' in kw:
        return query_user_search_by_contains(user_status=kw['user_status'])

    raise ServiceError(ErrorCode.SERVICE_INVALID_SEARCH_PARAM)
Beispiel #23
0
def service_search_video_by_aggregation(search_dict=None, **kw):
    # Search by aggregate (can search multi attributes)
    if search_dict is None and len(kw) == 0:
        raise ServiceError(ErrorCode.SERVICE_MISSING_PARAM)
    return query_video_search_by_aggregate(search_dict)