Code refactor and formatting
This commit is contained in:
parent
870b937bf4
commit
ff6b595652
BIN
mojabaza.db
BIN
mojabaza.db
Binary file not shown.
@ -6,6 +6,7 @@ readme = "README.md"
|
||||
requires-python = ">=3.11"
|
||||
dependencies = [
|
||||
"flask>=3.1.1",
|
||||
"python-dotenv>=1.1.0",
|
||||
"requests>=2.32.4",
|
||||
"sqlalchemy>=2.0.41",
|
||||
]
|
||||
|
||||
0
src/__init__.py
Normal file
0
src/__init__.py
Normal file
@ -6,6 +6,7 @@ from sqlalchemy.orm import sessionmaker
|
||||
engine = create_engine("sqlite:///mojabaza.db", echo=True)
|
||||
Base = declarative_base()
|
||||
|
||||
|
||||
# Schemat BD
|
||||
class User(Base):
|
||||
__tablename__ = "user"
|
||||
@ -19,6 +20,7 @@ class User(Base):
|
||||
self.nickname = nickname
|
||||
self.password = password
|
||||
|
||||
|
||||
# Metoda do tworzenia sesji...
|
||||
def create_db_session():
|
||||
Session = sessionmaker(bind=engine)
|
||||
@ -26,4 +28,5 @@ def create_db_session():
|
||||
|
||||
return session
|
||||
|
||||
|
||||
Base.metadata.create_all(engine)
|
||||
|
||||
54
src/main.py
54
src/main.py
@ -1,40 +1,42 @@
|
||||
import flask
|
||||
from flask import Flask, render_template, request, session, flash, redirect, url_for
|
||||
from flask import render_template, request, session, flash, redirect, url_for
|
||||
import os
|
||||
import traceback
|
||||
import json
|
||||
import random
|
||||
import datetime
|
||||
from dotenv import load_dotenv
|
||||
|
||||
from src.db_utils import create_db_session, User
|
||||
from src.weather_api import get_weather
|
||||
|
||||
# Inicjalizacja web app
|
||||
app = flask.Flask(__name__,
|
||||
template_folder='templates',
|
||||
static_folder='static')
|
||||
app = flask.Flask(__name__, template_folder="templates", static_folder="static")
|
||||
|
||||
app.secret_key = os.urandom(24)
|
||||
|
||||
@app.route('/')
|
||||
def test():
|
||||
return "<h1>To jesty test aplikacji!</h1>"
|
||||
|
||||
@app.route("/")
|
||||
def index():
|
||||
return render_template("index.html")
|
||||
|
||||
|
||||
# Przekazywanie prostych parametrów...
|
||||
@app.route('/hello/<name>')
|
||||
@app.route("/hello/<name>")
|
||||
def hello(name):
|
||||
return "<h1>Hello, %s!</h1>" % str(name)
|
||||
|
||||
|
||||
# Proste logowanie zdarzeń...
|
||||
plik_logu = "logi.txt"
|
||||
@app.route('/logi/<name>')
|
||||
|
||||
|
||||
@app.route("/logi/<name>")
|
||||
def logi(name):
|
||||
with open(plik_logu, 'a') as file:
|
||||
with open(plik_logu, "a") as file:
|
||||
now = datetime.datetime.now()
|
||||
file.write(now.isoformat() + ": odwiedziny przez " + name + "\n")
|
||||
|
||||
return "<h1>Hello z logiem, %s!</h1>" % name
|
||||
|
||||
|
||||
# Obsługa sesji...
|
||||
@app.route("/zaloguj")
|
||||
def zaloguj():
|
||||
@ -44,15 +46,18 @@ def zaloguj():
|
||||
else:
|
||||
return "Użytkownik niezalogowany!"
|
||||
|
||||
|
||||
@app.route("/wyloguj")
|
||||
def wyloguj():
|
||||
session["zalogowany"] = False
|
||||
return "Użytkownik wylogowany!"
|
||||
|
||||
|
||||
@app.route("/logowanie")
|
||||
def logowanie():
|
||||
return render_template("logowanie.html")
|
||||
|
||||
|
||||
@app.route("/login", methods=["POST"])
|
||||
def login():
|
||||
# Twózs obiekt sesji BD...
|
||||
@ -72,10 +77,12 @@ def login():
|
||||
|
||||
return result
|
||||
|
||||
|
||||
@app.route("/signup")
|
||||
def signup():
|
||||
return render_template("signup.html")
|
||||
|
||||
|
||||
@app.route("/register", methods=["POST"])
|
||||
def register():
|
||||
sqlsession = create_db_session()
|
||||
@ -85,7 +92,10 @@ def register():
|
||||
|
||||
# Walidacja hasła: min. 8 znaków i min. jedna cyfra
|
||||
if len(password) < 8 or not any(char.isdigit() for char in password):
|
||||
flash("Hasło musi mieć co najmniej 8 znaków i zawierać przynajmniej jedną cyfrę!", "error")
|
||||
flash(
|
||||
"Hasło musi mieć co najmniej 8 znaków i zawierać przynajmniej jedną cyfrę!",
|
||||
"error",
|
||||
)
|
||||
return redirect(url_for("signup"))
|
||||
|
||||
user = User(username, nickname, password)
|
||||
@ -93,13 +103,23 @@ def register():
|
||||
sqlsession.commit()
|
||||
return f"<h1>Zarejestrowano użytkownika: {username} ({nickname})</h1>"
|
||||
|
||||
|
||||
@app.route("/pogoda/<city>")
|
||||
def pogoda(city):
|
||||
result = get_weather(city)
|
||||
if not result:
|
||||
return f"<h2>Nie udało się pobrać pogody dla miasta: {city}</h2>", 404
|
||||
temp, humidity, type, rain = result
|
||||
return render_template("pogoda.html", temp=temp, humid=humidity, weathertype=type, rain=rain, city=city)
|
||||
return render_template(
|
||||
"pogoda.html", temp=temp, humid=humidity, weathertype=type, rain=rain, city=city
|
||||
)
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(debug=False)
|
||||
|
||||
if __name__ == "__main__":
|
||||
from src.db_utils import Base, engine
|
||||
|
||||
Base.metadata.create_all(engine)
|
||||
|
||||
load_dotenv()
|
||||
|
||||
app.run(debug=False)
|
||||
|
||||
33
src/templates/index.html
Normal file
33
src/templates/index.html
Normal file
@ -0,0 +1,33 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="pl">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Strona główna</title>
|
||||
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
|
||||
</head>
|
||||
<body>
|
||||
<h1>Witamy na stronie głównej!</h1>
|
||||
<ul>
|
||||
<li><a href="{{ url_for('logowanie') }}">Logowanie</a></li>
|
||||
<li><a href="{{ url_for('signup') }}">Rejestracja</a></li>
|
||||
<li>
|
||||
<form id="pogodaForm" action="#" method="get" style="display:inline;" onsubmit="return goToWeather();">
|
||||
<label for="city">Pogoda dla miasta:</label>
|
||||
<input type="text" id="city" name="city" required>
|
||||
<button type="submit">Sprawdź</button>
|
||||
</form>
|
||||
<script>
|
||||
function goToWeather() {
|
||||
var city = document.getElementById('city').value;
|
||||
if(city) {
|
||||
window.location.href = '/pogoda/' + encodeURIComponent(city);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
</script>
|
||||
</li>
|
||||
<li><a href="{{ url_for('zaloguj') }}">Zaloguj (test sesji)</a></li>
|
||||
<li><a href="{{ url_for('wyloguj') }}">Wyloguj (test sesji)</a></li>
|
||||
</ul>
|
||||
</body>
|
||||
</html>
|
||||
@ -24,4 +24,3 @@ def get_weather(city):
|
||||
weather = weather_request.content.strip()
|
||||
temp, humidity, type, rain = decode_weather(weather)
|
||||
return temp, humidity, type, rain
|
||||
|
||||
|
||||
11
uv.lock
generated
11
uv.lock
generated
@ -226,6 +226,15 @@ wheels = [
|
||||
{ url = "https://files.pythonhosted.org/packages/4f/65/6079a46068dfceaeabb5dcad6d674f5f5c61a6fa5673746f42a9f4c233b3/MarkupSafe-3.0.2-cp313-cp313t-win_amd64.whl", hash = "sha256:e444a31f8db13eb18ada366ab3cf45fd4b31e4db1236a4448f68778c1d1a5a2f", size = 15739, upload-time = "2024-10-18T15:21:42.784Z" },
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "python-dotenv"
|
||||
version = "1.1.0"
|
||||
source = { registry = "https://pypi.org/simple" }
|
||||
sdist = { url = "https://files.pythonhosted.org/packages/88/2c/7bb1416c5620485aa793f2de31d3df393d3686aa8a8506d11e10e13c5baf/python_dotenv-1.1.0.tar.gz", hash = "sha256:41f90bc6f5f177fb41f53e87666db362025010eb28f60a01c9143bfa33a2b2d5", size = 39920, upload-time = "2025-03-25T10:14:56.835Z" }
|
||||
wheels = [
|
||||
{ url = "https://files.pythonhosted.org/packages/1e/18/98a99ad95133c6a6e2005fe89faedf294a748bd5dc803008059409ac9b1e/python_dotenv-1.1.0-py3-none-any.whl", hash = "sha256:d7c01d9e2293916c18baf562d95698754b0dbbb5e74d457c45d4f6561fb9d55d", size = 20256, upload-time = "2025-03-25T10:14:55.034Z" },
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "requests"
|
||||
version = "2.32.4"
|
||||
@ -247,6 +256,7 @@ version = "0.1.0"
|
||||
source = { virtual = "." }
|
||||
dependencies = [
|
||||
{ name = "flask" },
|
||||
{ name = "python-dotenv" },
|
||||
{ name = "requests" },
|
||||
{ name = "sqlalchemy" },
|
||||
]
|
||||
@ -254,6 +264,7 @@ dependencies = [
|
||||
[package.metadata]
|
||||
requires-dist = [
|
||||
{ name = "flask", specifier = ">=3.1.1" },
|
||||
{ name = "python-dotenv", specifier = ">=1.1.0" },
|
||||
{ name = "requests", specifier = ">=2.32.4" },
|
||||
{ name = "sqlalchemy", specifier = ">=2.0.41" },
|
||||
]
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user