You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
74 lines
2.4 KiB
Python
74 lines
2.4 KiB
Python
import time
|
|
import datetime
|
|
import jwt
|
|
import hashlib
|
|
from os import path
|
|
from django.conf import settings
|
|
from rest_framework.response import Response
|
|
from django.contrib.auth.models import User
|
|
|
|
|
|
def generate_token(user):
|
|
"""
|
|
:param user: 用户对象
|
|
:return: 生成的token
|
|
"""
|
|
payload = {
|
|
'user_id': user.id,
|
|
'username': user.username,
|
|
'exp': datetime.datetime.utcnow() + datetime.timedelta(weeks=1) # token过期时间 1week
|
|
}
|
|
token = jwt.encode(payload=payload, key=settings.SECRET_KEY, algorithm='HS256')
|
|
return token
|
|
|
|
|
|
def decode_token_exp_time(token):
|
|
try:
|
|
res_dict = jwt.decode(token, settings.SECRET_KEY, algorithms=['HS256'])
|
|
exp = res_dict.get('exp')
|
|
exp_time = time.ctime(exp)
|
|
return exp_time
|
|
except Exception as e:
|
|
return None
|
|
|
|
|
|
def update_tp_querydict(querydict):
|
|
new_querydict = dict()
|
|
if 'record_time' in querydict:
|
|
new_querydict['record_time__icontains'] = ''.join(querydict.pop('record_time'))
|
|
if 'police_id' in querydict:
|
|
new_querydict['police_id__icontains'] = ''.join(querydict.pop('police_id'))
|
|
if 'event_type' in querydict:
|
|
new_querydict['event_type__icontains'] = ''.join(querydict.pop('event_type'))
|
|
if 'start_time' in querydict:
|
|
new_querydict['record_time__gte'] = ''.join(querydict.pop('start_time'))
|
|
if 'end_time' in querydict:
|
|
new_querydict['record_time__lte'] = datetime.datetime.strptime(''.join(querydict.pop('end_time')), "%Y-%m-%d") + datetime.timedelta(days=1)
|
|
if 'violation' in querydict:
|
|
if ''.join(querydict.get('violation')) == '1':
|
|
new_querydict['is_violation'] = True
|
|
elif ''.join(querydict.get('violation')) == '0':
|
|
new_querydict['is_violation'] = False
|
|
if 'violation_type' in querydict:
|
|
new_querydict['ai_analysis__icontains'] = ''.join(querydict.pop('violation_type'))
|
|
return new_querydict
|
|
|
|
|
|
def get_file_hash(filename):
|
|
if path.isfile(filename) is False:
|
|
return
|
|
|
|
# make a hash object
|
|
h_sha256 = hashlib.sha256()
|
|
|
|
# open file for reading in binary mode
|
|
with open(filename, "rb") as file:
|
|
# read file in chunks and update hash
|
|
chunk = 0
|
|
while chunk != b"":
|
|
chunk = file.read(1024)
|
|
h_sha256.update(chunk)
|
|
|
|
# return the hex digest
|
|
return h_sha256.hexdigest()
|