commit 9b12c1da7bd5dfbd13f30a7a231325045b9af5b4 Author: paov Date: Mon Oct 10 17:34:55 2022 +0300 init diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..46e17b3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/venv/ +/db.sqlite3 diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/deployment.xml b/.idea/deployment.xml new file mode 100644 index 0000000..b3f3305 --- /dev/null +++ b/.idea/deployment.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..478b1c7 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..52dc10c --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/tea.iml b/.idea/tea.iml new file mode 100644 index 0000000..45b59f9 --- /dev/null +++ b/.idea/tea.iml @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/choose/__init__.py b/choose/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/choose/admin.py b/choose/admin.py new file mode 100644 index 0000000..917c048 --- /dev/null +++ b/choose/admin.py @@ -0,0 +1,21 @@ +from django.contrib import admin + +# Register your models here. +from choose.models import Tea, TeaType, TeaCategory + + +class TeaCategoryAdmin(admin.ModelAdmin): + list_display = ('name', 'shop_name') + + +class TeaTypeAdmin(admin.ModelAdmin): + list_display = ('name', 'shop_name', 'category') + + +class TeaAdmin(admin.ModelAdmin): + list_display = ('name', 'price', 'pic', 'type') + + +admin.site.register(Tea, TeaAdmin) +admin.site.register(TeaType, TeaTypeAdmin) +admin.site.register(TeaCategory, TeaCategoryAdmin) diff --git a/choose/apps.py b/choose/apps.py new file mode 100644 index 0000000..ec1d4c2 --- /dev/null +++ b/choose/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class ChooseConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'choose' diff --git a/choose/migrations/0001_initial.py b/choose/migrations/0001_initial.py new file mode 100644 index 0000000..255803b --- /dev/null +++ b/choose/migrations/0001_initial.py @@ -0,0 +1,32 @@ +# Generated by Django 4.0.8 on 2022-10-10 12:03 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='TeaType', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(max_length=255)), + ], + ), + migrations.CreateModel( + name='Tea', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(max_length=255)), + ('price', models.IntegerField()), + ('pic', models.CharField(max_length=255)), + ('type', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='teas', to='choose.teatype')), + ], + ), + ] diff --git a/choose/migrations/0002_teatype_shop_name.py b/choose/migrations/0002_teatype_shop_name.py new file mode 100644 index 0000000..8e4651f --- /dev/null +++ b/choose/migrations/0002_teatype_shop_name.py @@ -0,0 +1,19 @@ +# Generated by Django 4.0.8 on 2022-10-10 12:14 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('choose', '0001_initial'), + ] + + operations = [ + migrations.AddField( + model_name='teatype', + name='shop_name', + field=models.CharField(default='', max_length=255), + preserve_default=False, + ), + ] diff --git a/choose/migrations/0003_teacategory_teatype_category.py b/choose/migrations/0003_teacategory_teatype_category.py new file mode 100644 index 0000000..010ec15 --- /dev/null +++ b/choose/migrations/0003_teacategory_teatype_category.py @@ -0,0 +1,27 @@ +# Generated by Django 4.0.8 on 2022-10-10 12:24 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('choose', '0002_teatype_shop_name'), + ] + + operations = [ + migrations.CreateModel( + name='TeaCategory', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(max_length=255)), + ('shop_name', models.CharField(max_length=255)), + ], + ), + migrations.AddField( + model_name='teatype', + name='category', + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='tea_types', to='choose.teacategory'), + ), + ] diff --git a/choose/migrations/__init__.py b/choose/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/choose/models.py b/choose/models.py new file mode 100644 index 0000000..3150dc4 --- /dev/null +++ b/choose/models.py @@ -0,0 +1,36 @@ +from django.db import models + + +# Create your models here. + +class TeaCategory(models.Model): + name = models.CharField(max_length=255) + shop_name = models.CharField(max_length=255) + + def __str__(self): + return self.name + + +class TeaType(models.Model): + name = models.CharField(max_length=255) + shop_name = models.CharField(max_length=255) + category = models.ForeignKey(TeaCategory, + related_name='tea_types', + on_delete=models.CASCADE, + blank=True, null=True + ) + + def __str__(self): + return self.name + + +class Tea(models.Model): + name = models.CharField(max_length=255) + price = models.IntegerField() + pic = models.CharField(max_length=255) + type = models.ForeignKey(TeaType, + related_name='teas', + on_delete=models.CASCADE) + + def __str__(self): + return self.name diff --git a/choose/tests.py b/choose/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/choose/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/choose/views.py b/choose/views.py new file mode 100644 index 0000000..91ea44a --- /dev/null +++ b/choose/views.py @@ -0,0 +1,3 @@ +from django.shortcuts import render + +# Create your views here. diff --git a/manage.py b/manage.py new file mode 100644 index 0000000..8afc040 --- /dev/null +++ b/manage.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python +"""Django's command-line utility for administrative tasks.""" +import os +import sys + + +def main(): + """Run administrative tasks.""" + os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'tea.settings') + try: + from django.core.management import execute_from_command_line + except ImportError as exc: + raise ImportError( + "Couldn't import Django. Are you sure it's installed and " + "available on your PYTHONPATH environment variable? Did you " + "forget to activate a virtual environment?" + ) from exc + execute_from_command_line(sys.argv) + + +if __name__ == '__main__': + main() diff --git a/sync.py b/sync.py new file mode 100644 index 0000000..9db7b28 --- /dev/null +++ b/sync.py @@ -0,0 +1,50 @@ +import django +from django.conf import settings +import requests +from bs4 import BeautifulSoup + +from pathlib import Path + +BASE_DIR = Path(__file__).resolve().parent +settings.configure( + DATABASES={ + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': BASE_DIR / 'db.sqlite3', + } + }, + INSTALLED_APPS=[ + 'django.contrib.admin', + 'django.contrib.auth', + 'django.contrib.contenttypes', + 'django.contrib.sessions', + 'django.contrib.messages', + 'django.contrib.staticfiles', + 'choose' + ] +) +django.setup() + +from choose.models import TeaType, Tea, TeaCategory + +db_cats = list(TeaCategory.objects.all()) +categories = ['https://chainiisvet.ru/product-category/' + x.shop_name + '/' for x in db_cats] +r = requests.get("https://chainiisvet.ru/") +soup = BeautifulSoup(r.content) +ul = soup.find("ul", {"id": "menu-katalog"}) +for item in ul.contents: + if item.__class__.__name__ == 'Tag': + if item.contents[0].attrs['href'] in categories: + category = TeaCategory.objects.get(name=item.contents[0].contents[0]) + for i in item.contents[2]: + if i.__class__.__name__ == 'Tag': + type = i.contents[0].contents[0] + href = i.contents[0].attrs['href'] if i.contents[0].attrs['href'].startswith( + 'https') else 'https://chainiisvet.ru' + i.contents[0].attrs['href'] + TeaType.objects.get_or_create(shop_name=href[href.rfind('/', 0, len(href)-1)+1:-1], + name=type, + category=category) + print(category, end=':') + print(type, end=':') + print(href) +pass diff --git a/tea/__init__.py b/tea/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tea/asgi.py b/tea/asgi.py new file mode 100644 index 0000000..4acf501 --- /dev/null +++ b/tea/asgi.py @@ -0,0 +1,16 @@ +""" +ASGI config for tea project. + +It exposes the ASGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/4.1/howto/deployment/asgi/ +""" + +import os + +from django.core.asgi import get_asgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'tea.settings') + +application = get_asgi_application() diff --git a/tea/settings.py b/tea/settings.py new file mode 100644 index 0000000..15e2fce --- /dev/null +++ b/tea/settings.py @@ -0,0 +1,126 @@ +""" +Django settings for tea project. + +Generated by 'django-admin startproject' using Django 4.1.2. + +For more information on this file, see +https://docs.djangoproject.com/en/4.1/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/4.1/ref/settings/ +""" + +from pathlib import Path + +# Build paths inside the project like this: BASE_DIR / 'subdir'. +BASE_DIR = Path(__file__).resolve().parent.parent + + +# Quick-start development settings - unsuitable for production +# See https://docs.djangoproject.com/en/4.1/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = 'django-insecure-u=yu7(3@719dv(dtgy3wz_%yaw((r9-sh4exp(qf@to8o-e=e@' + +# SECURITY WARNING: don't run with debug turned on in production! +DEBUG = True + +ALLOWED_HOSTS = [ + '10.15.0.1' +] + + +# Application definition + +INSTALLED_APPS = [ + 'django.contrib.admin', + 'django.contrib.auth', + 'django.contrib.contenttypes', + 'django.contrib.sessions', + 'django.contrib.messages', + 'django.contrib.staticfiles', + 'choose' +] + +MIDDLEWARE = [ + 'django.middleware.security.SecurityMiddleware', + 'django.contrib.sessions.middleware.SessionMiddleware', + 'django.middleware.common.CommonMiddleware', + 'django.middleware.csrf.CsrfViewMiddleware', + 'django.contrib.auth.middleware.AuthenticationMiddleware', + 'django.contrib.messages.middleware.MessageMiddleware', + 'django.middleware.clickjacking.XFrameOptionsMiddleware', +] + +ROOT_URLCONF = 'tea.urls' + +TEMPLATES = [ + { + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'DIRS': [], + 'APP_DIRS': True, + 'OPTIONS': { + 'context_processors': [ + 'django.template.context_processors.debug', + 'django.template.context_processors.request', + 'django.contrib.auth.context_processors.auth', + 'django.contrib.messages.context_processors.messages', + ], + }, + }, +] + +WSGI_APPLICATION = 'tea.wsgi.application' + + +# Database +# https://docs.djangoproject.com/en/4.1/ref/settings/#databases + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': BASE_DIR / 'db.sqlite3', + } +} + + +# Password validation +# https://docs.djangoproject.com/en/4.1/ref/settings/#auth-password-validators + +AUTH_PASSWORD_VALIDATORS = [ + { + 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', + }, +] + + +# Internationalization +# https://docs.djangoproject.com/en/4.1/topics/i18n/ + +LANGUAGE_CODE = 'en-us' + +TIME_ZONE = 'UTC' + +USE_I18N = True + +USE_TZ = True + + +# Static files (CSS, JavaScript, Images) +# https://docs.djangoproject.com/en/4.1/howto/static-files/ + +STATIC_URL = 'static/' + +# Default primary key field type +# https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field + +DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' diff --git a/tea/urls.py b/tea/urls.py new file mode 100644 index 0000000..a2d0df6 --- /dev/null +++ b/tea/urls.py @@ -0,0 +1,21 @@ +"""tea URL Configuration + +The `urlpatterns` list routes URLs to views. For more information please see: + https://docs.djangoproject.com/en/4.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.contrib import admin +from django.urls import path + +urlpatterns = [ + path('admin/', admin.site.urls), +] diff --git a/tea/wsgi.py b/tea/wsgi.py new file mode 100644 index 0000000..2669e15 --- /dev/null +++ b/tea/wsgi.py @@ -0,0 +1,16 @@ +""" +WSGI config for tea project. + +It exposes the WSGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/4.1/howto/deployment/wsgi/ +""" + +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'tea.settings') + +application = get_wsgi_application()