add message

This commit is contained in:
baoliang 2020-10-22 21:52:48 +08:00
parent 3927814156
commit 5b1d76639c
9 changed files with 66 additions and 32 deletions

View File

@ -14,6 +14,7 @@ urlpatterns = [
name='polls_password_recovery'), name='polls_password_recovery'),
path('password/recovery/save/', views.password_recover_step_two, path('password/recovery/save/', views.password_recover_step_two,
name='polls_password_recovery_save'), name='polls_password_recovery_save'),
path('password/reset/', views.reset_password, name='polls_password_reset'),
path('notices/list/', views.notices, name='polls_notices'), path('notices/list/', views.notices, name='polls_notices'),
path('notices/top/', views.notice_top, name='polls_notice_top'), path('notices/top/', views.notice_top, name='polls_notice_top'),
path('notices/read/', views.read_notice, name='polls_read_notice'), path('notices/read/', views.read_notice, name='polls_read_notice'),
@ -21,6 +22,8 @@ urlpatterns = [
path('notices/reject/', views.reject_notice, name='polls_reject_notice'), path('notices/reject/', views.reject_notice, name='polls_reject_notice'),
path('notices/detail/<str:notice_id>/', path('notices/detail/<str:notice_id>/',
views.detail_notice, name='polls_detail_notice'), views.detail_notice, name='polls_detail_notice'),
path('notices/unread/count/', views.unread_notice_count,
name='polls_notice_unread_count'),
path('medias/create/', views.create_media, name='polls_add_media'), path('medias/create/', views.create_media, name='polls_add_media'),
path('medias/update/', views.update_media, name='polls_update_media'), path('medias/update/', views.update_media, name='polls_update_media'),
path('medias/list/', views.medias, name='polls_medias'), path('medias/list/', views.medias, name='polls_medias'),

View File

@ -1,5 +1,5 @@
from .user import index, status_500, status_401, polls_login, send_code, register_step_one, register_step_two, password_recover_step_one, password_recover_step_two from .user import index, status_500, status_401, polls_login, send_code, register_step_one, register_step_two, password_recover_step_one, password_recover_step_two, reset_password
from .notice import notices, notice_top, read_notice, pass_notice, reject_notice, detail_notice from .notice import notices, notice_top, read_notice, pass_notice, reject_notice, detail_notice, unread_notice_count
from .media import medias, my_medias, create_media, update_media, media_detail, delete_media from .media import medias, my_medias, create_media, update_media, media_detail, delete_media
from .news import news_list, news_top, news_detail from .news import news_list, news_top, news_detail
from .monitor import monitor_statistics, monitor_result, media_statistics, city_ranking from .monitor import monitor_statistics, monitor_result, media_statistics, city_ranking

View File

@ -24,6 +24,7 @@ def last_messages(request):
result['type'] = o.type result['type'] = o.type
profile = Userprofile.objects.filter(user_id=o.send_from_id).first() profile = Userprofile.objects.filter(user_id=o.send_from_id).first()
result['user_name'] = profile.name result['user_name'] = profile.name
result['organization'] = profile.organization.name
result['content'] = o.content result['content'] = o.content
result['page_title'] = o.page_title result['page_title'] = o.page_title
result['page_description'] = o.page_description result['page_description'] = o.page_description

View File

@ -102,6 +102,8 @@ def detail_notice(request, notice_id):
results['groupId'] = notice.group_id results['groupId'] = notice.group_id
results['added'] = notice.added.strftime("%Y-%m-%d %H:%M:%S") results['added'] = notice.added.strftime("%Y-%m-%d %H:%M:%S")
results['updated'] = notice.updated.strftime("%Y-%m-%d %H:%M:%S") results['updated'] = notice.updated.strftime("%Y-%m-%d %H:%M:%S")
notice.is_read = True
notice.save()
return JsonResponse({'status': 'success', 'message': results}) return JsonResponse({'status': 'success', 'message': results})
except ObjectDoesNotExist: except ObjectDoesNotExist:
return JsonResponse({'status': 'error', 'message': '通知ID错误'}) return JsonResponse({'status': 'error', 'message': '通知ID错误'})
@ -158,3 +160,15 @@ def reject_notice(request):
return JsonResponse({'status': 'success', 'message': '申请已拒绝'}) return JsonResponse({'status': 'success', 'message': '申请已拒绝'})
except ObjectDoesNotExist: except ObjectDoesNotExist:
return JsonResponse({'status': 'error', 'message': '通知ID错误'}) return JsonResponse({'status': 'error', 'message': '通知ID错误'})
@csrf_exempt
@polls_login_required
def unread_notice_count(request):
if request.method == 'POST':
return HttpResponse(status=405)
user_id = request.user.id
count = Notice.objects.filter(user_id=user_id, is_read=False).count()
return JsonResponse({'status': 'success', 'message': count})

View File

@ -33,10 +33,10 @@ def polls_login(request):
UserModel = get_user_model() UserModel = get_user_model()
user = UserModel.objects.filter(username=phone).first() user = UserModel.objects.filter(username=phone).first()
if not user: if not user:
return JsonResponse({'status': 'error', 'message': '用户名或密码错误'}) return JsonResponse({'status': 'error', 'message': '账号不存在,请联系管理员'})
profile = user.userprofile_set.first() profile = user.userprofile_set.first()
if not profile or profile.status == 0: if not profile or profile.status == 0:
return JsonResponse({'status': 'error', 'message': '用户在审核中'}) return JsonResponse({'status': 'error', 'message': '用户尚未激话'})
u = authenticate(request, username=phone, password=password) u = authenticate(request, username=phone, password=password)
if u is not None: if u is not None:
@ -48,7 +48,8 @@ def polls_login(request):
result['token'] = token.key result['token'] = token.key
if profile: if profile:
result['name'] = profile.name result['name'] = profile.name
result['gender'] = profile.sex result['department'] = profile.department
result['post'] = profile.post
result['thumbnail'] = request.build_absolute_uri( result['thumbnail'] = request.build_absolute_uri(
profile.image.url) if profile.image else None profile.image.url) if profile.image else None
result['organization'] = profile.organization.name result['organization'] = profile.organization.name
@ -67,6 +68,10 @@ def send_code(request):
category = request.POST.get('category', 0) category = request.POST.get('category', 0)
if not phone: if not phone:
return JsonResponse({'status': 'error', 'message': '手机号不正确'}) return JsonResponse({'status': 'error', 'message': '手机号不正确'})
UserModel = get_user_model()
is_exists = UserModel.objects.filter(username=phone).exists()
if not is_exists:
return JsonResponse({'status': 'error', 'message': '账号不存在,请联系管理员'})
exist_code = VerifyCode.objects.filter( exist_code = VerifyCode.objects.filter(
phone=phone, category=category).first() phone=phone, category=category).first()
@ -78,7 +83,7 @@ def send_code(request):
after_1min = now + datetime.timedelta(minutes=1) after_1min = now + datetime.timedelta(minutes=1)
response = sent_sms_code(phone, code) response = sent_sms_code(phone, code)
result = response.decode('utf8') result = response.decode('utf8')
print(phone,result) print(phone, result)
if "OK" in result: if "OK" in result:
VerifyCode.objects.create( VerifyCode.objects.create(
code=code, phone=phone, category=category, timeouted=after_1min) code=code, phone=phone, category=category, timeouted=after_1min)
@ -98,13 +103,14 @@ def register_step_one(request):
UserModel = get_user_model() UserModel = get_user_model()
is_exists = UserModel.objects.filter(username=phone).exists() is_exists = UserModel.objects.filter(username=phone).exists()
if is_exists: if not is_exists:
return JsonResponse({'status': 'error', 'message': '该手机号已经使用'}) return JsonResponse({'status': 'error', 'message': '账号不存在,请联系管理员'})
verify_code = VerifyCode.objects.filter( verify_code = VerifyCode.objects.filter(
phone=phone, code=code, category=category).first() phone=phone, code=code, category=category).first()
if verify_code and verify_code.is_in_progress(): if verify_code and verify_code.is_in_progress():
return JsonResponse({'status': 'success', 'message': {phone: phone}}) profile = Userprofile.objects.filter(user__username=phone).first()
return JsonResponse({'status': 'success', 'message': {'id': profile.user_id, 'phone': phone, 'name': profile.name, 'organization': profile.organization.name}})
else: else:
return JsonResponse({'status': 'error', 'message': '验证码超时,请重发'}) return JsonResponse({'status': 'error', 'message': '验证码超时,请重发'})
@ -115,37 +121,30 @@ def register_step_two(request):
return HttpResponse(status=405) return HttpResponse(status=405)
phone = request.POST.get('phone') phone = request.POST.get('phone')
if not phone: if not phone:
return JsonResponse({'status': 'error', 'message': '手机号不正确'}) return JsonResponse({'status': 'error', 'message': '号不正确'})
name = request.POST.get('name') department = request.POST.get('department')
if not name: if not department:
return JsonResponse({'status': 'error', 'message': '姓名不能为空'}) return JsonResponse({'status': 'error', 'message': '处(科)室不能为空'})
password = request.POST.get('password') password = request.POST.get('password')
if not password: if not password:
return JsonResponse({'status': 'error', 'message': '密码不能为空'}) return JsonResponse({'status': 'error', 'message': '密码不能为空'})
unit = request.POST.get('unit') if len(password) < 6:
if not unit: return JsonResponse({'status': 'error', 'message': '密码长度至少6位'})
return JsonResponse({'status': 'error', 'message': '单位不能为空'})
department = request.POST.get('department')
if not department:
return JsonResponse({'status': 'error', 'message': '处/科室不能为空'})
post = request.POST.get('post') post = request.POST.get('post')
if not post: if not post:
return JsonResponse({'status': 'error', 'message': '职务不能为空'}) return JsonResponse({'status': 'error', 'message': '职务不能为空'})
profile = Userprofile.objects.filter(user__username=phone).first()
if not profile:
return JsonResponse({'status': 'error', 'message': '账号不存在'})
profile.department = department
profile.post = post
profile.save()
UserModel = get_user_model() UserModel = get_user_model()
is_exists = UserModel.objects.filter(username=phone).exists() user = UserModel.objects.get(username=phone)
if is_exists: user.set_password(password)
return JsonResponse({'status': 'error', 'message': '该手机号已经使用'}) user.save()
return JsonResponse({'status': 'success', 'message': '激活成功'})
u = UserModel.objects.create_user(username=phone, password=password)
profile = Userprofile.objects.create(name=name, user=u, status=0, unit=unit, department=department, post=post)
admins = Userprofile.objects.filter(organization__level__level=1)
for a in admins:
content = '用户%s注册了账号,请审核' % (name,)
Notice.create_reply_notice(a.user.id, content, 'dashboard', 'Userprofile', 'status', profile.id, '1')
return JsonResponse({'status': 'success', 'message': '注册成功'})
@csrf_exempt @csrf_exempt
@ -190,3 +189,20 @@ def password_recover_step_two(request):
user.set_password(password) user.set_password(password)
user.save() user.save()
return JsonResponse({'status': 'success', 'message': '密码修改成功'}) return JsonResponse({'status': 'success', 'message': '密码修改成功'})
@csrf_exempt
def reset_password(request):
if request.method == 'GET':
return HttpResponse(status=405)
password = request.POST.get('password')
if not password:
return JsonResponse({'status': 'error', 'message': '密码不能为空'})
if len(password) < 6:
return JsonResponse({'status': 'error', 'message': '密码长度不能少于6位'})
user_id = request.user.id
UserModel = get_user_model()
user = UserModel.objects.get(id=user_id)
user.set_password(password)
user.save()
return JsonResponse({'status': 'success', 'message': '密码修改成功'})