From 2e62f0c70af4e6045804a4949ca0f376d22af14f Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Tue, 24 Jul 2018 19:23:19 +0200 Subject: [PATCH] Calendar --- db.py | 4 +- static/nryg.less | 83 ++++++++++++++++++++++++++++++++- telegrambot.py | 22 ++++++++- templates/components/event.html | 20 ++++++++ templates/main.html | 26 +++++++++-- webserver.py | 17 ++++--- 6 files changed, 157 insertions(+), 15 deletions(-) create mode 100644 templates/components/event.html diff --git a/db.py b/db.py index 92b174e3..b8a2639a 100644 --- a/db.py +++ b/db.py @@ -2,7 +2,7 @@ import datetime from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import sessionmaker, relationship 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 +from sqlalchemy import Column, BigInteger, Integer, String, DateTime, ForeignKey, Float, Enum, create_engine, UniqueConstraint, PrimaryKeyConstraint, Boolean, or_, LargeBinary, Text, Date, func import requests from errors import RequestError, NotFoundError, AlreadyExistingError import re @@ -853,7 +853,7 @@ class Event(Base): id = Column(Integer, primary_key=True) author_id = Column(Integer, ForeignKey("royals.id"), nullable=False) - author = relationship("Royal") + author = relationship("Royal", lazy="joined") name = Column(String, nullable=False) description = Column(Text) time = Column(DateTime, nullable=False) diff --git a/static/nryg.less b/static/nryg.less index 8b739974..639ed403 100644 --- a/static/nryg.less +++ b/static/nryg.less @@ -120,6 +120,25 @@ nav { } } +.box { + border-radius: 4px; + background-color: rgba(red(@text-color), green(@text-color), blue(@text-color), 0.1); + padding: 4px; + + + &.upper-box { + border-radius: 4px 4px 0 0; + background-color: rgba(red(@text-color), green(@text-color), blue(@text-color), 0.2); + color: @accent-color; + font-size: small; + font-weight: bold; + } + + &.lower-box { + border-radius: 0 0 4px 4px; + } +} + .input-grid { display: grid; @@ -171,7 +190,7 @@ nav { } } -@media (min-width:600px) { +@media (min-width:601px) { .mobile-only { display: none; } @@ -593,6 +612,68 @@ nav { } } +.main-page { + display: grid; + + @media (min-width:601px) { + //Desktop + .left { + grid-column: 1; + margin: 4px; + } + + .right { + grid-column: 2; + margin: 4px; + } + } + + @media (max-width:600px) { + //Mobile + .left { + grid-row: 1; + margin-top: 4px; + margin-bottom: 4px; + } + + .right { + grid-row: 2; + margin-top: 4px; + margin-bottom: 4px; + } + } +} + +.event { + display: grid; + grid-template-columns: 10% 70% 20%; + padding-top: 4px; + padding-bottom: 4px; + + .time { + grid-column: 1; + grid-row: 1; + font-weight: bold; + } + + .name { + grid-column: 2; + grid-row: 1; + font-weight: bold; + } + + .author { + grid-column: 3; + grid-row: 1; + } + + .description { + grid-row: 2; + grid-column-start: 1; + grid-column-end: 4; + } +} + #edit-css { font-size: medium; } \ No newline at end of file diff --git a/telegrambot.py b/telegrambot.py index 5e5bbe36..a281a498 100644 --- a/telegrambot.py +++ b/telegrambot.py @@ -237,7 +237,8 @@ def cmd_vote(bot: Bot, update: Update): user = session.query(db.Telegram).filter_by(telegram_id=update.message.from_user.id).one_or_none() if user is None: bot.send_message(update.message.chat.id, - "⚠ Il tuo account Telegram non è registrato al RYGdb! Registrati con `/register@royalgamesbot `.") + "⚠ Il tuo account Telegram non è registrato al RYGdb!" + " Registrati con `/register@royalgamesbot `.") return try: _, mode, question = update.message.text.split(" ", 2) @@ -540,6 +541,22 @@ def cmd_newevent(bot: Bot, update: Update): bot.send_message(update.message.chat.id, "✅ Evento aggiunto al Calendario Royal Games!") +def cmd_calendar(bot: Bot, update: Update): + session = db.Session() + next_events = session.query(db.Event).filter(db.Event.time > datetime.datetime.now()).order_by(db.Event.time).all() + session.close() + msg = "📆 Prossimi eventi\n" + for event in next_events: + if event.time_left.days >= 1: + msg += event.time.strftime('%Y-%m-%d %H:%M') + else: + msg += f"{int(event.time_left.total_seconds() // 3600)}h" \ + f" {int((event.time_left.total_seconds() % 3600) // 60)}m" + msg += f" {event.name}\n" + msg += '\nPer ulteriori dettagli, visita Royalnet' + bot.send_message(update.message.chat.id, msg, parse_mode="HTML", disable_web_page_preview=True) + + def process(arg_discord_connection): print("Telegrambot starting...") if arg_discord_connection is not None: @@ -564,11 +581,12 @@ def process(arg_discord_connection): u.dispatcher.add_handler(CommandHandler("bridge", cmd_bridge)) u.dispatcher.add_handler(CommandHandler("wheel", cmd_wheel)) u.dispatcher.add_handler(CommandHandler("newevent", cmd_newevent)) + u.dispatcher.add_handler(CommandHandler("calendar", cmd_calendar)) u.dispatcher.add_handler(CallbackQueryHandler(on_callback_query)) u.bot.send_message(config["Telegram"]["main_group"], f"ℹ Royal Bot avviato e pronto a ricevere comandi!\n" f"Ultimo aggiornamento: `{version}: {commit_msg}`", - parse_mode="Markdown") + parse_mode="Markdown", disable_notification=True) while True: try: u.start_polling() diff --git a/templates/components/event.html b/templates/components/event.html new file mode 100644 index 00000000..4f740621 --- /dev/null +++ b/templates/components/event.html @@ -0,0 +1,20 @@ +
+ {% if event.time_left.days >= 1 %} +
+ {{ event.time.strftime('%Y-%m-%d %H:%M') }} +
+ {% else %} +
+ {{ (event.time_left.total_seconds() // 3600) | int }}h {{ ((event.time_left.total_seconds() % 3600) // 60) | int }}m +
+ {% endif %} +
+ {{ event.name }} +
+ +
+ {{ event.description }} +
+
\ No newline at end of file diff --git a/templates/main.html b/templates/main.html index e8a66b20..0b00f613 100644 --- a/templates/main.html +++ b/templates/main.html @@ -8,9 +8,27 @@

Royal Games

-

- Benvenuto al sito web della Royal Games! Sta lentamente migliorando, ma spero che comunque collaboriate a migliorarlo! -

+
+
+
+ Prossimi eventi +
+
+ {% for event in next_events %} + {% include "/components/event.html" %} + {% endfor %} +
+
+
+
+ Sassi +
+
+ CIAOOOOOO +
+
+
+ {% endblock %} \ No newline at end of file diff --git a/webserver.py b/webserver.py index de69f083..21da25dd 100644 --- a/webserver.py +++ b/webserver.py @@ -52,13 +52,16 @@ def page_500(): @app.route("/") def page_main(): - if fl_session.get("user_id"): - db_session = db.Session() - 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() - db_session.close() - return render_template("main.html", royals=royals, wiki_pages=wiki_pages, config=config) - return redirect(url_for("page_login")) + if not fl_session.get("user_id"): + return redirect(url_for("page_login")) + db_session = db.Session() + 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() + db_session.close() + return render_template("main.html", royals=royals, wiki_pages=wiki_pages, diario=random_diario, + next_events=next_events, config=config, a_day=datetime.timedelta(days=1)) @app.route("/profile/")