From 6e360fcdac2adc2692f4e13e8ffabc72579674f6 Mon Sep 17 00:00:00 2001 From: geoffrey45 Date: Fri, 8 Apr 2022 21:01:36 +0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=B6=20fix=20passed=20time=20converter?= =?UTF-8?q?=20function?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/app/api/playlist.py | 20 ++++++++---- server/app/lib/playlistlib.py | 4 +-- server/app/models.py | 1 - server/app/serializer.py | 58 ++++++++++++++++++++++++++++++++++- 4 files changed, 73 insertions(+), 10 deletions(-) diff --git a/server/app/api/playlist.py b/server/app/api/playlist.py index de44580b..8eb99ce3 100644 --- a/server/app/api/playlist.py +++ b/server/app/api/playlist.py @@ -2,6 +2,7 @@ Contains all the playlist routes. """ +from datetime import datetime from flask import Blueprint, request from app import instances, api from app.lib import playlistlib @@ -17,7 +18,14 @@ TrackExistsInPlaylist = exceptions.TrackExistsInPlaylist @playlist_bp.route("/playlists", methods=["GET"]) def get_all_playlists(): - return {"data": [serializer.Playlist(p) for p in api.PLAYLISTS]} + playlists = [ + serializer.Playlist(p, construct_last_updated=False) for p in api.PLAYLISTS + ] + playlists.sort( + key=lambda p: datetime.strptime(p.lastUpdated, "%Y-%m-%d %H:%M:%S"), + reverse=True, + ) + return {"data": playlists} @playlist_bp.route("/playlist/new", methods=["POST"]) @@ -28,7 +36,7 @@ def create_playlist(): "name": data["name"], "description": "", "pre_tracks": [], - "lastUpdated": 0, + "lastUpdated": data["lastUpdated"], "image": "", } @@ -68,7 +76,10 @@ def get_single_p_info(playlistid: str): for p in api.PLAYLISTS: if p.playlistid == playlistid: tracks = p.get_tracks() - return {"info": serializer.Playlist(p), "tracks": tracks} + return { + "info": serializer.Playlist(p), + "tracks": tracks, + } return {"info": {}, "tracks": []} @@ -82,9 +93,6 @@ def update_playlist(playlistid: str): data = request.form - print(type(image)) - print(image.content_type) - playlist = { "name": str(data.get("name")).strip(), "description": str(data.get("description").strip()), diff --git a/server/app/lib/playlistlib.py b/server/app/lib/playlistlib.py index feb502ed..26190028 100644 --- a/server/app/lib/playlistlib.py +++ b/server/app/lib/playlistlib.py @@ -1,8 +1,7 @@ """ This library contains all the functions related to playlists. """ -import base64 -import io + import os import random import string @@ -53,6 +52,7 @@ def create_all_playlists(): _bar = Bar("Creating playlists", max=len(playlists)) for playlist in playlists: api.PLAYLISTS.append(models.Playlist(playlist)) + _bar.next() _bar.finish() diff --git a/server/app/models.py b/server/app/models.py index 72ef86e3..185284a2 100644 --- a/server/app/models.py +++ b/server/app/models.py @@ -3,7 +3,6 @@ Contains all the models for objects generation and typing. """ from dataclasses import dataclass, field -from datetime import date from typing import List from app import api from app import settings diff --git a/server/app/serializer.py b/server/app/serializer.py index 455c93f5..b148b9a9 100644 --- a/server/app/serializer.py +++ b/server/app/serializer.py @@ -1,7 +1,57 @@ from dataclasses import dataclass +from datetime import datetime from app import models +def date_string_to_time_passed(dstring: str) -> str: + """ + Converts a date string to time passed. eg. 2 minutes ago, 1 hour ago, yesterday, 2 days ago, 2 weeks ago, etc. + """ + + now = datetime.now() + then = datetime.strptime(dstring, "%Y-%m-%d %H:%M:%S") + + diff = now - then + days = diff.days + + if days < 0: + return "in the future" + + elif days == 0: + seconds = diff.seconds + if seconds < 15: + return "now" + elif seconds < 60: + return str(seconds) + " seconds ago" + elif seconds < 3600: + return str(seconds // 60) + " minutes ago" + else: + return str(seconds // 3600) + " hours ago" + + elif days == 1: + return "yesterday" + elif days < 7: + if days == 1: + return "1 day ago" + + return str(days) + " days ago" + elif days < 30: + if days == 7: + return "1 week ago" + + return str(days // 7) + " weeks ago" + elif days < 365: + if days == 30: + return "1 month ago" + + return str(days // 30) + " months ago" + elif days > 365: + if days == 365: + return "1 year ago" + + return str(days // 365) + " years ago" + + @dataclass class Playlist: playlistid: str @@ -11,10 +61,16 @@ class Playlist: description: str count: int = 0 - def __init__(self, p: models.Playlist) -> None: + def __init__(self, p: models.Playlist, construct_last_updated: bool = True) -> None: self.playlistid = p.playlistid self.name = p.name self.image = p.image self.lastUpdated = p.lastUpdated self.description = p.description self.count = p.count + + if construct_last_updated: + self.lastUpdated = self.l_updated(p.lastUpdated) + + def l_updated(self, date: str) -> str: + return date_string_to_time_passed(date)