Example #1
0
def login_view(request):
    form = Login_form(request.POST)
    if form.is_valid():  # 验证表单数据,具体验证方法,也可以自己写。验证通过:
        telephone = form.cleaned_data.get('telephone')
        password = form.cleaned_data.get('password')
        remember = form.cleaned_data.get(
            'remember')  # 此处要用get,因为如果没有选中“记住我”选项,则remember为空
        user = authenticate(request, telephone=telephone,
                            password=password)  # 验证登录信息,验证成功,则返回user对象
        if user:  # 返回有效user对象,说明输入的账号密码正确
            if user.is_active:  # 用户未被禁用
                login(request, user)  # 将登录信息存入session
                if remember:  # 登录时选中了“记住我”
                    request.session.set_expiry(
                        None)  # 设置session过期时间,None为默认过期时间。默认过期时间可以在settings中设置
                else:  # 未选中
                    request.session.set_expiry(0)

                # 登录验证后,返回给前端的信息由后端和前端进行协商这里返回三个:
                # code状态码:200登录成功;405:登录失败,用户被禁用;400:用户名或者密码错误
                # message:发送什么信息,也是由前后端协商
                # data:一些数据,也是通过前后端协商
                # 不管是否成功,一般返回的信息都包含三个内容:自定义的状态码,message,data。内容根据需要可以为空,但一般这三个键都有
                return result.success()
            else:  # 用户被禁用,黑名单
                return result.auth_freeze()
        else:  # 未返回user信息:账号密码格式符合,但是不正确
            return result.auth_pwd_error()
    else:  # 验证表单数据未通过:账号密码格式不正确
        errors = form.get_errors()  # 返回form表单的验证错误信息
        if errors.get('img_verify'):
            message = '请输入正确的验证码'
        else:
            message = '手机号或密码错误'
        return result.auth_pwd_error(message=message, data=errors)
Example #2
0
def edit_categnews(request):
    pk = request.POST.get('pk')
    name = request.POST.get('name')
    if pk and name:
        if pk.isdigit():
            pk = int(pk)
            if CaetgNews.objects.filter(name=name).exists():
                return result.auth_pwd_error(message='该分类已存在')
            else:
                caetg = CaetgNews.objects.filter(pk=pk)
                if caetg:
                    caetg.update(name=name)
                    return result.success(message='修改成功')
    return result.auth_pwd_error(message='修改失败')
Example #3
0
 def post(self, request):
     form = NewsForm(request.POST)
     if form.is_valid():
         title = form.cleaned_data.get('title')
         desc = form.cleaned_data.get('desc')
         category_id = form.cleaned_data.get('category_id')
         thumbnail = form.cleaned_data.get('thumbnail')
         content = form.cleaned_data.get('content')
         author = request.user
         category = CaetgNews.objects.get(pk=category_id)
         pk = request.POST.get('pk')
         news = None
         if pk.isdigit():
             pk = int(pk)
             if News.objects.filter(pk=pk).exists():
                 news = News.objects.get(pk=pk)
                 news.title = title
                 news.desc = desc
                 news.category = category
                 news.thumbnail = thumbnail
                 news.content = content
             else:
                 news = News.objects.create(title=title,
                                            desc=desc,
                                            category=category,
                                            thumbnail=thumbnail,
                                            content=content,
                                            author=author)
         news.save()
         url = reverse('cms:news_manage')
         data = {'url': url}
         return result.success(data=data)
     else:
         error = form.get_errors()
         return result.auth_pwd_error(data=error)
Example #4
0
def delete_banner(request):
    id = request.POST.get('id')
    if Banner.objects.filter(pk=id).exists():
        Banner.objects.filter(pk=id).delete()
        return result.success()
    else:
        return result.auth_pwd_error()
Example #5
0
def delete_news(request):
    select_id = request.POST.get('select_id')
    message = '删除成功'
    if select_id:
        select_id = [i for i in select_id[:-1].split('$')]
        for i in select_id:
            if not i.isdigit():
                message = '您未正确选择需要删除的新闻'
                return result.auth_pwd_error(message=message)
        # 获取数据库中该id的数据并删除,由于没有判断该id是否存在,所以对不存在的数据会忽略掉,并返回删除成功。
        select_id = [int(i) for i in select_id]
        News.objects.filter(pk__in=select_id).delete()
        return result.success(message=message)
    else:
        message = '您未正确选择需要删除的新闻'
        return result.auth_pwd_error(message=message)
Example #6
0
def add_categnews(request):
    names = request.POST.get('name')
    if names:
        names = names.split(' ')
        for name in names:
            # 如果该类名不存在,添加,如果存在就不采取操作,达到了添加的目的,所以也是添加成功
            if not CaetgNews.objects.filter(name=name).exists():
                CaetgNews.objects.create(name=name)
        return result.success(message='添加成功')
    return result.auth_pwd_error(message='分类名称不能为空')
Example #7
0
 def wrapper(request, *args, **kwargs):
     if request.user.is_authenticated:
         return func(request, *args, **kwargs)
     else:
         if request.is_ajax():
             return result.auth_pwd_error(message='请先登录!')
         else:
             try:
                 return redirect(reverse(login_url))
             except:
                 return redirect(login_url)
Example #8
0
 def post(self, request):
     form = BannerForm(request.POST)
     if form.is_valid():
         priority = form.cleaned_data.get('priority')
         link_url = form.cleaned_data.get('link_url')
         banner_img = form.cleaned_data.get('banner_img')
         pk = request.POST.get('id')
         message = '添加成功!'
         if pk:
             if Banner.objects.filter(pk=pk).exists():
                 Banner.objects.filter(pk=pk).delete()
                 message = '更改成功!'
             else:
                 return result.auth_pwd_error(message='该轮播图不存在!')
         new_banner = Banner(priority=priority,
                             link_url=link_url,
                             banner_img=banner_img)
         new_banner.save()
         data = {
             'name': new_banner.pk,
         }
         return result.success(message=message, data=data)
     else:
         return result.auth_pwd_error(message='您的输入有误!')
Example #9
0
def register_view(request):
    form = Register_form(request.POST)
    if form.is_valid():
        telephone = form.cleaned_data.get('telephone')
        username = form.cleaned_data.get('username')
        password = form.cleaned_data.get('password1')
        email = form.cleaned_data.get('email')
        user = User.objects.create_user(telephone=telephone,
                                        username=username,
                                        password=password,
                                        email=email)
        login(request, user)
        return result.success(message='注册成功,正在跳转至首页...')
    else:
        errors = form.get_errors()
        return result.auth_pwd_error(message='您的注册信息有误,请修改', data=errors)
Example #10
0
def get_more(request, p):
    # p = request.GET.get('p') #获取第几页数据
    category_id = request.GET.get('category_id')
    news_num = settings.news_num
    begin = p * news_num
    end = (p + 1) * news_num
    newses = []
    if CaetgNews.objects.filter(id=category_id).exists():
        category = CaetgNews.objects.get(pk=category_id)
        newses = News.objects.select_related('author', 'category').filter(category=category).order_by('-pub_time')[
                 begin:end]
    else:
        return result.auth_pwd_error(message='新闻分类存在错误')
    seria = NewsSerializer(newses, many=True)
    data = None
    if seria.data:
        data = {'newses': seria.data}
    return result.success(data=data)
Example #11
0
def add_group(request):
    print(request.method)
    if request.method == 'GET':
        groups = Group.objects.all()
        context = {'groups': groups}
        return render(request, 'cms/add_group.html', context=context)
    if request.method == 'POST':
        telephone = request.POST.get('telephone')
        print(telephone)
        user = User.objects.filter(telephone=telephone).first()
        if not user:
            return result.auth_pwd_error()
        else:
            groups_id = request.POST.getlist('groups_id[]')
            groups = Group.objects.filter(pk__in=groups_id)
            user.groups.add(*groups)
            user.is_staff = True
            user.save()
            return result.success('设置成功!')
Example #12
0
def pub_comment(request):
    form = CommentForm(request.POST)
    if form.is_valid():
        commenter = request.user
        comment_to = request.POST.get('comment_to')
        news = request.POST.get('news')
        content = request.POST.get('content')
        master = request.POST.get('master')
        is_master = True
        print(master)
        if master:
            if Comments.objects.filter(pk=master).exists():
                is_master = False
        comment = Comments.objects.create(commenter=commenter, comment_to_id=comment_to, news_id=news, content=content,
                                master_id=master, is_master=is_master)
        comment.save()
        return result.success()
    else:
        error = form.get_errors()
        return result.auth_pwd_error(data=error)