Commit a1fcb106 authored by Jan Ericsson Ong Ang's avatar Jan Ericsson Ong Ang

Updated views.py

parent 511f08de
from django.http import HttpResponse
from django.shortcuts import render, redirect
from django.views.generic import DetailView, CreateView, UpdateView
from .models import Event, Location
# calendar view from .models
def calendarIndex(request):
title = 'Widget’s Calendar of Activities<br><br>'
events = Event.objects.all()
# calendar view for FBV implmentation
def calendar_view(request):
calendars = Event.objects.all().order_by('id')
return render(request, calendar.html, {'calendars', calendars})
output_view = ""
for event in events:
date_and_time = "Date and Time: " + event.target_datetime.strftime("%m/%d/%Y, %I:%M %p") + "<br>"
event_activity = "Activity: " + event.activity + "<br>"
estimated_duration = "Estimated Hours: " + str(event.estimated_hours) + "<br>"
course_section = "Course/Section: " + event.course.code + " " + event.course.title + "-" + event.course.section + "<br>"
event_mode = "Mode: " + event.location.mode + "<br>"
event_venue = "Venue: " + event.location.venue + "<br><br>"
#calendar Detail View for CBV Implementation
class EventDetailView(DetailView):
model = Event
template_name = 'event-details.html'
context_object_name = 'event'
output_view = output_view + date_and_time + event_activity + estimated_duration + course_section + event_mode + event_venue
class EventAddListView(CreateView):
model = Event
form_class = AddEventForm
template_name = "event-add.html"
return HttpResponse(title + output_view)
\ No newline at end of file
def post(self, request):
form = AddEventForm(request.POST)
if form.is_valid():
# onSave is called, the form will have essential data and this includes pk as well.
new_event = form.save()
# pass the pk and redirect.
return redirect('event_details', pk = new_event.pk)
else:
return render(request, 'event-details.html', {'form': form})
# calendar for editing CBV implementation.
class EventEditView(UpdateView):
model = Event
form_class = AddEventForm
template_name = 'event-edit.html'
# This will get the object, and this object is used to tell Django
# that we are updating.
def get_object(self, queryset = None):
pk = self.kwargs.get('pk')
return Event.objects.get(pk=pk)
def post(self, request, pk):
event = self.get_object()
form = AddEventForm(request.POST, instance = event)
if form.is_valid():
# onSave is called, the form will have essential data and this includes pk as well.
update_event = form.save()
# pass the pk and redirect.
return redirect('event_details', pk = update_event.pk)
else:
return render(request, 'event-details.html', {'form': form})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment