Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add border for bookings in the schedule image #28

Open
2 tasks
dantetemplar opened this issue Feb 23, 2024 · 0 comments
Open
2 tasks

Add border for bookings in the schedule image #28

dantetemplar opened this issue Feb 23, 2024 · 0 comments
Assignees

Comments

@dantetemplar
Copy link
Member

dantetemplar commented Feb 23, 2024

Current situation

No borders, so neighboring bookings merge
Image

Implementation

  • Try to use headless html rendering or svg
  • Or just add borders in Pillow

Related code

repository.py:145

    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
        ysize = 32  # length of the rect by x-axis

        # Create a new image using PIL
        image = Image.open("src/repositories/bookings/schedule.jpg")
        draw = ImageDraw.Draw(image)

        lightGray = (211, 211, 211)
        lightBlack = (48, 54, 59)
        red = (255, 0, 0)

        fontSimple = ImageFont.truetype("src/repositories/bookings/open_sans.ttf", size=14)

        bookings = await self.get_bookings_for_week(start_of_week)
        for booking in bookings:
            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)
            participant = await self.get_participant(booking.participant_id)

            alias = participant.alias
            max_alias_length: int = 10
            if len(alias) > max_alias_length:
                alias = f"{alias[:max_alias_length]}..."

            caption = f"{alias} "

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

        today = datetime.date.today()
        weekday = today.weekday()

        current = datetime.datetime.now()
        current_week = False
        if start_of_week <= current.date() <= start_of_week + timedelta(days=6):
            current_week = True

        # Drawing red line
        if 6 < current.hour < 23 and current_week:
            now_x0 = xbase + xsize * weekday
            now_y0 = ybase + int(ysize * ((current.hour - 7) + (current.minute / 60)))
            draw.rounded_rectangle((now_x0, now_y0, now_x0 + xsize, now_y0 + 2), 2, fill=red)

        await self.draw_week_numbers(draw, await self.is_start_of_week(start_of_week))
        await self.draw_month_and_year(draw, current_week)
        image_stream = io.BytesIO()
        image.save(image_stream, format="png")
        val = image_stream.getvalue()
        image_stream.close()
        return val
@dantetemplar dantetemplar changed the title Add a border for bookings in the schedule image Add border for bookings in the schedule image Feb 23, 2024
@thisisfabrics thisisfabrics self-assigned this Sep 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: 🔖 Todo
Development

No branches or pull requests

2 participants