From fed5c2789fa8c3c01f424aae47c1e81e2fb76f91 Mon Sep 17 00:00:00 2001 From: evdo Date: Tue, 1 Apr 2025 02:03:23 +0200 Subject: [PATCH] Initial commit --- .gitignore | 52 ++++++++ Dockerfile | 20 +++ requirements.txt | 2 + weather_project/manage.py | 22 ++++ weather_project/weather/__init__.py | 0 weather_project/weather/admin.py | 3 + weather_project/weather/apps.py | 6 + .../weather/migrations/__init__.py | 0 weather_project/weather/models.py | 3 + .../weather/templates/weather/index.html | 95 ++++++++++++++ weather_project/weather/tests.py | 3 + weather_project/weather/urls.py | 6 + weather_project/weather/views.py | 26 ++++ weather_project/weather_project/__init__.py | 0 weather_project/weather_project/asgi.py | 16 +++ weather_project/weather_project/settings.py | 124 ++++++++++++++++++ weather_project/weather_project/urls.py | 7 + weather_project/weather_project/wsgi.py | 16 +++ 18 files changed, 401 insertions(+) create mode 100644 .gitignore create mode 100644 Dockerfile create mode 100644 requirements.txt create mode 100644 weather_project/manage.py create mode 100644 weather_project/weather/__init__.py create mode 100644 weather_project/weather/admin.py create mode 100644 weather_project/weather/apps.py create mode 100644 weather_project/weather/migrations/__init__.py create mode 100644 weather_project/weather/models.py create mode 100644 weather_project/weather/templates/weather/index.html create mode 100644 weather_project/weather/tests.py create mode 100644 weather_project/weather/urls.py create mode 100644 weather_project/weather/views.py create mode 100644 weather_project/weather_project/__init__.py create mode 100644 weather_project/weather_project/asgi.py create mode 100644 weather_project/weather_project/settings.py create mode 100644 weather_project/weather_project/urls.py create mode 100644 weather_project/weather_project/wsgi.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ed64a72 --- /dev/null +++ b/.gitignore @@ -0,0 +1,52 @@ +__pycache__/ +*.py[cod] +*$py.class + +venv/ +.env/ +.venv/ + +.idea/ + +*.sqlite3 + +staticfiles/ +media/ + +*.log +logs/ +*.pot + +.DS_Store +Thumbs.db + +.env +.env.* + +**/migrations/__pycache__/ +**/migrations/*.pyc + +*.pid +*.tar +*.bak +*.swp + +htmlcov/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.tox/ +.cache/ +pytest_cache/ + +.ipynb_checkpoints + +.mypy_cache/ +.dmypy.json +.pyre/ + +.vscode/ diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..7bd5303 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,20 @@ +# Используем официальный образ Python +FROM python:3.11-slim + +# Устанавливаем рабочую директорию +WORKDIR /app + +# Копируем зависимости +COPY requirements.txt . + +# Устанавливаем зависимости +RUN pip install --no-cache-dir -r requirements.txt + +# Копируем всё приложение в контейнер +COPY . . + +# Указываем переменные окружения (они будут приходить извне) +ENV PYTHONUNBUFFERED=1 + +# Команда запуска сервера +CMD ["python", "weather_project/manage.py", "runserver", "0.0.0.0:8000"] diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..0139f1b --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +Django>=4.2 +requests \ No newline at end of file diff --git a/weather_project/manage.py b/weather_project/manage.py new file mode 100644 index 0000000..72f02a5 --- /dev/null +++ b/weather_project/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', 'weather_project.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/weather_project/weather/__init__.py b/weather_project/weather/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/weather_project/weather/admin.py b/weather_project/weather/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/weather_project/weather/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/weather_project/weather/apps.py b/weather_project/weather/apps.py new file mode 100644 index 0000000..8c4c86c --- /dev/null +++ b/weather_project/weather/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class WeatherConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'weather' diff --git a/weather_project/weather/migrations/__init__.py b/weather_project/weather/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/weather_project/weather/models.py b/weather_project/weather/models.py new file mode 100644 index 0000000..71a8362 --- /dev/null +++ b/weather_project/weather/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/weather_project/weather/templates/weather/index.html b/weather_project/weather/templates/weather/index.html new file mode 100644 index 0000000..8bd1133 --- /dev/null +++ b/weather_project/weather/templates/weather/index.html @@ -0,0 +1,95 @@ + + + + + Weather App + + + +

EDP Weather Request

+
+ {% csrf_token %} + + +
+ + {% if weather %} +
+

Weather in {{ weather.city }}

+

Temperature: {{ weather.temperature }} °C

+

Description: {{ weather.description }}

+ Weather icon +
+ {% endif %} + + {% if error %} +

{{ error }}

+ {% endif %} + + diff --git a/weather_project/weather/tests.py b/weather_project/weather/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/weather_project/weather/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/weather_project/weather/urls.py b/weather_project/weather/urls.py new file mode 100644 index 0000000..c486297 --- /dev/null +++ b/weather_project/weather/urls.py @@ -0,0 +1,6 @@ +from django.urls import path +from . import views + +urlpatterns = [ + path('', views.index, name='index'), +] \ No newline at end of file diff --git a/weather_project/weather/views.py b/weather_project/weather/views.py new file mode 100644 index 0000000..1264ac7 --- /dev/null +++ b/weather_project/weather/views.py @@ -0,0 +1,26 @@ +import requests +from django.shortcuts import render + +API_KEY = 'a7cc162fc60a76d2e31461071634b8ce' + +def index(request): + weather_data = None + error = None + + if request.method == 'POST': + city = request.POST.get('city') + if city: + url = f'https://api.openweathermap.org/data/2.5/weather?q={city}&appid={API_KEY}&units=metric&lang=eng' + response = requests.get(url) + if response.status_code == 200: + data = response.json() + weather_data = { + 'city': city, + 'temperature': data['main']['temp'], + 'description': data['weather'][0]['description'], + 'icon': data['weather'][0]['icon'] + } + else: + error = 'City not found' + + return render(request, 'weather/index.html', {'weather': weather_data, 'error': error}) diff --git a/weather_project/weather_project/__init__.py b/weather_project/weather_project/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/weather_project/weather_project/asgi.py b/weather_project/weather_project/asgi.py new file mode 100644 index 0000000..82190eb --- /dev/null +++ b/weather_project/weather_project/asgi.py @@ -0,0 +1,16 @@ +""" +ASGI config for weather_project 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.1/howto/deployment/asgi/ +""" + +import os + +from django.core.asgi import get_asgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'weather_project.settings') + +application = get_asgi_application() diff --git a/weather_project/weather_project/settings.py b/weather_project/weather_project/settings.py new file mode 100644 index 0000000..b02663c --- /dev/null +++ b/weather_project/weather_project/settings.py @@ -0,0 +1,124 @@ +""" +Django settings for weather_project project. + +Generated by 'django-admin startproject' using Django 5.1.7. + +For more information on this file, see +https://docs.djangoproject.com/en/5.1/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/5.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/5.1/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = 'django-insecure-g1*+7b$t@0wumyr_j*fx+)_il(_93idih^25=86-b6^&neg=h$' + +# SECURITY WARNING: don't run with debug turned on in production! +DEBUG = True + +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' +] + +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 = 'weather_project.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 = 'weather_project.wsgi.application' + + +# Database +# https://docs.djangoproject.com/en/5.1/ref/settings/#databases + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': BASE_DIR / 'db.sqlite3', + } +} + + +# Password validation +# https://docs.djangoproject.com/en/5.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/5.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/5.1/howto/static-files/ + +STATIC_URL = 'static/' + +# Default primary key field type +# https://docs.djangoproject.com/en/5.1/ref/settings/#default-auto-field + +DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' diff --git a/weather_project/weather_project/urls.py b/weather_project/weather_project/urls.py new file mode 100644 index 0000000..2656b63 --- /dev/null +++ b/weather_project/weather_project/urls.py @@ -0,0 +1,7 @@ +from django.contrib import admin +from django.urls import path, include + +urlpatterns = [ + path('admin/', admin.site.urls), + path('', include('weather.urls')), +] \ No newline at end of file diff --git a/weather_project/weather_project/wsgi.py b/weather_project/weather_project/wsgi.py new file mode 100644 index 0000000..33547c7 --- /dev/null +++ b/weather_project/weather_project/wsgi.py @@ -0,0 +1,16 @@ +""" +WSGI config for weather_project 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.1/howto/deployment/wsgi/ +""" + +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'weather_project.settings') + +application = get_wsgi_application()