示例#1
0
    def commit(self):

        #make the user
        myUser = User()

        #load up the user
        myUser.first_name = self.cleaned_data.get('first_name')
        myUser.last_name = self.cleaned_data.get('last_name')
        myUser.email = self.cleaned_data.get('email')
        myUser.birthdate = self.cleaned_data.get('birthdate')
        myUser.address = self.cleaned_data.get('address')
        myUser.city = self.cleaned_data.get('city')
        myUser.state = self.cleaned_data.get('state')
        myUser.zipcode = self.cleaned_data.get('zipcode')
        myUser.set_password(self.cleaned_data.get('password'))

        #save the new user
        myUser.save()

        user = authenticate(email=self.cleaned_data.get('email'),
                            password=self.cleaned_data.get('password'))
        login(self.request, user)
示例#2
0
    def post(self, request):

        parameter_missing = False
        parameter_missing_name = ""
        if request.data.get("first_name") is None or request.data.get(
                "first_name") == "":
            parameter_missing_name = parameter_missing_name + "first_name, "
            parameter_missing = True
        elif request.data.get("last_name") is None or request.data.get(
                "last_name") == "":
            parameter_missing_name = parameter_missing_name + "last_name, "
            parameter_missing = True
        elif request.data.get("email") is None or request.data.get(
                "email") == "":
            parameter_missing_name = parameter_missing_name + "email, "
            parameter_missing = True
        elif request.data.get("phone") is None or request.data.get(
                "phone") == "":
            parameter_missing_name = parameter_missing_name + "phone, "
            parameter_missing = True
        elif request.data.get("aadhar") is None or request.data.get(
                "aadhar") == "":
            parameter_missing_name = parameter_missing_name + "aadhar, "
            parameter_missing = True

        if parameter_missing:
            return Response({
                "status": False,
                "status_code": HTTP_204_NO_CONTENT,
                "message": parameter_missing_name + " missing"
            })

        existed_user_email = User.objects.filter(
            email=request.data.get("email")).first()
        if existed_user_email is None:

            existed_user_phone = User.objects.filter(
                phone=request.data.get("phone")).first()
            if existed_user_phone is None:

                existed_user_aadhar = User.objects.filter(
                    aadhar=request.data.get("aadhar")).first()
                if existed_user_aadhar is None:

                    user = User()
                    user.first_name = request.data.get("first_name")
                    user.middle_name = request.data.get("middle_name")
                    user.last_name = request.data.get("last_name")
                    user.email = request.data.get("email")
                    user.phone = request.data.get("phone")
                    user.aadhar = request.data.get("aadhar")
                    user.country = request.data.get("country")
                    user.state = request.data.get("state")
                    user.city = request.data.get("city")
                    user.constitucy = request.data.get("constitucy")
                    user.ward = request.data.get("ward")
                    user.landmark = request.data.get("landmark")
                    user.pincode = request.data.get("pincode")
                    user.gender = request.data.get("gender")
                    user.date_of_birth = request.data.get("date_of_birth")
                    user.role = "member"
                    user.occupation_type = request.data.get("occupation_type")
                    user.occupation_title = request.data.get(
                        "occupation_title")
                    user.save()

                    return Response({
                        "status": True,
                        "status_code": HTTP_200_OK,
                        "message": "User Created.",
                        "user": UserSerializer(user).data
                    })
                else:
                    return Response({
                        "status": False,
                        "status_code": HTTP_302_FOUND,
                        "message": "Aadhar Exist."
                    })
            else:
                return Response({
                    "status": False,
                    "status_code": HTTP_302_FOUND,
                    "message": "Phone Exist."
                })
        else:
            return Response({
                "status": False,
                "status_code": HTTP_302_FOUND,
                "message": "Email Exist."
            })
示例#3
0
def create_order(request):
    args = {
        'form': OrderForm(),
        'delivery_mass': DeliveryType.objects.all(),
        'user': request.user,
        'user_active': request.user.is_authenticated(),
        'cart_sum': get_sum(request),
    }
    args.update(csrf(request))
    is_valid = True

    if request.user.is_authenticated():
        ord = Order()
        ord.first_name = request.user.first_name
        ord.last_name = request.user.last_name
        ord.email = request.user.email
        ord.phone = request.user.phone
        ord.city = request.user.city
        ord.address = request.user.address
        args['form'] = OrderForm(instance=ord)

    if request.POST:
        if request.POST.get('first_name', "") == "":
            args['user_inform_error'] = True
            args['first_name_error'] = "error_field"
            is_valid = False
        if request.POST.get('last_name', "") == "":
            args['user_inform_error'] = True
            args['last_name_error'] = "error_field"
            is_valid = False
        if request.POST.get('email', "") == "":
            args['user_inform_error'] = True
            args['email_error'] = "error_field"
            is_valid = False
        if request.POST.get('phone', "") == "":
            args['user_inform_error'] = True
            args['phone_error'] = "error_field"
            is_valid = False

        if request.POST.get('city', "") == "":
            args['address_inform_error'] = True
            args['city_error'] = "error_field"
            is_valid = False
        if request.POST.get('address', "") == "":
            args['address_inform_error'] = True
            args['address_error'] = "error_field"
            is_valid = False

        if request.POST.get('delivery', "") == "":
            args['delivery_error'] = "выберите тип доставки"
            args['delivery_error'] = "error_field"
            is_valid = False

        if 'register' in request.POST:
            try:
                email = request.POST.get('email', "")
                User.objects.get(email=email)
                args['register_error'] = "Пользователь с таким email уже разегистрирован"
                is_valid = False
            except User.DoesNotExist:
                pass

        form = OrderForm(request.POST)
        if is_valid and form.is_valid():
            order = form.save(commit=False)
            delivery = DeliveryType.objects.get(id=request.POST['delivery'])
            order.delivery = delivery.title
            order.delivery_price = delivery.price
            order.status = 0
            print(get_sum(request))
            order.sum = get_sum(request)
            if request.user.is_authenticated():
                order.user = request.user
            order.save()
            cart = get_cart(request)
            if cart:
                for pr in CartProduct.objects.filter(cart=cart):
                    if pr.product.sale_status:
                        pr.price_order = pr.product.price_sale
                    else:
                        pr.price_order = pr.product.price
                    pr.save()
                    order.products.add(pr)
            else:
                args['cart_error'] = "в вашей корзине ничего нет"
            order.save()

            if 'register' in request.POST:
                new_user = User()
                new_user.first_name = request.POST.get('first_name', "")
                new_user.last_name = request.POST.get('last_name', "")
                new_user.email = request.POST.get('email', "")
                new_user.phone = request.POST.get('phone', "")
                new_user.city = request.POST.get('city', "")
                new_user.address = request.POST.get('address', "")
                new_user.username = translit(new_user.first_name) + "_" + random_str(6)
                password = random_str(7)
                new_user.set_password(password)
                new_user.save()
                # отправка уведомления
                user = auth.authenticate(username=new_user.username, password=password)
                if user is not None and user.is_active:
                    auth.login(request, user)
                return redirect("/orders/reg_thanks/")
            elif request.user.is_authenticated():
                request.user.last_name = request.POST.get('last_name', "")
                request.user.first_name = request.POST.get('first_name', "")
                request.user.phone = request.POST.get('phone', "")
                request.user.city = request.POST.get('city', "")
                request.user.address = request.POST.get('address', "")
                request.user.save()

            return redirect("/orders/thanks/")

        args['form'] = form
    return render_to_response("create_order.html", args)