Skip to content

Commit

Permalink
fix: time out of borders
Browse files Browse the repository at this point in the history
  • Loading branch information
thisisfabrics committed Sep 29, 2024
1 parent fd73612 commit dac461f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 15 deletions.
58 changes: 43 additions & 15 deletions src/repositories/bookings/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
from src.schemas import CreateBooking, ViewBooking, ViewUser
from src.storage.sql import AbstractSQLAlchemyStorage
from src.storage.sql.models import Booking, User
from src.tools import count_duration
from src.tools.utils import get_week_numbers


Expand Down Expand Up @@ -135,7 +134,7 @@ async def is_start_of_week(self, start_of_week: datetime.date):
async def form_schedule(self, start_of_week: datetime.date) -> bytes:
xbase = 48 # origin for x
ybase = 73 # origin for y
xsize = 175.5 # length of the rect by x-axis
xsize = round(175.5) # length of the rect by x-axis
ysize = 32 # length of the rect by x-axis

# Create a new image using PIL
Expand All @@ -150,31 +149,60 @@ async def form_schedule(self, start_of_week: datetime.date) -> bytes:

bookings = await self.get_bookings_for_week(start_of_week)
for booking in bookings:
cell_canvas = Image.new("RGB", (xsize, ysize), "white")
cell_canvas_painter = ImageDraw.Draw(cell_canvas)
day = booking.time_start.weekday()

ylength = count_duration(booking.time_start, booking.time_end)
x0 = xbase + xsize * day
y0 = ybase + int(ysize * ((booking.time_start.hour - 7) + (booking.time_start.minute / 60.0)))
x1 = x0 + xsize
y1 = y0 + 31.5 * ylength

draw.rounded_rectangle((x0, y0, x1, y1), 2, fill=lightGray, outline=lightBlack, width=2)
cell_canvas_painter.rounded_rectangle(
(0, 0, cell_canvas.width - 1, cell_canvas.height - 1), 2, fill=lightGray, outline=lightBlack, width=2
)
user = await self.get_user(booking.user_id)
caption = user.alias

alias = user.alias
max_alias_length: int = 10
if len(alias) > max_alias_length:
alias = f"{alias[:max_alias_length]}..."
# noinspection SqlAlchemyUnsafeQuery
cell_canvas_painter.text(
(8, cell_canvas.height / 2 - 9),
text=caption,
fill=lightBlack,
font=fontSimple,
)

caption = f"{alias} "
time_canvas = Image.new("RGBA", (xsize, ysize), (0, 0, 0, 0))
time_canvas_painter = ImageDraw.Draw(time_canvas)

# noinspection SqlAlchemyUnsafeQuery
draw.text(
(x0 + 8, (y0 + y1) / 2 - 9),
text=f"{caption}{booking.time_start.strftime('%H:%M')}-{booking.time_end.strftime('%H:%M')}",
time_canvas_painter.text(
(9, 0),
text=f"{booking.time_start.strftime('%H:%M')}-{booking.time_end.strftime('%H:%M')}",
fill=lightBlack,
font=fontSimple,
)
time_canvas_painter.rectangle((0, 0, 9, ysize - 10), lightGray)

time_canvas_rect = time_canvas.getbbox()
cell_canvas_painter.rectangle(
(
cell_canvas.width - time_canvas_rect[2] + time_canvas_rect[0] - 9,
2,
cell_canvas.width
- time_canvas_rect[2]
+ time_canvas_rect[0]
- 9
+ time_canvas_rect[2]
- time_canvas_rect[0],
ysize - 3,
),
lightGray,
)
cell_canvas.paste(
time_canvas,
(cell_canvas.width - time_canvas_rect[2] + time_canvas_rect[0] - 9, cell_canvas.height // 2 - 9),
time_canvas,
)

image.paste(cell_canvas, (int(x0), y0))

today = datetime.date.today()
weekday = today.weekday()
Expand Down
Binary file modified src/repositories/bookings/schedule.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit dac461f

Please sign in to comment.