diff --git a/mojabaza.db b/mojabaza.db index 0c00b34..e180eef 100644 Binary files a/mojabaza.db and b/mojabaza.db differ diff --git a/pyproject.toml b/pyproject.toml index 7a418c2..04392ed 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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", ] diff --git a/src/__init__.py b/src/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/db_utils.py b/src/db_utils.py index 002a5b2..44819aa 100644 --- a/src/db_utils.py +++ b/src/db_utils.py @@ -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) diff --git a/src/main.py b/src/main.py index 4f7af2f..6fd7a6c 100644 --- a/src/main.py +++ b/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 "

To jesty test aplikacji!

" + +@app.route("/") +def index(): + return render_template("index.html") + # Przekazywanie prostych parametrów... -@app.route('/hello/') +@app.route("/hello/") def hello(name): return "

Hello, %s!

" % str(name) + # Proste logowanie zdarzeń... plik_logu = "logi.txt" -@app.route('/logi/') + + +@app.route("/logi/") 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 "

Hello z logiem, %s!

" % 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"

Zarejestrowano użytkownika: {username} ({nickname})

" + @app.route("/pogoda/") def pogoda(city): result = get_weather(city) if not result: return f"

Nie udało się pobrać pogody dla miasta: {city}

", 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) \ No newline at end of file + +if __name__ == "__main__": + from src.db_utils import Base, engine + + Base.metadata.create_all(engine) + + load_dotenv() + + app.run(debug=False) diff --git a/src/templates/index.html b/src/templates/index.html new file mode 100644 index 0000000..bdbbacb --- /dev/null +++ b/src/templates/index.html @@ -0,0 +1,33 @@ + + + + + Strona główna + + + +

Witamy na stronie głównej!

+
    +
  • Logowanie
  • +
  • Rejestracja
  • +
  • +
    + + + +
    + +
  • +
  • Zaloguj (test sesji)
  • +
  • Wyloguj (test sesji)
  • +
+ + diff --git a/src/weather_api.py b/src/weather_api.py index 9aa012f..7c8f9d5 100644 --- a/src/weather_api.py +++ b/src/weather_api.py @@ -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 - diff --git a/uv.lock b/uv.lock index 0fad757..424e884 100644 --- a/uv.lock +++ b/uv.lock @@ -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" }, ]