Exemplo n.º 1
0
 def save_models(self):
     '''
     在后台管理系统中对数据进行保存时会经过该操作
     :return:
     '''
     obj = self.new_obj  # 当修改操作时,该属性的id为添加的数据ID,当是添加的时候没有值为None
     if not obj.id:  # 该对象,实际就是usercourse的表数据对象。
         obj.save()
         course = obj.course
         course.students += 1
         course.save()
         send_message = UserMessage(user=obj.user)
         send_message.message = "欢迎学习" + obj.course.name + "课程"
         send_message.save()
Exemplo n.º 2
0
    def get(self,request,course_id,*args,**kwargs):
        course=Course.objects.get(id=int(course_id))

        #用户与课程的关系
        user_courses=UserCourses.objects.filter(user=request.user,course=course)
        if not user_courses:
            user_course=UserCourses(user=request.user,course=course)
            user_course.save()
            course.students_num+=1
            course.save()

            # 欢迎用户加入课程
            message = UserMessage()
            message.user = request.user
            message.messgae = "欢迎您加入课程:{}的学习!".format(course.name)
            message.has_read = False
            message.save()


        #课程资源
        course_resource=CourseResource.objects.filter(course=course)

        #学习过该课程的同学
        user_courses=UserCourses.objects.filter(course=course)
        user_ids=[user_course.user_id for user_course in user_courses]
        all_courses=UserCourses.objects.filter(user_id__in=user_ids).order_by("-course__click_nums")[:3]
        related_couses=[user_course.course for user_course in all_courses]

        return render(request,"course-video.html",{
            "course":course,
            "course_resource":course_resource,
            "all_courses":related_couses,
        })
Exemplo n.º 3
0
    def post(self, request, *args, **kwargs):
        login_form = LoginForm(request.POST)

        banners = Banner.objects.all().order_by("-add_time")[:3]

        if login_form.is_valid():

            # user_name= request.POST.get("username","")
            # password= request.POST.get("password","")
            # if not user_name:
            #     return render(request, "login.html", {"msg": "请输入用户名"})
            #
            # if not password:
            #     return render(request, "login.html", {"msg": "请输入密码"})

            user_name = login_form.cleaned_data["username"]
            password = login_form.cleaned_data["password"]
            user = authenticate(username=user_name, password=password)
            # user = authenticate(username="******", password="******")
            if user is not None:
                if user.is_active:
                    login(request, user)
                    next = request.GET.get("next", "")
                    message = UserMessage()
                    message.user = user
                    message.messgae = "登录成功!"
                    message.has_read = False
                    message.save()
                    if next:
                        return HttpResponseRedirect(next)
                    else:
                        return HttpResponseRedirect(reverse("index"))
                else:
                    msg = "用户未激活!"
                    login_form._errors["msg"] = login_form.error_class([msg])
                    return render(
                        request, "login.html", {
                            "msg": "用户未激活!请前往邮箱激活",
                            "login_form": login_form,
                            "banners": banners,
                        })

            else:
                print(233)
                return render(
                    request, "login.html", {
                        "msg": "用户名或密码错误",
                        "login_form": login_form,
                        "banners": banners,
                    })

        else:
            return render(request, "login.html", {
                "login_form": login_form,
                "banners": banners,
            })
Exemplo n.º 4
0
    def post(self, request):
        register_post_form = RegisterPostForm(request.POST)
        if register_post_form.is_valid():
            user_name = request.POST.get('mobile', '')
            password = request.POST.get('password', '')
            user = UserProfile()
            user.username = user_name
            user.mobile = user_name
            user.set_password(password)
            user.save()

            # 写入欢迎注册消息
            user_message = UserMessage()
            user_message.user = user
            user_message.message = '欢迎注册!!!!!'
            user_message.save()

            # send_register_email(user_name, 'register')
            login(request, user)
            return HttpResponseRedirect(reverse('index'))
        else:
            register_get_form = RegisterGetForm()
            return render(
                request, 'register.html', {
                    'register_get_form': register_get_form,
                    'register_post_form': register_post_form
                })
Exemplo n.º 5
0
 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:
             user_list = UserProfile.objects.filter(username=email)
             if user_list[0].is_start:
                 login(request, user)
                 a = UserMessage()
                 a.message_content = '欢迎' + email + '登录'
                 a.message_man = user.id
                 a.save()
                 url = request.COOKIES.get('url', '/users/index')
                 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
         })
Exemplo n.º 6
0
    def post(self, request, *args, **kwargs):
        user_info_form = UserInfoForm(request.POST, instance=request.user)
        if user_info_form.is_valid():
            user_info_form.save()

            message = UserMessage()
            message.user = request.user
            message.messgae = "用户信息修改成功!"
            message.has_read = False
            message.save()

            return JsonResponse({
                "status": "success",
            })
        else:
            return JsonResponse(user_info_form.errors)
Exemplo n.º 7
0
    def save_models(self):
        obj = self.new_obj
        if not obj.id:
            obj.save()
            course = obj.course
            course.students_num += 1
            course.save()

            #欢迎用户加入课程
            message = UserMessage()
            message.user = obj.user
            message.messgae = "欢迎您加入课程:{}的学习!".format(obj.course.name)
            message.has_read = False
            message.save()

        else:
            obj.save()
Exemplo n.º 8
0
    def get(self, request, active_code):
        # print(active_code)
        check_exist = EmailVerifyRecord.objects.filter(code=active_code)
        if check_exist:
            for item in check_exist:
                email = item.email
                user = UserProfile.objects.get(email=email)
                user.is_active = True
                user.save()
                message = UserMessage()
                message.user = user
                message.messgae = "注册成功!欢迎来到慕学在线!"
                message.has_read = False
                message.save()
        else:
            return render(request, "active_fail.html")

        messages.info(request, "激活成功!")
        return render(request, "login.html")
Exemplo n.º 9
0
    def post(self, request, *args, **kwargs):
        image_form = UploadImageForm(request.POST,
                                     request.FILES,
                                     instance=request.user)
        if image_form.is_valid():
            image_form.save()
            messages.info(request, "头像修改成功!")

            message = UserMessage()
            message.user = request.user
            message.messgae = "用户头像修改成功!"
            message.has_read = False
            message.save()

            return JsonResponse({
                "status": "success",
            })
        else:
            messages.error(request, "头像修改失败!")
            return JsonResponse({
                "status": "fail",
            })
Exemplo n.º 10
0
    def post(self, request, *args, **kwargs):
        pwd_form = ChangePwdForm(request.POST)
        if pwd_form.is_valid():
            oldpwd = pwd_form.cleaned_data["oldpassword"]
            pwd1 = pwd_form.cleaned_data["password1"]
            pwd2 = pwd_form.cleaned_data["password2"]

            user = authenticate(username=request.user.username,
                                password=oldpwd)
            if user is not None:
                if pwd1 != pwd2:
                    return JsonResponse({
                        "status": "fail",
                        "msg": "密码不一致",
                    })
                else:
                    user = request.user
                    user.set_password(pwd1)
                    user.save()
                    login(request, user)

                    message = UserMessage()
                    message.user = request.user
                    message.messgae = "密码修改成功!"
                    message.has_read = False
                    message.save()

                    return JsonResponse({
                        "status": "success",
                    })
            else:
                return JsonResponse({
                    "status": "fail",
                    "msg": "原始密码错误!",
                })

        else:
            return JsonResponse(pwd_form.errors)
Exemplo n.º 11
0
    def post(self, request):
        modifypwd_form = ModifyPwdForm(request.POST)
        print(333333)
        if modifypwd_form.is_valid():

            pwd1 = request.POST.get("pwd1", "")
            pwd2 = request.POST.get("pwd2", "")
            email = request.POST.get("email", "")
            if pwd1 != pwd2:
                messages.error(request, "密码不一致!")
                return render(request, "password_reset.html", {
                    "email": email,
                    "msg": "密码不一致!"
                })
            else:
                user = UserProfile.objects.get(email=email)
                user.password = make_password(pwd1)
                user.save()

                message = UserMessage()
                message.user = user
                message.messgae = "密码修改成功!"
                message.has_read = False
                message.save()

                EmailVerifyRecord.objects.filter(email=email).delete()
                messages.info(request, "密码修改成功!")
                return render(request, "login.html")

        else:
            email = request.POST.get("email", "")
            messages.error(request, "密码长度过短!")
            return render(request, "password_reset.html", {
                "email": email,
                "msg": "密码长度过短!"
            })
Exemplo n.º 12
0
 def post(self, request, *args, **kwargs):
     register_post_form = RegisterPostForm(request.POST)
     if register_post_form.is_valid():
         email = register_post_form.cleaned_data["email"]
         password = register_post_form.cleaned_data["password"]
         # 新建一个用户
         user = UserProfile(username=email)  #username必须唯一,比如如果数据库存在该username,就会出现无法插入数据库记录错误。
         user.set_password(password)
         user.email = email
         user.save()
         login(request, user)
         new_message = "欢迎使用FreeStudy,祝您学习愉快!"
         messages = UserMessage()
         messages.user = request.user
         messages.message = new_message
         messages.save()
         return HttpResponseRedirect(reverse("index")) #注册成功返回主页
     else:
         register_get_form = RegisterGetForm()
         return render(request, "register.html", {
             "register_get_form":register_get_form,
             "register_post_form": register_post_form
         })
Exemplo n.º 13
0
    def post(self, request, *args, **kwargs):
        if not request.user.is_authenticated:
            messages.error(request, "用户未登录!")
            return JsonResponse({"status": "fail", "msg": "用户未登录"})

        user_fav_form = UserFavForm(request.POST)
        if user_fav_form.is_valid():
            fav_id = user_fav_form.cleaned_data["fav_id"]
            fav_type = user_fav_form.cleaned_data["fav_type"]

            existed_record = UserFavourite.objects.filter(user=request.user,
                                                          fav_id=fav_id,
                                                          fav_type=fav_type)
            if existed_record:
                existed_record.delete()

                if fav_type == 1:
                    course = Course.objects.get(id=fav_id)
                    course.fav_num -= 1
                    course.save()
                elif fav_type == 2:
                    courseorg = CourseOrg.objects.get(id=fav_id)
                    courseorg.fav_nums -= 1
                    courseorg.save()
                else:
                    teacher = Teacher.objects.get(id=fav_id)
                    teacher.fav_nums -= 1
                    teacher.save()

                message = UserMessage()
                message.user = request.user
                message.messgae = "取消收藏成功!"
                message.has_read = False
                message.save()

                return JsonResponse({"status": "success", "msg": "收藏"})
            else:
                user_fav = UserFavourite()
                user_fav.fav_id = fav_id
                user_fav.fav_type = fav_type
                user_fav.user = request.user
                user_fav.save()

                if fav_type == 1:
                    course = Course.objects.get(id=fav_id)
                    course.fav_num += 1
                    course.save()
                elif fav_type == 2:
                    courseorg = CourseOrg.objects.get(id=fav_id)
                    courseorg.fav_nums += 1
                    courseorg.save()
                else:
                    teacher = Teacher.objects.get(id=fav_id)
                    teacher.fav_nums += 1
                    teacher.save()

                message = UserMessage()
                message.user = request.user
                message.messgae = "收藏成功!"
                message.has_read = False
                message.save()

                return JsonResponse({"status": "success", "msg": "已收藏"})
        else:
            return JsonResponse({"status": "fail", "msg": "参数错误"})