summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--config/settings.py2
-rw-r--r--config/urls.py9
-rw-r--r--core/templates/core/login.html2
-rw-r--r--core/templates/core/product_form.html15
-rw-r--r--core/templates/core/product_list.html42
-rw-r--r--core/views.py65
6 files changed, 131 insertions, 4 deletions
diff --git a/config/settings.py b/config/settings.py
index 8ba7d50..d31c34e 100644
--- a/config/settings.py
+++ b/config/settings.py
@@ -125,3 +125,5 @@ STATICFILES_DIRS = [BASE_DIR / "static"]
MEDIA_URL = "/media/"
MEDIA_ROOT = BASE_DIR / "media"
+LOGIN_REDIRECT_URL = "product_list"
+LOGOUT_REDIRECT_URL = "login"
diff --git a/config/urls.py b/config/urls.py
index ff27744..1808f0f 100644
--- a/config/urls.py
+++ b/config/urls.py
@@ -20,13 +20,18 @@ from django.contrib.auth.views import LogoutView
from django.conf import settings
from django.conf.urls.static import static
-from core.views import UserLoginView, ProductListView
+from core.views import (
+ UserLoginView,
+ ProductListView,
+ ProductCreateView,
+)
urlpatterns = [
path('admin/', admin.site.urls),
path("", UserLoginView.as_view(), name="login"),
path("logout/", LogoutView.as_view(), name="logout"),
- path("products/", ProductListView.as_view(), name="products"),
+ path("products/", ProductListView.as_view(), name="product_list"),
+ path("products/add/", ProductCreateView.as_view(), name="product_create"),
]
if settings.DEBUG:
diff --git a/core/templates/core/login.html b/core/templates/core/login.html
index c17f521..098a9fd 100644
--- a/core/templates/core/login.html
+++ b/core/templates/core/login.html
@@ -27,5 +27,7 @@
<input type="password" name="password">
<button type="submit" class="btn">Войти</button>
</form>
+
+ <a href="{% url 'product_list' %}">Войти как гость</a>
</div>
{% endblock %}
diff --git a/core/templates/core/product_form.html b/core/templates/core/product_form.html
new file mode 100644
index 0000000..81a7075
--- /dev/null
+++ b/core/templates/core/product_form.html
@@ -0,0 +1,15 @@
+{% extends 'core/base.html' %}
+
+{% block title %}{% if is_edit %}Редактирование{% else %}Добавление{% endif %} товара - ОООБувь{% endblock %}
+
+{% block h1 %}{% if is_edit %}Редактирование{% else %}Добавление{% endif %} товара{% endblock %}
+
+{% block content %}
+<div>
+ <form method="post" enctype="multipart/form-data">
+ {% csrf_token %}
+ {{ form }}
+ <button type="submit" class="btn">Сохранить</button>
+ </form>
+</div>
+{% endblock %}
diff --git a/core/templates/core/product_list.html b/core/templates/core/product_list.html
index ac4b8c7..ad7a5bf 100644
--- a/core/templates/core/product_list.html
+++ b/core/templates/core/product_list.html
@@ -5,6 +5,48 @@
{% block h1 %}Каталог товаров{% endblock %}
{% block content %}
+{% if user.role.name == "Администратор" or user.role.name == "Менеджер" %}
+<div>
+ <form method="get" style="display: flex;">
+ <div>
+ <label>Поиск:</label><br/>
+ <input type="text" name="search" value="{{ current_search }}" placeholder="Найти..." oninput="clearTimeout(this.delay); this.delay = setTimeout(() => this.form.submit(), 500);">
+ </div>
+
+ <div>
+ <label>Поставщик:</label><br/>
+ <select name="supplier" onchange="this.form.submit()">
+ <option value="all">Все поставщики</option>
+ {% for supplier in suppliers %}
+ supplier.id|stringformat:"i"
+ <option value="{{ supplier.id }}" {% if current_supplier == supplier.id|stringformat:"i" %}selected{% endif %}>{{ supplier.name }}</option>
+ {% endfor %}
+ </select>
+ </div>
+
+ <div>
+ <label>Сортировка:</label><br/>
+ <select name="sort" onchange="this.form.submit()">
+ <option value="">Без сортировки</option>
+ <option value="asc" {% if current_sort == "asc" %}selected{% endif %}>По возрастанию</option>
+ <option value="desc" {% if current_sort == "desc" %}selected{% endif %}>По убыванию</option>
+ </select>
+ </div>
+
+ {% comment %}
+ <div style="margin-left: 10px;">
+ <a href="{% url 'order_list' %}" class="btn" style="background-color: #5bc0de;">Заказы</a>
+ </div>
+ {% endcomment %}
+
+ {% if user.role.name == 'Администратор' %}
+ <div style="margin-left: auto;">
+ <a href="{% url 'product_create' %}" class="btn">Добавить товар</a>
+ </div>
+ {% endif %}
+ </form>
+</div>
+{% endif %}
<div>
{% for product in products %}
<div class="product-card
diff --git a/core/views.py b/core/views.py
index 6957abc..f090d81 100644
--- a/core/views.py
+++ b/core/views.py
@@ -1,8 +1,15 @@
+from typing import Any
+
from django.shortcuts import render
-from django.views.generic import ListView
+from django.views.generic import ListView, CreateView, UpdateView
+from django.urls import reverse_lazy
from django.contrib.auth.views import LoginView
+from django.contrib import messages
+from django.db.models import Q
+from django.contrib.auth.mixins import UserPassesTestMixin
-from .models import Product
+from .models import Product, Supplier
+from .forms import ProductForm
class UserLoginView(LoginView):
template_name = "core/login.html"
@@ -11,3 +18,57 @@ class ProductListView(ListView):
model = Product
template_name = "core/product_list.html"
context_object_name = "products"
+
+ def get_queryset(self):
+ queryset = Product.objects.all().select_related("supplier")
+ search_query = self.request.GET.get("search", "")
+ if search_query:
+ queryset = queryset.filter(
+ Q(name__icontains=search_query)
+ | Q(description__icontains=search_query)
+ | Q(manufacturer__icontains=search_query)
+ | Q(category__icontains=search_query)
+ )
+ supplier_id = self.request.GET.get("supplier", "")
+ if supplier_id and supplier_id != 'all':
+ queryset = queryset.filter(supplier_id=supplier_id)
+ sort = self.request.GET.get("sort")
+ if sort == "asc":
+ queryset = queryset.order_by("quantity")
+ if sort == "desc":
+ queryset = queryset.order_by("-quantity")
+ return queryset
+
+ def get_context_data(self, **kwargs) -> dict[str, Any]:
+ context = super().get_context_data(**kwargs)
+ context["suppliers"] = Supplier.objects.all()
+ context["current_supplier"] = self.request.GET.get("supplier", "")
+ context["current_search"] = self.request.GET.get("search", "")
+ context["current_sort"] = self.request.GET.get("sort", "")
+
+ return context
+
+
+class AdminRequiredMixin(UserPassesTestMixin):
+ def test_func(self):
+ return (
+ self.request.user.is_authenticated
+ and self.request.user.role
+ and self.request.user.role == "Администратор"
+ )
+
+class ProductCreateUpdateMixin:
+ def get_context_data(self, **kwargs):
+ context = super().get_context_data(**kwargs)
+ context["all_suppliers"] = Supplier.objects.all()
+ return context
+
+class ProductCreateView(AdminRequiredMixin, ProductCreateUpdateMixin, CreateView):
+ model = Product
+ form_class = ProductForm
+ template_name = "core/product_form.html"
+ success_url = reverse_lazy("product_list")
+
+ def form_valid(self, form):
+ messages.success(self.request, "Товар успешно добавлен")
+ return super().form_valid(form)