mirror of
https://github.com/hay-kot/mealie.git
synced 2025-08-22 14:33:33 -07:00
group dashboard init
This commit is contained in:
parent
f5a193f3b6
commit
1605c41431
9 changed files with 41 additions and 18 deletions
|
@ -3,7 +3,7 @@ from fastapi import FastAPI
|
|||
from fastapi.logger import logger
|
||||
|
||||
# import utils.startup as startup
|
||||
from core.config import APP_VERSION, PORT, SECRET, docs_url, redoc_url
|
||||
from core.config import APP_VERSION, PORT, docs_url, redoc_url
|
||||
from db.db_setup import sql_exists
|
||||
from db.init_db import init_db
|
||||
from routes import (
|
||||
|
|
|
@ -22,7 +22,7 @@ class Group(SqlAlchemyBase, BaseMixins):
|
|||
mealplans = orm.relationship(
|
||||
"MealPlanModel", back_populates="group", single_parent=True
|
||||
)
|
||||
mealplan_categories = orm.relationship(
|
||||
categories = orm.relationship(
|
||||
"Category", secondary=group2categories, single_parent=True
|
||||
)
|
||||
|
||||
|
@ -47,7 +47,7 @@ class Group(SqlAlchemyBase, BaseMixins):
|
|||
) -> None:
|
||||
self.name = name
|
||||
self.categories = [
|
||||
Category.create_if_not_exist(session=session, name=cat.get("name"))
|
||||
Category.get_ref(session=session, slug=cat.get("slug"))
|
||||
for cat in categories
|
||||
]
|
||||
|
||||
|
@ -58,7 +58,7 @@ class Group(SqlAlchemyBase, BaseMixins):
|
|||
def update(self, session: Session, *args, **kwargs):
|
||||
self._sql_remove_list(session, [WebhookURLModel], self.id)
|
||||
|
||||
self.__init__(*args, **kwargs)
|
||||
self.__init__(session=session, *args, **kwargs)
|
||||
|
||||
@staticmethod
|
||||
def create_if_not_exist(session, name: str = DEFAULT_GROUP):
|
||||
|
|
|
@ -45,6 +45,10 @@ class Category(SqlAlchemyBase):
|
|||
self.name = name.strip()
|
||||
self.slug = slugify(name)
|
||||
|
||||
@staticmethod
|
||||
def get_ref(session, slug: str):
|
||||
return session.query(Category).filter(Category.slug == slug).one()
|
||||
|
||||
@staticmethod
|
||||
def create_if_not_exist(session, name: str = None):
|
||||
test_slug = slugify(name)
|
||||
|
|
|
@ -30,7 +30,7 @@ class SiteSettings(SqlAlchemyBase, BaseMixins):
|
|||
self.cards_per_section = cards_per_section
|
||||
self.show_recent = show_recent
|
||||
self.categories = [
|
||||
Category.create_if_not_exist(session=session, name=cat.get("name"))
|
||||
Category.get_ref(session=session, name=cat.get("slug"))
|
||||
for cat in categories
|
||||
]
|
||||
|
||||
|
|
|
@ -14,7 +14,7 @@ class SiteThemeModel(SqlAlchemyBase):
|
|||
|
||||
def update(self, session=None, name: str = None, colors: dict = None) -> dict:
|
||||
self.colors.update(**colors)
|
||||
return self.dict()
|
||||
return self
|
||||
|
||||
|
||||
class ThemeColorsModel(SqlAlchemyBase):
|
||||
|
|
|
@ -3,7 +3,7 @@ from db.db_setup import generate_session
|
|||
from fastapi import APIRouter, Depends
|
||||
from routes.deps import manager
|
||||
from schema.snackbar import SnackResponse
|
||||
from schema.user import GroupBase, GroupInDB
|
||||
from schema.user import GroupBase, GroupInDB, UpdateGroup, UserInDB
|
||||
from sqlalchemy.orm.session import Session
|
||||
|
||||
router = APIRouter(prefix="/api/groups", tags=["Groups"])
|
||||
|
@ -19,6 +19,17 @@ async def get_all_groups(
|
|||
return db.groups.get_all(session)
|
||||
|
||||
|
||||
@router.get("/self", response_model=GroupInDB)
|
||||
async def get_current_user_group(
|
||||
current_user=Depends(manager),
|
||||
session: Session = Depends(generate_session),
|
||||
):
|
||||
""" Returns the Group Data for the Current User """
|
||||
current_user: UserInDB
|
||||
|
||||
return db.groups.get(session, current_user.group, "name")
|
||||
|
||||
|
||||
@router.post("")
|
||||
async def create_group(
|
||||
group_data: GroupBase,
|
||||
|
@ -27,21 +38,24 @@ async def create_group(
|
|||
):
|
||||
""" Creates a Group in the Database """
|
||||
|
||||
db.groups.create(session, group_data.dict())
|
||||
|
||||
return
|
||||
try:
|
||||
db.groups.create(session, group_data.dict())
|
||||
return SnackResponse.success("User Group Created", {"created": True})
|
||||
except:
|
||||
return SnackResponse.error("User Group Creation Failed")
|
||||
|
||||
|
||||
@router.put("/{id}")
|
||||
async def update_group_data(
|
||||
id: int,
|
||||
group_data: GroupInDB,
|
||||
group_data: UpdateGroup,
|
||||
current_user=Depends(manager),
|
||||
session: Session = Depends(generate_session),
|
||||
):
|
||||
""" Updates a User Group """
|
||||
db.groups.update(session, id, group_data.dict())
|
||||
|
||||
return db.groups.update(session, id, group_data.dict())
|
||||
return SnackResponse.success("Group Settings Updated")
|
||||
|
||||
|
||||
@router.delete("/{id}")
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
import shutil
|
||||
from datetime import timedelta
|
||||
from os import access
|
||||
|
||||
from core.config import USER_DIR
|
||||
from core.security import get_password_hash, verify_password
|
||||
|
@ -65,6 +66,7 @@ async def update_user(
|
|||
session: Session = Depends(generate_session),
|
||||
):
|
||||
|
||||
access_token = None
|
||||
if current_user.id == id or current_user.admin:
|
||||
updated_user: UserInDB = db.users.update(session, id, new_data.dict())
|
||||
email = updated_user.email
|
||||
|
|
|
@ -73,16 +73,19 @@ class UserInDB(UserOut):
|
|||
orm_mode = True
|
||||
|
||||
|
||||
class GroupInDB(GroupBase):
|
||||
class UpdateGroup(GroupBase):
|
||||
id: int
|
||||
name: str
|
||||
users: Optional[list[UserOut]]
|
||||
mealplans: Optional[list[MealPlanInDB]]
|
||||
categories: Optional[list[CategoryBase]] = []
|
||||
|
||||
webhook_urls: list[str] = []
|
||||
webhook_time: str = "00:00"
|
||||
webhook_enable: bool = False
|
||||
webhook_enable: bool
|
||||
|
||||
|
||||
class GroupInDB(UpdateGroup):
|
||||
users: Optional[list[UserOut]]
|
||||
mealplans: Optional[list[MealPlanInDB]]
|
||||
|
||||
class Config:
|
||||
orm_mode = True
|
||||
|
|
|
@ -70,13 +70,13 @@ class ExportDatabase:
|
|||
if self.templates:
|
||||
self._export_template(recipe)
|
||||
|
||||
def _export_template(self, recipe_data: dict):
|
||||
def _export_template(self, recipe_data: Recipe):
|
||||
for template_path in self.templates:
|
||||
|
||||
with open(template_path, "r") as f:
|
||||
template = Template(f.read())
|
||||
|
||||
filename = recipe_data.get("name") + template_path.suffix
|
||||
filename = recipe_data.name + template_path.suffix
|
||||
out_file = self.templates_dir.joinpath(filename)
|
||||
|
||||
content = template.render(recipe=recipe_data)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue