예제 #1
0
def prod_groups(request):
    context = {}
    context["request_path"] = request.path

    cat_id = request.GET.get("cat_id")

    catalog = get_object_or_404(models.Catalog, id=cat_id)

    context["cat_id"] = cat_id
    context["section_name"] = catalog.title
    all_prod_groups = catalog.prod_groups.all()
    context["prod_groups"] = all_prod_groups

    if request.method == 'POST':
        post_name = request.POST.get("post_name")
        if post_name == "login":
            login_form = forms.Login(request.POST)
            result = check_user(request)
            if result is True:
                return http.HttpResponseRedirect("/catalog/prod_groups")
            else:
                context["login_error"] = result
                context["login_form"] = login_form
                return render_to_response("Sections.html", context, context_instance=RequestContext(request))
    return render_to_response("Sections.html", context, context_instance=RequestContext(request))
예제 #2
0
def order(request):
    context = {}

    prod_id = request.session.get("basket", [])
    basket_products = models.Product.objects.filter(id__in = prod_id)
    price_sum = sum([i.price for i in basket_products])

    context["request_path"] = request.path
    context["products"] = basket_products
    context["price"] = price_sum

    if request.method == 'POST':
        post_name = request.POST.get("post_name")
        if post_name == "login":
            login_form = forms.Login(request.POST)
            result = check_user(request)
            if result is True:
                return http.HttpResponseRedirect(request.path)
            else:
                context["login_error"] = result
                context["login_form"] = login_form
                return render_to_response("Order.html", context, context_instance=RequestContext(request))
        elif post_name == "order":
            order_form = forms.Order(request.POST)
            context["order_form"] = order_form
            if order_form.is_valid():
                if basket_products:
                    full_name = request.POST.get("full_name")
                    address = request.POST.get("address")
                    phone_number = request.POST.get("phone_number")

                    order_prods = ",".join([prod.name for prod in basket_products])

                    models.Order.objects.create(full_name=full_name, address=address, phone_number=phone_number,
                                                prod_name=order_prods, price=price_sum, order_date=datetime.now())

                    basket_products.update(sale_count=F('sale_count')+1)
                    request.session["basket"] = []
                    return http.HttpResponseRedirect("?notice_successful=Заказ оформлен. Спасибо, что выбрали нас!")
                else:
                    context["order_error"] = "Выбирите товар для заказа!"
                    return render_to_response("Order.html", context, context_instance=RequestContext(request))
            else:
                errors = {}
                errors["full_name"] = order_form.errors.get("full_name")
                errors["address"] = order_form.errors.get("address")
                errors["phone_number"] = order_form.errors.get("phone_number")
                json_errors = json.dumps(errors)
                if request.is_ajax():
                    return http.HttpResponse(json_errors, content_type="application/json")
                else:
                    return render_to_response("Order.html", context, context_instance=RequestContext(request))
    else:
        order_form = forms.Order()
        context["order_form"] = order_form
        context["notice_successful"] = request.GET.get("notice_successful")
    return render_to_response("Order.html", context, context_instance=RequestContext(request))
예제 #3
0
def registration(request):
    context = {}
    context.update(csrf(request))
    context["request_path"] = request.path
    if request.method == 'POST':
        post_name = request.POST.get("post_name")
        if post_name == "registration":
            reg_form = auth_forms.UserCreationForm(request.POST)
            if reg_form.is_valid():
                user_password = request.POST.get("password1")
                user_login = request.POST.get("username")
                full_name = request.POST.get("full_name")

                user = User.objects.create_user(user_login, None, user_password)

                User.objects.filter(username=user_login).update(first_name=full_name)
                group = Group.objects.get(name="Зарегистрированные пользователи")
                user.groups.add(group)

                user = auth.authenticate(username=user_login, password=user_password)
                auth.login(request, user)
                print("saaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
                if request.is_ajax():
                    return http.HttpResponse("ok", content_type="text/html")
                else:
                    return http.HttpResponseRedirect("/")
            else:
                errors = {}
                errors["username"] = reg_form.errors.get("username")
                errors["password1"] = reg_form.errors.get("password1")
                errors["password2"] = reg_form.errors.get("password2")
                context["errors"] = errors
                context["reg_form"] = reg_form
                json_errors = json.dumps(errors)
                if request.is_ajax():
                    return http.HttpResponse(json_errors, content_type="application/json")
                else:
                    return render_to_response("Registration.html", context, context_instance=RequestContext(request))
        elif post_name == "login":
            login_form = forms.Login(request.POST)
            result = check_user(request)
            if result is True:
                return http.HttpResponseRedirect("/registration/")
            else:
                context["login_error"] = result
                context["login_form"] = login_form
                return render_to_response("Registration.html", context, context_instance=RequestContext(request))
    else:
        reg_form = auth_forms.UserCreationForm()
        context["reg_form"] = reg_form

    return render_to_response("Registration.html", context, context_instance=RequestContext(request))
예제 #4
0
def delivery(request):
    context = {}
    context["request_path"] = request.path
    if request.method == 'POST':
        post_name = request.POST.get("post_name")
        if post_name == "login":
            login_form = forms.Login(request.POST)
            result = check_user(request)
            if result is True:
                return http.HttpResponseRedirect("/delivery/")
            else:
                context["login_error"] = result
                context["login_form"] = login_form
                return render_to_response("Delivery.html", context, context_instance=RequestContext(request))
    return render_to_response("Delivery.html", context, context_instance=RequestContext(request))
예제 #5
0
def catalog(request):
    context = {}
    context["request_path"] = request.path

    catalog = models.Catalog.objects.all()
    context["catalog"] = catalog

    if request.method == 'POST':
        post_name = request.POST.get("post_name")
        if post_name == "login":
            login_form = forms.Login(request.POST)
            result = check_user(request)
            if result is True:
                return http.HttpResponseRedirect("/catalog/")
            else:
                context["login_error"] = result
                context["login_form"] = login_form
                return render_to_response("Catalog.html", context, context_instance=RequestContext(request))
    return render_to_response("Catalog.html", context, context_instance=RequestContext(request))
예제 #6
0
def news(request):
    context = {}
    context["request_path"] = request.path

    all_news = models.News.objects.all()
    context["all_news"] = all_news

    if request.method == 'POST':
        post_name = request.POST.get("post_name")
        if post_name == "login":
            login_form = forms.Login(request.POST)
            result = check_user(request)
            if result is True:
                return http.HttpResponseRedirect("/news/")
            else:
                context["login_error"] = result
                context["login_form"] = login_form
                return render_to_response("News.html", context, context_instance=RequestContext(request))
    return render_to_response("News.html", context, context_instance=RequestContext(request))
예제 #7
0
def user_form(request):
    login_form = forms.Login()
    return {"login_form": login_form}
예제 #8
0
def index(request):
    context = {}
    context.update(csrf(request))
    context["request_path"] = request.path

    catalog = models.Catalog.objects.all()

    producers = models.Producer.objects.all()
    top_products = models.Product.objects.order_by("-sale_count")[:4]
    sliders = models.Slider.objects.all()

    context["producers"] = producers
    context["products"] = top_products
    context["sliders"] = sliders
    context["catalog"] = catalog

    if request.method == 'POST':
        post_name = request.POST.get("post_name")
        if post_name == "login":
            login_form = forms.Login(request.POST)
            result = check_user(request)
            if result is True:
                if request.is_ajax():
                    return http.HttpResponse("ok", content_type="text/html")
                else:
                    return http.HttpResponseRedirect("/")
            else:
                context["login_error"] = result
                context["login_form"] = login_form
                if request.is_ajax():
                    return http.HttpResponse(result, content_type="text/html")
                else:
                    return render_to_response("Index.html", context, context_instance=RequestContext(request))
        elif post_name == "letter":
            letter_form = forms.Letter(request.POST)

            name = request.POST.get("name")
            email = request.POST.get("email")
            text = request.POST.get("text")

            if name and email and text:
                models.Letter.objects.create(name=name, email=email, text=text, send_date=datetime.now())
                return http.HttpResponseRedirect("?notice_successful=Письмо отправлено")
            else:
                context["letter_form"] = letter_form
                context["letter_error"] = "Заполните все поля!"
                render_to_response("Index.html", context)

        elif post_name == "basket":
            prod_id = int(request.POST.get("prod_id"))
            in_basket = add_in_basket(request, prod_id)
            if request.is_ajax():
                response_data = json.dumps({"basket_count": len(in_basket), "id": prod_id})
                return http.HttpResponse(response_data, content_type="application/json")
            else:
                return http.HttpResponseRedirect(request.path)

        elif post_name == "bookmarks":
            prod_id = int(request.POST.get("prod_id"))
            in_bookmarks = add_in_bookmarks(request, prod_id)
            if request.is_ajax():
                response_data = json.dumps({"bookmarks_count": len(in_bookmarks), "id": prod_id})
                return http.HttpResponse(response_data, content_type="application/json")
            else:
                return http.HttpResponseRedirect(request.path)
    else:
        letter_form = forms.Letter()
        context["letter_form"] = letter_form
        context["notice_successful"] = request.GET.get("notice_successful")

    return render_to_response("Index.html", context, context_instance=RequestContext(request))
예제 #9
0
def products(request):
    context = {}
    context["request_path"] = request.path
    context.update(csrf(request))

    group_id = request.GET.get("group_id")
    cat_id = request.GET.get("cat_id")
    choose_producers = request.GET.getlist("producers")
    choose_distinction = request.GET.get("distinction")
    min_cost = int(request.GET.get("min_cost", 0))
    max_cost = int(request.GET.get("max_cost", 100000))
    filter = request.GET.get("filter", "-add_date")
    direction = request.GET.get("direction", "up")
    choose_producers[:] = [int(i) for i in choose_producers]
    page = request.GET.get("page")

    prod_group = get_object_or_404(models.ProductGroup, id=group_id)
    get_parameters = "?group_id={0}&min_cost={1}&max_cost={2}&filter={3}&direction={4}&page={5}&".format(str(group_id),
                                                                                                str(min_cost),str(max_cost),
                                                                                                filter, direction, page)
    form_get_parameters = get_parameters
    get_producers = ""
    if choose_producers:
        if choose_distinction:
            get_parameters += "distinction={0}&".format(str(choose_distinction))
            for identity in choose_producers:
                get_producers += "producers={0}&".format(str(identity))
            filter_products = models.Product.objects.filter(producer__in=choose_producers,
                                                            distinction=choose_distinction,
                                                            price__range=(min_cost, max_cost)).order_by(filter)
        else:
            for identity in choose_producers:
                get_producers += "producers={0}&".format(str(identity))
            filter_products = models.Product.objects.filter(producer__in=choose_producers,
                                                            price__range=(min_cost, max_cost)).order_by(filter)
    else:
        if choose_distinction:
            get_parameters += "distinction={0}&".format(str(choose_distinction))
            filter_products = models.Product.objects.filter(distinction=choose_distinction,
                                                            price__range=(min_cost, max_cost)).order_by(filter)
        else:
            filter_products = models.Product.objects.filter(price__range=(min_cost, max_cost)).order_by(filter)

    get_parameters += get_producers

    all_distinctions = prod_group.distinctions.all()
    all_producers = models.Producer.objects.all()

    if direction == "down":
        filter_products = list(reversed(filter_products))

    page_list, prod_page, show_pagination = create_pagination(filter_products, page)

    context["page_list"] = page_list
    context["prod_page"] = prod_page
    context["show_pagination"] = show_pagination
    context["section_name"] = prod_group.name
    context["producers"] = all_producers
    context["distinctions"] = all_distinctions
    context["group_id"] = group_id
    context["min_cost"] = min_cost
    context["max_cost"] = max_cost
    context["filter"] = filter
    context["get_parameters"] = get_parameters
    context["form_get_parameters"] = form_get_parameters
    context["cat_id"] = cat_id

    in_basket = request.session.get("basket", [])
    in_bookmarks = request.session.get("bookmarks", [])
    context["basket"] = in_basket
    context["bookmarks"] = in_bookmarks

    if request.method == 'POST':
        post_name = request.POST.get("post_name")
        if post_name == "login":
            login_form = forms.Login(request.POST)
            result = check_user(request)
            if result is True:
                return http.HttpResponseRedirect("/products/")
            else:
                context["login_error"] = result
                context["login_form"] = login_form
                return render_to_response("Products.html", context, context_instance=RequestContext(request))
        elif post_name == "basket":
            prod_id = int(request.POST.get("prod_id"))
            in_basket = add_in_basket(request, prod_id)
            if request.is_ajax():
                response_data = json.dumps({"basket_count": len(in_basket), "id": prod_id})
                return http.HttpResponse(response_data, content_type="application/json")
            else:
                return http.HttpResponseRedirect(request.path + get_parameters)
        elif post_name == "bookmarks":
            prod_id = int(request.POST.get("prod_id"))
            in_bookmarks = add_in_bookmarks(request, prod_id)
            if request.is_ajax():
                response_data = json.dumps({"bookmarks_count": len(in_bookmarks), "id": prod_id})
                return http.HttpResponse(response_data, content_type="application/json")
            else:
                return http.HttpResponseRedirect(request.path + get_parameters)
    return render_to_response("Products.html", context, context_instance=RequestContext(request))