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

feat: 7days timetable image #988

Merged
merged 5 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions apps/timetable/services.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from enum import Enum
from typing import List, Optional, Tuple
import datetime
import pytz
Expand All @@ -8,7 +9,7 @@
from django.db.models import F, Case, When

from apps.session.models import UserProfile
from apps.subject.models import Lecture, Semester
from apps.subject.models import ClassTime, Lecture, Semester
from .models import Timetable


Expand All @@ -32,6 +33,10 @@
"#f4badb",
]

class TimetableType(Enum):
FIVE_DAYS = "5days"
SEVEN_DAYS = "7days"


def reorder_timetable(timetable: Timetable, target_arrange_order: int):
related_timetables = Timetable.get_related_timetables(timetable.user,
Expand Down Expand Up @@ -67,6 +72,17 @@ def get_timetable_entries(profile: UserProfile, table_id: int, year: int, semest

return list(table.lectures.filter(deleted=False))

def _get_timetable_type(lectures: List[Lecture]) -> TimetableType:
def _has_weekend():
for lecture in lectures:
classtimes: List[ClassTime] = lecture.classtimes.all()
for classtime in classtimes:
if classtime.day >= 5:
return True

return False

return TimetableType.SEVEN_DAYS if _has_weekend() else TimetableType.FIVE_DAYS

def _draw_rounded_rectangle(draw, points: Tuple[int, int, int, int], radius: int, color):
draw.pieslice([points[0], points[1], points[0] + radius * 2, points[1] + radius * 2],
Expand Down Expand Up @@ -150,7 +166,8 @@ def create_timetable_image(semester: Semester, lectures: List[Lecture], language
else:
file_path = "/var/www/otlplus/static/"

image = Image.open(file_path + "img/Image_template.png")
timetable_type = _get_timetable_type(lectures)
image = Image.open(file_path + f"img/Image_template_{timetable_type.value}.png")
draw = ImageDraw.Draw(image)
text_image = Image.new("RGBA", image.size)
text_draw = ImageDraw.Draw(text_image)
Expand All @@ -160,7 +177,8 @@ def create_timetable_image(semester: Semester, lectures: List[Lecture], language
is_english = language and ("en" in language)

semester_name = semester.get_name(language=language).title()
text_draw.text((952, 78),
semester_name_begin = 952 if timetable_type == TimetableType.FIVE_DAYS else 952 + 350
text_draw.text((semester_name_begin, 78),
semester_name,
fill=(204, 204, 204),
font=semester_font,
Expand Down
Binary file removed static/img/Image_template.png
Binary file not shown.
Binary file added static/img/Image_template_5days.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added static/img/Image_template_7days.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.