Compare commits

..

1 commit
master ... shop

Author SHA1 Message Date
Nikola Kubeczkova
0dc669f1da shop progress 2025-06-20 13:09:19 +02:00
16 changed files with 215 additions and 21 deletions

View file

@ -31,27 +31,29 @@ MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
SECRET_KEY = env.str('SECRET_KEY', 'django-insecure-x&7qy$na7*@u_4(izkfaz2yiea9+t(nf&#p9cnlq6x)_)jkacf')
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False
DEBUG = True
DEBUG_TOOLBAR = True
DEBUG_TOOLBAR_CONFIG = {
"SHOW_TOOLBAR_CALLBACK": lambda request: False,
"INTERCEPT_REDIRECTS": False,
"SHOW_TOOLBAR_CALLBACK": lambda request: True,
"IS_RUNNING_TESTS": False,
}
DEBUG_TOOLBAR_PANELS = [
'debug_toolbar.panels.history.HistoryPanel',
'debug_toolbar.panels.versions.VersionsPanel',
# 'debug_toolbar.panels.history.HistoryPanel',
# 'debug_toolbar.panels.versions.VersionsPanel',
'debug_toolbar.panels.timer.TimerPanel',
'debug_toolbar.panels.settings.SettingsPanel',
'debug_toolbar.panels.headers.HeadersPanel',
'debug_toolbar.panels.request.RequestPanel',
# 'debug_toolbar.panels.settings.SettingsPanel',
# 'debug_toolbar.panels.headers.HeadersPanel',
# 'debug_toolbar.panels.request.RequestPanel',
'debug_toolbar.panels.sql.SQLPanel',
'debug_toolbar.panels.staticfiles.StaticFilesPanel',
'debug_toolbar.panels.templates.TemplatesPanel',
'debug_toolbar.panels.alerts.AlertsPanel',
'debug_toolbar.panels.cache.CachePanel',
'debug_toolbar.panels.signals.SignalsPanel',
'debug_toolbar.panels.redirects.RedirectsPanel',
'debug_toolbar.panels.profiling.ProfilingPanel',
# 'debug_toolbar.panels.staticfiles.StaticFilesPanel',
# 'debug_toolbar.panels.templates.TemplatesPanel',
# 'debug_toolbar.panels.alerts.AlertsPanel',
# 'debug_toolbar.panels.cache.CachePanel',
# 'debug_toolbar.panels.signals.SignalsPanel',
# 'debug_toolbar.panels.redirects.RedirectsPanel',
# 'debug_toolbar.panels.profiling.ProfilingPanel',
]
ALLOWED_HOSTS = [
@ -77,6 +79,7 @@ INSTALLED_APPS = [
'django.contrib.messages',
'django.contrib.staticfiles',
'tko.apps.TkoConfig',
'shop.apps.ShopConfig',
'debug_toolbar',
'rest_framework',
'post_office',

View file

@ -15,7 +15,7 @@ Including another URLconf
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from django.urls import path, include
from debug_toolbar.toolbar import debug_toolbar_urls
from django.conf.urls.static import static
@ -25,6 +25,7 @@ from tko.views import ContactView, NewArticleListView, AllArticleListView, Event
urlpatterns = [
path('admin/', admin.site.urls),
path('shop/', include("shop.urls")),
path('create-contact/', ContactView.as_view(), name='create-contact'),
path('load-articles/', NewArticleListView.as_view(), name='load-articles'),
path('load-all-articles/', AllArticleListView.as_view(), name='load-all-articles'),

View file

View file

@ -0,0 +1,21 @@
from django.contrib import admin
from shop import models
from shop.models import OrderProduct
# Register your models here.
@admin.register(models.Product)
class ProductAdmin(admin.ModelAdmin):
list_display = ["name", "price", "count"]
class OrderProductInline(admin.TabularInline):
model = OrderProduct
extra = 1
@admin.register(models.Order)
class OrderAdmin(admin.ModelAdmin):
list_display = ["customer_name", "created"]
inlines = [OrderProductInline]

View file

@ -0,0 +1,6 @@
from django.apps import AppConfig
class ShopConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'shop'

View file

@ -0,0 +1,12 @@
PRODUCTS = [
{
"name": "Sušenka",
"price": 10,
},
{
"name": "Caprison",
"price": 15,
}
]

View file

@ -0,0 +1,40 @@
# Generated by Django 5.1.5 on 2025-06-05 19:20
import django.db.models.deletion
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='Order',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('customer_name', models.CharField(max_length=100)),
('created', models.DateTimeField(auto_now_add=True)),
],
),
migrations.CreateModel(
name='Product',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=100)),
('price', models.DecimalField(decimal_places=2, max_digits=10)),
('count', models.IntegerField()),
],
),
migrations.CreateModel(
name='OrderProduct',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('order', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='products', to='shop.order')),
('product', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='shop.product')),
],
),
]

View file

@ -0,0 +1,38 @@
from django.db import models
class Order(models.Model):
customer_name = models.CharField(max_length=100)
created = models.DateTimeField(auto_now_add=True)
@property
def get_total_price(self):
return sum(self.products.price)
def get_qr_code(self):
return self.customer_name
def __str__(self):
return f"Order from {self.customer_name}, {self.created}"
class Product(models.Model):
name = models.CharField(max_length=100)
price = models.DecimalField(decimal_places=2, max_digits=10)
count = models.IntegerField()
def __str__(self):
return f"{self.name}, {self.price} Kč ({self.count} pcs)"
class OrderProduct(models.Model):
order = models.ForeignKey(Order, on_delete=models.CASCADE, related_name='products')
product = models.ForeignKey(Product, on_delete=models.CASCADE)
count = models.PositiveIntegerField()
@property
def price(self):
return self.product.price * self.count
def __str__(self):
return f"{self.product.name}, {self.count} pcs, each for {self.product.price}"

View file

@ -0,0 +1,23 @@
from rest_framework import serializers
from shop.models import Product, OrderProduct, Order
class ProductSerializer(serializers.ModelSerializer):
class Meta:
model = Product
fields = ["name", "price"]
class OrderProductSerializer(serializers.ModelSerializer):
class Meta:
model = OrderProduct
fields = ["product", "count"]
class OrderSerializer(serializers.ModelSerializer):
products = OrderProductSerializer(many=True, read_only=True)
class Meta:
model = Order
fields = ["customer_name", "created", "products"]

View file

@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

View file

@ -0,0 +1,24 @@
"""
URL configuration.
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/5.1/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.urls import path
from shop.views import LoadProductsView, CreateOrderView
urlpatterns = [
path('load-products/', LoadProductsView.as_view(), name='load-products'),
path('create-order/', CreateOrderView.as_view(), name='create-order'),
]

View file

@ -0,0 +1,21 @@
from django.shortcuts import render
from rest_framework.generics import ListAPIView, CreateAPIView
from shop.serializers import ProductSerializer, OrderSerializer
from shop.models import Product
# Create your views here.
class LoadProductsView(ListAPIView):
serializer_class = ProductSerializer
template_name = 'shop/shop.html'
def get_queryset(self):
return Product.objects.all()
class CreateOrderView(CreateAPIView):
serializer_class = OrderSerializer

View file

@ -36,8 +36,7 @@ class ArticleListSerializer(serializers.ModelSerializer):
return obj.date.strftime("%-d. %-m. %Y")
def get_image(self, obj):
main_image = next(iter(obj.images.all()), None)
main_image = obj.images.order_by("main").first()
if main_image:
return ArticleImageSerializer(main_image, context=self.context).data
return {

View file

@ -1,5 +1,5 @@
from django.utils import timezone
from django.db.models import Q, Prefetch
from django.db.models import Q
from post_office import mail
@ -46,9 +46,7 @@ class AllArticleListView(ListAPIView):
def get_queryset(self):
return Article.objects.filter(
Q(active_to__gte=timezone.now()) | Q(active_to__isnull=True)
).order_by('-date').prefetch_related(
Prefetch('images', queryset=ArticleImage.objects.order_by('-main'))
)
).order_by('-date')
class GalleryView(ListAPIView):

View file

@ -0,0 +1,5 @@
<template>
<h1>ncdsk</h1>Hoja
</template>
<script setup lang="ts">
</script>