Issue
I created an event calendar in Python using a tutorial I found online. I’ve tried to improve it to schedule events with start and end time ranges that can span months. That means it could start in June and end in July. I partially figured it out… You can display events based on the start date being the lte day with the Python HTMLCalendar module. I don’t know how I’m going to take the rest of July 1st. June is fully visible.
Here’s my code…
HTMLCALENDAR utility..
class Calendar(HTMLCalendar):
def __init__(self, year=None, month=None, dropdown=None):
self.dropdown = dropdown
self.year = year
self.month = month
super(calendar, self).__init__()
# format day as td
# Filter events by day
def formatday(self, day, request):
requests_per_day = requests.filter(start_time__day__lte=day,start_time__month=self.month)
d = ''
For requests_per_day requests:
d += f'{request.get_html_url} '
If days != 0:
return f"{day} {d}
"
returns ' '
# Format week as tr
def formatweek(self, theweek, requests):
week = ''
For d, the day of the week:
week += self.formatday(d, requests)
returns f' {week} '
# format months as a table
# Filter events by year and month
def formatmonth(self, withyear=True):
# requests = VacationRequest.objects.filter(Q(vacation_calendar=self.dropdown,start_time__year=self.year,start_time__month=self.month) | Q(vacation_calendar=self.dropdown,start_time__year=self.year,end_time__month=self.month) ). Clear()
requests = VacationRequest.objects.filter(vacation_calendar=self.dropdown,start_time__year=self.year,start_time__month=self.month)
cal = f'\n'
cal += f'{self.formatmonthname(self.year, self.month, withyear=withyear)}\n'
cal += f'{self.formatweekheader()}\n'
Week of self.monthdays2calendar(self.year, self.month) :
cal += f'{self.formatweek(week, requests)}\n'
cal += f'
\n'
cal += f' Vacation schedule for {self.formatmonthname(self.year, self.month, withyear=withyear)}
\n'
Return value
My view…
class VacationRequestCalendarView(LoginRequiredMixin,generic.ListView):
Model = VacationRequest
template_name = 'Vacation request_calendar_view.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
Dropdown = (self.request.GET.get("Dropdown", None))
d = get_date(self.request.GET.get('month', none))
cal = Calendar(d.year, d.month, dropdown)
cal.setfirstweekday(calendar.SUNDAY)
html_cal = cal.formatmonth(withyear=True)
Vacation_calendar = VacationCalendar.objects.get(id=self.request.GET.get("dropdown", None))
context['calendar'] = mark_safe(html_cal)
context['dropdown'] = dropdown
context['next_month'] = next_month(d)
context['prev_month'] = prev_month(d)
context['vacation_calendar'] = vacation_calendar
return the context
def get_object (self, queryset=None):
return get_object_or_404(VacationCalendar, id=self.request.GET.get("Dropdown"))
def get (self, request, *args, **kwargs):
dropdown=self.request.GET.get("dropdown")
If the dropdown is not None:
If VacationCalendar.objects.filter(id=dropdown,team_members=self.request.user):
self.object_list = self.get_queryset()
context = self.get_context_data(object=self.object_list)
return self.render_to_response(context)
other than that:
Raise Http404
other than that:
messages.add_message(self.request, messages.INFO, 'A vacation calendar is required.')
return HttpResponseRedirect(reverse('VacationRequests:vacation_request_by_calendar_name'))
Default get_date(req_month):
For req_month:
year, month = (int(x) of x in req_month.split('-'))
Return date (date = 1)
returns datetime.today()
def prev_month(d):
first = d.replace(day = 1)
prev_month = first - timedelta(days=1)
month = 'month=' + str(previous month.year) + '-' + str(previous month.month)
return month
Default next_month(d):
days_in_month = calendar.monthrange(d.year, d.month)[1]
last = d.replace (day = number of days in month)
next_month = last + timedelta(day=1)
month = 'month=' + str(next month.year) + '-' + str(next month.month)
return month
June events appear as expected…
But it doesn’t show up on July 1st…
The event ends in July and starts in June…
How can I show this event on July 1st as well?
My latest attempt at this is over…..
events = VacationRequest.objects.filter(Q(vacation_calendar=dropdown,start_time__year = d.year,start_time__month = d.month)
| | Q(vacation_calendar=dropdown,end_time__month = d.month )).distinct().order_by('start_time')
still trying to figure it out…
Solution
requests = VacationRequest.objects.filter(Q(vacation_calendar=self.dropdown,start_time__year=self.year,start_time__month=self.month)
| Q(vacation_calendar=self.dropdown,start_time__year=self.year,end_time__month=self.month)).distinct()
Seemed to do the trick.
Answered By – Steve Smith
Answer Checked By – Laura B. (Easybugfix Admin)