2020-07-27 09:38:53 +00:00
|
|
|
from captcha.helpers import captcha_image_url
|
|
|
|
from captcha.models import CaptchaStore
|
2020-09-17 08:10:13 +00:00
|
|
|
from dateutil import relativedelta
|
2020-07-27 09:38:53 +00:00
|
|
|
from django.contrib import messages
|
|
|
|
from django.contrib.auth import logout, authenticate, login
|
|
|
|
from django.contrib.auth.models import User
|
|
|
|
from django.db.models import Q
|
|
|
|
from django.http import HttpResponseRedirect, JsonResponse
|
2020-07-24 03:01:32 +00:00
|
|
|
from django.shortcuts import render
|
2020-07-27 09:38:53 +00:00
|
|
|
import datetime
|
|
|
|
|
2020-07-24 03:01:32 +00:00
|
|
|
# Create your views here.
|
2020-09-17 08:10:13 +00:00
|
|
|
from dashboard.models import Userprofile, Organization, Area_code_2020, Weixin, Weibo, Toutiao, Qita, News, Douyin
|
2020-07-27 09:38:53 +00:00
|
|
|
|
|
|
|
|
2020-08-20 07:50:21 +00:00
|
|
|
def refresh_captcha(request):
|
|
|
|
to_json_response = dict()
|
|
|
|
to_json_response['status'] = 1
|
|
|
|
to_json_response['new_cptch_key'] = CaptchaStore.generate_key()
|
|
|
|
to_json_response['new_cptch_image'] = captcha_image_url(to_json_response['new_cptch_key'])
|
|
|
|
return JsonResponse(to_json_response)
|
2020-09-17 08:10:13 +00:00
|
|
|
|
|
|
|
|
2020-08-20 07:50:21 +00:00
|
|
|
def index(request):
|
|
|
|
weixin_count = Weixin.objects.all().count()
|
|
|
|
weibo_count = Weibo.objects.all().count()
|
|
|
|
toutiao_count = Toutiao.objects.all().count()
|
|
|
|
qita_count = Qita.objects.all().count()
|
2020-09-17 08:10:13 +00:00
|
|
|
douyin_count = Douyin.objects.all().count()
|
|
|
|
organization_count = Organization.objects.all().count()
|
|
|
|
year = datetime.datetime.now().year
|
|
|
|
month_from = datetime.datetime.now().month
|
|
|
|
month_to = int(month_from)-1
|
|
|
|
return render(request, 'dashboard/index.html',
|
|
|
|
{'weixin_count': weixin_count, 'weibo_count': weibo_count, 'toutiao_count': toutiao_count,
|
|
|
|
'qita_count': qita_count, 'douyin_count': douyin_count, 'organization_count': organization_count,'year':year,'month_from':month_from,'month_to':month_to})
|
|
|
|
|
|
|
|
|
2020-08-20 07:50:21 +00:00
|
|
|
def user_login(request):
|
|
|
|
username = None
|
|
|
|
password = None
|
|
|
|
hash_key = CaptchaStore.generate_key()
|
|
|
|
image_url = captcha_image_url(hash_key)
|
|
|
|
if request.method == 'POST':
|
|
|
|
if not request.POST.get('username'):
|
|
|
|
messages.error(request, '请输入用户名')
|
|
|
|
else:
|
|
|
|
username = request.POST.get('username')
|
|
|
|
if not request.POST.get('password'):
|
|
|
|
messages.error(request, '请输入密码')
|
|
|
|
else:
|
|
|
|
password = request.POST.get('password')
|
|
|
|
captcha_input = request.POST.get('captcha_1')
|
|
|
|
captcha_hashkey = request.POST.get('captcha_0')
|
|
|
|
|
|
|
|
try:
|
|
|
|
CaptchaStore.objects.get(response=captcha_input.lower(), hashkey=captcha_hashkey,
|
|
|
|
expiration__gt=datetime.datetime.now()).delete()
|
|
|
|
except CaptchaStore.DoesNotExist:
|
|
|
|
messages.error(request, '验证码错误')
|
|
|
|
return HttpResponseRedirect('/login/')
|
2020-09-17 08:10:13 +00:00
|
|
|
print(username, password)
|
2020-08-20 07:50:21 +00:00
|
|
|
if username is not None and password is not None:
|
|
|
|
try:
|
2020-08-23 04:06:04 +00:00
|
|
|
user = authenticate(username=username, password=password)
|
|
|
|
# user = User.objects.get(username=phone)
|
2020-08-20 07:50:21 +00:00
|
|
|
if user is not None:
|
|
|
|
if user.check_password(password):
|
|
|
|
login(request, user)
|
|
|
|
return HttpResponseRedirect('/')
|
|
|
|
else:
|
|
|
|
messages.error(request, '无效的账号')
|
|
|
|
else:
|
|
|
|
messages.error(request, '账号或密码错误,请您确认账号和密码')
|
|
|
|
except:
|
2020-08-29 02:21:12 +00:00
|
|
|
messages.error(request, '账号或密码错误,请您确认账号和密码')
|
2020-09-17 08:10:13 +00:00
|
|
|
return render(request, 'dashboard/login.html', {'hash_key': hash_key, 'image_url': image_url})
|
|
|
|
|
|
|
|
|
2020-08-20 07:50:21 +00:00
|
|
|
def user_logout(request):
|
|
|
|
logout(request)
|
|
|
|
return HttpResponseRedirect('/')
|
2020-09-17 08:10:13 +00:00
|
|
|
|
|
|
|
|
2020-08-20 07:50:21 +00:00
|
|
|
def register(request):
|
|
|
|
username = None
|
|
|
|
email = None
|
|
|
|
password = None
|
|
|
|
confirm_password = None
|
|
|
|
name = None
|
2020-08-23 04:06:04 +00:00
|
|
|
# phone = None
|
2020-08-20 07:50:21 +00:00
|
|
|
image = None
|
|
|
|
flag = False
|
|
|
|
o = None
|
|
|
|
sex = None
|
|
|
|
hash_key = CaptchaStore.generate_key()
|
|
|
|
image_url = captcha_image_url(hash_key)
|
|
|
|
organization = Organization.objects.all()
|
|
|
|
if request.method == 'POST':
|
|
|
|
if not request.POST.get('organization'):
|
|
|
|
messages.error(request, '请选择单位')
|
|
|
|
else:
|
|
|
|
o = request.POST.get('organization')
|
|
|
|
if not request.POST.get('name'):
|
|
|
|
messages.error(request, '请输入姓名')
|
|
|
|
else:
|
|
|
|
name = request.POST.get('name')
|
|
|
|
if not request.POST.get('email'):
|
|
|
|
messages.error(request, '请输入邮箱')
|
|
|
|
else:
|
|
|
|
email = request.POST.get('email')
|
2020-08-23 04:06:04 +00:00
|
|
|
# if not request.POST.get('phone'):
|
|
|
|
# messages.error(request, '请输入电话')
|
|
|
|
# else:
|
|
|
|
# phone = request.POST.get('phone')
|
2020-08-20 07:50:21 +00:00
|
|
|
if not request.POST.get('username'):
|
|
|
|
messages.error(request, '请输入用户名')
|
|
|
|
else:
|
|
|
|
username = request.POST.get('username')
|
|
|
|
|
|
|
|
if not request.POST.get('password'):
|
|
|
|
messages.error(request, '请输入密码')
|
|
|
|
else:
|
|
|
|
password = request.POST.get('password')
|
|
|
|
if not request.POST.get('confirm_password'):
|
|
|
|
messages.error(request, '请再次输入密码')
|
|
|
|
else:
|
|
|
|
confirm_password = request.POST.get('confirm_password')
|
|
|
|
if not request.FILES.get('image'):
|
|
|
|
messages.error(request, '请选择头像')
|
|
|
|
else:
|
|
|
|
image = request.FILES.get('image')
|
2020-09-17 08:10:13 +00:00
|
|
|
print(str(image) + "1111111111111111111111111111111111111111111")
|
2020-08-23 04:06:04 +00:00
|
|
|
if request.POST.get('sex') == '1':
|
2020-08-20 07:50:21 +00:00
|
|
|
sex = '男'
|
2020-08-23 04:06:04 +00:00
|
|
|
elif request.POST.get('sex') == '2':
|
2020-08-20 07:50:21 +00:00
|
|
|
sex = '女'
|
|
|
|
captcha_input = request.POST.get('captcha_1')
|
|
|
|
captcha_hashkey = request.POST.get('captcha_0')
|
|
|
|
|
|
|
|
try:
|
|
|
|
CaptchaStore.objects.get(response=captcha_input.lower(), hashkey=captcha_hashkey,
|
|
|
|
expiration__gt=datetime.datetime.now()).delete()
|
|
|
|
except CaptchaStore.DoesNotExist:
|
|
|
|
messages.error(request, '验证码错误')
|
|
|
|
return HttpResponseRedirect('/register/')
|
|
|
|
if password is not None and confirm_password is not None:
|
|
|
|
if password == confirm_password:
|
|
|
|
flag = True
|
|
|
|
else:
|
|
|
|
messages.error(request, '两次输入的密码不一致,请重新输入')
|
|
|
|
|
|
|
|
filter_result = User.objects.filter(username=username)
|
|
|
|
if len(filter_result) > 0:
|
2020-08-23 04:06:04 +00:00
|
|
|
messages.error(request, '对不起,您输入的电话号码已被注册')
|
|
|
|
return HttpResponseRedirect('/register/')
|
2020-08-20 07:50:21 +00:00
|
|
|
|
|
|
|
if username is not None and password is not None and confirm_password is not None and email is not None and flag:
|
2020-09-17 08:10:13 +00:00
|
|
|
user = User.objects.create_user(username, email, password)
|
2020-08-20 07:50:21 +00:00
|
|
|
user.is_active = True
|
2020-08-23 04:06:04 +00:00
|
|
|
# user.is_staff = True
|
|
|
|
# user.first_name = phone
|
2020-08-20 07:50:21 +00:00
|
|
|
user.save()
|
2020-09-17 08:10:13 +00:00
|
|
|
userprofile = Userprofile(name=name, image=image, user_id=user.id, organization_id=o, sex=sex, status=0)
|
2020-08-20 07:50:21 +00:00
|
|
|
userprofile.save()
|
|
|
|
messages.success(request, '注册成功,请登录')
|
|
|
|
return HttpResponseRedirect('/login/')
|
2020-09-17 08:10:13 +00:00
|
|
|
return render(request, 'dashboard/register.html',
|
|
|
|
{'hash_key': hash_key, 'image_url': image_url, 'organization': organization})
|
|
|
|
|
2020-08-20 07:50:21 +00:00
|
|
|
|
|
|
|
def get_province(request):
|
2020-09-17 08:10:13 +00:00
|
|
|
# 省
|
2020-08-20 07:50:21 +00:00
|
|
|
province = Area_code_2020.objects.filter(level=1)
|
|
|
|
res = []
|
|
|
|
for i in province:
|
2020-09-17 08:10:13 +00:00
|
|
|
res.append([i.code, i.name, i.level, i.pcode])
|
|
|
|
return JsonResponse({"province": res})
|
|
|
|
|
|
|
|
|
2020-08-20 07:50:21 +00:00
|
|
|
def get_city(request):
|
|
|
|
code = request.GET.get('code')
|
|
|
|
print(code)
|
2020-09-17 08:10:13 +00:00
|
|
|
# 市
|
2020-08-20 07:50:21 +00:00
|
|
|
cities = Area_code_2020.objects.filter(pcode=code)
|
|
|
|
res = []
|
|
|
|
for i in cities:
|
2020-09-17 08:10:13 +00:00
|
|
|
res.append([i.code, i.name, i.level, i.pcode])
|
2020-08-20 07:50:21 +00:00
|
|
|
return JsonResponse({"city": res})
|
2020-09-17 08:10:13 +00:00
|
|
|
|
|
|
|
|
2020-08-20 07:50:21 +00:00
|
|
|
def get_district(request):
|
|
|
|
code = request.GET.get('code')
|
2020-09-17 08:10:13 +00:00
|
|
|
# 县
|
2020-08-20 07:50:21 +00:00
|
|
|
district = Area_code_2020.objects.filter(pcode=code)
|
|
|
|
res = []
|
|
|
|
for i in district:
|
|
|
|
res.append([i.code, i.name, i.level, i.pcode])
|
|
|
|
return JsonResponse({"district": res})
|
2020-09-17 08:10:13 +00:00
|
|
|
|
|
|
|
|
2020-08-20 07:50:21 +00:00
|
|
|
def get_town(request):
|
|
|
|
code = request.GET.get('code')
|
|
|
|
# 乡
|
|
|
|
town = Area_code_2020.objects.filter(pcode=code)
|
|
|
|
res = []
|
|
|
|
for i in town:
|
|
|
|
res.append([i.code, i.name, i.level, i.pcode])
|
|
|
|
return JsonResponse({"town": res})
|
2020-09-17 08:10:13 +00:00
|
|
|
|
|
|
|
|
2020-08-20 07:50:21 +00:00
|
|
|
def get_village(request):
|
|
|
|
code = request.GET.get('code')
|
|
|
|
# 村
|
|
|
|
village = Area_code_2020.objects.filter(pcode=code)
|
|
|
|
res = []
|
|
|
|
for i in village:
|
|
|
|
res.append([i.code, i.name, i.level, i.pcode])
|
|
|
|
return JsonResponse({"village": res})
|
2020-09-17 08:10:13 +00:00
|
|
|
|
2020-09-13 06:06:21 +00:00
|
|
|
|
|
|
|
|