shorturl_be/lib/functions.py
2020-08-05 00:28:01 +08:00

44 lines
1.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from .db import getUrlByTail, getTailByUrl, insertTail
from .systemSwitch import getTail, getTime
# 处理新增 参数url
def manageInsert(request):
# 校验参数
try:
data = request.json
if not data.__contains__('url'):
raise Exception
except Exception as e:
print(e)
return '数据不合法', 400
url = data['url']
tail_res = getTailByUrl(url)
# 如果存在对应尾缀或者数据库查询失败就直接返回
if tail_res[-1] != 102:
return tail_res
# 生成新的尾缀
tail = getTail()
# 查询是否存在重复的尾缀,直到生成一个不重复的尾缀
while getUrlByTail(tail)[-1] != 100:
tail = getTail()
# 组织数据
data = {
'url': url,
'time': getTime(),
'tail': tail
}
# 插入数据
insert_res = insertTail(data)
if(insert_res[-1] == 200):
return {'tail': tail}, 200
return insert_res
# 访问链接进行跳转
def manageRequest(tail):
if(len(str(tail)) != 5):
return ('参数错误', 400)
url_res = getUrlByTail(tail)
if(url_res[-1] != 200):
return ('无该链接', 404)
return url_res[0]['url'], 200