def post(self): auth = request.json if (not auth or not auth.get("username", None) or not auth.get("password", None)): return json_response({"message": "could not verify"}, 400) user = User.get(username=auth["username"]) if check_passwords(auth["password"], user.password): token = jwt.encode( { "id": user.id, "exp": datetime.datetime.utcnow() + datetime.timedelta(hours=1500), }, os.environ.get("SECRET_KEY"), ) json_user = model_to_dict(user) json_user.pop("password") json_user["created_at"] = json_user["created_at"].isoformat() return json_response( { "token": token.decode("UTF-8"), "user": json_user }, 200) return json_response({"message": "could not verify"}, 400)
def decorator(self, *args, **kwargs): token = None if "Authorization" in request.headers: authorization = request.headers.get("Authorization") token = authorization.split(" ")[-1] if not token: return json_response({"message": "a valid token is missing"}, 401) try: data = jwt.decode(token, os.environ.get("SECRET_KEY")) current_user = User.get_by_id(data["id"]) except jwt.exceptions.DecodeError: return json_response({"message": "token is invalid"}, 401) return f(self, current_user, *args, **kwargs)
def tag_list(): ret = [] if request.args.get('rg') == 'large': records = models.get_all('Tag') for t in records: ret.append(t.format()) else: records = models.get_keys('Content') for t in records: ret.append(t.id()) return common.json_response(ret)
def content_list(): ret = [] if request.args.get("rg") == "large": records = models.get_all("Content") for t in records: ret.append(t.format()) else: records = models.get_keys("Content") for t in records: ret.append(t.id()) return common.json_response(ret)
def content_create(data): title = data.get("title") body = data.get("body") published_at = datetime.datetime.strptime(data.get("published_at"), "%Y-%m-%dT%H:%M:%S.%f") tags = [] for t in data.get("tag_nums"): k = models.Tag.get_by_num(t) if k: tags.append(k) record = models.Content(title=title, published_at=published_at, body=body, tags=tags) if data.get("num"): record.num = int(data.get("num")) else: record.put() record.num = record.key().id() record.put() return common.json_response(record.format())
def tag_create(data): logging.error(data) num = data.get('num') namespace = data.get('namespace') value = data.get('value') tag1 = models.Tag.get_key_by_namespace_and_value(namespace, value) if tag1: logging.info('tag1 namespace and value exists') return common.error_response(None, 400) parent = None if data.get('parentNum'): parentNum = data.get('parentNum') if not isinstance(parent, int): logging.error('parentNum must be int type') return common.error_response(None, 400) parent = models.get_by_num('Tag', parentNum) if not parent: logging.error('parent_num(%d) not found' % parentNum) return common.error_response(None, 400) if num: num = int(num) tag2 = models.get_by_num('Tag', num) if tag2: logging.info('tag2 namespace and value exists') return common.error_response(None, 400) tag = models.Tag(num=num, namespace=namespace, value=value, parent_tag = parent) else: tag = models.Tag(namespace=namespace, value=value, parent_tag = parent) tag.put() tag.num = tag.key().id() tag.put() return common.json_response(tag.format())
def tag_create(data): logging.error(data) num = data.get('num') namespace = data.get('namespace') value = data.get('value') tag1 = models.Tag.get_key_by_namespace_and_value(namespace, value) if tag1: logging.info('tag1 namespace and value exists') return common.error_response(None, 400) parent = None if data.get('parentNum'): parentNum = data.get('parentNum') if not isinstance(parent, int): logging.error('parentNum must be int type') return common.error_response(None, 400) parent = models.get_by_num('Tag', parentNum) if not parent: logging.error('parent_num(%d) not found' % parentNum) return common.error_response(None, 400) if num: num = int(num) tag2 = models.get_by_num('Tag', num) if tag2: logging.info('tag2 namespace and value exists') return common.error_response(None, 400) tag = models.Tag(num=num, namespace=namespace, value=value, parent_tag=parent) else: tag = models.Tag(namespace=namespace, value=value, parent_tag=parent) tag.put() tag.num = tag.key().id() tag.put() return common.json_response(tag.format())
def content_detail(content_id): record = models.Content.get_by_id(content_id) if not record: return common.error_response(None, 404) return common.json_response(record.format())
def tag_detail(tag_id): tag = models.Tag.get_by_id(tag_id) if not tag: return common.error_response(None, 404) return common.json_response(tag.format())
def tag_detail_by_num(tag_num): tag = models.get_by_num('Tag', tag_num, key_only=False) if not tag: return common.error_response(None, 404) return common.json_response(tag.format())
def tag_delete(tag_id): tag = models.Tag.get_by_id(tag_id) if not tag: return common.error_response(None, 404) tag.delete() return common.json_response(body={})
def tag_num_list(): tags = models.get_all('Tag') ret = [] for t in tags: ret.append(t.num) return common.json_response(ret)
def content_delete(content_id): record = models.Content.get_by_id(content_id) if not record: return common.error_response(None, 404) record.delete() return common.json_response(body={})
def wrap_decorator(*args, **kwargs): resp = func(*args, **kwargs) response = make_response(json_response(resp)) response.status_code = 200 response.headers['Content-Type'] = 'application/json' return response