add refresh tokens

This commit is contained in:
Piotr Domański 2023-04-02 15:31:38 +02:00
parent e38949e03a
commit ed004e99a4
3 changed files with 16 additions and 4 deletions

View file

@ -4,8 +4,6 @@ Simple API for food diary application.
## TODO ## TODO
- [ ] Sort meals and entries within meals by date
- [ ] Add refresh tokenization
- [ ] Add access restriction on each endpoint - [ ] Add access restriction on each endpoint
- [ ] Add tests - [ ] Add tests
- [ ] Add default servings - [ ] Add default servings

View file

@ -1,18 +1,20 @@
from sqlalchemy.orm import relationship, Mapped, mapped_column, joinedload from sqlalchemy.orm import relationship, Mapped, mapped_column, joinedload
from sqlalchemy import ForeignKey, Integer, Date from sqlalchemy import ForeignKey, Integer, Date
from sqlalchemy import select from sqlalchemy import select
from sqlalchemy.sql.selectable import Select
from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy.ext.asyncio import AsyncSession
from datetime import date from datetime import date
from typing import Optional from typing import Optional
from .base import Base, CommonMixin from .base import Base, CommonMixin
from .meal import Meal from .meal import Meal
from .entry import Entry
class Diary(Base, CommonMixin): class Diary(Base, CommonMixin):
"""Diary represents user diary for given day""" """Diary represents user diary for given day"""
meals: Mapped[list[Meal]] = relationship(lazy="selectin") meals: Mapped[list[Meal]] = relationship(lazy="selectin", order_by=Meal.order)
date: Mapped[date] = mapped_column(Date) date: Mapped[date] = mapped_column(Date)
user_id: Mapped[int] = mapped_column(Integer, ForeignKey("user.id")) user_id: Mapped[int] = mapped_column(Integer, ForeignKey("user.id"))
@ -48,6 +50,18 @@ class Diary(Base, CommonMixin):
""" """
return sum(meal.fat for meal in self.meals) return sum(meal.fat for meal in self.meals)
@classmethod
def query(cls, user_id: int) -> Select:
"""get_all."""
query = (
select(cls)
.where(cls.user_id == user_id)
.options(
joinedload(cls.meals).joinedload(Meal.entries).joinedload(Entry.product)
)
)
return query
@classmethod @classmethod
async def get_diary( async def get_diary(
cls, session: AsyncSession, user_id: int, date: date cls, session: AsyncSession, user_id: int, date: date

View file

@ -15,7 +15,7 @@ class Meal(Base, CommonMixin):
name: Mapped[str] name: Mapped[str]
order: Mapped[int] order: Mapped[int]
diary_id: Mapped[int] = mapped_column(Integer, ForeignKey("diary.id")) diary_id: Mapped[int] = mapped_column(Integer, ForeignKey("diary.id"))
entries: Mapped[list[Entry]] = relationship(lazy="selectin") entries: Mapped[list[Entry]] = relationship(lazy="selectin", order_by=Entry.last_changed)
@property @property
def calories(self) -> float: def calories(self) -> float: