Ejemplo n.º 1
0
Archivo: views.py Proyecto: NAZBIN/Web
def login_view(request):
    #需要一个表单对数据进行验证
    #导入表单使用表单做验证
    form = LoginForm(request.POST)
    if form.is_valid():
        #如果验证成功,拿到数据
        telephone = form.cleaned_data.get('telephone')
        password = form.cleaned_data.get('password')
        remember = form.cleaned_data.get('remember')
        user = authenticate(request,username = telephone,password=password)
        if user:
            #验证成功后判断 is_active看该用户是否可用
            if user.is_active:
                login(request,user)
                if remember:
                    request.session.set_expiry(None)
                else:
                    request.session.set_expiry(0)
                return restful.ok()
            else:
                return restful.unauth(message="您的账号已经被冻结")
        else:
            return restful.paramserror(message="手机号码或者密码错误")
    else:
        errors = form.get_errors()
        return restful.paramserror(message=errors)
Ejemplo n.º 2
0
def my_login(request):
    if request.method == 'GET':
        return render(request,'front/login.html')
    if request.method == "POST":
        form = SigninForm(request.POST)
        if form.is_valid():
            username = form.cleaned_data.get('username')
            password = form.cleaned_data.get('password')
            imgcaptcha = form.cleaned_data.get('imgcaptcha')
            user = authenticate(request,username=username,password=password)
            if user:
                if user.is_active:
                    login(request,user)
                    request.session.set_expiry(0)
                    next_url = request.GET.get('next')
                    if next_url:
                        return redirect(next_url)
                    return restful.ok()
                else:
                    return restful.noauth(message='该用户已被加入黑名单')
            else:
                return restful.paramserror(message='用户名或密码错误!')
        else:
            errors = form.get_error()
            print(errors)
            return restful.paramserror(message=errors)
Ejemplo n.º 3
0
def login_view(request):
    print('test')
    form = LoginForm(request.POST)
    if form.is_valid():
        telephone = form.cleaned_data.get('telephone')
        password = form.cleaned_data.get('password')
        remember = form.cleaned_data.get('remember')

        user = authenticate(request, username=telephone, password=password)
        # print('login user :' + user)
        if user:
            if user.is_active:
                login(request, user)
                if remember:
                    request.session.set_expiry(None)
                else:
                    request.session.set_expiry(0)
                return restful.ok()
            else:
                return restful.unautherror(message="您的账号已被冻结!")
        else:
            return restful.paramserror(message="手机号或着密码错误")
    else:
        errors = form.get_errors()
        return restful.paramserror(message=errors)
Ejemplo n.º 4
0
Archivo: views.py Proyecto: jc5055/xfz
def del_news_category(request):
    pk = request.POST.get('pk')
    if pk:
        try:
            NewsCategory.objects.filter(pk=pk).delete()
            return restful.ok()
        except:
            return restful.paramserror('分类不存在')
    else:
        return restful.paramserror('分类参数异常')
Ejemplo n.º 5
0
def dboard():
    board_id = request.form.get('board_id')
    if not board_id:
        return restful.paramserror('请传入板块id')
    board = BoardModel.query.get(board_id)
    if not board:
        return restful.paramserror('没有这个板块')
    db.session.delete(board)
    db.session.commit()
    return restful.success()
Ejemplo n.º 6
0
def aboards():
    form = BoardsForm(request.form)
    if form.validate():
        name = request.form.get("name")
        boards = BoardsModel(name=name)
        db.session.add(boards)
        db.session.commit()
        return restful.success()
    else:
        message = form.errors.popitem()[1][0]
        restful.paramserror(message)
Ejemplo n.º 7
0
def uhpost():
    post_id = request.form.get('post_id')
    if not post_id:
        return restful.paramserror('请传入帖子id')
    post = PostModel.query.get(post_id)
    if not post:
        return restful.paramserror('没有这篇帖子')
    highlight = HighlightPostModel.query.filter_by(post_id=post_id).first()
    db.session.delete(highlight)
    db.session.commit()
    return restful.success()
Ejemplo n.º 8
0
def add_category(request):
	category = request.POST.get('category')
	if category:
		exists = NewsCategory.objects.filter(name=category)
		if not exists:
			NewsCategory.objects.create(name=category)
			return restful.ok()
		else:
			return restful.paramserror(message="分类名已存在!")
	else:
		return restful.paramserror(message="请输入分类名!")
Ejemplo n.º 9
0
def hpost():
    post_id = request.form.get('post_id')
    if not post_id:
        return restful.paramserror('请传入帖子id')
    post = PostModel.query.get(post_id)
    if not post:
        return restful.paramserror('没有这篇帖子')
    highlight = HighlightPostModel()
    highlight.post = post
    db.session.add(highlight)
    db.session.commit()
    return restful.success()
Ejemplo n.º 10
0
def dbanner():
    banner_id = request.form.get('banner_id')
    print(banner_id)
    if not banner_id:
        return restful.paramserror(message='请传入轮播图id')

    banner = BannerModel.query.get(banner_id)
    if not banner:
        return restful.paramserror(message='没有这个轮播图')
    db.session.delete(banner)
    db.session.commit()
    return restful.success()
Ejemplo n.º 11
0
Archivo: views.py Proyecto: jc5055/xfz
def edit_news_category(request):
    form = NewsEditForm(request.POST)
    if form.is_valid():
        pk = form.cleaned_data.get('pk')
        name = form.cleaned_data.get('name')
        try:
            NewsCategory.objects.filter(pk=pk).update(name=name)
            return restful.ok()
        except:
            return restful.paramserror('分类不存在')
    else:
        restful.paramserror('分类参数异常')
Ejemplo n.º 12
0
Archivo: views.py Proyecto: CZ2016/zedd
def unhighp():
    post_id = request.form.get('post_id')
    if not post_id:
        return restful.paramserror(message='请传入帖子ID')
    else:
        post = PostModel.query.get(post_id)
        if not post:
            return restful.paramserror(message='不存在该帖子')
        else:
            highlight = HighPostModel.query.filter_by(post_id=post_id).first()
            db.session.delete(highlight)
            db.session.commit()
            return restful.success()
Ejemplo n.º 13
0
Archivo: views.py Proyecto: NAZBIN/Web
def edit_news_category(request):
    #form表单验证数据
    form = EditNewsCategoryForm(request.POST)
    if form.is_valid():
        pk = form.cleaned_data.get('pk')
        name = form.cleaned_data.get('name')
        try:
            NewsCategory.objects.filter(pk=pk).update(name=name)
            return restful.ok()
        except:
            return restful.paramserror(message="不存在该分类!")
    else:
        return restful.paramserror(message=form.get_errors())
Ejemplo n.º 14
0
def edit_category(request):
	form = EditCategory(request.POST)
	if form.is_valid():
		pk = request.POST.get('pk')
		category = request.POST.get('category')
		try:
			NewsCategory.objects.filter(pk=pk).update(name=category)
			return restful.ok()
		except:
			message = "该分类不存在!"
			return restful.paramserror(message=message)
	else:
		return restful.paramserror("参数错误!")
Ejemplo n.º 15
0
Archivo: views.py Proyecto: CZ2016/zedd
def highp():
    post_id = request.form.get('post_id')
    if not post_id:
        return restful.paramserror(message='请传入帖子ID')
    else:
        post = PostModel.query.get(post_id)
        if not post:
            return restful.paramserror(message='不存在该帖子')
        highpost = HighPostModel()
        highpost.post = post
        db.session.add(highpost)
        db.session.commit()
        return restful.success()
Ejemplo n.º 16
0
def send():
    form = SmscaptchaForm(request.form)
    if form.validate():
        mobile = request.form.get("telephone")
        captcha = Captcha.gene_text()  #调用验证码内容
        if send_captcha.send_capt(mobile, captcha):  #调用封装好的短信发送函数
            cache.set(mobile, captcha)  #将短信验证码存储在memcache中
            return restful.success(message="发送成功")  #成功直接调用不传参数
        else:
            return restful.paramserror(message="发送失败")
    else:
        message = form.errors.popitem()[1][0]
        return restful.paramserror(message)
Ejemplo n.º 17
0
def uboard():
    form = UpdateBoard(request.form)
    if form.validate():
        board_id = form.board_id.data
        name = form.name.data
        board = BoardModel.query.get(board_id)
        if board:
            board.name = name
            db.session.commit()
            return restful.success()
        else:
            return restful.paramserror(message='没有这个版块')
    else:
        return restful.paramserror(form.get_error())
Ejemplo n.º 18
0
 def post(self):
     form = ResetpwdForm(request.form)
     if form.validate():
         newpwd = form.newpwd.data
         oldpwd = form.oldpwd.data
         user = g.cms_user
         if user.check_password(oldpwd):
             user.password = newpwd
             db.session.commit()
             return restful.success()  # utils.py中restful定义的http状态
         else:
             return restful.paramserror('旧密码错误')
     else:
         return restful.paramserror(form.get_error())
Ejemplo n.º 19
0
 def post(self):
     form = SigninForm(request.form)
     if form.validate():
         telephone = form.telephone.data
         password = form.password.data
         remember = form.remember.data
         user = FrontUser.query.filter_by(telephone=telephone).first()
         if user and user.check_password(password):
             session[config.FRONT_USER_ID] = user.id
             if remember:
                 session.permanent = True
             return restful.success()
         else:
             return restful.paramserror(message='手机号码或密码错误')
     return restful.paramserror(message=form.get_error())
Ejemplo n.º 20
0
 def post(self, request):
     form = CourseForm(request.POST)
     if form.is_valid():
         title = form.cleaned_data.get('title')
         category_id = form.cleaned_data.get('category_id')
         teacher_id = form.cleaned_data.get('teacher_id')
         price = form.cleaned_data.get('price')
         duration = form.cleaned_data.get('duration')
         video_url = form.cleaned_data.get('video_url')
         cover_url = form.cleaned_data.get('cover_url')
         desc = form.cleaned_data.get('desc')
         category = CourseCategory.objects.get(pk=category_id)
         teacher = Teacher.objects.get(pk=teacher_id)
         Course.objects.create(title=title,
                               teacher=teacher,
                               category=category,
                               price=price,
                               desc=desc,
                               duration=duration,
                               video_url=video_url,
                               cover_url=cover_url)
         return restful.ok()
     else:
         print(form.get_error())
         return restful.paramserror(message=form.get_error())
Ejemplo n.º 21
0
def uboards():
    form = UboardsForm(request.form)
    if form.validate():
        id = request.form.get("id")
        name = request.form.get("name")
        boards = BoardsModel.query.filter(
            BoardsModel.id == id).first()  #查是否有这个对象
        if boards:  #有则编辑
            boards.name = name
            db.session.commit()
            return restful.success()
        else:
            return restful.paramserror(message="没有这个数据")
    else:
        message = form.errors.popitem()[1][0]
        return restful.paramserror(message)
Ejemplo n.º 22
0
def del_news(request):
	news_id = request.POST.get('news_id')
	try:
		News.objects.get(pk=news_id).delete()
		return restful.ok()
	except News.DoesNotExist:
		return restful.paramserror(message='该新闻不存在!')
Ejemplo n.º 23
0
	def post(self,request):
		form = WriteNewsForm(request.POST)
		if form.is_valid():
			title = form.cleaned_data.get('title')
			desc = form.cleaned_data.get('desc')
			thumbnail = form.cleaned_data.get('thumbnail')
			content = form.cleaned_data.get('content')
			category_id = form.cleaned_data.get('category')
			category = NewsCategory.objects.get(pk=category_id)
			News.objects.create(title=title,desc=desc,thumbnail=thumbnail,
			                    content=content, category=category,author=request.user)
			return restful.ok()
		else:
			message = form.get_errors()
			print(message)
			restful.paramserror(message=message)
Ejemplo n.º 24
0
Archivo: views.py Proyecto: jc5055/xfz
def add_news_comment(request):
    form = CommentForm(request.POST)
    if form.is_valid():
        news_id = form.cleaned_data.get('news_id')
        content = form.cleaned_data.get('content')
    try:
        news = News.objects.get(pk=news_id)
        comment = Comment.objects.create(content=content,
                                         news=news,
                                         author=request.user)
        serializer = NewsCommentSerializer(comment)
        return restful.result(data=serializer.data)
    except:
        return restful.paramserror(message='新闻不存在')

    return restful.paramserror(message='请求参数异常')
Ejemplo n.º 25
0
Archivo: views.py Proyecto: CZ2016/zedd
    def post(self):
        form = ResetPwdForm(request.form)
        if form.validate():
            oldpwd = form.oldpwd.data
            newpwd = form.newpwd.data
            user = g.cms_user
            if user.check_password(oldpwd):
                user.password = newpwd
                db.session.commit()
                # {'code':200,message=''}
                return restful.success()
            else:
                return restful.paramserror('旧密码错误')

        else:
            return restful.paramserror(message=form.get_errors())
Ejemplo n.º 26
0
 def post(self):
     form = ResetPaw(request.form)
     if form.validate():
         oldpaw = request.form.get("oldpaw")
         newpaw = request.form.get("newpaw")
         user = g.cms_user  #通过g对象拿到登陆用户的对象
         if user.check_password(oldpaw):  #调用配置文件中核查函数,看旧密码是否存在
             user.password = newpaw  #更改密码
             db.session.commit()
             return restful.success("密码修改成功")
         else:
             return restful.paramserror("旧密码错误")  #在utils中对返回jsonify进行封装
     else:
         message = form.errors.popitem()[1][0]  #取出错误信息
         print(message)
         return restful.paramserror("两次密码不一致")
Ejemplo n.º 27
0
def del_category(request):
    pk = request.POST.get('pk')
    try:
        NewsCategory.objects.filter(pk=pk).delete()
        return restful.ok()
    except:
        return restful.paramserror(message='此分类不存在!')
Ejemplo n.º 28
0
def add_comment():
    form = AddCommentForm(request.form)
    if form.validate():
        content = form.content.data
        post_id = form.post_id.data
        post = PostModel.query.get(post_id)
        if post:
            comment = CommentModel(content=content)
            comment.post = post
            comment.author = g.front_user
            db.session.add(comment)
            db.session.commit()
            return restful.success()
        else:
            return restful.paramserror(message='没有这个帖子')
    else:
        return restful.paramserror(form.get_error())
Ejemplo n.º 29
0
 def post(self, request):
     form = AddCourseCategoryForm(request.POST)
     if form.is_valid():
         name = form.cleaned_data.get('name')
         CourseCategory.objects.create(name=name)
         return restful.ok()
     else:
         return restful.paramserror(message=form.get_error())
Ejemplo n.º 30
0
def add_category(request):
    category = request.POST.get('category')
    exists = NewsCategory.objects.filter(name=category).exists()
    if not exists:
        NewsCategory.objects.create(name=category)
        return restful.ok()
    else:
        return restful.paramserror(message='该分类已经存在!')