from django import http from django.contrib.auth import authenticate, login, logout from django.contrib.auth.backends import ModelBackend # 验证基类 from django.contrib.auth.mixins import LoginRequiredMixin from django.http import JsonResponse from django.shortcuts import redirect from django.urls import reverse from rest_framework.views import APIView from server.util.constant_util import * from server.util.mix_util import build_response from django.shortcuts import render from server.taskform import TaskForm from django.utils import timezone from server.models import Task,StudentandTercher,MindUser,Liuyan,Jiaoben from server.serializers import * import json import asyncio from zhipuai import ZhipuAI def gml_chat(messages): print("--------------------") print(messages) client = ZhipuAI(api_key="92a0e4868cda21d22fe6fd0f4c0bf6fb.oZ4IE9PPDOhsLKPp") response = client.chat.completions.create( model="glm-3-turbo", # messages=[ # { # "role": "user", # "content": prompt # } # ], messages =[{ "role": "user", "content": "你现在是一名教育领域专家,请你从清晰性、完整性、布鲁姆认知层级三个方面对学生评价内容进行分析,并给出相应的分数,总分为10分,给我相应的返回结果即可。用户评论内容是:" + messages }], top_p=0.7, temperature=0.95, max_tokens=1024, stream=True, ) content = "" for trunk in response: # print(trunk.choices[0].delta) content = content + trunk.choices[0].delta.content return content class AuthBackend(ModelBackend): """ login 方法会调用该方法 """ pass # 重写验证方式 # def authenticate(self, request, username=None, password=None, **kwargs): # user = MindUser.objects.get(id=1) # if user is not None and user.check_password(password): # return user # 登录方式有多种 # 1、用户名密码登录:直接匹配数据库的用户名密码 # 2、微信扫码登录:1、生成带标记的二维码 2、用户扫码 3、微信通知服务器端 4、服务器校验是否有注册,如果没有注册则新增用户 5、服务器通知前端界面 # 3、手机号短信验证码登录 1、发送短信验证码 2、用户界面向服务器提交手机号和验证码 3、服务器请求短服务商 4、服务器校验是否有注册,如果没有注册则新增用户 class LoginView(APIView): """ """ def post(self, request): username = request.POST.get('username') password = request.POST.get('password') print(username) print(password) # django内置认证来校验登录用户 user = authenticate(username=username, password=password) if user is not None: if user.is_active and user.type_t_or_s == 2: # 记住登录状态 login(request, user) select_class = MindUser.objects.get(username=user).select_class select_xiaozu = MindUser.objects.get(username=user).select_xiaozu print("---------------------------------------------------------------------") print(select_class) print(select_xiaozu) print("---------------------------------------------------------------------") data = {"class":select_class,"xiaozu": select_xiaozu} return build_response(msg='学生端登录成功',data = data) elif user.is_active and user.type_t_or_s == 3: # 记住登录状态 # 记住登录状态 login(request, user) return build_response(msg='管理员登录成功') else: return build_response(code=CODE_FAILED, msg='登录失败') else: return build_response(code=CODE_FAILED, msg='登录失败') class LoginView_teacher(APIView): """ """ def post(self, request): username = request.POST.get('username') password = request.POST.get('password') # django内置认证来校验登录用户 user = authenticate(username=username, password=password) if user is not None: if user.is_active and user.type_t_or_s == 1: # 记住登录状态 login(request, user) return build_response(msg='老师端登录成功') else: return build_response(code=CODE_FAILED, msg='登录失败') else: return build_response(code=CODE_FAILED, msg='登录失败') class LogoutView(APIView): """ 注销 """ def get(self, request): logout(request) return build_response(msg='注销成功') def post(self, request): logout(request) return build_response(msg='注销成功') class LoginRequiredJSONMixin(LoginRequiredMixin): """自定义LoginRequiredMixin 如果用户未登录,响应JSON,且状态码为400 """ def handle_no_permission(self): # return build_response(code=CODE_FAILED, msg='用户未登录!') return http.JsonResponse({'code': CODE_FAILED, 'msg': '用户未登录'}) class TeacherView(LoginRequiredJSONMixin,APIView): """ 教师端信息展示 """ def get(self, request, *args, **kwargs): """ 通过链接即uuid、hash获得分享的导图详情 """ user = request.user print(user.type_t_or_s) if user.type_t_or_s == 2 : print("用户未登录或对当前页无权限") return build_response(code=CODE_FAILED, msg="用户未登录或对当前页无权限") else: print("用户已登录") return render(request, "html/jiaoshi1.html") def post_task(request): if request.user.type_t_or_s == 1: if request.method == 'POST': start_time = timezone.now() title = request.POST.get("title") description = request.POST.get("description") end_time = request.POST.get("deadline") rengwu = Task(title=title, description=description, publish_time=start_time, deadline=end_time,publisher=request.user) rengwu.save() return http.JsonResponse({'code': CODE_SUCCESS, 'msg': '发布成功'}) else: return render(request, 'html/post_task.html') else: return render(request, 'html/dashboard.html') def jiaoben(request): if request.user.type_t_or_s == 1: if request.method == 'POST': jiaoben = request.POST.get("jiaoben") print(jiaoben) jiaoben = Jiaoben(jiaoben=jiaoben) jiaoben.save() # rengwu = Task(title=title, description=description, publish_time=start_time, deadline=end_time,publisher=request.user) # rengwu.save() return http.JsonResponse({'code': CODE_SUCCESS, 'msg': '发布成功'}) else: return render(request, 'html/post_task.html') else: return render(request, 'html/dashboard.html') def post_kecheng(request): if request.user.type_t_or_s == 1: if request.method == 'POST': add_time = timezone.now() class_id = request.POST.get("class_id") class_name = request.POST.get("class_name") tercher_name = request.POST.get("tercher_name") class_info = request.POST.get("class_info") class_status = request.POST.get("class_status") comment_type = request.POST.get("comment_type") print("===============================") print(comment_type) evaluations = comment_type.split(',') print(evaluations) default = json.dumps({'type': evaluations}) rengwu = StudentandTercher(add_time=add_time, class_id=class_id, class_name=class_name, tercher_name=tercher_name, class_info=class_info, class_status=class_status, comment_type=default) rengwu.save() return http.JsonResponse({'code': CODE_SUCCESS, 'msg': '发布成功'}) else: return render(request, 'html/post_kecheng.html') else: return render(request, 'html/dashboard.html') def come_kecheng(request): if request.method == 'POST': if request.user.type_t_or_s == 1: return render(request, 'html/error.html') else: user = request.user kecheng_name = request.POST.get("class_name") xiaozu = request.POST.get("xiaozu") old_class = json.loads(user.select_class)['class'] old_xiaozu = json.loads(user.select_xiaozu)['xiaozu'] old_class.append(kecheng_name) old_xiaozu.append(xiaozu) new_class = json.dumps({'class': old_class}) new_xiaozu = json.dumps({'xiaozu': old_xiaozu}) print(new_xiaozu) # new_class = json.dumps({'class': old_class}) # print(new_class) save_data = MindUser.objects.filter(username=user.username).update(select_class=new_class,select_xiaozu=new_xiaozu) # save_data.save() return http.JsonResponse({'code': CODE_SUCCESS, 'msg': '发布成功'}) def comment_control(request): # 提交评论的处理函数 comment_content = request.POST.get('comment_content') article_id = request.POST.get('article_id') pid = request.POST.get('pid') id = MindUser.objects.get(username=request.user.username).id author_id = id # 获取当前用户的ID gpt_content = gml_chat(str(comment_content)) Comment.objects.create(comment_content=comment_content, pre_comment_id=pid, pinglun_id=article_id, user_id=author_id,gpt_content=gpt_content) # 将提交的数据保存到数据库中 article = list( Comment.objects.values('id', 'comment_content', 'pre_comment_id', 'pinglun_id', 'user_id', 'comment_time','gpt_content')) # 以键值对的形式取出评论对象,并且转化为列表list类型 return JsonResponse(article, safe=False) # JsonResponse返回JSON字符串,自动序列化,如果不是字典类型,则需要添加safe参数为False def admin_fun(request): # 提交评论的处理函数 if request.method == 'POST': id = request.POST.get('user_id') MindUser.objects.filter(id=id).update(type_t_or_s=1) print(id) return http.JsonResponse({'code': CODE_SUCCESS, 'msg': '修改成功'}) else: return http.JsonResponse({'code': CODE_FAILED, 'msg': '修改失败'}) def liuyan_fun(request): # 提交留言的处理函数 if request.method == 'POST': username = request.user.username userid = MindUser.objects.get(username=username).id print("-----------------") print(userid) id = request.POST.get('user_id') liuyan = request.POST.get('liuyan_con') Liuyan.objects.create(liuyan_id=id,user_id=userid,liuyan_content=liuyan,liuyan_time=timezone.now()) return http.JsonResponse({'code': CODE_SUCCESS, 'msg': '留言成功'}) else: return http.JsonResponse({'code': CODE_FAILED, 'msg': '修改失败'})