2020-08-20 07:38:42 +00:00
|
|
|
from captcha.helpers import captcha_image_url
|
|
|
|
from captcha.models import CaptchaStore
|
|
|
|
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
|
|
|
|
from django.shortcuts import render
|
|
|
|
import datetime
|
|
|
|
|
|
|
|
|
|
|
|
# Create your views here.
|
|
|
|
from dashboard.models import Userprofile, Organization, Area_code_2020, Weixin, Weibo, Toutiao, Qita
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
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()
|
|
|
|
return render(request,'dashboard/index.html',{'weixin_count':weixin_count,'weibo_count':weibo_count,'toutiao_count':toutiao_count,'qita_count':qita_count})
|
|
|
|
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/')
|
|
|
|
if username is not None and password is not None:
|
|
|
|
try:
|
|
|
|
# user = authenticate(username=username, password=password)
|
|
|
|
user = User.objects.get(Q(username=username) | Q(first_name=username) | Q(email=username))
|
|
|
|
if user is not None:
|
|
|
|
if user.check_password(password):
|
|
|
|
login(request, user)
|
|
|
|
return HttpResponseRedirect('/')
|
|
|
|
else:
|
|
|
|
messages.error(request, '无效的账号')
|
|
|
|
else:
|
|
|
|
messages.error(request, '账号或密码错误,请您确认账号和密码')
|
|
|
|
except:
|
|
|
|
messages.error(request, '账号或密码错误,请您确认账号和密码')
|
|
|
|
return render(request,'dashboard/login.html',{'hash_key':hash_key,'image_url':image_url})
|
|
|
|
def user_logout(request):
|
|
|
|
logout(request)
|
|
|
|
return HttpResponseRedirect('/')
|
|
|
|
def register(request):
|
|
|
|
username = None
|
|
|
|
email = None
|
|
|
|
password = None
|
|
|
|
confirm_password = None
|
|
|
|
name = None
|
|
|
|
phone = None
|
|
|
|
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')
|
|
|
|
if not request.POST.get('phone'):
|
|
|
|
messages.error(request, '请输入电话')
|
|
|
|
else:
|
|
|
|
phone = request.POST.get('phone')
|
|
|
|
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')
|
|
|
|
print(str(image)+"1111111111111111111111111111111111111111111")
|
|
|
|
if request.POST.get('sex') == 0:
|
|
|
|
sex = '男'
|
|
|
|
else:
|
|
|
|
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:
|
|
|
|
messages.error(request, '对不起,您输入的用户名已被注册')
|
|
|
|
return render(request, "dashboard/register.html")
|
|
|
|
|
|
|
|
if username is not None and password is not None and confirm_password is not None and email is not None and flag:
|
|
|
|
user = User.objects.create_user(username, email, password)
|
|
|
|
user.is_active = True
|
|
|
|
user.is_staff = True
|
|
|
|
user.first_name = phone
|
|
|
|
user.save()
|
|
|
|
userprofile = Userprofile(name=name,image=image,user_id=user.id,organization_id=o,sex=sex)
|
|
|
|
userprofile.save()
|
|
|
|
messages.success(request, '注册成功,请登录')
|
|
|
|
return HttpResponseRedirect('/login/')
|
|
|
|
return render(request, 'dashboard/register.html', {'hash_key': hash_key, 'image_url': image_url,'organization':organization})
|
|
|
|
|
|
|
|
def get_province(request):
|
|
|
|
#省
|
|
|
|
province = Area_code_2020.objects.filter(level=1)
|
|
|
|
res = []
|
|
|
|
for i in province:
|
|
|
|
res.append([i.code,i.name,i.level,i.pcode])
|
|
|
|
return JsonResponse({"province":res})
|
|
|
|
def get_city(request):
|
|
|
|
code = request.GET.get('code')
|
|
|
|
print(code)
|
|
|
|
#市
|
|
|
|
cities = Area_code_2020.objects.filter(pcode=code)
|
|
|
|
res = []
|
|
|
|
for i in cities:
|
|
|
|
res.append([i.code,i.name,i.level,i.pcode])
|
|
|
|
return JsonResponse({"city": res})
|
|
|
|
def get_district(request):
|
|
|
|
code = request.GET.get('code')
|
|
|
|
#县
|
|
|
|
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})
|
|
|
|
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})
|
|
|
|
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})
|