Skip to content

Commit

Permalink
Merge pull request #9 from meirtolpin11/feature/database_refactor
Browse files Browse the repository at this point in the history
Feature/database refactor
  • Loading branch information
meirtolpin11 authored Nov 19, 2023
2 parents 91a4165 + fd4e8c3 commit 0ef3b06
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
10 changes: 6 additions & 4 deletions database.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,19 @@ def cheapest_flights_by_source_dest(
table: Type[WeekendFlights],
scan_timestamp: int,
one_per_city: bool = True,
limit: int = 5,
limit: int = 10,
):
query = table.select().where((table.scan_date == scan_timestamp))
query = table.select(
table, fn.MIN(table.discounted_price).alias("discounted_price")
).where((table.scan_date == scan_timestamp))
if one_per_city:
return (
query.group_by(table.dest, table.source)
.order_by(table.discounted_price)
.order_by(SQL("discounted_price"))
.limit(limit)
)
else:
return query.order_by(table.discounted_price)
return query.order_by(SQL("discounted_price"))


db.create_tables([ConfigToFlightMap, DirectFlight, WeekendFlights, HolidayFlights])
12 changes: 10 additions & 2 deletions telegram/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,21 @@ def generate_report_per_month(month: int, scan_timestamp: int):
month_name = datetime.strptime(str(month), "%m").strftime("%B")

month_query = (
db.WeekendFlights.select()
db.WeekendFlights.select(
db.WeekendFlights.id,
db.WeekendFlights.month,
db.WeekendFlights.source,
db.WeekendFlights.dest,
db.WeekendFlights.outbound_flight,
db.WeekendFlights.scan_date,
db.fn.MIN(db.WeekendFlights.discounted_price).alias("discounted_price"),
)
.where(
(db.WeekendFlights.month == month)
& (db.WeekendFlights.scan_date == scan_timestamp)
)
.group_by(db.WeekendFlights.dest, db.WeekendFlights.source)
.order_by(db.WeekendFlights.discounted_price)
.order_by(db.SQL("discounted_price"))
)

if not month_query.count():
Expand Down
4 changes: 2 additions & 2 deletions telegram/jinja/flight.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@
{% else -%}
{% if link_to -%}
{{ "\t\N{airplane}" }} <a href='{{ link_to }}'>{{ airlines[0] }}</a>,
{% else -%}
{%- else -%}
{{ "\t\N{airplane}" }} {{ airlines[0]}},
{%- endif -%}
{%- if link_from -%}
<a href='{{ link_from }}'>{{ airlines[1] }}</a> {{ "\N{airplane}" }}
{%- else -%}
{{ airlines[1]}}, {{ "\N{airplane}" }}
{{ airlines[1]}} {{ "\N{airplane}" }}
{% endif -%}
{% endif -%}
{% else -%}
Expand Down

0 comments on commit 0ef3b06

Please sign in to comment.