diff --git a/db.py b/db.py index b8a2639a..cce9f755 100644 --- a/db.py +++ b/db.py @@ -1,8 +1,8 @@ import datetime from sqlalchemy.ext.declarative import declarative_base -from sqlalchemy.orm import sessionmaker, relationship +from sqlalchemy.orm import sessionmaker, relationship, joinedload from sqlalchemy.ext.hybrid import hybrid_property -from sqlalchemy import Column, BigInteger, Integer, String, DateTime, ForeignKey, Float, Enum, create_engine, UniqueConstraint, PrimaryKeyConstraint, Boolean, or_, LargeBinary, Text, Date, func +from sqlalchemy import Column, BigInteger, Integer, String, DateTime, ForeignKey, Float, Enum, create_engine, UniqueConstraint, PrimaryKeyConstraint, Boolean, or_, LargeBinary, Text, Date, func, desc import requests from errors import RequestError, NotFoundError, AlreadyExistingError import re @@ -741,6 +741,7 @@ class PlayedMusic(Base): enqueuer_id = Column(BigInteger, ForeignKey("discord.discord_id")) enqueuer = relationship("Discord", backref="music_played", lazy="joined") filename = Column(String) + timestamp = Column(DateTime, nullable=False) def __repr__(self): return f"" diff --git a/discordbot.py b/discordbot.py index 2812ca7a..b8d05c6e 100644 --- a/discordbot.py +++ b/discordbot.py @@ -18,6 +18,7 @@ import async_timeout import raven import logging import errors +import datetime logging.basicConfig() @@ -555,7 +556,8 @@ async def queue_play_next_video(): if now_playing.enqueuer is not None: session = await loop.run_in_executor(executor, db.Session) played_music = db.PlayedMusic(enqueuer=now_playing.enqueuer, - filename=str(now_playing)) + filename=str(now_playing), + timestamp=datetime.datetime.now()) session.add(played_music) await loop.run_in_executor(executor, session.commit) await loop.run_in_executor(executor, session.close) diff --git a/static/nryg.less b/static/nryg.less index 5b87b4a0..71aba555 100644 --- a/static/nryg.less +++ b/static/nryg.less @@ -614,7 +614,6 @@ nav { padding: 18px; color: white; font-family: "Whitney", sans-serif; - opacity: 0.8; a, a:hover, a:active, a:visited { color: white; @@ -624,8 +623,16 @@ nav { border-radius: 16px; } - .useless-stuff { - grid-column: 1; + .last-video { + grid-column: 1 + } + + .fav-video { + grid-column: 2; + } + + .none { + opacity: 0.3; } } diff --git a/templates/minis/discord.html b/templates/minis/discord.html index e03643b9..f40339c8 100644 --- a/templates/minis/discord.html +++ b/templates/minis/discord.html @@ -7,9 +7,26 @@
- - {{ record }} + + {{ record[0] }}
-
รจ su Discord!
+ {% if record[0].music_played %} +
Ultima canzone
+
+ {{ (record[0].music_played|sort(attribute='timestamp'))[-1].filename }} +
+ {% else %} +
Ultima canzone
+
+ Nessuna +
+ {% endif %} + {% if record|length > 1 %} +
Canzone preferita
+
{{ record[1] }}
+ {% else %} +
Canzone preferita
+ + {% endif %}
diff --git a/webserver.py b/webserver.py index 2b798734..ec4cb4a5 100644 --- a/webserver.py +++ b/webserver.py @@ -58,7 +58,8 @@ def page_main(): royals = db_session.query(db.Royal).order_by(db.Royal.username).all() wiki_pages = db_session.query(db.WikiEntry).order_by(db.WikiEntry.key).all() random_diario = db_session.query(db.Diario).order_by(db.func.random()).first() - next_events = db_session.query(db.Event).filter(db.Event.time > datetime.datetime.now()).order_by(db.Event.time).all() + next_events = db_session.query(db.Event).filter(db.Event.time > datetime.datetime.now()).order_by( + db.Event.time).all() db_session.close() return render_template("main.html", royals=royals, wiki_pages=wiki_pages, entry=random_diario, next_events=next_events, config=config, escape=escape) @@ -80,7 +81,15 @@ def page_profile(name: str): lol = db_session.query(db.LeagueOfLegends).filter_by(royal=user).one_or_none() ow = db_session.query(db.Overwatch).filter_by(royal=user).one_or_none() tg = db_session.query(db.Telegram).filter_by(royal=user).one_or_none() - discord = db_session.query(db.Discord).filter_by(royal=user).one_or_none() + fav_song = db_session.query(db.PlayedMusic.enqueuer_id, db.PlayedMusic.filename, db.func.count("*").label("plays")) \ + .group_by(db.PlayedMusic.filename, db.PlayedMusic.enqueuer_id) \ + .order_by(db.desc("plays")) \ + .subquery() + discord = db_session.query(db.Discord, db.PlayedMusic.filename) \ + .options(db.joinedload(db.Discord.music_played)) \ + .filter_by(royal=user) \ + .outerjoin(fav_song) \ + .first() db_session.close() return render_template("profile.html", ryg=user, css=css, osu=osu, rl=rl, dota=dota, lol=lol, steam=steam, ow=ow, tg=tg, discord=discord, config=config) @@ -174,6 +183,8 @@ def page_setcss(): @app.route("/game/") def page_game(name: str): db_session = db.Session() + fav_song = None + last_song = None if name == "rl": game_name = "Rocket League" query = db_session.query(db.RocketLeague).join(db.Steam).all() @@ -200,8 +211,10 @@ def page_game(name: str): query = db_session.query(db.Telegram).all() elif name == "discord": game_name = "Discord" - # noinspection PyComparisonWithNone - query = db_session.query(db.Discord).filter(db.Discord.royal_id != None).all() + partial_query = db_session.query(db.Discord) \ + .options(db.joinedload(db.Discord.music_played)) \ + .all() + query = [[discord] for discord in partial_query] else: abort(404) return @@ -215,12 +228,12 @@ def page_wiki(key: str): wiki_page = db_session.query(db.WikiEntry).filter_by(key=key).one_or_none() if request.method == "GET": wiki_latest_edit = db_session.query(db.WikiLog).filter_by(edited_key=key) \ - .order_by(db.WikiLog.timestamp.desc()).first() + .order_by(db.WikiLog.timestamp.desc()).first() db_session.close() if wiki_page is None: return render_template("wiki.html", key=key, config=config) converted_md = Markup(markdown2.markdown(wiki_page.content.replace("<", "<"), - extras=["spoiler", "tables"])) + extras=["spoiler", "tables"])) return render_template("wiki.html", key=key, wiki_page=wiki_page, converted_md=converted_md, wiki_log=wiki_latest_edit, config=config) elif request.method == "POST":