diff --git a/config/__pycache__/__init__.cpython-311.pyc b/config/__pycache__/__init__.cpython-311.pyc index 423a636..d6476d6 100644 Binary files a/config/__pycache__/__init__.cpython-311.pyc and b/config/__pycache__/__init__.cpython-311.pyc differ diff --git a/config/__pycache__/settings.cpython-311.pyc b/config/__pycache__/settings.cpython-311.pyc index 96bce55..293bddf 100644 Binary files a/config/__pycache__/settings.cpython-311.pyc and b/config/__pycache__/settings.cpython-311.pyc differ diff --git a/config/__pycache__/urls.cpython-311.pyc b/config/__pycache__/urls.cpython-311.pyc index 0b85e94..8d9744a 100644 Binary files a/config/__pycache__/urls.cpython-311.pyc and b/config/__pycache__/urls.cpython-311.pyc differ diff --git a/config/__pycache__/wsgi.cpython-311.pyc b/config/__pycache__/wsgi.cpython-311.pyc index 9c49e09..27aaa70 100644 Binary files a/config/__pycache__/wsgi.cpython-311.pyc and b/config/__pycache__/wsgi.cpython-311.pyc differ diff --git a/config/settings.py b/config/settings.py index 291d043..d1b022f 100644 --- a/config/settings.py +++ b/config/settings.py @@ -133,7 +133,7 @@ AUTH_PASSWORD_VALIDATORS = [ # Internationalization # https://docs.djangoproject.com/en/5.2/topics/i18n/ -LANGUAGE_CODE = 'en-us' +LANGUAGE_CODE = 'uk' TIME_ZONE = 'UTC' diff --git a/core/__pycache__/__init__.cpython-311.pyc b/core/__pycache__/__init__.cpython-311.pyc index 74b1112..f6bacc0 100644 Binary files a/core/__pycache__/__init__.cpython-311.pyc and b/core/__pycache__/__init__.cpython-311.pyc differ diff --git a/core/__pycache__/admin.cpython-311.pyc b/core/__pycache__/admin.cpython-311.pyc index a5ed392..5f9034e 100644 Binary files a/core/__pycache__/admin.cpython-311.pyc and b/core/__pycache__/admin.cpython-311.pyc differ diff --git a/core/__pycache__/apps.cpython-311.pyc b/core/__pycache__/apps.cpython-311.pyc index 6f131d4..3015915 100644 Binary files a/core/__pycache__/apps.cpython-311.pyc and b/core/__pycache__/apps.cpython-311.pyc differ diff --git a/core/__pycache__/context_processors.cpython-311.pyc b/core/__pycache__/context_processors.cpython-311.pyc index 75bf223..927f7c5 100644 Binary files a/core/__pycache__/context_processors.cpython-311.pyc and b/core/__pycache__/context_processors.cpython-311.pyc differ diff --git a/core/__pycache__/models.cpython-311.pyc b/core/__pycache__/models.cpython-311.pyc index e061640..5f509be 100644 Binary files a/core/__pycache__/models.cpython-311.pyc and b/core/__pycache__/models.cpython-311.pyc differ diff --git a/core/__pycache__/urls.cpython-311.pyc b/core/__pycache__/urls.cpython-311.pyc index 5a69659..d858499 100644 Binary files a/core/__pycache__/urls.cpython-311.pyc and b/core/__pycache__/urls.cpython-311.pyc differ diff --git a/core/__pycache__/views.cpython-311.pyc b/core/__pycache__/views.cpython-311.pyc index 2a36fd6..d5c6a5a 100644 Binary files a/core/__pycache__/views.cpython-311.pyc and b/core/__pycache__/views.cpython-311.pyc differ diff --git a/core/admin.py b/core/admin.py index 8c38f3f..5f89531 100644 --- a/core/admin.py +++ b/core/admin.py @@ -1,3 +1,18 @@ from django.contrib import admin +from .models import Product, Variation -# Register your models here. +class VariationInline(admin.TabularInline): + model = Variation + extra = 0 + +@admin.register(Product) +class ProductAdmin(admin.ModelAdmin): + list_display = ('name', 'sku', 'price', 'stock_status', 'total_sales') + search_fields = ('name', 'sku', 'wp_id') + list_filter = ('stock_status',) + inlines = [VariationInline] + +@admin.register(Variation) +class VariationAdmin(admin.ModelAdmin): + list_display = ('product', 'size', 'price', 'stock_quantity') + search_fields = ('product__name', 'size') \ No newline at end of file diff --git a/core/management/__init__.py b/core/management/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/core/management/__pycache__/__init__.cpython-311.pyc b/core/management/__pycache__/__init__.cpython-311.pyc new file mode 100644 index 0000000..fa911a1 Binary files /dev/null and b/core/management/__pycache__/__init__.cpython-311.pyc differ diff --git a/core/management/commands/__init__.py b/core/management/commands/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/core/management/commands/__pycache__/__init__.cpython-311.pyc b/core/management/commands/__pycache__/__init__.cpython-311.pyc new file mode 100644 index 0000000..2e9120f Binary files /dev/null and b/core/management/commands/__pycache__/__init__.cpython-311.pyc differ diff --git a/core/management/commands/__pycache__/sync_woo.cpython-311.pyc b/core/management/commands/__pycache__/sync_woo.cpython-311.pyc new file mode 100644 index 0000000..ca3b480 Binary files /dev/null and b/core/management/commands/__pycache__/sync_woo.cpython-311.pyc differ diff --git a/core/management/commands/sync_woo.py b/core/management/commands/sync_woo.py new file mode 100644 index 0000000..62622ab --- /dev/null +++ b/core/management/commands/sync_woo.py @@ -0,0 +1,93 @@ +import requests +from django.core.management.base import BaseCommand +from core.models import Product, Variation +from django.conf import settings +from decimal import Decimal + +class Command(BaseCommand): + help = "Sync products and variations from WooCommerce REST API" + + CK = "ck_e4107000984fd8752473cdd7974e41d227705215" + CS = "cs_c200854aaf7a9798feae9c0844219a36cb5e46bd" + BASE_URL = "https://theothers.com.ua/wp-json/wc/v3/" + + def handle(self, *args, **options): + self.stdout.write("Starting sync from WooCommerce...") + + # 1. Fetch Products + page = 1 + while True: + response = requests.get( + f"{self.BASE_URL}products", + auth=(self.CK, self.CS), + params={"page": page, "per_page": 100} + ) + + if response.status_code != 200: + self.stderr.write(f"Error fetching products: {response.text}") + break + + products_data = response.json() + if not products_data: + break + + for p in products_data: + # Map product fields + wp_id = str(p.get("id")) + name = p.get("name") + sku = p.get("sku") + price = Decimal(p.get("price") or 0) + stock_status = p.get("stock_status", "outofstock") + total_sales = p.get("total_sales", 0) + image_url = p.get("images")[0]["src"] if p.get("images") else None + + product, created = Product.objects.update_or_create( + wp_id=wp_id, + defaults={ + "name": name, + "sku": sku, + "price": price, + "stock_status": stock_status, + "total_sales": total_sales, + "image": image_url, + } + ) + + # 2. Fetch Variations for variable products + if p.get("type") == "variable": + v_response = requests.get( + f"{self.BASE_URL}products/{wp_id}/variations", + auth=(self.CK, self.CS), + params={"per_page": 100} + ) + + if v_response.status_code == 200: + variations_data = v_response.json() + # Clear old variations to keep it clean (optional, but safer for simple sync) + # Variation.objects.filter(product=product).delete() + + for v in variations_data: + # Use attributes to find size + attributes = v.get("attributes", []) + size = "Default" + for attr in attributes: + if attr.get("name").lower() in ["size", "розмір"]: + size = attr.get("option") + break + + v_price = Decimal(v.get("price") or 0) + v_stock = v.get("stock_quantity") or 0 + + Variation.objects.update_or_create( + product=product, + size=size, + defaults={ + "price": v_price, + "stock_quantity": v_stock, + } + ) + + self.stdout.write(f"Processed page {page}") + page += 1 + + self.stdout.write(self.style.SUCCESS("Successfully synced WooCommerce data!")) diff --git a/core/migrations/0001_initial.py b/core/migrations/0001_initial.py new file mode 100644 index 0000000..b4ced57 --- /dev/null +++ b/core/migrations/0001_initial.py @@ -0,0 +1,49 @@ +# Generated by Django 5.2.7 on 2026-02-22 20:39 + +import django.db.models.deletion +from django.db import migrations, models + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='Product', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('wp_id', models.CharField(max_length=50, unique=True, verbose_name='ID WordPress')), + ('image', models.URLField(blank=True, max_length=500, null=True, verbose_name='Фото')), + ('name', models.CharField(max_length=255, verbose_name='Назва товару')), + ('sku', models.CharField(blank=True, max_length=100, null=True, verbose_name='Артикул (SKU)')), + ('price', models.DecimalField(decimal_places=2, max_digits=12, verbose_name='Ціна (грн)')), + ('stock_status', models.CharField(choices=[('instock', 'В наявності'), ('outofstock', 'Немає в наявності')], max_length=20, verbose_name='Статус')), + ('total_sales', models.IntegerField(default=0, verbose_name='Продано')), + ('created_at', models.DateTimeField(auto_now_add=True)), + ('updated_at', models.DateTimeField(auto_now=True)), + ], + options={ + 'verbose_name': 'Товар', + 'verbose_name_plural': 'Товари', + 'ordering': ['-total_sales'], + }, + ), + migrations.CreateModel( + name='Variation', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('size', models.CharField(max_length=50, verbose_name='Розмір')), + ('price', models.DecimalField(decimal_places=2, max_digits=12, verbose_name='Ціна варіації')), + ('stock_quantity', models.IntegerField(default=0, verbose_name='Залишок на складі')), + ('product', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='variations', to='core.product', verbose_name='Основний товар')), + ], + options={ + 'verbose_name': 'Варіація', + 'verbose_name_plural': 'Варіації', + }, + ), + ] diff --git a/core/migrations/__pycache__/0001_initial.cpython-311.pyc b/core/migrations/__pycache__/0001_initial.cpython-311.pyc new file mode 100644 index 0000000..2129629 Binary files /dev/null and b/core/migrations/__pycache__/0001_initial.cpython-311.pyc differ diff --git a/core/migrations/__pycache__/__init__.cpython-311.pyc b/core/migrations/__pycache__/__init__.cpython-311.pyc index 9c833c8..6b6bd61 100644 Binary files a/core/migrations/__pycache__/__init__.cpython-311.pyc and b/core/migrations/__pycache__/__init__.cpython-311.pyc differ diff --git a/core/models.py b/core/models.py index 71a8362..0bb5926 100644 --- a/core/models.py +++ b/core/models.py @@ -1,3 +1,39 @@ from django.db import models -# Create your models here. +class Product(models.Model): + STOCK_STATUS_CHOICES = [ + ('instock', 'В наявності'), + ('outofstock', 'Немає в наявності'), + ] + + wp_id = models.CharField(max_length=50, unique=True, verbose_name="ID WordPress") + image = models.URLField(max_length=500, blank=True, null=True, verbose_name="Фото") + name = models.CharField(max_length=255, verbose_name="Назва товару") + sku = models.CharField(max_length=100, blank=True, null=True, verbose_name="Артикул (SKU)") + price = models.DecimalField(max_digits=12, decimal_places=2, verbose_name="Ціна (грн)") + stock_status = models.CharField(max_length=20, choices=STOCK_STATUS_CHOICES, verbose_name="Статус") + total_sales = models.IntegerField(default=0, verbose_name="Продано") + + created_at = models.DateTimeField(auto_now_add=True) + updated_at = models.DateTimeField(auto_now=True) + + def __str__(self): + return self.name + + class Meta: + verbose_name = "Товар" + verbose_name_plural = "Товари" + ordering = ['-total_sales'] + +class Variation(models.Model): + product = models.ForeignKey(Product, on_delete=models.CASCADE, related_name='variations', verbose_name="Основний товар") + size = models.CharField(max_length=50, verbose_name="Розмір") + price = models.DecimalField(max_digits=12, decimal_places=2, verbose_name="Ціна варіації") + stock_quantity = models.IntegerField(default=0, verbose_name="Залишок на складі") + + def __str__(self): + return f"{self.product.name} - {self.size}" + + class Meta: + verbose_name = "Варіація" + verbose_name_plural = "Варіації" \ No newline at end of file diff --git a/core/templates/base.html b/core/templates/base.html index 1e7e5fb..3987c48 100644 --- a/core/templates/base.html +++ b/core/templates/base.html @@ -1,25 +1,43 @@ - - +
- -