Example #1
0
def register_staff(request):
    context = {
        'page': 'register_staff',
    }
    if request.session.get('is_login', None):
        username = request.session.get('username')
        store = request.session.get('storeid')
        context['username'] = username
        if request.session.get('identity') != 'shopkeeper':
            context['errmsg'] = '请以店主账号登录'
            shopkeeper = False
            return render(request,
                          'staff/register_staff.html',
                          context=context)
        else:
            shopkeeper = True
    else:
        return login(request)

    if request.method == "POST" and shopkeeper:
        username = request.POST.get('username')
        name = request.POST.get('name')
        password = request.POST.get('password')
        phonenumber = request.POST.get("phonenumber")
        desc = request.POST.get("desc")
        user_model = Staff.objects.filter(username=username).filter(
            store_id=store)

        # 参数验证
        if not all([username, name, password, phonenumber, desc]):
            # 参数不完整
            context['errmsg'] = '数据不完整'
            return render(request,
                          'staff/register_staff.html',
                          context=context)

        if user_model:  #不为NULL
            context['errmsg'] = '用户名已存在'
            return render(request,
                          'staff/register_staff.html',
                          context=context)
        else:
            user = Staff()
            user.username = username
            user.name = name
            user.password = password
            user.phonenumber = phonenumber
            user.desc = desc
            user.store_id = store
            user.save()
            return redirect(reverse('staff:find_staff'))
    return render(request, 'staff/register_staff.html', context=context)