diff --git a/Pipfile b/Pipfile index e981b0a..7006e54 100644 --- a/Pipfile +++ b/Pipfile @@ -8,6 +8,8 @@ python-dotenv = "*" flask = "*" flask-login = "*" flask-sqlalchemy = "*" +pytz = "*" +faker = "*" [dev-packages] diff --git a/Pipfile.lock b/Pipfile.lock index 31f236a..fa44b19 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "19860a3b78a38697affcbceb509d17ee2c110dde2bf8080a178441a42dbff3d1" + "sha256": "274a5de62926ba1c658817a40cb4b731272e26e154ce7a4a3b2f2095805a5dde" }, "pipfile-spec": 6, "requires": { @@ -32,6 +32,14 @@ "markers": "platform_system == 'Windows'", "version": "==0.4.4" }, + "faker": { + "hashes": [ + "sha256:5536ceb63380f0d598c026b7c330c17d719a19d1a495e9397ee8f5259420a696", + "sha256:85ed0cb379e3b7414bdb6b484994beaf9bddc74472c8c35f743c16cf5fc0c314" + ], + "index": "pypi", + "version": "==13.3.3" + }, "flask": { "hashes": [ "sha256:59da8a3170004800a2837844bfa84d49b022550616070f7cb1a659682b2e7c9f", @@ -179,6 +187,14 @@ "markers": "python_version >= '3.7'", "version": "==2.1.1" }, + "python-dateutil": { + "hashes": [ + "sha256:0123cacc1627ae19ddf3c27a5de5bd67ee4586fbdd6440d9748f8abb483d3e86", + "sha256:961d03dc3453ebbc59dbdea9e4e11c5651520a876d0f4db161e8674aae935da9" + ], + "markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'", + "version": "==2.8.2" + }, "python-dotenv": { "hashes": [ "sha256:b7e3b04a59693c42c36f9ab1cc2acc46fa5df8c78e178fc33a8d4cd05c8d498f", @@ -187,6 +203,22 @@ "index": "pypi", "version": "==0.20.0" }, + "pytz": { + "hashes": [ + "sha256:1e760e2fe6a8163bc0b3d9a19c4f84342afa0a2affebfaa84b01b978a02ecaa7", + "sha256:e68985985296d9a66a881eb3193b0906246245294a881e7c8afe623866ac6a5c" + ], + "index": "pypi", + "version": "==2022.1" + }, + "six": { + "hashes": [ + "sha256:1e61c37477a1626458e36f7b1d82aa5c9b094fa4802892072e49de9c60c4c926", + "sha256:8abb2f1d86890a2dfb989f9a77cfcfd3e47c2a354b01111771326f8aa26e0254" + ], + "markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'", + "version": "==1.16.0" + }, "sqlalchemy": { "hashes": [ "sha256:04164e0063feb7aedd9d073db0fd496edb244be40d46ea1f0d8990815e4b8c34", diff --git a/create_app.py b/create_app.py index 5926c8e..0a50e68 100644 --- a/create_app.py +++ b/create_app.py @@ -1,8 +1,14 @@ -from flask import Flask, render_template -from flask_sqlalchemy import SQLAlchemy -from flask_login import LoginManager +from datetime import datetime +import click +import pytz +from faker import Faker +from flask import Flask, render_template, request +from flask_login import LoginManager, current_user +from flask_sqlalchemy import SQLAlchemy # init SQLAlchemy +from werkzeug.security import generate_password_hash + db = SQLAlchemy() @@ -36,10 +42,36 @@ def create_app(): # note that we set the 404 status explicitly return render_template('errors/404.html'), 404 + @app.before_request + def update_last_seen(): + if current_user.is_authenticated: + current_user.last_seen = datetime.now(tz=pytz.UTC) # datetime.utcnow doesn't actually attach a timezone + current_user.last_ip = str(request.remote_addr) + db.session.add(current_user) + db.session.commit() + # CLI commands setup @app.shell_context_processor def shell_context(): """Provides specific Flask components to the shell.""" return {'app': app, 'db': db} + @app.cli.command("fake") + @click.argument("count") + def create_fake_users(count: int): + fake = Faker() + users = {} + for _ in range(int(count)): + profile: dict = fake.simple_profile() + users[profile['username']] = profile + + for profile in users.values(): + new_user = User(username=profile['username'], + name=profile['name'], + password=generate_password_hash('password', method='sha256')) + db.session.add(new_user) + + print(f'Committing {len(users)} users into DB.') + db.session.commit() + return app