Beispiel #1
0
    def post(self, request, *args, **kwargs):
        action = request.query_params.get('action')
        if action == 'register':
            staff_name = request.data.get('staff_name')
            password = request.data.get('password')
            email = request.data.get('email')
            department = request.data.get('department')
            dp = Department.objects.get(pk=department)
            is_leader = request.data.get('is_leader')
            staff = Staff()
            staff.staff_name = staff_name
            staff.password = password
            staff.email = email
            staff.department = dp
            if is_leader:
                staff.is_leader = is_leader
            staff.save()

            data = {
                'status': 200,
                'msg': 'create staff success',
                'staff': {
                    'id': staff.id,
                    'staff_name': staff.staff_name,
                    'email': staff.email,
                    'department': staff.department.dp_name,
                    'is_leader': staff.is_leader,
                    'is_delete': staff.is_delete
                }
            }
            return Response(data)
        elif action == 'login':
            return self.login(request, *args, **kwargs)
        else:
            raise ValidationError(detail='请提供正确的请求动作')
Beispiel #2
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)