Room Calendars

Jeong Ha Seung·2021년 11월 21일

cal.py //파일 이름을 calendar라고 하면 안됨!(python standard library에 있는 calendar와 겹치기 때문)

from django.utils import timezone
import calendar //calendar 모듈 import


class Day:
    def __init__(self, number, past):
        self.number = number
        self.past = past

    def __str__(self):
        return str(self.number)

class Calendar(calendar.Calendar):
    def __init__(self, year, month):
        super().__init__(firstweekday=6) #꼭 호출해줘야 함
        self.year = year
        self.month = month
        self.day_names = ("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun")
        self.months = (
            "January",
            "Feburary",
            "March",
            "April",
            "May",
            "June",
            "July",
            "August",
            "September",
            "October",
            "November",
            "December",
        )

    def get_days(self):
    	weeks = self.monthdays2calendar(self.year, self.month)
   	days = []
   	for week in weeks:
       # print(week) #각각의 week 출력
       	for day, _ in week: #day에만 관심이 있기 때문에 day 다음 변수는 _로 처리
            now = timezone.now()
            today = now.day
            month = now.month
            past = False
            if month == self.month: #현재의 month가 calendar의 month와 같은 경우
                if day <= today: #week에서 불러온 날짜가 오늘 날짜와 동일하면
                    past = True
            new_day = Day(day, past) #number -> day , past -> past(지난 날)
            days.append(new_day)
   	return days


    def get_month(self):
        return self.months[self.month - 1]


new_cal = Calendar(2021, 11)
new_cal.get_days()

room/models.py

from django.utils import timezone
from cal import Calendar
.....

def get_calendars(self):
    now = timezone.now()
    this_year = now.year
    this_month = now.month
    next_month = this_month + 1
    if this_month == 12:
        next_month = 1
    this_month_cal = Calendar(this_year, this_month)
    next_month_cal = Calendar(this_year, next_month)
    return [this_month_cal, next_month_cal]

monthdays2calendar

year의 month 월에 있는 주의 리스트를 전체 week로 반환한다. week는 day 번호와 요일 번호 튜플,7개의 리스트.

profile
블로그 이전했습니다. https://morethan-haseung-log.vercel.app

0개의 댓글