diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..615fa9d --- /dev/null +++ b/.env.example @@ -0,0 +1,10 @@ +# django +SECRET_KEY= +DEBUG=False + +# yandex weather +YANDEX_GEOCODER_KEY= +YANDEX_WEATHER_KEY= +YANDEX_SUGGEST_KEY= + +DJANGO_SETTINGS_MODULE= diff --git a/.gitignore b/.gitignore index 6e9e657..43f1192 100644 --- a/.gitignore +++ b/.gitignore @@ -4,7 +4,8 @@ __pycache__/ *.py[cod] *$py.class - +/.idea +.xml # C extensions *.so diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..325d5b3 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,20 @@ +FROM python:3.13-slim + +ENV PYTHONDONTWRITEBYTECODE=1 +ENV PYTHONUNBUFFERED=1 + +RUN apt-get update && apt-get install -y \ + build-essential \ + libpq-dev \ + && rm -rf /var/lib/apt/lists/* + +WORKDIR /app + +RUN pip install --no-cache-dir uv +COPY pyproject.toml uv.lock ./ + +RUN uv pip install --system -r uv.lock + +COPY . . + +EXPOSE 8000 diff --git a/README.md b/README.md new file mode 100644 index 0000000..5956100 --- /dev/null +++ b/README.md @@ -0,0 +1,160 @@ +# Weather Web Application + +Веб-приложение для получения прогноза погоды по названию города. + +--- + +## Оглавление + +- [Описание проекта](#описание-проекта) +- [Реализованный функционал](#реализованный-функционал) +- [Технологии](#технологии) +- [Как запустить проект](#как-запустить-проект) + - [Требования](#требования) + - [Запуск с помощью Docker](#запуск-с-помощью-docker) +- [Тестирование и линтинг](#тестирование-и-линтинг) +- [Контакты](#контакты) + +--- + +## Описание проекта + +Это веб-приложение позволяет получать прогноз погоды по введённому названию города. Приложение выводит данные о погоде в удобном формате: +- Прогноз на сегодня. +- Прогноз на 3 дня вперёд. + +При вводе города реализовано автодополнение с помощью Яндекс.Геосаджеста, что упрощает выбор нужного города. Также в приложении сохраняется история поисковых запросов, и при повторном посещении сайта пользователю предлагается посмотреть погоду в последнем искомом городе. + +Кроме веб-интерфейса, реализован API-эндпоинт `/api/stats/` для отображения статистики поисковых запросов (какой город сколько раз искали), отсортированной по популярности. + +--- + +## Реализованный функционал + +- **Основной функционал**: + - Ввод названия города. + - Получение прогноза погоды. + +- **Достоверное отображение прогноза**: + - Прогноз на сегодня. + - Прогноз на 3 дня вперёд. + +- **Тестирование**: + - Написаны тесты для `views` и `services` с использованием `pytest` и `mock`. + +- **Контейнеризация**: + - Приложение полностью обернуто в Docker-контейнеры (`web`, `db`). + +- **Автодополнение**: + - Реализовано подсказки при вводе города с помощью Яндекс.Геосаджеста. + +- **Сессии Django**: + - При повторном посещении сайта предлагается посмотреть погоду в последнем искомом городе. + +- **История поиска**: + - Сохраняется общая история поиска городов. + +- **API статистики**: + - Эндпоинт `/api/stats/` показывает статистику поисковых запросов. + +--- + +## Технологии + +- **Backend**: Python, Django, Django REST Framework +- **База данных**: PostgreSQL +- **Frontend**: HTML, CSS, JavaScript (используется API Яндекс.Карт) +- **API погоды**: + - Яндекс.Погода + - Яндекс.Геокодер + - Яндекс.Геосаджест +- **Тестирование**: Pytest, pytest-django, Mock +- **Линтинг и форматирование**: Ruff +- **Контейнеризация**: Docker, Docker Compose +- **Менеджер пакетов**: uv + +--- + +## Как запустить проект + +### Требования + +- Docker +- Docker Compose + +### Запуск с помощью Docker + +1. **Клонируйте репозиторий:** + + ```bash + git clone + cd weather_project + ``` + +2. **Создайте файл `.env`:** + + ```bash + cp example.env .env + ``` + +3. **Заполните `.env` своими данными:** + + ```env + DJANGO_SECRET_KEY='your-django-secret-key' + YANDEX_API_KEY='your-yandex-api-key-for-weather-and-geocoder' + YANDEX_JS_API_KEY='your-yandex-js-api-key-for-suggest' + + # PostgreSQL + POSTGRES_DB=weather_db + POSTGRES_USER=weather_user + POSTGRES_PASSWORD=strongpassword + POSTGRES_HOST=db + POSTGRES_PORT=5432 + ``` + +4. **Сборка и запуск контейнеров:** + + ```bash + docker-compose up --build -d + ``` + +5. **Применение миграций:** + + ```bash + docker-compose exec web python manage.py migrate + ``` + +После выполнения этих шагов приложение будет доступно по адресу: [http://localhost:8000](http://localhost:8000) +API статистики будет работать по адресу: [http://localhost:8000/api/stats/](http://localhost:8000/api/stats/) + +--- + +## Тестирование и линтинг + +### Запуск тестов + +```bash +docker-compose exec web pytest +``` + +Что тестируется: +- Корректность работы с API Яндекса (с моками запросов). +- Сохранение истории поиска. +- Логика сессий (последний город). +- Формат ответа API статистики. + +### Проверка стиля кода (Ruff) + +```bash +docker-compose exec web ruff check . +``` + +#### Автоисправление ошибок с помощью Ruff: + +```bash +docker-compose exec web ruff check --fix . +``` + +--- + +*Наслаждайтесь использованием Weather Web Application!* diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..da48c33 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,31 @@ +services: + db: + image: postgres:14-alpine + container_name: weather_db + volumes: + - postgres_data:/var/lib/postgresql/data/ + environment: + - POSTGRES_DB=${DB_NAME} + - POSTGRES_USER=${DB_USER} + - POSTGRES_PASSWORD=${DB_PASSWORD} + + web: + build: . + container_name: weather_web + command: python src/manage.py runserver 0.0.0.0:8000 + volumes: + - .:/app + - static_volume:/app/src/static + ports: + - "8000:8000" + environment: + - DB_HOST=db + - DB_NAME=${DB_NAME} + - DB_USER=${DB_USER} + - DB_PASSWORD=${DB_PASSWORD} + depends_on: + - db + +volumes: + postgres_data: + static_volume: \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml index b8a5288..845646d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -3,4 +3,37 @@ name = "weather-web" version = "0.1.0" description = "Add your description here" requires-python = ">=3.13" -dependencies = [] + +dependencies = [ + "django>=5.2.1", + "django-location-field>=2.7.3", + "django-rest-framework>=0.1.0", + "djangorestframework>=3.16.0", + "httpx>=0.28.1", + "psycopg2-binary", + "pytest>=8.3.5", + "pytest-django>=4.11.1", + "pytest-mock>=3.14.1", + "python-dotenv>=1.1.0", + "requests>=2.32.3", + "six>=1.17.0", +] + +[tool.ruff] +line-length = 120 # Общая настройка длины строки +target-version = "py313" # Указываем версию Python +exclude = [ # Исключаемые файлы/папки + ".venv", + "__pycache__", + "build", + "dist", + "migrations" +] + +[tool.ruff.lint] +select = ["E", "F", "I", "N", "UP", "B", "A"] +ignore = ["E501"] +fixable = ["ALL"] + +[tool.ruff.lint.isort] +known-first-party = ["my_package"] diff --git a/src/config/__init__.py b/src/config/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/config/asgi.py b/src/config/asgi.py new file mode 100644 index 0000000..cd6907c --- /dev/null +++ b/src/config/asgi.py @@ -0,0 +1,16 @@ +""" +ASGI config for config 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/5.2/howto/deployment/asgi/ +""" + +import os + +from django.core.asgi import get_asgi_application + +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings") + +application = get_asgi_application() diff --git a/src/config/settings.py b/src/config/settings.py new file mode 100644 index 0000000..db7ac8b --- /dev/null +++ b/src/config/settings.py @@ -0,0 +1,178 @@ +""" +Django settings for config project. + +Generated by 'django-admin startproject' using Django 5.2.1. + +For more information on this file, see +https://docs.djangoproject.com/en/5.2/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/5.2/ref/settings/ +""" + +import os +from pathlib import Path + +from dotenv import load_dotenv + +# Build paths inside the project like this: BASE_DIR / 'subdir'. +BASE_DIR = Path(__file__).resolve().parent.parent +load_dotenv(BASE_DIR / ".." / ".env") + +# Quick-start development settings - unsuitable for production +# See https://docs.djangoproject.com/en/5.2/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = os.environ.get("SECRET_KEY") + +# SECURITY WARNING: don't run with debug turned on in production! +DEBUG = os.environ.get("DEBUG") + +ALLOWED_HOSTS = [] + + +# Application definition + +INSTALLED_APPS = [ + "django.contrib.admin", + "django.contrib.auth", + "django.contrib.contenttypes", + "django.contrib.sessions", + "django.contrib.messages", + "django.contrib.staticfiles", + "weather_app", + "users", + "location_field.apps.DefaultConfig", + "rest_framework", +] + +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", +] + +REST_FRAMEWORK = { + "DEFAULT_RENDERER_CLASSES": [ + "rest_framework.renderers.JSONRenderer", + ] +} + +ROOT_URLCONF = "config.urls" + +TEMPLATES = [ + { + "BACKEND": "django.template.backends.django.DjangoTemplates", + "DIRS": [], + "APP_DIRS": True, + "OPTIONS": { + "context_processors": [ + "django.template.context_processors.request", + "django.contrib.auth.context_processors.auth", + "django.contrib.messages.context_processors.messages", + ], + }, + }, +] + +WSGI_APPLICATION = "config.wsgi.application" + + +# Database +# https://docs.djangoproject.com/en/5.2/ref/settings/#databases + +DATABASES = { + "default": { + "ENGINE": "django.db.backends.postgresql_psycopg2", + "NAME": os.environ.get("DB_NAME"), + "USER": os.environ.get("DB_USER"), + "PASSWORD": os.environ.get("DB_PASSWORD"), + "POST": os.environ.get("DB_POST"), + "HOST": "db", + } +} + + +# Password validation +# https://docs.djangoproject.com/en/5.2/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/5.2/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/5.2/howto/static-files/ + +STATIC_URL = "/static/" +STATICFILES_DIRS = [ + os.path.join(BASE_DIR, "static"), +] + +STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles") + +# Default primary key field type +# https://docs.djangoproject.com/en/5.2/ref/settings/#default-auto-field + +DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField" + +AUTH_USER_MODEL = "users.User" +CACHES = { + "default": { + "BACKEND": "django.core.cache.backends.redis.RedisCache", + "LOCATION": "redis://redis:6379/1", + } +} +YANDEX_GEOCODER_KEY = os.getenv("YANDEX_GEOCODER_KEY") +YANDEX_SUGGEST_KEY = os.getenv("YANDEX_SUGGEST_KEY") +YANDEX_WEATHER_KEY = os.getenv("YANDEX_WEATHER_KEY") + + +LOGGING = { + "version": 1, + "disable_existing_loggers": False, + "formatters": { + "verbose": { + "format": "{levelname} {asctime} {module} {message}", + "style": "{", + }, + }, + "handlers": { + "file": {"level": "ERROR", "class": "logging.FileHandler", "filename": "debug.log", "formatter": "verbose"}, + "console": {"level": "DEBUG", "class": "logging.StreamHandler", "formatter": "verbose"}, + }, + "loggers": { + "weather_app": { + "handlers": ["file", "console"], + "level": "ERROR", + "propagate": True, + }, + }, +} diff --git a/src/config/urls.py b/src/config/urls.py new file mode 100644 index 0000000..59dab72 --- /dev/null +++ b/src/config/urls.py @@ -0,0 +1,25 @@ +""" +URL configuration for config project. + +The `urlpatterns` list routes URLs to views. For more information please see: + https://docs.djangoproject.com/en/5.2/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 include, path + +urlpatterns = [ + path("admin/", admin.site.urls), + path("", include("weather_app.urls", "weather_app")), + path("users/", include("users.urls", "users")), +] diff --git a/src/config/wsgi.py b/src/config/wsgi.py new file mode 100644 index 0000000..27c0377 --- /dev/null +++ b/src/config/wsgi.py @@ -0,0 +1,16 @@ +""" +WSGI config for config 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/5.2/howto/deployment/wsgi/ +""" + +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings") + +application = get_wsgi_application() diff --git a/src/manage.py b/src/manage.py new file mode 100644 index 0000000..aabb818 --- /dev/null +++ b/src/manage.py @@ -0,0 +1,23 @@ +#!/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", "config.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/src/pytest.ini b/src/pytest.ini new file mode 100644 index 0000000..0c815d0 --- /dev/null +++ b/src/pytest.ini @@ -0,0 +1,5 @@ +[pytest] +DJANGO_SETTINGS_MODULE = config.settings +python_files = tests.py test_*.py *_tests.py +addopts = --ds=config.settings --pythonwarnings=ignore +testpaths = src/tests diff --git a/src/static/css/style.css b/src/static/css/style.css new file mode 100644 index 0000000..ff81f4c --- /dev/null +++ b/src/static/css/style.css @@ -0,0 +1,384 @@ +:root { + --breakpoint-mobile: 576px; + --breakpoint-tablet: 768px; + --breakpoint-desktop: 1200px; + + --primary-color: #2A4B7C; + --secondary-color: #5B8CBE; + --accent-color: #FF7F50; + --background-gradient: linear-gradient(135deg, #2A4B7C 0%, #5B8CBE 100%); + --text-color: #F0F4F8; + --link-text-color: #90ef98; + --text-light: rgba(248, 241, 241, 0.95); +} + +/* Базовые стили (Mobile First) */ +body { + font-family: 'Poppins', sans-serif; + margin: 0; + padding: 1rem; + min-height: 100vh; + background: var(--background-gradient); + color: var(--text-color); +} + +.content-container { + width: 100%; + max-width: 100%; + margin: 0 auto; + padding: 1.5rem; + background: rgba(255, 255, 255, 0.1); + backdrop-filter: blur(10px); + border-radius: 1rem; + box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1); + text-align: center; +} + +/* Заголовки */ +h1, h2, h3 { + text-align: center; + margin: 0 auto 1.5rem; + padding: 0 15px; + color: var(--text-light); + text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); +} + +h1 { + font-size: 2.2rem; + line-height: 1.2; +} + +/* Поисковая форма */ +.search-form { + position: relative; + margin: 0 auto 3rem; + max-width: 700px; +} + +.city-input { + width: 100%; + padding: 1.2rem; + font-size: 1.1rem; + border: none; + border-radius: 1rem; + background: rgba(255, 255, 255, 0.9); + transition: all 0.3s ease; +} + +.city-input:focus { + box-shadow: 0 0 0 3px rgba(42, 75, 124, 0.2); +} + +/* Текущая погода */ +.current-weather { + display: flex; + flex-direction: column; + align-items: center; + text-align: center; + margin: 2rem 0; + padding: 2rem; + background: rgba(255, 255, 255, 0.15); + border-radius: 1rem; +} + +.weather-icon { + width: 100px; + height: 100px; + margin-bottom: 1.5rem; + filter: drop-shadow(0 4px 6px rgba(0, 0, 0, 0.1)); +} + +.current-details { + text-align: center; +} + +.current-temp { + font-size: 3rem; + font-weight: 700; + color: var(--text-light); +} + +/* Прогноз по дням */ +.forecast-container { + display: grid; + grid-template-columns: 1fr; + gap: 1rem; + margin: 2rem 0; +} + +.forecast-day { + padding: 1.5rem; + background: rgba(255, 255, 255, 0.1); + border-radius: 1rem; + backdrop-filter: blur(5px); +} + +/* История поиска */ +.search-history { + margin-top: 3rem; + padding: 2rem; + background: rgba(255, 255, 255, 0.1); + border-radius: 1rem; +} + +.search-history ul { + display: grid; + grid-template-columns: 1fr; + gap: 0.75rem; + padding: 0; + margin: 0; + list-style: none; +} + +.search-history a { + display: block; + padding: 1rem; + background: rgba(255, 255, 255, 0.15); + color: var(--text-light) !important; + border-radius: 0.75rem; + text-decoration: none; + transition: all 0.3s ease; + border: 1px solid rgba(255, 255, 255, 0.2); +} + +.search-history a:hover { + background: rgba(248, 196, 196, 0.25); + transform: translateY(-2px); +} + +.last-search-prompt a { + display: block; + width: 10%; + margin: auto; + padding: 1rem; + background: rgba(255, 255, 255, 0.15); + color: var(--text-light) !important; + border-radius: 0.75rem; + text-decoration: none; + transition: all 0.3s ease; + border: 1px solid rgba(255, 255, 255, 0.2); +} + +.last-search-prompt a:hover { + background: rgba(248, 196, 196, 0.25); + transform: translateY(-2px); +} + +/* Обновленные стили для блока погоды */ +.weather-result { + background: rgba(255, 255, 255, 0.15); + border-radius: 1.5rem; + padding: 2rem; + margin: 2rem 0; + backdrop-filter: blur(8px); + box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1); +} + +.city-name { + text-align: center; + font-size: 2.5rem; + margin-bottom: 2rem; + color: var(--text-light); + text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); +} + +.current-weather { + display: flex; + flex-direction: column; + align-items: center; + gap: 2rem; + position: relative; +} + +.weather-icon { + width: 120px; + height: 120px; + filter: drop-shadow(0 4px 6px rgba(0, 0, 0, 0.1)); + transition: transform 0.3s ease; +} + +.current-details { + text-align: center; +} + +.current-temp { + font-size: 3.5rem; + font-weight: 700; + color: var(--text-light); + line-height: 1; + margin-bottom: 0.5rem; +} + +.current-condition { + font-size: 1.25rem; + color: rgba(255, 255, 255, 0.9); + margin-bottom: 0.75rem; +} + +.current-feels-like { + font-size: 1rem; + color: rgba(255, 255, 255, 0.8); + opacity: 0.9; +} + +/* Стили для выпадающего списка */ +.suggestions-list { + position: absolute; + width: 100%; + max-height: 300px; + overflow-y: auto; + background: #ffffff !important; + border: 2px solid var(--primary-color); + border-radius: 12px; + box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15); + margin-top: 8px; + z-index: 1000; + list-style: none; + padding: 0; +} + +.suggestion-item { + padding: 12px 20px; + color: var(--primary-color); + font-weight: 500; + cursor: pointer; + transition: all 0.2s ease; + display: flex; + align-items: center; + border-bottom: 1px solid rgba(42, 75, 124, 0.1); +} + +.suggestion-item:last-child { + border-bottom: none; +} + +.suggestion-item:hover { + background: rgba(42, 75, 124, 0.05); + transform: translateX(5px); +} + +.suggestion-item::before { + content: "📍"; + margin-right: 12px; + font-size: 1.1em; + opacity: 0.7; +} + + + +/* Адаптация для десктопов */ +@media (min-width: 768px) { + .current-weather { + flex-direction: row; + justify-content: center; + gap: 3rem; + } + + .weather-icon { + width: 150px; + height: 150px; + } + + .current-temp { + font-size: 4rem; + } + + .current-condition { + font-size: 1.5rem; + } +} + +/* Анимация при наведении */ +.weather-icon:hover { + transform: scale(1.05) rotate(5deg); +} + +/* Для темной темы */ +@media (prefers-color-scheme: dark) { + .suggestions-list { + background: #2A4B7C !important; + border-color: rgba(255, 255, 255, 0.2); + } + + .suggestion-item { + color: white; + border-color: rgba(255, 255, 255, 0.1); + } + + .suggestion-item:hover { + background: rgba(255, 255, 255, 0.05); + } +} + +/* Адаптивные стили */ +@media (min-width: 576px) { + .content-container { + padding: 2rem; + } + + .forecast-container { + grid-template-columns: repeat(2, 1fr); + } + + .search-history ul { + grid-template-columns: repeat(2, 1fr); + } +} + +@media (min-width: 768px) { + body { + padding: 2rem; + } + + h1 { + font-size: 2.5rem; + } + + .current-weather { + flex-direction: row; + text-align: left; + padding: 3rem; + } + + .weather-icon { + margin-bottom: 0; + margin-right: 3rem; + } + + .current-details { + text-align: left; + } + + .forecast-container { + grid-template-columns: repeat(3, 1fr); + } + + .search-history ul { + grid-template-columns: repeat(3, 1fr); + } +} + +@media (min-width: 1200px) { + .content-container { + max-width: 1200px; + } + + .forecast-container { + gap: 2rem; + } +} + +@media (max-width: 480px) { + h1 { + font-size: 1.8rem; + } + + .current-temp { + font-size: 2.5rem; + } + + .search-history a { + padding: 0.8rem; + font-size: 0.9rem; + } +} \ No newline at end of file diff --git a/src/tests/__init__.py b/src/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/tests/weather_app/__init__.py b/src/tests/weather_app/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/tests/weather_app/test_api.py b/src/tests/weather_app/test_api.py new file mode 100644 index 0000000..e23979f --- /dev/null +++ b/src/tests/weather_app/test_api.py @@ -0,0 +1,20 @@ +import pytest +from rest_framework.test import APIClient + +from weather_app.models import SearchHistory + + +@pytest.mark.django_db +def test_search_stats_api(): + """ + Тест API статистики поиска + """ + + client = APIClient() + SearchHistory.objects.create(city="Москва", search_count=5) + + response = client.get("/api/stats/") + + assert response.status_code == 200 + assert response.data[0]["city"] == "Москва" + assert response.data[0]["search_count"] == 5 diff --git a/src/tests/weather_app/test_models.py b/src/tests/weather_app/test_models.py new file mode 100644 index 0000000..a5b4a74 --- /dev/null +++ b/src/tests/weather_app/test_models.py @@ -0,0 +1,30 @@ +import pytest +from django.db import IntegrityError, transaction + +from weather_app.models import SearchHistory + + +@pytest.mark.django_db +def test_search_history_creation(): + """ + Тестирование создания записи истории поиска + """ + + city = SearchHistory.objects.create(city="Москва", latitude=55.7558, longitude=37.6176, search_count=5) + + assert city.city == "Москва" + assert city.search_count == 5 + assert str(city) == "Москва (найден 5 раз)" + + +@pytest.mark.django_db +def test_unique_city_constraint(): + """ + Проверка уникальности города + """ + + SearchHistory.objects.create(city="Москва") + + with pytest.raises(IntegrityError): + with transaction.atomic(): + SearchHistory.objects.create(city="Москва") diff --git a/src/tests/weather_app/test_services.py b/src/tests/weather_app/test_services.py new file mode 100644 index 0000000..2de5825 --- /dev/null +++ b/src/tests/weather_app/test_services.py @@ -0,0 +1,47 @@ +from unittest.mock import patch + +import requests + +from weather_app import services + + +@patch("weather_app.services.requests.get") +def test_get_coordinates_success(mock_get): + """ + Тест успешного получения координат + """ + + mock_response = { + "response": { + "GeoObjectCollection": { + "featureMember": [ + { + "GeoObject": { + "Point": {"pos": "37.6176 55.7558"}, + "name": "Москва", + "metaDataProperty": { + "GeocoderMetaData": { + "Address": {"Components": [{"kind": "locality", "name": "Москва"}]} + } + }, + } + } + ] + } + } + } + mock_get.return_value.json.return_value = mock_response + mock_get.return_value.raise_for_status.return_value = None + + result = services.get_coordinates("Москва") + assert result == (55.7558, 37.6176, "Москва") + + +@patch("weather_app.services.requests.get") +def test_get_coordinates_failure(mock_get): + """ + Тест ошибки при получении координат + """ + + mock_get.side_effect = requests.exceptions.RequestException + assert services.get_coordinates("Несуществующий город") is None diff --git a/src/tests/weather_app/test_views.py b/src/tests/weather_app/test_views.py new file mode 100644 index 0000000..b5d78a7 --- /dev/null +++ b/src/tests/weather_app/test_views.py @@ -0,0 +1,46 @@ +import pytest + +from weather_app.models import SearchHistory + + +@pytest.mark.django_db +def test_weather_view_get(client): + """ + Тест GET-запроса к WeatherView + """ + + response = client.get("/") + assert response.status_code == 200 + assert "yandex_geo_api_key" in response.context + + +@pytest.mark.django_db +def test_weather_view_post_valid(client, mocker): + """ + Тест успешного POST-запроса + """ + + mocker.patch("weather_app.services.get_coordinates", return_value=(55.7558, 37.6176, "Москва")) + mocker.patch("weather_app.services.get_weather_forecast", return_value={"fact": {}}) + + response = client.post("/", {"city": "Москва"}) + assert response.status_code == 200 + assert "Москва" in response.content.decode() + + +@pytest.mark.django_db +def test_search_history_update(client, mocker): + """ + Тест обновления истории поиска + """ + + mocker.patch("weather_app.services.get_coordinates", return_value=(55.7558, 37.6176, "Москва")) + mocker.patch("weather_app.services.get_weather_forecast", return_value={"fact": {}}) + + # Первый запрос + client.post("/", {"city": "Москва"}) + # Второй запрос + client.post("/", {"city": "Москва"}) + + history = SearchHistory.objects.get(city="Москва") + assert history.search_count == 2 diff --git a/src/users/__init__.py b/src/users/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/users/admin.py b/src/users/admin.py new file mode 100644 index 0000000..846f6b4 --- /dev/null +++ b/src/users/admin.py @@ -0,0 +1 @@ +# Register your models here. diff --git a/src/users/apps.py b/src/users/apps.py new file mode 100644 index 0000000..88f7b17 --- /dev/null +++ b/src/users/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class UsersConfig(AppConfig): + default_auto_field = "django.db.models.BigAutoField" + name = "users" diff --git a/src/users/migrations/0001_initial.py b/src/users/migrations/0001_initial.py new file mode 100644 index 0000000..c2a93cd --- /dev/null +++ b/src/users/migrations/0001_initial.py @@ -0,0 +1,42 @@ +# Generated by Django 5.2.1 on 2025-05-25 16:29 + +import django.contrib.auth.models +import django.utils.timezone +from django.db import migrations, models + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ('auth', '0012_alter_user_first_name_max_length'), + ] + + operations = [ + migrations.CreateModel( + name='User', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('password', models.CharField(max_length=128, verbose_name='password')), + ('last_login', models.DateTimeField(blank=True, null=True, verbose_name='last login')), + ('is_superuser', models.BooleanField(default=False, help_text='Designates that this user has all permissions without explicitly assigning them.', verbose_name='superuser status')), + ('first_name', models.CharField(blank=True, max_length=150, verbose_name='first name')), + ('is_staff', models.BooleanField(default=False, help_text='Designates whether the user can log into this admin site.', verbose_name='staff status')), + ('is_active', models.BooleanField(default=True, help_text='Designates whether this user should be treated as active. Unselect this instead of deleting accounts.', verbose_name='active')), + ('date_joined', models.DateTimeField(default=django.utils.timezone.now, verbose_name='date joined')), + ('email', models.EmailField(max_length=254, unique=True, verbose_name='Email')), + ('name', models.CharField(max_length=255, verbose_name='Имя')), + ('last_name', models.CharField(max_length=255, verbose_name='Фамилия')), + ('groups', models.ManyToManyField(blank=True, help_text='The groups this user belongs to. A user will get all permissions granted to each of their groups.', related_name='user_set', related_query_name='user', to='auth.group', verbose_name='groups')), + ('user_permissions', models.ManyToManyField(blank=True, help_text='Specific permissions for this user.', related_name='user_set', related_query_name='user', to='auth.permission', verbose_name='user permissions')), + ], + options={ + 'verbose_name': 'Пользователь', + 'verbose_name_plural': 'Пользователи', + }, + managers=[ + ('objects', django.contrib.auth.models.UserManager()), + ], + ), + ] diff --git a/src/users/migrations/__init__.py b/src/users/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/users/models.py b/src/users/models.py new file mode 100644 index 0000000..6c8c881 --- /dev/null +++ b/src/users/models.py @@ -0,0 +1,22 @@ +from django.contrib.auth.models import AbstractUser +from django.db import models + + +class User(AbstractUser): + """Модель пользователя""" + + username = None + + email = models.EmailField(unique=True, verbose_name="Email") + name = models.CharField(max_length=255, verbose_name="Имя") + last_name = models.CharField(max_length=255, verbose_name="Фамилия") + + USERNAME_FIELD = "email" + REQUIRED_FIELDS = [] + + class Meta: + verbose_name = "Пользователь" + verbose_name_plural = "Пользователи" + + def __str__(self): + return self.email diff --git a/src/users/tests.py b/src/users/tests.py new file mode 100644 index 0000000..a39b155 --- /dev/null +++ b/src/users/tests.py @@ -0,0 +1 @@ +# Create your tests here. diff --git a/src/users/urls.py b/src/users/urls.py new file mode 100644 index 0000000..34d3391 --- /dev/null +++ b/src/users/urls.py @@ -0,0 +1,5 @@ +from users.apps import UsersConfig + +app_name = UsersConfig.name + +urlpatterns = [] diff --git a/src/users/views.py b/src/users/views.py new file mode 100644 index 0000000..60f00ef --- /dev/null +++ b/src/users/views.py @@ -0,0 +1 @@ +# Create your views here. diff --git a/src/weather_app/__init__.py b/src/weather_app/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/weather_app/admin.py b/src/weather_app/admin.py new file mode 100644 index 0000000..7253421 --- /dev/null +++ b/src/weather_app/admin.py @@ -0,0 +1,6 @@ +# Register your models here. +from django.contrib import admin + +from .models import SearchHistory + +admin.site.register(SearchHistory) diff --git a/src/weather_app/apps.py b/src/weather_app/apps.py new file mode 100644 index 0000000..0d23ac7 --- /dev/null +++ b/src/weather_app/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class WeatherAppConfig(AppConfig): + default_auto_field = "django.db.models.BigAutoField" + name = "weather_app" diff --git a/src/weather_app/migrations/0001_initial.py b/src/weather_app/migrations/0001_initial.py new file mode 100644 index 0000000..ea78ec2 --- /dev/null +++ b/src/weather_app/migrations/0001_initial.py @@ -0,0 +1,23 @@ +# Generated by Django 5.2.1 on 2025-05-25 16:29 + +import location_field.models.plain +from django.db import migrations, models + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='Place', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('city', models.CharField(max_length=255)), + ('location', location_field.models.plain.PlainLocationField(max_length=63)), + ], + ), + ] diff --git a/src/weather_app/migrations/0002_city_delete_place.py b/src/weather_app/migrations/0002_city_delete_place.py new file mode 100644 index 0000000..d301c4b --- /dev/null +++ b/src/weather_app/migrations/0002_city_delete_place.py @@ -0,0 +1,29 @@ +# Generated by Django 5.2.1 on 2025-05-25 17:28 + +import location_field.models.plain +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('weather_app', '0001_initial'), + ] + + operations = [ + migrations.CreateModel( + name='City', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(max_length=255, verbose_name='Название города')), + ('location', location_field.models.plain.PlainLocationField(max_length=63, verbose_name='Город на карте')), + ], + options={ + 'verbose_name': 'Город', + 'verbose_name_plural': 'Города', + }, + ), + migrations.DeleteModel( + name='Place', + ), + ] diff --git a/src/weather_app/migrations/0003_searchhistory_delete_city.py b/src/weather_app/migrations/0003_searchhistory_delete_city.py new file mode 100644 index 0000000..6a47969 --- /dev/null +++ b/src/weather_app/migrations/0003_searchhistory_delete_city.py @@ -0,0 +1,33 @@ +# Generated by Django 5.2.1 on 2025-05-25 21:18 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('weather_app', '0002_city_delete_place'), + ] + + operations = [ + migrations.CreateModel( + name='SearchHistory', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('session_key', models.CharField(db_index=True, max_length=40)), + ('city', models.CharField(max_length=100)), + ('yandex_city_id', models.CharField(max_length=50)), + ('count', models.PositiveIntegerField(default=1)), + ('last_search', models.DateTimeField(auto_now=True)), + ], + options={ + 'verbose_name': 'История поиска', + 'verbose_name_plural': 'Истории поиска', + 'ordering': ['-last_search'], + 'unique_together': {('session_key', 'yandex_city_id')}, + }, + ), + migrations.DeleteModel( + name='City', + ), + ] diff --git a/src/weather_app/migrations/0004_alter_searchhistory_options_and_more.py b/src/weather_app/migrations/0004_alter_searchhistory_options_and_more.py new file mode 100644 index 0000000..1e832da --- /dev/null +++ b/src/weather_app/migrations/0004_alter_searchhistory_options_and_more.py @@ -0,0 +1,62 @@ +# Generated by Django 5.2.1 on 2025-05-26 18:44 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('weather_app', '0003_searchhistory_delete_city'), + ] + + operations = [ + migrations.AlterModelOptions( + name='searchhistory', + options={'verbose_name': 'История поиска', 'verbose_name_plural': 'Истории поиска'}, + ), + migrations.AlterUniqueTogether( + name='searchhistory', + unique_together={('city',)}, + ), + migrations.AddField( + model_name='searchhistory', + name='last_searched', + field=models.DateTimeField(auto_now=True, verbose_name='Последний запрос'), + ), + migrations.AddField( + model_name='searchhistory', + name='latitude', + field=models.FloatField(null=True, verbose_name='Широта'), + ), + migrations.AddField( + model_name='searchhistory', + name='longitude', + field=models.FloatField(null=True, verbose_name='Долгота'), + ), + migrations.AddField( + model_name='searchhistory', + name='search_count', + field=models.PositiveIntegerField(default=1, verbose_name='Количество запросов'), + ), + migrations.AlterField( + model_name='searchhistory', + name='city', + field=models.CharField(max_length=100, verbose_name='Город'), + ), + migrations.RemoveField( + model_name='searchhistory', + name='count', + ), + migrations.RemoveField( + model_name='searchhistory', + name='last_search', + ), + migrations.RemoveField( + model_name='searchhistory', + name='session_key', + ), + migrations.RemoveField( + model_name='searchhistory', + name='yandex_city_id', + ), + ] diff --git a/src/weather_app/migrations/0005_alter_searchhistory_options_alter_searchhistory_city_and_more.py b/src/weather_app/migrations/0005_alter_searchhistory_options_alter_searchhistory_city_and_more.py new file mode 100644 index 0000000..caaef2a --- /dev/null +++ b/src/weather_app/migrations/0005_alter_searchhistory_options_alter_searchhistory_city_and_more.py @@ -0,0 +1,45 @@ +# Generated by Django 5.2.1 on 2025-05-26 22:53 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('weather_app', '0004_alter_searchhistory_options_and_more'), + ] + + operations = [ + migrations.AlterModelOptions( + name='searchhistory', + options={'ordering': ['-last_searched'], 'verbose_name': 'Запись истории поиска', 'verbose_name_plural': 'История поисковых запросов'}, + ), + migrations.AlterField( + model_name='searchhistory', + name='city', + field=models.CharField(max_length=100, verbose_name='Название населенного пункта'), + ), + migrations.AlterField( + model_name='searchhistory', + name='last_searched', + field=models.DateTimeField(auto_now=True, verbose_name='Последний поиск'), + ), + migrations.AlterField( + model_name='searchhistory', + name='latitude', + field=models.FloatField(blank=True, null=True, verbose_name='Широта'), + ), + migrations.AlterField( + model_name='searchhistory', + name='longitude', + field=models.FloatField(blank=True, null=True, verbose_name='Долгота'), + ), + migrations.AddIndex( + model_name='searchhistory', + index=models.Index(fields=['city'], name='weather_app_city_fe6ef8_idx'), + ), + migrations.AddIndex( + model_name='searchhistory', + index=models.Index(fields=['last_searched'], name='weather_app_last_se_c34bfb_idx'), + ), + ] diff --git a/src/weather_app/migrations/__init__.py b/src/weather_app/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/weather_app/models.py b/src/weather_app/models.py new file mode 100644 index 0000000..498ebaf --- /dev/null +++ b/src/weather_app/models.py @@ -0,0 +1,35 @@ +from django.db import models + +NULLABLE = {"null": True, "blank": True} + + +class SearchHistory(models.Model): + """ + Модель для хранения истории поиска погоды по городам. + """ + + # Если нужно привязать к пользователю: + # user = models.ForeignKey(User, on_delete=models.CASCADE, **NULLABLE, verbose_name="Пользователь") + + city = models.CharField(max_length=100, verbose_name="Название населенного пункта") + latitude = models.FloatField(verbose_name="Широта", **NULLABLE) + longitude = models.FloatField(verbose_name="Долгота", **NULLABLE) + search_count = models.PositiveIntegerField(default=1, verbose_name="Количество запросов") + last_searched = models.DateTimeField(auto_now=True, verbose_name="Последний поиск") + + class Meta: + """ + Дополнительные настройки модели. + """ + + verbose_name = "Запись истории поиска" + verbose_name_plural = "История поисковых запросов" + unique_together = ("city",) + ordering = ["-last_searched"] + indexes = [ + models.Index(fields=["city"]), + models.Index(fields=["last_searched"]), + ] + + def __str__(self): + return f"{self.city} (найден {self.search_count} раз)" diff --git a/src/weather_app/serializers.py b/src/weather_app/serializers.py new file mode 100644 index 0000000..62146c1 --- /dev/null +++ b/src/weather_app/serializers.py @@ -0,0 +1,14 @@ +from rest_framework import serializers + +from .models import SearchHistory + + +class SearchHistoryStatSerializer(serializers.ModelSerializer): + """ + Сериализатор для отображения статистики поисковых запросов. + Предоставляет название города и количество поисков. + """ + + class Meta: + model = SearchHistory + fields = ("city", "search_count") diff --git a/src/weather_app/services.py b/src/weather_app/services.py new file mode 100644 index 0000000..a26b9af --- /dev/null +++ b/src/weather_app/services.py @@ -0,0 +1,116 @@ +import logging +from typing import Any + +import requests +from django.conf import settings + +GEOCODER_API_URL = "https://geocode-maps.yandex.ru/1.x/" +WEATHER_API_URL = "https://api.weather.yandex.ru/v2/forecast" + +logger = logging.getLogger("weather_app") + + +def get_coordinates(city_name: str) -> tuple[float, float, str] | None: + """ + Получает координаты и нормализованное название города. + :param city_name: Название города. + :return: Кортеж (широта, долгота, нормализованное название) или None. + """ + + params = { + "apikey": settings.YANDEX_GEOCODER_KEY, + "geocode": city_name, + "format": "json", + "results": 1, + "kind": "locality", + "lang": "ru_RU", + } + + try: + response = requests.get(GEOCODER_API_URL, params=params) + response.raise_for_status() + data = response.json() + features = data.get("response", {}).get("GeoObjectCollection", {}).get("featureMember", []) + + if not features: + logger.warning(f"Geocoder: не найдены объекты для города '{city_name}'") + return None + + geo_object = features[0].get("GeoObject", {}) + + if not geo_object: + logger.error(f"Geocoder: отсутствует GeoObject для города '{city_name}'") + return None + + point = geo_object.get("Point", {}) + pos = point.get("pos") + + if not pos: + logger.error(f"Geocoder: отсутствуют координаты (pos) для города '{city_name}'") + return None + + longitude, latitude = map(float, pos.split()) + normalized_city = geo_object.get("name") + + meta_data = geo_object.get("metaDataProperty", {}).get("GeocoderMetaData", {}) + components = meta_data.get("Address", {}).get("Components", []) + + for comp in components: + if comp.get("kind") == "locality": + normalized_city = comp.get("name") + break + + if not normalized_city: + try: + address_details = meta_data.get("AddressDetails", {}) + country = address_details.get("Country", {}) + admin_area = country.get("AdministrativeArea", {}) + locality = admin_area.get("Locality", {}) + loc_name = locality.get("LocalityName") + + if loc_name: + normalized_city = loc_name + except Exception: + logger.warning(f"Не удалось получить LocalityName для города '{city_name}'.") + + if not normalized_city: + normalized_city = geo_object.get("name") + + if not normalized_city: + logger.error(f"Geocoder: не удалось определить название города '{city_name}'. Ответ: {data}") + return None + + logger.info(f"Geocoder: найдено '{normalized_city}' ({latitude}, {longitude}) для ввода '{city_name}'") + + return latitude, longitude, normalized_city + + except requests.RequestException as e: + logger.error(f"Geocoder RequestException: {str(e)}") + + return None + except (KeyError, IndexError, ValueError) as e: + logger.error(f"Geocoder ошибка парсинга: {type(e).__name__} - {str(e)}") + + return None + + +def get_weather_forecast(lat: float, lon: float) -> dict[str, Any] | None: + """ + Получает прогноз погоды по координатам. + :param lat: Широта в градусах. + :param lon: Долгота в градусах. + :return: Словарь с данными погоды от API или None в случае ошибки. + """ + + headers = {"X-Yandex-API-Key": settings.YANDEX_WEATHER_KEY} + params = {"lat": lat, "lon": lon, "lang": "ru_RU", "limit": 3} + + try: + response = requests.get(WEATHER_API_URL, headers=headers, params=params) + response.raise_for_status() + + return response.json() + except requests.RequestException as e: + logger.error(f"Ошибка API: {e}") + + return None diff --git a/src/weather_app/templates/weather_app/base.html b/src/weather_app/templates/weather_app/base.html new file mode 100644 index 0000000..6c0a330 --- /dev/null +++ b/src/weather_app/templates/weather_app/base.html @@ -0,0 +1,27 @@ + +{% load static %} + + + + + Прогноз погоды + + + + + + + + + + + + + +
+ {% block content %}{% endblock %} +
+ {% block scripts %}{% endblock %} + + diff --git a/src/weather_app/templates/weather_app/index.html b/src/weather_app/templates/weather_app/index.html new file mode 100644 index 0000000..3dfdf31 --- /dev/null +++ b/src/weather_app/templates/weather_app/index.html @@ -0,0 +1,151 @@ +{% extends "weather_app/base.html" %} +{% load custom_filters %} + +{% block content %} +
+

Прогноз погоды

+ +
+ {% csrf_token %} +
+ +
    +
    +
    + + {% if error %} +
    {{ error }}
    + {% endif %} + + {% if last_city and not forecast %} +

    + Недавно вы искали: + + {{ last_city }} + +

    + {% endif %} + + {% if forecast %} +
    +

    {{ city }}

    + +
    + Иконка погоды +
    +
    {{ now.temp }}°C
    +
    {{ now.condition|translate_condition|capfirst }}
    +
    Ощущается как {{ now.feels_like }}°C
    +
    +
    + +

    Прогноз на 3 дня

    +
    + {% for day in days %} +
    +
    {{ day.date|date:"D, d M" }}
    + Иконка погоды +
    + {{ day.parts.day_short.temp }}° / + {{ day.parts.night_short.temp }}° +
    +
    {{ day.parts.day_short.condition|translate_condition|capfirst }}
    +
    + {% endfor %} +
    +
    + {% endif %} + + {% if search_history %} +
    +

    История поиска:

    + +
    + {% endif %} +
    +{% endblock %} + +{% block scripts %} + +{% endblock %} diff --git a/src/weather_app/templatetags/custom_filters.py b/src/weather_app/templatetags/custom_filters.py new file mode 100644 index 0000000..dfc3ca4 --- /dev/null +++ b/src/weather_app/templatetags/custom_filters.py @@ -0,0 +1,56 @@ +from django import template + +register = template.Library() + + +@register.filter +def split(value: str, delimiter: str) -> dict[str, str]: + """ + Разбивает строку на словарь, используя `delimiter` для разделения элементов. + Каждый элемент делится на ключ и значение по первому вхождению ':'. + Если разделитель ':' отсутствует, ключ и значение совпадают. + """ + + result = {} + + for item in value.split(delimiter): + if ":" in item: + key_part, _, value_part = item.partition(":") + result[key_part.strip()] = value_part.strip() + else: + cleaned_item = item.strip() + result[cleaned_item] = cleaned_item + + return result + + +@register.filter +def get_key(dictionary: dict[str, str], key: str) -> str: + """ + Возвращает значение из словаря по ключу. Если ключ отсутствует, возвращает сам ключ. + """ + + return dictionary.get(key, key) + + +@register.filter +def translate_condition(value: str) -> str: + """ + Переводит состояние погоды на русский язык по точному совпадению ключа. + Если перевод не найден, возвращает исходное значение. + """ + + conditions = { + "clear": "ясно", + "partly-cloudy": "малооблачно", + "cloudy": "облачно", + "overcast": "пасмурно", + "rain": "дождь", + "light-rain": "небольшой дождь", + "heavy-rain": "сильный дождь", + "snow": "снег", + "light-snow": "небольшой снег", + "thunderstorm": "гроза", + } + + return conditions.get(value, value) diff --git a/src/weather_app/urls.py b/src/weather_app/urls.py new file mode 100644 index 0000000..69106e4 --- /dev/null +++ b/src/weather_app/urls.py @@ -0,0 +1,11 @@ +from django.urls import path + +from weather_app.apps import WeatherAppConfig +from weather_app.views import SearchStatsAPIView, WeatherView + +app_name = WeatherAppConfig.name + +urlpatterns = [ + path("", WeatherView.as_view(), name="weather"), + path("api/stats/", SearchStatsAPIView.as_view(), name="search_stats_api"), +] diff --git a/src/weather_app/views.py b/src/weather_app/views.py new file mode 100644 index 0000000..cbc4acd --- /dev/null +++ b/src/weather_app/views.py @@ -0,0 +1,112 @@ +import logging + +from django.db import transaction +from django.shortcuts import render +from django.views import View +from rest_framework.response import Response +from rest_framework.views import APIView + +from config import settings + +from . import services +from .models import SearchHistory +from .serializers import SearchHistoryStatSerializer + +logger = logging.getLogger("weather_app") + + +class WeatherView(View): + """ + Обрабатывает отображение и обработку запросов для получения прогноза погоды. + """ + + template_name = "weather_app/index.html" + + def get(self, request): + """ + Отображает главную страницу с формой поиска погоды. + :param request: HTTP-запрос. + :return: Страница с формой поиска и последним сохраненным городом. + """ + + last_city = request.session.get("last_city") + + context = { + "yandex_geo_api_key": settings.YANDEX_GEOCODER_KEY, + "yandex_suggest_key": settings.YANDEX_SUGGEST_KEY, + } + + if last_city: + context["last_city"] = last_city + + return render(request, self.template_name, context) + + def post(self, request): + """ + Обрабатывает запрос на получение прогноза погоды. + :param request: HTTP-запрос с параметром 'city'. + :return: Страница с результатами поиска или сообщением об ошибке. + :raises: Страница с ошибкой при пустом вводе, отсутствии координат, ошибке получения прогноза. + """ + + city_input = request.POST.get("city", "").strip() + + if not city_input: + return render(request, self.template_name, {"error": "Название города не может быть пустым."}) + + result = services.get_coordinates(city_input) + + if not result: + return render(request, self.template_name, {"error": f'Город "{city_input}" не найден'}) + + lat, lon, normalized_city = result + forecast_data = services.get_weather_forecast(lat, lon) + + if not forecast_data: + return render( + request, self.template_name, {"error": "Не удалось получить прогноз погоды. Попробуйте позже."} + ) + + try: + with transaction.atomic(): + history_entry, created = SearchHistory.objects.get_or_create( + city__iexact=normalized_city, + defaults={"city": normalized_city, "latitude": lat, "longitude": lon}, + ) + if not created: + history_entry.search_count += 1 + history_entry.save(update_fields=["search_count", "last_searched"]) + except Exception as e: + logger.error(f"Ошибка сохранения истории: {str(e)}") + + request.session["last_city"] = normalized_city + + context = { + "city": normalized_city, + "forecast": forecast_data, + "now": forecast_data.get("fact", {}), + "days": forecast_data.get("forecasts", []), + "search_history": SearchHistory.objects.order_by("-last_searched")[:5], + "yandex_suggest_key": settings.YANDEX_SUGGEST_KEY, + } + + return render(request, self.template_name, context) + + +class SearchStatsAPIView(APIView): + """ + API для получения статистики поисковых запросов. + Предоставляет данные в формате JSON, отсортированные по количеству поисков. + """ + + def get(self, request): + """ + Возвращает статистику поисковых запросов. + :param request: HTTP-запрос. + :return: JSON-ответ с данными статистики. + """ + + stats = SearchHistory.objects.order_by("-search_count") + serializer = SearchHistoryStatSerializer(stats, many=True) + + return Response(serializer.data) diff --git a/uv.lock b/uv.lock new file mode 100644 index 0000000..faeda7d --- /dev/null +++ b/uv.lock @@ -0,0 +1,69 @@ +# This file was autogenerated by uv via the following command: +# uv pip compile pyproject.toml -o uv.lock +anyio==4.9.0 + # via httpx +asgiref==3.8.1 + # via django +certifi==2025.4.26 + # via + # httpcore + # httpx + # requests +charset-normalizer==3.4.2 + # via requests +colorama==0.4.6 + # via pytest +django==5.2.1 + # via + # weather-web (pyproject.toml) + # djangorestframework +django-location-field==2.7.3 + # via weather-web (pyproject.toml) +django-rest-framework==0.1.0 + # via weather-web (pyproject.toml) +djangorestframework==3.16.0 + # via + # weather-web (pyproject.toml) + # django-rest-framework +h11==0.16.0 + # via httpcore +httpcore==1.0.9 + # via httpx +httpx==0.28.1 + # via weather-web (pyproject.toml) +idna==3.10 + # via + # anyio + # httpx + # requests +iniconfig==2.1.0 + # via pytest +packaging==25.0 + # via pytest +pluggy==1.6.0 + # via pytest +psycopg2-binary==2.9.10 + # via weather-web (pyproject.toml) +pytest==8.3.5 + # via + # weather-web (pyproject.toml) + # pytest-django + # pytest-mock +pytest-django==4.11.1 + # via weather-web (pyproject.toml) +pytest-mock==3.14.1 + # via weather-web (pyproject.toml) +python-dotenv==1.1.0 + # via weather-web (pyproject.toml) +requests==2.32.3 + # via weather-web (pyproject.toml) +six==1.17.0 + # via weather-web (pyproject.toml) +sniffio==1.3.1 + # via anyio +sqlparse==0.5.3 + # via django +tzdata==2025.2 + # via django +urllib3==2.4.0 + # via requests