diff --git a/Pipfile b/Pipfile index 7006e54..ef20fff 100644 --- a/Pipfile +++ b/Pipfile @@ -10,6 +10,7 @@ flask-login = "*" flask-sqlalchemy = "*" pytz = "*" faker = "*" +humanize = "*" [dev-packages] diff --git a/Pipfile.lock b/Pipfile.lock index fa44b19..2d513a0 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "274a5de62926ba1c658817a40cb4b731272e26e154ce7a4a3b2f2095805a5dde" + "sha256": "d91ae74e8b4c1a9e9603e27dbdddd314ae6a462649cbff848d12c89ac84b0034" }, "pipfile-spec": 6, "requires": { @@ -125,6 +125,14 @@ "markers": "python_version >= '3' and platform_machine == 'aarch64' or (platform_machine == 'ppc64le' or (platform_machine == 'x86_64' or (platform_machine == 'amd64' or (platform_machine == 'AMD64' or (platform_machine == 'win32' or platform_machine == 'WIN32')))))", "version": "==1.1.2" }, + "humanize": { + "hashes": [ + "sha256:8d86333b8557dacffd4dce1dbe09c81c189e2caf7bb17a970b2212f0f58f10f2", + "sha256:ee1f872fdfc7d2ef4a28d4f80ddde9f96d36955b5d6b0dac4bdeb99502bddb00" + ], + "index": "pypi", + "version": "==4.0.0" + }, "itsdangerous": { "hashes": [ "sha256:2c2349112351b88699d8d4b6b075022c0808887cb7ad10069318a8b0bc88db44", diff --git a/models.py b/models.py index 4555e04..db32c0b 100644 --- a/models.py +++ b/models.py @@ -1,7 +1,13 @@ +import datetime +import json +from typing import List + +import humanize from flask_login import UserMixin from sqlalchemy import func from .create_app import db +MAXIMUM_ONLINE_DELTA = datetime.timedelta(minutes=5) class User(UserMixin, db.Model): @@ -14,6 +20,16 @@ class User(UserMixin, db.Model): last_seen = db.Column(db.DateTime, nullable=False, server_default=func.now()) last_ip = db.Column(db.String(64), nullable=True) + def get_last_seen(self) -> str: + delta: datetime.timedelta = datetime.datetime.utcnow() - self.last_seen + if delta > MAXIMUM_ONLINE_DELTA: + return f'Last seen {humanize.naturaldelta(delta)} ago' + return 'Online now!' + + def get_registration_delta(self) -> str: + delta: datetime.timedelta = datetime.datetime.utcnow() - self.time_registered + return humanize.naturaldelta(delta) + class Post(db.Model): id = db.Column(db.Integer, primary_key=True) @@ -22,3 +38,18 @@ class Post(db.Model): date_posted = db.Column(db.DateTime, server_default=func.now()) date_updated = db.Column(db.DateTime, nullable=True) likes = db.Column(db.Text, default='[]') + + def get_likes(self) -> List[int]: + """Return the IDs of the Users who have liked this post.""" + return json.loads(self.likes) + + def set_likes(self, likes: List[int]) -> None: + """Set the likes c""" + self.likes = list(dict.fromkeys(json.dumps(likes))) + self.save() + + def add_like(self, user_id: int) -> None: + likes: List[int] = self.get_likes() + if user_id not in likes: + likes.append(user_id) + self.set_likes(likes) diff --git a/routes.py b/routes.py index ed234c7..0977258 100644 --- a/routes.py +++ b/routes.py @@ -41,6 +41,7 @@ def user(username: str): user = User.query.filter_by(username=username).first_or_404() return render_template('pages/user.html', user=user) + @blueprint.route('/user//edit') @login_required def profile(): diff --git a/templates/pages/user.html b/templates/pages/user.html index 263ab4b..eb88131 100644 --- a/templates/pages/user.html +++ b/templates/pages/user.html @@ -4,13 +4,19 @@
{{ user.username }} - Online Now! + {% with seen_text = user.get_last_seen() %} + {% if seen_text == 'Online now!' %} + {{ seen_text }} + {% else %} + {{ seen_text }} + {% endif %} + {% endwith %}
{{ user.username }}'s Profile Picture
- Registered 33 days ago
+ Registered {{ user.get_registration_delta() }} ago
67 likes
14 posts