Пример #1
0
def validate_dict(json: Dict, key: str) -> ResultWithData[Dict]:
    if key not in json:
        return get_result_with_error(f"Missing {key}")

    value = json[key]
    if type(value) is not dict:
        return get_result_with_error(f"{key} must be an object")

    return get_result_with_data(value)
Пример #2
0
def validate_short_id(id_str: str) -> ResultWithData[str]:
    if len(id_str) != 4:
        return get_result_with_error(ID_NOT_LENGTH_FOUR)

    for c in id_str:
        if c not in string.ascii_lowercase:
            return get_result_with_error(ID_NOT_LOWERCASE)

    return get_result_with_data(id_str)
Пример #3
0
def validate_str(json: Dict, key: str) -> ResultWithData[str]:
    if key not in json:
        return get_result_with_error(f"Missing {key}")

    str_str = json[key]
    if isinstance(str_str, str):
        return get_result_with_data(str_str)
    else:
        return get_result_with_error(
            f"Error in {key}: {str_str} is not a string")
Пример #4
0
def validate_bool(json: Dict, key: str) -> ResultWithData[bool]:
    if key not in json:
        return get_result_with_error(f"Missing {key}")

    bool_str = json[key]
    try:
        val = bool(bool_str)
        return get_result_with_data(val)
    except ValueError:
        return get_result_with_error(
            f"Error in {key}: {bool_str} is not a valid boolean")
Пример #5
0
def validate_int(json: Dict, key: str) -> ResultWithData[int]:
    if key not in json:
        return get_result_with_error(f"Missing {key}")

    int_str = json[key]
    try:
        val = int(int_str)
        return get_result_with_data(val)
    except ValueError:
        return get_result_with_error(
            f"Error in {key}: {int_str} is not a valid integer")
Пример #6
0
def remove_tag(tag_id: str) -> ResultWithData:
    tag = Tag.get(tag_id=tag_id)
    if tag is None:
        return get_result_with_error(TAG_ID_NOT_EXIST)
    else:
        tag.delete()
        return get_result_with_data("The tag was successfully deleted")
Пример #7
0
def validate_short_id_key(json: Dict, key: str) -> ResultWithData[str]:
    if key not in json:
        return get_result_with_error(f"Missing {key}")
    id_str = json[key]

    res = validate_short_id(id_str)
    res.message = f"Error in {key}: " + res.message
    return res
Пример #8
0
def validate_song_update(song: Dict,
                         song_id: str) -> ResultWithData[RequestSongObject]:
    id_res = validate_short_id(song_id)
    if id_res.is_error:
        return get_result_with_error(id_res.message)

    valid_song_res = validate_song(song)
    if valid_song_res.is_error:
        return get_result_with_error(valid_song_res.message)
    valid_song = valid_song_res.data

    return get_result_with_data(
        RequestSongObject(song_id=id_res.data,
                          title=valid_song.title,
                          melody=valid_song.melody,
                          author=valid_song.author,
                          text=valid_song.text,
                          tags=valid_song.tags))
Пример #9
0
def validate_song(song: Dict) -> ResultWithData[RequestSongObject]:
    title_res = validate_required_str(song, 'title')
    if title_res.is_error:
        return get_result_with_error(title_res.message)

    melody_res = validate_str(song, 'melody')
    if melody_res.is_error:
        return get_result_with_error(melody_res.message)

    author_res = validate_str(song, 'author')
    if author_res.is_error:
        return get_result_with_error(author_res.message)

    text_res = validate_required_str(song, 'text')
    if text_res.is_error:
        return get_result_with_error(text_res.message)

    tags_res = validate_list(song, 'tags')
    if tags_res.is_error:
        return get_result_with_error(tags_res.message)
    tag_ids = []
    for tag in tags_res.data:
        tag_id_res = validate_short_id(tag)
        if tag_id_res.is_error:
            return get_result_with_error(tag_id_res.message)
        tag_ids.append(tag_id_res.data)

    return get_result_with_data(
        RequestSongObject(song_id=None,
                          title=title_res.data,
                          melody=melody_res.data,
                          author=author_res.data,
                          text=text_res.data,
                          tags=tag_ids))
Пример #10
0
def get_tag_by_id(tag_id: str) -> ResultWithData[TagObject]:
    tag = Tag.get(tag_id=tag_id)
    if tag is None:
        return get_result_with_error(TAG_ID_NOT_EXIST)
    else:
        return get_result_with_data(db_tag_to_tag_object(tag))
Пример #11
0
def get_song_by_name(title: str) -> ResultWithData[SongObject]:
    song = Song.get(title=title)
    if song is None:
        return get_result_with_error(SONG_TITLE_NOT_EXIST)
    else:
        return get_result_with_data(db_song_to_song_object(song))
Пример #12
0
def get_song_by_id(song_id: str) -> ResultWithData[SongObject]:
    song = Song.get(song_id=song_id)
    if song is None:
        return get_result_with_error(SONG_ID_NOT_EXIST)
    else:
        return get_result_with_data(db_song_to_song_object(song))