feat: Added notes creation, view and deletion, added route planning, added location list with current weather and nearby locations, code improvements and fixes

This commit is contained in:
ITQ
2024-03-26 07:49:50 +03:00
parent 65719a61ef
commit 88dfe1704d
24 changed files with 1571 additions and 301 deletions
+75 -10
View File
@@ -14,8 +14,16 @@ from app.utils.geo import get_location_by_name
association_table = sa.Table(
"user_travel_association",
Base.metadata,
sa.Column("user_id", sa.BigInteger, sa.ForeignKey("users.telegram_id")),
sa.Column("travel_id", sa.Integer, sa.ForeignKey("travels.id")),
sa.Column(
"user_id",
sa.BigInteger,
sa.ForeignKey("users.telegram_id", ondelete="CASCADE"),
),
sa.Column(
"travel_id",
sa.Integer,
sa.ForeignKey("travels.id", ondelete="CASCADE"),
),
)
@@ -42,7 +50,7 @@ class Travel(Base):
author_id = sa.Column(
sa.BigInteger,
sa.ForeignKey(User.telegram_id),
sa.ForeignKey(User.telegram_id, ondelete="CASCADE"),
nullable=False,
)
@@ -83,6 +91,18 @@ class Travel(Base):
),
)
@classmethod
def get_notes(cls, author_id, travel, public=True):
return (
session.query(Note)
.filter(
Note.author_id == author_id,
Note.travel_id == travel.id,
Note.public == public,
)
.all()
)
@classmethod
def get_travel_by_id(cls, travel_id):
return session.query(Travel).filter(Travel.id == travel_id).first()
@@ -91,6 +111,14 @@ class Travel(Base):
def get_travel_queryset_by_id(cls, travel_id):
return session.query(Travel).filter(Travel.id == travel_id)
@classmethod
def get_sorted_locations(cls, travel, asc=True):
return sorted(
travel.locations,
key=lambda location: location.date_end,
reverse=asc,
)
class Location(Base):
__tablename__ = "locations"
@@ -114,17 +142,16 @@ class Location(Base):
travel_id = sa.Column(
sa.Integer,
sa.ForeignKey("travels.id"),
sa.ForeignKey("travels.id", ondelete="CASCADE"),
nullable=False,
)
@validates("location")
def validate_location(self, key, value):
geocoder = get_location_by_name(value)
geocode = get_location_by_name(value)
assert geocoder[0], "Invalid location."
assert geocode[0], "Invalid location."
return geocoder[1].raw["display_name"]
return geocode[1].raw["display_name"]
def validate_date_start(self, key, value):
try:
@@ -158,6 +185,29 @@ class Location(Base):
return value_datetime
def get_location_text(self):
return messages.LOCATION_DETAIL.format(
location=self.location,
date_start=datetime.datetime.strftime(
self.date_start,
"%Y-%m-%d %H:%M",
),
date_end=datetime.datetime.strftime(
self.date_end,
"%Y-%m-%d %H:%M",
),
)
@classmethod
def get_location_by_id(cls, location_id):
return (
session.query(Location).filter(Location.id == location_id).first()
)
@classmethod
def get_location_queryset_by_id(cls, location_id):
return session.query(Location).filter(Location.id == location_id)
class Note(Base):
__tablename__ = "notes"
@@ -181,11 +231,26 @@ class Note(Base):
author_id = sa.Column(
sa.BigInteger,
sa.ForeignKey(User.telegram_id),
sa.ForeignKey(User.telegram_id, ondelete="CASCADE"),
nullable=False,
)
travel_id = sa.Column(
sa.Integer,
sa.ForeignKey("travels.id"),
sa.ForeignKey("travels.id", ondelete="CASCADE"),
nullable=False,
)
def get_note_text(self):
return messages.NOTE_DETAIL.format(
file_name=self.file_name,
file_type=self.file_type,
public="Yes" if self.public else "No",
)
@classmethod
def get_note_by_id(cls, note_id):
return session.query(Note).filter(Note.id == note_id).first()
@classmethod
def get_note_queryset_by_id(cls, note_id):
return session.query(Note).filter(Note.id == note_id)