def user_login(request): if request.method == 'GET': return render(request, 'user/login.html') else: login_form = LoginForm(request.POST) if login_form.is_valid(): email = login_form.cleaned_data['username'] password = login_form.cleaned_data['password'] user = authenticate(username=email, password=password) if user: if user.is_start: login(request, user) msg = UserMessage() msg.userid_id = user.id msg.msg_status = True msg.message = '欢迎登录尚在线' msg.save() # return redirect(reverse('index')) url = request.COOKIES.get('url', '/') ret = redirect(url) ret.delete_cookie('url') return ret else: return HttpResponse('请去激活') else: return render(request, 'user/login.html', {'msg': '用户名或密码错误'}) else: return render(request, 'user/login.html', {'login_form': login_form})
def post(self, request): register_form = RegisterForm(request.POST) # 通过form检查字段合法 if register_form.is_valid(): user_name = request.POST.get('email', '') # 判断邮箱是否已经注册 if UserProfile.objects.filter(email=user_name): return render(request, 'register.html', { 'register_form': register_form, 'msg': '用户已存在' }) pass_word = request.POST.get('password', '') user = UserProfile() user.username = user_name user.email = user_name user.password = make_password(pass_word) # 默认激活状态为false user.is_active = False user.save() # 用户信息通知 usermessage = UserMessage() usermessage.user = user.id usermessage.message = '欢迎注册' usermessage.save() send_register_email(user_name, 'register') return redirect('/login/') else: return render(request, 'register.html', {'register_form': register_form})
def post(self, request): register_form = RegisterForm(request.POST) if register_form.is_valid(): user_name = request.POST.get('email', "") if UserProfile.objects.filter( email=user_name) or UserProfile.objects.filter( username=user_name): return render(request, 'register.html', { 'register_form': register_form, 'msg': '用户已存在' }) pass_word = request.POST.get('password', '') user_profile = UserProfile() user_profile.username = user_name user_profile.email = user_name user_profile.is_active = False user_profile.password = make_password(pass_word) user_profile.save() #写入欢迎注册消息 user_message = UserMessage() user_message.user = user_profile.id user_message.message = '欢迎注册慕学在线网' user_message.save() send_register_email(user_name, "register") return render(request, 'tiao.html') else: return render(request, 'register.html', {'register_form': register_form})
def post(self, request): register_form = RegisterForm(request.POST) if register_form.is_valid(): user_name = request.POST.get("email", "") if UserProfile.objects.filter(email=user_name): return render(request, 'register.html', { "register_form": register_form, "msg": "用户已存在" }) pass_word = request.POST.get("password", "") user_profile = UserProfile() user_profile.username = user_name user_profile.email = user_name user_profile.is_active = False user_profile.password = make_password(pass_word) user_profile.save() # 用户消息 user_message = UserMessage() user_message.user = user_profile.id user_message.message = "欢迎注册慕学在线网" user_message.save() send_verify_code(user_name, 'register') return render(request, 'login.html') else: return render(request, 'register.html', {"register_form": register_form})
def post(self, request): register_form = RegisterForm(request.POST) if register_form.is_valid(): email = request.POST.get('email', '') if UserProfile.objects.filter(email=email): return render(request, 'register.html', { 'register_form': register_form, 'msg': u'该邮箱已被注册!' }) password = request.POST.get('password', '') user_profile = UserProfile() user_profile.username = email user_profile.email = email user_profile.password = make_password(password) user_profile.is_active = False user_profile.save() user_message = UserMessage() user_message.user = user_profile.id user_message.message = u'欢迎注册慕学网!' user_message.save() send_register_email(email, 'register') return render(request, 'login.html') return render(request, 'register.html', {'register_form': register_form})
def post(self, request): group_id = request.POST.get("group_id", None) student_id = request.POST.get("student_id", 0) is_teacher = parse_bool(request.POST.get("is_teacher", False)) message = request.POST.get("message", "") if not group_id: return JsonResponse({"status": "fail", "reason": "bad input"}) target_users = [] # find group administrators admin_users = UserGroup.objects.filter(Q(role=2) | Q(role=3), group_id=group_id).all() if admin_users: for admin in admin_users: target_users.append(admin.user) else: # no admin user found. tell site admin target_users = UserProfile.objects.filter(is_staff=True) # put a message to all target users for target in target_users: msg = UserMessage() msg.to_user = target.id msg.from_user = None # system message msg.title = u"用户{0}请求加入班级".format(request.user) msg.message_type = UserMessage.MSG_TYPE_JOIN_GROUP msg.message = json.dumps({ "group_id": group_id, "user_id": request.user.id, "is_teacher": is_teacher, "student_id": student_id, "extra_message": message }) msg.save() return JsonResponse({"status": "success"})
def post(self, request): group_name = request.POST.get("group_name", None) group_description = request.POST.get("group_description", None) organization_id = request.POST.get("organization_id", None) message = request.POST.get("message", "") # validate if not group_name or not group_description or not organization_id: return JsonResponse({"status": "fail", "reason": u"错误:输入不正确"}) # check if the name is already being used if Group.objects.filter(organization_id=organization_id, name=group_name).count(): return JsonResponse({"status": "fail", "reason": u"错误:班级名称已经被使用"}) # find the organization try: target_users = find_organization_admin_users(int(organization_id)) for target in target_users: msg = UserMessage() msg.to_user = target.id msg.from_user = None # system message msg.title = u"用户{0}请求创建班级".format(request.user) msg.message_type = UserMessage.MSG_TYPE_CREATE_GROUP msg.message = json.dumps({ "organization_id": organization_id, "user_id": request.user.id, "group_name": group_name, "group_description": group_description, "extra_message": message }) msg.save() return JsonResponse({"status": "success"}) except: return JsonResponse({"status": "fail", "reason": "bad request"})
def post(self, request): # 验证码会自动进行验证 # 验证数据格式 # 保存数据进数据库 register_form = RegisterForm(request.POST) if register_form.is_valid(): user_name = request.POST.get('email', '') if UserProfile.objects.filter(email=user_name): return render(request, 'users/login.html', {'register_form': register_form, "msg": '用户已存在'}) pass_word = request.POST.get('password', '') # 明文 user_profile = UserProfile() user_profile.username = user_name user_profile.email = user_name user_profile.password = make_password(pass_word) user_profile.is_active = False user_profile.save() # 发送消息 user_message = UserMessage() user_message.user = user_profile.id user_message.message = "welcome to China" user_message.save() send_register_email(user_name, 'register') return redirect('/login/') else: return render(request, 'users/register.html', {'register_form': register_form})
def post(self, request): register_form = RegisterForm(request.POST) if register_form.is_valid(): user_name = request.POST.get('email', '') if UserProfile.objects.filter(email=user_name): return render(request, 'register.html', { 'msg': '用户已经存在!!', 'register_form': register_form }) pass_word = request.POST.get('password', '') user_profile = UserProfile() user_profile.username = user_name user_profile.email = user_name user_profile.password = make_password(pass_word) #加密 user_profile.is_active = False user_profile.save() send_register_email(user_name, 'register') #写入消息 user_message = UserMessage() user_message.user = user_profile user_message.message = '欢迎注册慕课网' user_message.save() return render(request, 'login.html') else: return render(request, 'register.html', {'register_form': register_form})
def post(self, request): register_form = RegisterForm(request.POST) if register_form.is_valid(): user_name = request.POST.get("email", "") if UserProfile.objects.filter(email=user_name): return render(request, "register.html", { "register_form": register_form, "msg": "用户已经存在" }) pass_word = request.POST.get("password", "") user_profile = UserProfile() user_profile.username = user_name user_profile.email = user_name user_profile.is_active = False user_profile.password = make_password(pass_word) user_profile.save() user_message = UserMessage() user_message.user = user_profile user_message.message = "欢迎注册,老妹" user_message.save() send_register_email(user_name, "register") return render(request, "login.html") else: return render(request, "register.html", {"register_form": register_form})
def post(self, request): register_form = RegisterForm(request.POST) if register_form.is_valid(): user_name = request.POST.get('email', '') if UserProfile.objects.filter(email=user_name): return render(request, 'register.html', { 'register_form': register_form, 'msg': '用户已经存在' }) pass_word = request.POST.get('password', '') user_profile = UserProfile() user_profile.username = user_name user_profile.email = user_name user_profile.is_active = False user_profile.password = make_password(pass_word) user_profile.save() # 写入欢迎注册消息 user_message = UserMessage() user_message.user = user_profile.id user_message.message = "欢迎注册我们新闻网" user_message.save() send_register_email(user_name, 'register') return HttpResponseRedirect(reverse("index")) else: return render( request, 'register.html', {'register_form': register_form} ) # 回填用户信息,缺了这个变量 重新返回的时候就不能显示验证码了 因为此时此刻读不不出register_form了,从而验证错误信息也无法显示
def post(self, request): user_id = request.POST.get("user_id", 0) group_id = request.POST.get("group_id", 0) role = request.POST.get("role", 1) student_id = request.POST.get("student_id", 0) try: user = UserProfile.objects.filter(id=user_id).get() group = Group.objects.filter(id=group_id).get() if not UserGroup.objects.filter(user=user, group=group).count(): ug = UserGroup() ug.user = user ug.group = group ug.role = role ug.student_id = student_id ug.save() m = UserMessage() m.from_user = request.user m.message_type = UserMessage.MSG_TYPE_JOIN_GROUP_OK m.title = u"你已经成功加入班级" + group.name m.message = json.dumps({ "group_name": group.name, "group_id": group.id }) m.to_user = user_id m.save() return JsonResponse({"status": "success"}) except Exception as e: return JsonResponse({"status": "fail"})
def send_message(text="NULL", sender="NULL", addressee="*****@*****.**"): """ 发送站内信,text信件正文,sender发件人,addressee收件人,收件人缺省即发送至管理员 """ user_message = UserMessage() user_message.email = addressee user_message.message = "发送人:" + sender + "| 发送信息:" + text user_message.save() pass
def post(self, request, user_id): title = request.POST.get("title", "") text = request.POST.get("message", "") m = UserMessage() m.message_type = UserMessage.MSG_TYPE_USER m.title = title m.message = text m.from_user = request.user m.to_user = user_id m.save() return render(request, 'user_cowntact.html', {"message": u"消息发送成功"})
def post(self, request): user_id = request.POST.get("user_id", 0) message_type = request.POST.get("message_type", UserMessage.MSG_TYPE_USER) if user_id: # create a message to tell the user m = UserMessage() m.from_user = request.user m.to_user = user_id m.title = u"申请被拒绝" request_type = UserMessage( message_type=int(message_type)).get_message_type_display() m.message = u"你的{0}申请审核未通过。如有疑问,请和我私信联系。".format(request_type) m.save() return JsonResponse({"status": "success"})
def post(self, request): register_form = RegisterForm(request.POST) if register_form.is_valid(): user_name = request.POST.get('username', '') password = request.POST.get('password', '') user_email = request.POST.get('email', '') if UserProfiles.objects.filter(username=user_name): return render(request, 'users/register.html', {'msg_username_error': u'用户名已被注册,请更换用户名', 'register_form': register_form}) if UserProfiles.objects.filter(email=user_email): return render(request, 'users/register.html', {'msg_email_error': u'邮箱已被注册,请更换邮箱', 'register_form': register_form}) # 通过格式验证后,记录用户的数据到数据库中 user_profile = UserProfiles() user_profile.username = user_name user_profile.email = user_email user_profile.is_active = False user_profile.password = make_password(password) user_profile.save() # 用户注册消息 register_message = UserMessage() register_message.user = user_profile.id register_message.message = u'欢迎你注册称为点点在线网的用户' register_message.save() # 发送邮件验证码,获取发送状态,发送失败为 False send_status = send_email_verify_record(user_name, user_email, 'register') # 验证是否发送成功,如果送失败,重发三次,如果还是失败,删除用户数据,提示用户重新注册 if not send_status: resend_status = False for times in range(3): resend_status = send_email_verify_record(user_email, 'register') if resend_status: break if not resend_status: UserProfiles.objects.filter(username=user_name).delete() return render(request, 'users/register.html', {'msg_email_send_error': u'抱歉,验证邮件多次发送失败,请重新进行注册', 'register_form': register_form}) return render(request, 'users/login.html', {}) else: return render(request, 'users/register.html', {'register_form': register_form})
def post(self, request): user_id = request.POST.get("user_id", 0) group_id = request.POST.get("group_id", 0) try: group = Group.objects.filter(id=group_id).get() deleted, _ = UserGroup.objects.filter(user_id=user_id, group_id=group_id).delete() if deleted: m = UserMessage() m.from_user = request.user m.message_type = UserMessage.MSG_TYPE_USER m.title = u"你已经成功退出班级'" + group.description + "'" m.message = u"" m.to_user = user_id m.save() return JsonResponse({"status": "success"}) except Exception as e: return JsonResponse({"status": "fail"})
def add_collection_message_to_user_message(user_id, collection_id, collection_type): id = { '1': Course, '2': CourseOrganization, '3': Teacher, } model_instance = id.get(str(collection_type)) model_instance = model_instance.objects.get(id=collection_id) user_message = UserMessage() user_message.user = user_id name = '' if collection_type == 1: name = model_instance.course_name elif collection_type == 2: name = model_instance.organization_name elif collection_type == 3: name = model_instance.teacher_name user_message.message = "欢迎你收藏<{}\n{}>".format(name, model_instance.description) user_message.save()
def post(self, request): group_id = request.POST.get("group_id", None) message = request.POST.get("message", "") if not group_id: return JsonResponse({"status": "fail", "reason": "bad input"}) target_users = find_group_admin_users(int(group_id)) # put a message to all target users for target in target_users: msg = UserMessage() msg.to_user = target.id msg.from_user = None # system message msg.title = u"用户{0}请求退出班级".format(request.user) msg.message_type = UserMessage.MSG_TYPE_LEAVE_GROUP msg.message = json.dumps({ "group_id": group_id, "user_id": request.user.id, "extra_message": message }) msg.save() return JsonResponse({"status": "success"})
def post(self, request): # 获取课程的 id course_id = request.POST.get('course_id', '') # 判断用户登录状态 if not request.user.is_authenticated: return JsonResponse({'status': 'fail', 'msg': u'用户未登录'}) # 判断用户是否已经处于学习状态 user_course = UserCourse.objects.filter(user_id=request.user.id, course_id=course_id) if user_course: return JsonResponse({'status': 'success', 'msg': u'已添加学习'}) else: user_course = UserCourse() # 实例化一个课程对象 course = Course.objects.get(id=course_id) user_course.course = course user_course.user = request.user user_course.save() # 点击学习,学习人数加 1 course.student_number += 1 course.save() # 课程机构的学习人加 1 org = course.course_teacher.organization org.students += 1 org.save() # 用户添加学习时,发送消息给用户 user_message = UserMessage() user_message.user = request.user.id user_message.message = '欢迎你学习<{}>这门课程\n{}'.format( course.course_name, course.description) user_message.save() return JsonResponse({'status': 'success', 'msg': u'已添加学习'})
def post(self,request): register_form = RegisterForm(request.POST) if register_form.is_valid(): username = request.POST.get('email') if UserProfile.objects.filter(username=username): return render(request, 'register.html', {"msg": "账号已存在","register_form":register_form}) password = request.POST.get('password') UserProfile.objects.create(username=username,email=username,password=make_password(password)) UserProfile.objects.filter(email=username).update(is_active=False) #写入欢迎注册消息 userMessage =UserMessage() userMessage.user=UserProfile.objects.get(username=username).id userMessage.message='欢迎来到会飞的鱼,晓晓' userMessage.save() #发送邮件 email_send.send_register_email(username,"register") return redirect('/') else: return render(request,'register.html',{"register_form":register_form}) return render(request,'register.html')
def post(self, request): register_mobile_form = RegisterMobileForm(request.POST) if register_mobile_form.is_valid(): mobile = request.POST.get("mobile") password = request.POST.get("password") nickname = request.POST.get("nickname") if UserProfile.objects.filter(mobile_phone=mobile): return render( request, "user_register.html", { "register_mobile_form": register_mobile_form, "msg": "用户{0}已经存在".format(mobile), "method": "mobile" }) user = UserProfile() user.nick_name = nickname user.mobile_phone = mobile user.username = mobile user.is_active = True user.email = "" user.password = make_password(password) user.save() # 写入欢迎消息 user_message = UserMessage() user_message.to_user = user.id user_message.from_user = user user_message.message = "欢迎注册" user_message.save() return render(request, "user_login.html", {"login_title": u"注册成功,请登录"}) else: return render(request, "user_register.html", { "register_mobile_form": register_mobile_form, "method": "mobile" })
def post(self, request): register_form = RegisterForm(request.POST) if register_form.is_valid(): user_name = request.POST.get("email", "") if UserProfile.objects.filter(email=user_name): return render( request, "user_register.html", { "register_form": register_form, "msg": "用户{0}已经存在".format(user_name), "method": "email" }) pass_word = request.POST.get("password", "") user_profile = UserProfile() user_profile.nick_name = register_form.data["nickname"] user_profile.username = user_name user_profile.email = user_name user_profile.is_active = False user_profile.password = make_password(pass_word) user_profile.save() #写入欢迎注册消息 user_message = UserMessage() user_message.to_user = user_profile.id user_message.from_user = user_profile user_message.title = "欢迎注册" user_message.message = "欢迎来到Nova背单词。你可以先看看有没有感兴趣的单词书,或者去找到感兴趣的班级。" user_message.save() send_register_email_async.delay(user_name, "register", get_base_url(request)) return render(request, "user_login.html", {"login_title": u"注册成功,请检查你的邮箱中的确认邮件。账号激活之后就可以登录了。"}) else: return render(request, "user_register.html", { "register_form": register_form, "method": "email" })
def post(self, request): user_id = request.POST.get("user_id", 0) group_name = request.POST.get("group_name", None) group_description = request.POST.get("group_description", None) organization_id = request.POST.get("organization_id", None) # No validation. We assume they are all valid try: # check if the group alredy exists if Group.objects.filter(organization_id=organization_id, name=group_name).count() == 0: # create the group group = Group() group.organization_id = organization_id group.name = group_name group.description = group_description group.save() # add admin user user_group = UserGroup() user_group.user_id = user_id user_group.group = group user_group.role = 3 # 管理员 user_group.save() # send a message to user m = UserMessage() m.message_type = UserMessage.MSG_TYPE_USER m.title = u"你申请的班级已经创建: {0}".format(group.name) m.message = u"你申请的班级已经创建,可以在 “班级” “全部班级” 中找到。" m.to_user = user_id m.from_user = request.user m.save() return JsonResponse({"status": "success"}) except: return JsonResponse({"status": "fail", "reason": "bad request"})
try: send_register_email(user_name, "register") except: return render(request, "register.html", {"register_form": register_form, "msg": "发生错误,请稍候再试"}) else: # user_message.save() send_message("欢迎注册ADP-攻防演练平台", "系统欢迎消息", user_name) user_profile.save() return render(request, "register.html", {"email": user_name, "is_registered": True}) # return render(request, "login.html") ======= user_profile.save() # 写入欢迎注册的消息 user_message = UserMessage() user_message.user= user_profile user_message.message = "欢迎注册ADP-攻防演练平台" user_message.save() send_register_email(user_name, "register") return render(request, "login.html") >>>>>>> github/master else: return render(request, "register.html", {"register_form": register_form}) class LogoutView(View): def get(self, request): <<<<<<< HEAD client = docker.from_env() dockers = Docker.objects.filter(user=request.user.username).all() exp_dockers = exp_docker.objects.filter(user=request.user.username).all() for doc in chain(dockers, exp_dockers):
def user_love(request): type_id = request.GET.get('type_id') love_id = request.GET.get('love_id') love1 = None type = '' if type_id and love_id: if int(type_id) == 1: type = '一个机构' love1 = OrgInfo.objects.filter(id=int(love_id))[0] if int(type_id) == 2: type = '一门课程' love1 = CourseInfo.objects.filter(id=int(love_id))[0] if int(type_id) == 3: type = '一个老师' love1 = TeacherInfo.objects.filter(id=int(love_id))[0] love = UsreLoverInfo.objects.filter(userid=request.user, love_type=int(type_id), love_id=int(love_id)) if love: if love[0].love_status: love1.love_num -= 1 love1.save() love[0].love_status = False love[0].save() return JsonResponse({'status': 'ok', 'msg': '收藏'}) else: love1.love_num += 1 love1.save() love[0].love_status = True love[0].save() mymsg = UserMessage() mymsg.userid_id = request.user.id mymsg.message = '您成功收藏了' + type mymsg.msg_status = True mymsg.save() return JsonResponse({'status': 'ok', 'msg': '取消收藏'}) else: love1.love_num += 1 love1.save() love = UsreLoverInfo() love.userid = request.user love.love_type = int(type_id) love.love_status = True love.love_id = int(love_id) love.save() mymsg = UserMessage() mymsg.userid_id = request.user.id mymsg.message = '您成功收藏了' + type mymsg.msg_status = True mymsg.save() return JsonResponse({'status': 'ok', 'msg': '取消收藏'}) else: return JsonResponse({'status': 'fail', 'msg': '收藏失败'})
def post(self, request): fav_id = request.POST.get('fav_id', 0) fav_type = request.POST.get('fav_type', 0) # 判断用户是否登录 if not request.user.is_authenticated(): return HttpResponse("{'status': 'fail', 'msg': '用户未登录'}", content_type='application/json') # 查找用户是否已经收藏 exist_records = UserFavorite.objects.filter(user=request.user, fav_id=int(fav_id), fav_type=int(fav_type)) if exist_records: # 取消收藏 exist_records.delete() if int(fav_type) == 1: course = Course.objects.get(id=int(fav_id)) course.fav_nums -= 1 if course.fav_nums < 0: course.fav_nums = 0 course.save() elif int(fav_type) == 2: course_org = CourseOrg.objects.get(id=int(fav_id)) course_org.fav_nums -= 1 if course_org.fav_nums < 0: course_org.fav_nums = 0 course_org.save() elif int(fav_type) == 3: teacher = Teacher.objects.get(id=int(fav_id)) teacher.fav_nums -= 1 if teacher.fav_nums < 0: teacher.fav_nums = 0 teacher.save() return HttpResponse("{'status': 'success', 'msg': '用户已取消收藏'}", content_type='application/json') else: user_fav = UserFavorite() if int(fav_id) > 0 and int(fav_type) > 0: user_fav.user = request.user user_fav.fav_id = int(fav_id) user_fav.fav_type = int(fav_type) user_fav.save() if int(fav_type) == 1: course = Course.objects.get(id=int(fav_id)) course.fav_nums += 1 course.save() elif int(fav_type) == 2: course_org = CourseOrg.objects.get(id=int(fav_id)) course_org.fav_nums += 1 course_org.save() elif int(fav_type) == 3: teacher = Teacher.objects.get(id=int(fav_id)) teacher.fav_nums += 1 teacher.save() # 发送消息 user_message = UserMessage() user_message.user = request.user user_message.message = "welcome to China" user_message.save() return HttpResponse("{'status': 'success', 'msg': '用户已收藏'}", content_type='application/json') else: return HttpResponse("{'status': 'fail', 'msg': '收藏出错'}", content_type='application/json')