def post(self, request): register_form = RegisterForm(request.POST) if register_form.is_valid(): user_name = request.POST.get('email', '') if UserProfile.objects.get(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() # 注册消息 message = UserMessage(user=user_profile.id, message="欢迎注册慕课在线网!") 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): 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): 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 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, 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 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): 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, path=None): print(request.POST) login_form = UserLoginForm(request.POST) if login_form.is_valid(): username = login_form.cleaned_data['username'] password = login_form.cleaned_data['password'] print(f'{username=},{password=}') # user = UserProfile.objects.filter(Q(username=username) | Q(email=username) | Q(phone=username) & Q(password=password)) user = authenticate(username=username, password=password) print(f'{user=}') if user: if user.is_status: login(request, user) # 登录成功后将登录消息保存到UserMessage中 usermessage = UserMessage() usermessage.message_man = request.user.id usermessage.message_content = 'Hello,{}欢迎来到尚硅谷教育网!'.format( request.user.nick_name) usermessage.save() path = request.POST.get('path') # 获取登录跳转前页面的路径 # url = request.COOKIE.get('url', '/') # 从cookie中获取保存的url # ret = redirect('url') # ret.delete_cookie('url') # return ret print(f"{path=}") if path and path != 'None': return redirect(path) # 跳转回之前的页面 return redirect(reverse('index')) return HttpResponse("请激活你的邮箱再登录") return render(request, 'login.html', {'err': '用户名或密码错误'}) return render(request, '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(request): user_register_form = UserRegisterForm(request.POST) if user_register_form.is_valid(): email = user_register_form.cleaned_data['email'] password = user_register_form.cleaned_data['password'] # 查找是否已经注册 user_list = UserProfile.objects.filter( Q(username=email) | Q(email=email)) if user_list: return render(request, 'users/register.html', { 'msg': '用户已经存在', }) else: """ 默认未激活,需要点击邮箱链接进行激活 """ user = UserProfile(username=email, email=email) user.is_active = False user.set_password(password) user.save() """ 给用户发送激活邮件并发送欢迎注册消息 """ msg = UserMessage() msg.msg_content = '欢迎注册[在线教育网],请遵守国家相关法律法规' msg.msg_user = user.id msg.save() send_mail_code(email, 1) return HttpResponse('请尽快前往您的邮箱激活,否则无法登录') else: return render(request, 'users/register.html', { 'user_register_form': user_register_form, })
def post(request): user_login_form = UserLoginForm(request.POST) if user_login_form.is_valid(): username = user_login_form.cleaned_data['username'] password = user_login_form.cleaned_data['password'] user = authenticate(username=username, password=password) if user: """" 仅允许激活用户允许进行登录 """ if user.is_active: login(request, user) # 登录消息 msg = UserMessage() msg.msg_user = user.id msg.msg_content = '欢迎登录' msg.save() url = request.COOKIES.get('url', '/') ret = redirect(url) ret.delete_cookie('url') return ret else: return render(request, 'users/login.html', { 'msg': '该账户未激活,请先去邮箱激活。', }) else: return render(request, 'users/login.html', { 'msg': '用户名或密码错误', }) else: return render(request, 'users/login.html', { 'user_login_form': user_login_form, })
def post(self, request): user_login_form = UserLoginForm(request.POST) if user_login_form.is_valid(): username = user_login_form.cleaned_data['username'] password = user_login_form.cleaned_data['password'] user = authenticate(username=username, password=password) if user: #如果这个账号是激活了的 if user.is_start: login(request, user) # 当登陆成功的时候,给加入一条消息 a = UserMessage() a.message_man = user.id a.message_content = '欢迎登陆' a.save() url = request.COOKIES.get('url', '/') ret = redirect(url) ret.delete_cookie( 'url' ) #从COOKIES拿键url的值,只拿一次,用delete_cookie删掉。如:未登录状态下点“开始学习”,跳登录页,登录账号后,跳学习视频页面。之后,未登录状态下点“收藏”,跳登录页,登录账号后,避免仍然跳学习视频页面 return ret else: return HttpResponse('请去您的邮箱激活,否则无法登陆') else: return render(request, 'users/login.html', {'msg': '邮箱或者密码有误'}) else: return render(request, 'users/login.html', {'user_login_form': user_login_form})
def post(self, request): user_login_form = UserLoginForm(request.POST) if user_login_form.is_valid(): email = user_login_form.cleaned_data['email'] password = user_login_form.cleaned_data['password'] #这个方法验证的就是username和password,不要换成别的字段 user = authenticate(username=email, password=password) #邮箱和密码验证通过 if user: #已经被激活的用户可以登录 if user.is_start: login(request, user) #用户每次登录成功,就给他发一个系统消息,欢迎他登录 msg = UserMessage() msg.message_man = user.id msg.message_content = '欢迎登录。' msg.save() #取出cookie里存放的url,这是我们来到登录页面前所在的页面,如果能取到值就原路返回,取不到值就去首页 url = request.COOKIES.get('url', '/') #获取我们要重定向到的页面的response对象,但是删除其中的cookie(为了避免以后每次都重定向到这个页面,尤其是ajax请求会受到影响) ret = redirect(url) ret.delete_cookie('url') return ret else: return HttpResponse('请去您的邮箱激活您的账号,否则无法登录') #验证未通过 else: return render(request, 'users/login.html', {'msg': '邮箱或者密码有误'}) #如果邮箱密码填写格式不对 else: return render(request, 'users/login.html', {'user_login_form': user_login_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, '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): blog_form = BlogContentForm(request.POST, request.FILES or None) if blog_form.is_valid(): blog = MiniBlog() print(blog.id) blog.user = request.user blog.content = request.POST.get("content", "") blog.image = blog_form.cleaned_data["image"] if blog.image: blog.has_pic = 1 # 替换@用户 re_str = blog.content users_list = [] users_str = re.findall(r'@(.+?)\s', re_str) users_str_1 = re.findall(r'^(.+?)@', re_str[::-1]) if users_str_1: users_str.append(users_str_1[0][::-1]) for user_str in users_str: user = UserProfile.objects.filter(nickname=user_str) if user: users_list.append(user[0]) re_str = re_str.replace( '@' + user_str, '<a href="/user/' + str(user[0].id) + '/">' + '@' + user_str + '</a>') blog.content = re_str blog.save() print(blog.id) # 新建@提醒 for user in users_list: message = UserMessage() message.user = user message.blog_id = blog.id message.save() user.message_nums = UserMessage.objects.filter( user=user, has_read=False).count() user.save() # blog_form.save() self.topic_test(blog.content, blog) # 替换话题 re_str = blog.content topics_str = re.findall(r'#(.+?)#', re_str) for topic_str in topics_str: topic = Topic.objects.filter(topicname=topic_str) if topic: re_str = re_str.replace( '#' + topic_str + '#', '<a href="/weibo/topic/' + str(topic[0].id) + '">' + '#' + topic_str + '#' + '</a>') blog.content = re_str blog.save() return HttpResponseRedirect(reverse('mainpage'))
def user_login(request): if request.method == 'GET': return render(request, 'users/login.html') else: user_login_form = UserLoginForm(request.POST) if user_login_form.is_valid(): email = user_login_form.cleaned_data['email'] password = user_login_form.cleaned_data['password'] user = authenticate(username=email, password=password) if user: if user.is_start: login(request, user) a = UserMessage() a.msg_user = request.user.id a.msg_content = '欢迎登陆谷粒教育网' a.save() url = request.COOKIES.get('url', '/') return redirect(url) else: return HttpResponse('你的邮箱未激活,请去邮箱激活后再登录!') else: return render(request, 'users/login.html', {'msg': '邮箱或者密码错误'}) else: return render(request, 'users/login.html', {'user_login_form': user_login_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): user_login_form = UserLoginForm(request.POST) if user_login_form.is_valid(): username = user_login_form.cleaned_data['username'] password = user_login_form.cleaned_data['password'] user = authenticate(username=username, password=password) if user: if user.is_start: login(request, user) # 当登陆成功的时候,给加入一条消息 a = UserMessage() a.message_man = user.id a.message_content = '欢迎登陆' a.save() url = request.COOKIES.get('url', '/') ret = redirect(url) ret.delete_cookie('url') return ret else: return render(request, 'users/login.html', {'msg': '请去邮箱激活账号'}) else: return render(request, 'users/login.html', {'msg': '邮箱或者密码有误'}) else: return render(request, 'users/login.html', { 'user_login_form': user_login_form, 'msg': '邮箱或者密码有误' })
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): user_login_form = UserLoginForm(request.POST) if user_login_form.is_valid(): email = user_login_form.cleaned_data['email'] password = user_login_form.cleaned_data['password'] user = authenticate(username=email, password=password) if user: if user.is_start: login(request, user) # 当登陆成功,就加入一条消息 a = UserMessage() a.message_man = user.id a.message_content = '欢迎登录' a.save() url = request.COOKIES.get('url', '/') ret = redirect(url) ret.delete_cookie('url') return ret else: return HttpResponse('请去您的邮箱激活,否则无法登陆') else: return redirect(request, 'login.html', { 'msg': '邮箱或密码有误' }) else: return render(request, 'users/login.html', { 'user_login_form': user_login_form })
def post(self, request): register_form = RegisterForm(request.POST) if register_form.is_valid: user_name = request.POST.get("username", "") if UserProfile.objects.filter(email=user_name): return render(request, 'register.html', { "msg": "用户已存在", 'register_form': register_form }) user_password = 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(user_password) user_profile.save() # 发送注册成功消息 user_message = UserMessage() user_message.user = user_profile.id user_message.message_content = '欢迎注册加入神童研究院' user_message.save() send_register_email(user_name, 'register') return render(request, 'login.html')
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 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): 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): 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"})
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" })