示例#1
0
文件: views.py 项目: rkdl/codeshare
def update():
    request_params = request.get_json(force=True)

    text = request_params.get('text')
    language = request_params.get('language')
    expire_time = request_params.get('expireTime')
    identifier = request_params.get('identifier')

    if not text or not language or not expire_time:
        return jsonify_error(error_type='MISSING_REQUIRED_PARAMS')

    text_document = Texts.get_by_identifier(identifier)

    if not text_document:
        return jsonify_error(error_type='TEXT_NOT_FOUND')

    if text_document['expire_datetime'] < datetime.now():
        return jsonify_error(error_type='TEXT_IS_EXPIRED')

    if text_document['user_identifier']:
        try:
            user_identifier = get_user_identifier()
        except Exception as error:
            return jsonify_error(error_type=error.args[0])
        if text_document['user_identifier'] != user_identifier:
            return jsonify_error(error_type='ACCESS_DENIED')

    expire_datetime = datetime.strptime(expire_time, STD_DATE_FORMAT)
    text_document['text'] = text
    text_document['language'] = language
    text_document['expire_datetime'] = expire_datetime
    Texts.update({'identifier': text_document['identifier']}, text_document)

    return jsonify_ok(data=None)
示例#2
0
文件: views.py 项目: rkdl/codeshare
def user_statistics():
    try:
        user_identifier = get_user_identifier()
    except Exception as error:
        return jsonify_error(error_type=error.args[0])

    stats = Texts.get_statistics(user_identifier)
    return jsonify_ok(data={'textStats': snake_to_camelcase_strategy(stats)})
示例#3
0
文件: views.py 项目: rkdl/codeshare
def random():
    request_params = request.get_json(force=True)
    expire_time = request_params.get('expireTime')

    expire_datetime = (expire_time
                       and datetime.strptime(expire_time, STD_DATE_FORMAT))
    text_document = Texts.get_random(expire_datetime)

    return jsonify_ok(text_document
                      and prepare_text_document_structure(text_document))
示例#4
0
文件: views.py 项目: rkdl/codeshare
def get_all():
    try:
        user_identifier = get_user_identifier()
    except Exception as error:
        return jsonify_error(error_type=error.args[0])

    all_texts = Texts.get_all_by_user_identifier(user_identifier)

    result = list()
    for text_document in all_texts:
        result.append(prepare_text_document_structure(text_document))

    return jsonify_ok(data=result)
示例#5
0
文件: views.py 项目: rkdl/codeshare
def read():
    request_params = request.get_json(force=True)
    text_identifier = request_params.get('identifier')
    if not text_identifier:
        return jsonify_error(error_type='MISSING_REQUIRED_PARAMS')

    text_document = Texts.get_by_identifier(text_identifier)

    if not text_document:
        return jsonify_error(error_type='TEXT_NOT_FOUND')
    if text_document['expire_datetime'] < datetime.now():
        return jsonify_error(error_type='TEXT_IS_EXPIRED')

    return jsonify_ok(data=prepare_text_document_structure(text_document))
示例#6
0
文件: views.py 项目: rkdl/codeshare
def delete():
    request_params = request.get_json(force=True)
    identifier = request_params.get('identifier')
    text_document = Texts.get_by_identifier(identifier)
    if not text_document:
        return jsonify_error(error_type='TEXT_NOT_FOUND')

    if text_document['user_identifier']:
        try:
            user_identifier = get_user_identifier()
        except Exception as error:
            return jsonify_error(error_type=error.args[0])

        if text_document['user_identifier'] != user_identifier:
            return jsonify_error(error_type='ACCESS_DENIED')

    Texts.delete({'identifier': identifier})

    return jsonify_ok(data=None)
示例#7
0
文件: views.py 项目: rkdl/codeshare
def create():
    request_params = request.get_json(force=True)
    text = request_params.get('text')
    language = request_params.get('language')
    expire_time = request_params.get('expireTime')

    if not text or not language or not expire_time:
        return jsonify_error(error_type='MISSING_REQUIRED_PARAMS')

    expire_datetime = datetime.strptime(expire_time, STD_DATE_FORMAT)

    try:
        user_identifier = get_user_identifier()
    except Exception as error:
        if (error.args[0] != 'MISSING_ACCESS_TOKEN'):
            return jsonify_error(error_type=error.args[0])
        else:
            user_identifier = None

    result = Texts.create(text, language, expire_datetime, user_identifier)
    inserted_id = result.inserted_id
    text_document = Texts.get_by_mongo_id(inserted_id)

    return jsonify_ok(data={'identifier': text_document['identifier']})
示例#8
0
文件: views.py 项目: rkdl/codeshare
def total_statistics():
    return jsonify_ok(
        data={
            'textStats': snake_to_camelcase_strategy(Texts.get_statistics())
        })