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)
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)})
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))
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)
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))
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)
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']})
def total_statistics(): return jsonify_ok( data={ 'textStats': snake_to_camelcase_strategy(Texts.get_statistics()) })