Skip to content

Commit

Permalink
Add export for demo page
Browse files Browse the repository at this point in the history
  • Loading branch information
daya0576 committed Sep 15, 2024
1 parent 113a8cb commit 3661db3
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 1,433 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/fly.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
name: Fly Deploy
on:
push:
tags:
- v**
branches:
- main

jobs:
pre-deploy-test:
Expand Down
28 changes: 19 additions & 9 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
FROM python:3.12-slim
FROM python:3.12-slim AS builder

LABEL maintainer="Henry Zhu <[email protected]>"

COPY requirements.txt .
RUN apt-get update \
&& apt-get install -y --no-install-recommends gcc python3-dev python3-pip libxml2-dev libxslt1-dev zlib1g-dev g++ \
&& pip install -r requirements.txt \
RUN apt-get update && \
DEBIAN_FRONTEND=noninteractive TZ=Etc/UTC \
apt-get install -y --no-install-recommends \
build-essential \
&& rm -rf /var/lib/apt/lists/* \
&& apt-get purge -y --auto-remove gcc python3-dev python3-pip libxml2-dev libxslt1-dev zlib1g-dev g++
&& apt-get clean
RUN python -m pip install --upgrade pip
RUN python -m pip install --upgrade libsass

FROM python:3.12-slim AS release
COPY --from=builder /usr/local/lib/python3.12/site-packages /usr/local/lib/python3.12/site-packages
ARG VERSION

LABEL maintainer="Henry Zhu <[email protected]>"
RUN python -m pip install --upgrade pip

COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt

COPY . .
WORKDIR /app
COPY start.sh .
COPY beaverhabits ./beaverhabits
CMD ["sh", "start.sh", "prd"]
33 changes: 20 additions & 13 deletions beaverhabits/frontend/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,34 @@
from beaverhabits.storage.meta import get_page_title, get_root_path


def menu_component(root_path: str) -> None:
"""Dropdown menu for the top-right corner of the page."""
with ui.menu():
compat_menu("Add", lambda: ui.open(os.path.join(root_path, "add")))
ui.separator()

compat_menu(
"Export",
lambda: ui.open(os.path.join(root_path, "export"), new_tab=True),
)
ui.separator()

compat_menu("Import", lambda: ui.open(os.path.join(root_path, "import")))
ui.separator()

compat_menu("Logout", lambda: user_logout() and ui.open("/login"))


@contextmanager
def layout(title: str | None = None):
"""Base layout for all pages."""
root_path = get_root_path()
title = title or get_page_title(root_path)
with ui.column().classes("max-w-sm mx-auto sm:mx-0"):
with ui.row().classes("min-w-full"):
menu_header(title, target=root_path)

ui.space()

with menu_icon_button(icons.MENU):
with ui.menu():
compat_menu("Add", lambda: ui.open(os.path.join(root_path, "add")))
ui.separator()
compat_menu(
"Export",
lambda: ui.open(
os.path.join(root_path, "export"), new_tab=True
),
)
ui.separator()
compat_menu("Logout", lambda: user_logout() and ui.open("/login"))
menu_component(root_path)

yield
21 changes: 17 additions & 4 deletions beaverhabits/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ async def demo_habit_page(habit_id: str) -> None:
habit_page_ui(today, habit)


@ui.page("/demo/export")
async def demo_export() -> None:
habit_list = views.get_session_habit_list()
if not habit_list:
ui.notify("No habits to export", color="negative")
return
await views.export_user_habit_list(habit_list, "demo")


@ui.page("/gui")
@ui.page("/")
async def index_page(
Expand Down Expand Up @@ -80,11 +89,15 @@ async def gui_habit_page_heatmap(
today = await get_user_today_date()
heatmap_page(today, habit)


@ui.page("/gui/export")
async def export(
user: User = Depends(current_active_user)
) -> None:
await views.export_user_habit_list(user)
async def gui_export(user: User = Depends(current_active_user)) -> None:
habit_list = await views.get_user_habit_list(user)
if not habit_list:
ui.notify("No habits to export", color="negative")
return
await views.export_user_habit_list(habit_list, user.email)


@ui.page("/login")
async def login_page() -> Optional[RedirectResponse]:
Expand Down
6 changes: 3 additions & 3 deletions beaverhabits/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,12 @@ async def get_or_create_user_habit_list(
return habit_list


async def export_user_habit_list(user: User) -> None:
habit_list = await get_user_habit_list(user)
async def export_user_habit_list(habit_list: HabitList, user_identify: str) -> None:
# json to binary
if isinstance(habit_list, DictHabitList):
data = {
"user_email": user.email,
"user_email": user_identify,
"exported_at": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
**habit_list.data,
}
binary_data = json.dumps(data).encode()
Expand Down
Loading

0 comments on commit 3661db3

Please sign in to comment.