Initial commit
This commit is contained in:
parent
1a630cbaf0
commit
fed5c2789f
18 changed files with 401 additions and 0 deletions
52
.gitignore
vendored
Normal file
52
.gitignore
vendored
Normal file
|
@ -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/
|
20
Dockerfile
Normal file
20
Dockerfile
Normal file
|
@ -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"]
|
2
requirements.txt
Normal file
2
requirements.txt
Normal file
|
@ -0,0 +1,2 @@
|
|||
Django>=4.2
|
||||
requests
|
22
weather_project/manage.py
Normal file
22
weather_project/manage.py
Normal file
|
@ -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()
|
0
weather_project/weather/__init__.py
Normal file
0
weather_project/weather/__init__.py
Normal file
3
weather_project/weather/admin.py
Normal file
3
weather_project/weather/admin.py
Normal file
|
@ -0,0 +1,3 @@
|
|||
from django.contrib import admin
|
||||
|
||||
# Register your models here.
|
6
weather_project/weather/apps.py
Normal file
6
weather_project/weather/apps.py
Normal file
|
@ -0,0 +1,6 @@
|
|||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class WeatherConfig(AppConfig):
|
||||
default_auto_field = 'django.db.models.BigAutoField'
|
||||
name = 'weather'
|
0
weather_project/weather/migrations/__init__.py
Normal file
0
weather_project/weather/migrations/__init__.py
Normal file
3
weather_project/weather/models.py
Normal file
3
weather_project/weather/models.py
Normal file
|
@ -0,0 +1,3 @@
|
|||
from django.db import models
|
||||
|
||||
# Create your models here.
|
95
weather_project/weather/templates/weather/index.html
Normal file
95
weather_project/weather/templates/weather/index.html
Normal file
|
@ -0,0 +1,95 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Weather App</title>
|
||||
<style>
|
||||
body {
|
||||
font-family: "Segoe UI", sans-serif;
|
||||
background: #f4f6f8;
|
||||
margin: 0;
|
||||
padding: 2rem;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
h1 {
|
||||
margin-bottom: 1rem;
|
||||
font-size: 2.5rem;
|
||||
color: #222;
|
||||
}
|
||||
|
||||
form {
|
||||
display: flex;
|
||||
gap: 0.5rem;
|
||||
margin-bottom: 2rem;
|
||||
}
|
||||
|
||||
input[type="text"] {
|
||||
padding: 0.6rem 1rem;
|
||||
font-size: 1rem;
|
||||
border: 1px solid #ccc;
|
||||
border-radius: 8px;
|
||||
width: 250px;
|
||||
}
|
||||
|
||||
button {
|
||||
padding: 0.6rem 1.2rem;
|
||||
font-size: 1rem;
|
||||
border: none;
|
||||
background-color: #007bff;
|
||||
color: white;
|
||||
border-radius: 8px;
|
||||
cursor: pointer;
|
||||
transition: background-color 0.3s ease;
|
||||
}
|
||||
|
||||
button:hover {
|
||||
background-color: #0056b3;
|
||||
}
|
||||
|
||||
.weather-box {
|
||||
background-color: white;
|
||||
padding: 2rem;
|
||||
border-radius: 12px;
|
||||
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
|
||||
max-width: 400px;
|
||||
width: 100%;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.weather-box img {
|
||||
width: 80px;
|
||||
height: 80px;
|
||||
margin-top: 1rem;
|
||||
}
|
||||
|
||||
.error {
|
||||
color: red;
|
||||
font-weight: bold;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<h1> EDP Weather Request</h1>
|
||||
<form method="post">
|
||||
{% csrf_token %}
|
||||
<input type="text" name="city" placeholder="Enter the name of the city" required>
|
||||
<button type="submit">Show the weather</button>
|
||||
</form>
|
||||
|
||||
{% if weather %}
|
||||
<div class="weather-box">
|
||||
<h2>Weather in {{ weather.city }}</h2>
|
||||
<p><strong>Temperature:</strong> {{ weather.temperature }} °C</p>
|
||||
<p><strong>Description:</strong> {{ weather.description }}</p>
|
||||
<img src="http://openweathermap.org/img/w/{{ weather.icon }}.png" alt="Weather icon">
|
||||
</div>
|
||||
{% endif %}
|
||||
|
||||
{% if error %}
|
||||
<p class="error">{{ error }}</p>
|
||||
{% endif %}
|
||||
</body>
|
||||
</html>
|
3
weather_project/weather/tests.py
Normal file
3
weather_project/weather/tests.py
Normal file
|
@ -0,0 +1,3 @@
|
|||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
6
weather_project/weather/urls.py
Normal file
6
weather_project/weather/urls.py
Normal file
|
@ -0,0 +1,6 @@
|
|||
from django.urls import path
|
||||
from . import views
|
||||
|
||||
urlpatterns = [
|
||||
path('', views.index, name='index'),
|
||||
]
|
26
weather_project/weather/views.py
Normal file
26
weather_project/weather/views.py
Normal file
|
@ -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})
|
0
weather_project/weather_project/__init__.py
Normal file
0
weather_project/weather_project/__init__.py
Normal file
16
weather_project/weather_project/asgi.py
Normal file
16
weather_project/weather_project/asgi.py
Normal file
|
@ -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()
|
124
weather_project/weather_project/settings.py
Normal file
124
weather_project/weather_project/settings.py
Normal file
|
@ -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'
|
7
weather_project/weather_project/urls.py
Normal file
7
weather_project/weather_project/urls.py
Normal file
|
@ -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')),
|
||||
]
|
16
weather_project/weather_project/wsgi.py
Normal file
16
weather_project/weather_project/wsgi.py
Normal file
|
@ -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()
|
Loading…
Reference in a new issue