🔶 fix passed time converter function

This commit is contained in:
geoffrey45
2022-04-08 21:01:36 +03:00
parent 6cd982c8ee
commit 6e360fcdac
4 changed files with 73 additions and 10 deletions
+14 -6
View File
@@ -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()),
+2 -2
View File
@@ -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()
-1
View File
@@ -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
+57 -1
View File
@@ -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)