diff --git a/vms/shift/models.py b/vms/shift/models.py index 29cf54d..dff0ff3 100644 --- a/vms/shift/models.py +++ b/vms/shift/models.py @@ -1,8 +1,8 @@ from django.core.validators import ( - MaxValueValidator, - MinValueValidator, - RegexValidator - ) + MaxValueValidator, + MinValueValidator, + RegexValidator +) from django.db import models from job.models import Job @@ -11,6 +11,7 @@ class Shift(models.Model): + id = models.AutoField(primary_key=True) date = models.DateField() start_time = models.TimeField() end_time = models.TimeField() diff --git a/vms/shift/templates/shift/delete.html b/vms/shift/templates/shift/delete.html index 68373db..b22cb67 100644 --- a/vms/shift/templates/shift/delete.html +++ b/vms/shift/templates/shift/delete.html @@ -4,7 +4,7 @@ {% block setting_content %}
-
+ {% csrf_token %}
diff --git a/vms/shift/urls.py b/vms/shift/urls.py index 79f597d..bd9ffc3 100644 --- a/vms/shift/urls.py +++ b/vms/shift/urls.py @@ -1,24 +1,25 @@ from django.conf.urls import patterns, url from shift import views +from shift.views import * urlpatterns = patterns('', - url(r'^add_hours/(?P\d+)/(?P\d+)$', views.add_hours, name='add_hours'), - url(r'^add_hours_manager/(?P\d+)/(?P\d+)$', views.add_hours_manager, name='add_hours_manager'), - url(r'^create/(?P\d+)$', views.create, name='create'), + url(r'^add_hours/(?P\d+)/(?P\d+)$', AddHoursView.as_view(), name='add_hours'), + url(r'^add_hours_manager/(?P\d+)/(?P\d+)$', AddHoursManagerView.as_view(), name='add_hours_manager'), + url(r'^create/(?P\d+)$', ShiftCreateView.as_view(), name='create'), url(r'^cancel/(?P\d+)/(?P\d+)$', views.cancel, name='cancel'), - url(r'^delete/(?P\d+)$', views.delete, name='delete'), - url(r'^clear_hours/(?P\d+)/(?P\d+)$', views.clear_hours, name='clear_hours'), - url(r'^clear_hours_manager/(?P\d+)/(?P\d+)$', views.clear_hours_manager, name='clear_hours_manager'), - url(r'^edit/(?P\d+)$', views.edit, name='edit'), - url(r'^edit_hours/(?P\d+)/(?P\d+)$', views.edit_hours, name='edit_hours'), - url(r'^edit_hours_manager/(?P\d+)/(?P\d+)$', views.edit_hours_manager, name='edit_hours_manager'), - url(r'^list_jobs/$', views.list_jobs, name='list_jobs'), - url(r'^list_shifts/(?P\d+)$', views.list_shifts, name='list_shifts'), + url(r'^delete/(?P\d+)$', ShiftDeleteView.as_view(), name='delete'), + url(r'^clear_hours/(?P\d+)/(?P\d+)$', ClearHoursView.as_view(), name='clear_hours'), + url(r'^clear_hours_manager/(?P\d+)/(?P\d+)$', ClearHoursManager.as_view(), name='clear_hours_manager'), + url(r'^edit/(?P\d+)$', ShiftUpdateView.as_view(), name='edit'), + url(r'^edit_hours/(?P\d+)/(?P\d+)$', EditHoursView.as_view(), name='edit_hours'), + url(r'^edit_hours_manager/(?P\d+)/(?P\d+)$', EditHoursManagerView.as_view(), name='edit_hours_manager'), + url(r'^list_jobs/$', JobListView.as_view(), name='list_jobs'), + url(r'^list_shifts/(?P\d+)$', ShiftListView.as_view(), name='list_shifts'), url(r'^view_shift/(?P\d+)$', views.view_volunteers, name='view_volunteers'), url(r'^list_shifts_sign_up/(?P\d+)/(?P\d+)$', views.list_shifts_sign_up, name='list_shifts_sign_up'), - url(r'^manage_volunteer_shifts/(?P\d+)$', views.manage_volunteer_shifts, name='manage_volunteer_shifts'), + url(r'^manage_volunteer_shifts/(?P\d+)$', ManageVolunteerShiftView.as_view(), name='manage_volunteer_shifts'), url(r'^sign_up/(?P\d+)/(?P\d+)$', views.sign_up, name='sign_up'), - url(r'^view_hours/(?P\d+)$', views.view_hours, name='view_hours'), + url(r'^view_hours/(?P\d+)$', ViewHoursView.as_view(), name='view_hours'), url(r'^view_volunteer_shifts/(?P\d+)$', views.view_volunteer_shifts, name='view_volunteer_shifts'), - url(r'^volunteer_search/$', views.volunteer_search, name='volunteer_search'), + url(r'^volunteer_search/$', VolunteerSearchView.as_view(), name='volunteer_search'), ) diff --git a/vms/shift/views.py b/vms/shift/views.py index 7df9c23..479dadd 100644 --- a/vms/shift/views.py +++ b/vms/shift/views.py @@ -5,144 +5,140 @@ from django.shortcuts import render from datetime import date from job.services import * +from django.views.generic import TemplateView +from braces.views import LoginRequiredMixin, AnonymousRequiredMixin +from django.views.generic.edit import FormView, UpdateView +from django.views.generic import DeleteView from shift.forms import HoursForm, ShiftForm from shift.models import Shift from shift.services import * from volunteer.forms import SearchVolunteerForm from volunteer.services import get_all_volunteers, search_volunteers from django.contrib import messages +from django.views.generic import ListView +from django.utils.decorators import method_decorator +from django.core.urlresolvers import reverse_lazy -@login_required -def add_hours(request, shift_id, volunteer_id): - if shift_id and volunteer_id: + +class AdministratorLoginRequiredMixin(object): + + @method_decorator(login_required) + def dispatch(self, request, *args, **kwargs): user = request.user + admin = None + try: + admin = user.administrator + except ObjectDoesNotExist: + pass + if not admin: + return render(request, 'vms/no_admin_rights.html') + else: + return super(AdministratorLoginRequiredMixin, self).dispatch(request, *args, **kwargs) - if int(user.volunteer.id) == int(volunteer_id): - shift = None - if shift_id: - shift = get_shift_by_id(shift_id) - if request.method == 'POST': - form = HoursForm(request.POST) - if form.is_valid(): - start_time = form.cleaned_data['start_time'] - end_time = form.cleaned_data['end_time'] - shift_start_time=shift.start_time - shift_end_time=shift.end_time - try: - if(end_time>start_time): - if(start_time >= shift_start_time and end_time<=shift_end_time): - add_shift_hours( - volunteer_id, - shift_id, - start_time, - end_time - ) - return HttpResponseRedirect(reverse( - 'shift:view_hours', - args=(volunteer_id,) - )) - else: - messages.add_message(request, messages.INFO, 'Logged hours should be between shift hours') - return render( - request, - 'shift/add_hours.html', - {'form': form, 'shift_id': shift_id, 'volunteer_id': volunteer_id, } - ) - else: - messages.add_message(request, messages.INFO, 'End time should be greater than start time') - return render( - request, - 'shift/add_hours.html', - {'form': form, 'shift_id': shift_id, 'volunteer_id': volunteer_id, } - ) - except: - raise Http404 + +class AddHoursView(LoginRequiredMixin, FormView): + template_name = 'shift/add_hours.html' + form_class = HoursForm + + def get_context_data(self, **kwargs): + context = super(AddHoursView, self).get_context_data(**kwargs) + shift_id = self.kwargs['shift_id'] + volunteer_id = self.kwargs['volunteer_id'] + context['volunteer_id'] = volunteer_id + context['shift_id'] = shift_id + context['shift'] = get_shift_by_id(shift_id) + return context + + def form_valid(self, form): + shift_id = self.kwargs['shift_id'] + volunteer_id = self.kwargs['volunteer_id'] + shift = get_shift_by_id(shift_id) + start_time = form.cleaned_data['start_time'] + end_time = form.cleaned_data['end_time'] + shift_start_time = shift.start_time + shift_end_time = shift.end_time + try: + if (end_time > start_time): + if (start_time >= shift_start_time and end_time <= shift_end_time): + add_shift_hours( + volunteer_id, + shift_id, + start_time, + end_time + ) + return HttpResponseRedirect(reverse( + 'shift:view_hours', + args=(volunteer_id,) + )) else: + messages.add_message(self.request, messages.INFO, 'Logged hours should be between shift hours') return render( - request, + self.request, 'shift/add_hours.html', - {'form': form, 'shift_id': shift_id, 'volunteer_id': volunteer_id, } - ) + {'form': form, 'shift_id': shift_id, 'volunteer_id': volunteer_id,} + ) else: - form = HoursForm() + messages.add_message(self.request, messages.INFO, 'End time should be greater than start time') return render( - request, + self.request, 'shift/add_hours.html', - {'form': form, 'shift_id': shift_id, 'volunteer_id': volunteer_id, } - ) - else: - return HttpResponse(status=403) - else: - raise Http404 + {'form': form, 'shift_id': shift_id, 'volunteer_id': volunteer_id,} + ) + except: + raise Http404 -def add_hours_manager(request, shift_id, volunteer_id): - user = request.user - admin = None +class AddHoursManagerView(AdministratorLoginRequiredMixin, FormView): + template_name = 'shift/add_hours_manager.html' + form_class = HoursForm - try: - admin = user.administrator - except ObjectDoesNotExist: - pass + def get_context_data(self, **kwargs): + context = super(AddHoursManagerView, self).get_context_data(**kwargs) + shift_id = self.kwargs['shift_id'] + volunteer_id = self.kwargs['volunteer_id'] + context['volunteer_id'] = volunteer_id + context['shift_id'] = shift_id + context['shift'] = get_shift_by_id(shift_id) + return context - # check that an admin is logged in - if not admin: - return render(request, 'vms/no_admin_rights.html') - else: - shift = None - if shift_id: - shift = get_shift_by_id(shift_id) - - if request.method == 'POST': - form = HoursForm(request.POST) - if form.is_valid(): - start_time = form.cleaned_data['start_time'] - end_time = form.cleaned_data['end_time'] - shift_start_time=shift.start_time - shift_end_time=shift.end_time - try: - if(end_time>start_time): - if(start_time >= shift_start_time and end_time<=shift_end_time): - add_shift_hours( - volunteer_id, - shift_id, - start_time, - end_time - ) - return HttpResponseRedirect(reverse( - 'shift:manage_volunteer_shifts', - args=(volunteer_id, ) - )) - else: - messages.add_message(request, messages.INFO, 'Logged hours should be between shift hours') - return render( - request, - 'shift/add_hours_manager.html', - {'form': form, 'shift_id': shift_id, 'volunteer_id': volunteer_id, } - ) + def form_valid(self, form): + shift_id = self.kwargs['shift_id'] + volunteer_id = self.kwargs['volunteer_id'] + shift = get_shift_by_id(shift_id) + start_time = form.cleaned_data['start_time'] + end_time = form.cleaned_data['end_time'] + shift_start_time = shift.start_time + shift_end_time = shift.end_time + try: + if (end_time > start_time): + if (start_time >= shift_start_time and end_time <= shift_end_time): + add_shift_hours( + volunteer_id, + shift_id, + start_time, + end_time + ) + return HttpResponseRedirect(reverse( + 'shift:manage_volunteer_shifts', + args=(volunteer_id,) + )) + else: + messages.add_message(self.request, messages.INFO, 'Logged hours should be between shift hours') + return render( + self.request, + 'shift/add_hours_manager.html', + {'form': form, 'shift_id': shift_id, 'volunteer_id': volunteer_id,} + ) - else: - messages.add_message(request, messages.INFO, 'End time should be greater than start time') - return render( - request, - 'shift/add_hours_manager.html', - {'form': form, 'shift_id': shift_id, 'volunteer_id': volunteer_id, } - ) - except: - raise Http404 else: + messages.add_message(self.request, messages.INFO, 'End time should be greater than start time') return render( - request, + self.request, 'shift/add_hours_manager.html', - {'form': form, 'shift_id': shift_id, 'volunteer_id': volunteer_id, } - ) - else: - form = HoursForm() - return render( - request, - 'shift/add_hours_manager.html', - {'form': form, 'shift_id': shift_id, 'volunteer_id': volunteer_id, } + {'form': form, 'shift_id': shift_id, 'volunteer_id': volunteer_id,} ) + except: + raise Http404 @login_required @@ -199,401 +195,285 @@ def cancel(request, shift_id, volunteer_id): raise Http404 -@login_required -def clear_hours(request, shift_id, volunteer_id): - - if shift_id and volunteer_id: - if request.method == 'POST': - result = clear_shift_hours(volunteer_id, shift_id) - if result: - return HttpResponseRedirect(reverse( - 'shift:view_hours', - args=(volunteer_id, ) - )) - else: - raise Http404 +class ClearHoursView(LoginRequiredMixin, TemplateView): + template_name = 'shift/clear_hours.html' + success_url = reverse_lazy('shift:view_hours') + + def get_context_data(self, **kwargs): + context = super(ClearHoursView, self).get_context_data(**kwargs) + shift_id = self.kwargs['shift_id'] + volunteer_id = self.kwargs['volunteer_id'] + context['volunteer_id'] = volunteer_id + context['shift_id'] = shift_id + context['result'] = clear_shift_hours(volunteer_id, shift_id) + return context + + def post(self, request, *args, **kwargs): + volunteer_id = self.kwargs['volunteer_id'] + shift_id = self.kwargs['shift_id'] + result = clear_shift_hours(volunteer_id, shift_id) + if result: + return HttpResponseRedirect(reverse( + 'shift:view_hours', + args=(volunteer_id,) + )) else: - return render(request, 'shift/clear_hours.html') - else: - raise Http404 - + raise Http404 -def clear_hours_manager(request, shift_id, volunteer_id): - user = request.user - admin = None - try: - admin = user.administrator - except ObjectDoesNotExist: - pass - - # check that an admin is logged in - if not admin: - return render(request, 'vms/no_admin_rights.html') - else: - if shift_id and volunteer_id: - if request.method == 'POST': - result = clear_shift_hours(volunteer_id, shift_id) - if result: - return HttpResponseRedirect(reverse( - 'shift:manage_volunteer_shifts', - args=(volunteer_id, ) - )) - else: - raise Http404 - else: - return render(request, 'shift/clear_hours.html') +class ClearHoursManager(AdministratorLoginRequiredMixin, TemplateView): + template_name = 'shift/clear_hours.html' + + def get_context_data(self, **kwargs): + context = super(ClearHoursManager, self).get_context_data(**kwargs) + shift_id = self.kwargs['shift_id'] + volunteer_id = self.kwargs['volunteer_id'] + context['volunteer_id'] = volunteer_id + context['shift_id'] = shift_id + context['result'] = clear_shift_hours(volunteer_id, shift_id) + return context + + def post(self, request, *args, **kwargs): + volunteer_id = self.kwargs['volunteer_id'] + shift_id = self.kwargs['shift_id'] + result = clear_shift_hours(volunteer_id, shift_id) + if result: + return HttpResponseRedirect(reverse( + 'shift:manage_volunteer_shifts', + args=(volunteer_id,) + )) else: raise Http404 -@login_required -def create(request, job_id): - user = request.user - admin = None +class ShiftCreateView(AdministratorLoginRequiredMixin, FormView): + template_name = 'shift/create.html' + form_class = ShiftForm + success_url = 'shift:list_shifts' - try: - admin = user.administrator - except ObjectDoesNotExist: - pass - - # check that an admin is logged in - if not admin: - return render(request, 'vms/no_admin_rights.html') - else: - if job_id: - job = get_job_by_id(job_id) - event = job.event - if request.method == 'POST': - if job: - form = ShiftForm(request.POST) - if form.is_valid(): - start_date_job=job.start_date - end_date_job=job.end_date - shift_date=form.cleaned_data['date'] - shift_start_time=form.cleaned_data['start_time'] - shift_end_time=form.cleaned_data['end_time'] - if( shift_date >= start_date_job and shift_date <= end_date_job and shift_end_time > shift_start_time): - shift = form.save(commit=False) - shift.job = job - shift.save() - return HttpResponseRedirect(reverse('shift:list_shifts', args=(job_id,))) - else: - if (shift_date < start_date_job or shift_date > end_date_job): - messages.add_message(request, messages.INFO, 'Shift date should lie within Job dates') - if shift_end_time <= shift_start_time: - messages.add_message(request, messages.INFO, 'Shift end time should be greater than start time') - return render( - request, - 'shift/create.html', - {'form': form, 'job_id': job_id, 'job': job } - ) - - else: - return render( - request, - 'shift/create.html', - {'form': form, 'job_id': job_id, 'job': job } - ) - else: - raise Http404 - else: - form = ShiftForm() - country = event.country - state = event.state - city = event.city - address = event.address - venue = event.venue - return render( - request, - 'shift/create.html', - {'form': form, 'job_id': job_id, 'country': country, 'state': state, 'city': city, 'address': address, 'venue': venue, 'job': job} - ) + def get_context_data(self, **kwargs): + context = super(ShiftCreateView, self).get_context_data(**kwargs) + job_id = self.kwargs['job_id'] + context['job_id'] = job_id + job = get_job_by_id(job_id) + event = job.event + context['job'] = job + context['event'] = job.event + context['country'] = event.country + context['state'] = event.state + context['city'] = event.city + context['address'] = event.address + context['venue'] = event.venue + return context + + + def form_valid(self, form): + job_id = self.kwargs['job_id'] + job = get_job_by_id(job_id) + start_date_job = job.start_date + end_date_job = job.end_date + shift_date = form.cleaned_data['date'] + shift_start_time = form.cleaned_data['start_time'] + shift_end_time = form.cleaned_data['end_time'] + if (shift_date >= start_date_job and shift_date <= end_date_job and shift_end_time > shift_start_time): + shift = form.save(commit=False) + shift.job = job + shift.save() + return HttpResponseRedirect(reverse('shift:list_shifts', args=(job_id,))) else: - raise Http404 - + if (shift_date < start_date_job or shift_date > end_date_job): + messages.add_message(self.request, messages.INFO, 'Shift date should lie within Job dates') + if shift_end_time <= shift_start_time: + messages.add_message(self.request, messages.INFO, 'Shift end time should be greater than start time') + return render( + self.request, + 'shift/create.html', + {'form': form, 'job_id': job_id, 'job': job} + ) -@login_required -def delete(request, shift_id): - user = request.user - admin = None - try: - admin = user.administrator - except ObjectDoesNotExist: - pass +class ShiftDeleteView(AdministratorLoginRequiredMixin, DeleteView): + model_form = Shift + template_name = 'shift/delete.html' + success_url = reverse_lazy('shift:list_jobs') - # check that an admin is logged in - if not admin: - return render(request, 'vms/no_admin_rights.html') - else: - if shift_id: - if request.method == 'POST': - shift = get_shift_by_id(shift_id) - job_id = shift.job.id - result = delete_shift(shift_id) - if result: - shift_list = get_shifts_by_job_id(job_id) - if shift_list: - return HttpResponseRedirect(reverse('shift:list_shifts', args=(job_id,))) - else: - return HttpResponseRedirect(reverse('shift:list_jobs')) - else: - return render(request, 'shift/delete_error.html') - return render( - request, - 'shift/delete.html', - {'shift_id': shift_id} - ) + def get_object(self, queryset=None): + shift_id = self.kwargs['shift_id'] + shift = Shift.objects.get(pk=shift_id) + if shift: + return shift + + def delete(self, request, *args, **kwargs): + shift_id = self.kwargs['shift_id'] + shift = self.get_object() + job_id = shift.job.id + result = delete_shift(shift_id) + if result: + shift_list = get_shifts_by_job_id(job_id) + if shift_list: + return HttpResponseRedirect(reverse('shift:list_shifts', args=(job_id,))) + else: + return HttpResponseRedirect(reverse('shift:list_jobs')) else: - raise Http404 + return render(request, 'shift/delete_error.html') -@login_required -def edit(request, shift_id): - user = request.user - admin = None +class ShiftUpdateView(AdministratorLoginRequiredMixin, UpdateView): + form_class = ShiftForm + template_name = 'shift/edit.html' + success_url = reverse_lazy('shift:list_shifts') - try: - admin = user.administrator - except ObjectDoesNotExist: - pass - # check that an admin is logged in - if not admin: - return render(request, 'vms/no_admin_rights.html') - else: - shift = None - if shift_id: - shift = get_shift_by_id(shift_id) - job=shift.job + def get_context_data(self, **kwargs): + context = super(ShiftUpdateView, self).get_context_data(**kwargs) + shift = get_shift_by_id(self.kwargs['shift_id']) + context['shift'] = shift + context['job'] = shift.job + return context - if request.method == 'POST': - if job: - form = ShiftForm(request.POST, instance=shift) - if form.is_valid(): - - start_date_job = job.start_date - end_date_job = job.end_date - shift_date=form.cleaned_data['date'] - shift_start_time=form.cleaned_data['start_time'] - shift_end_time=form.cleaned_data['end_time'] - - #save when all conditions satisfied - if( shift_date >= start_date_job and shift_date <= end_date_job and shift_end_time > shift_start_time): - shift_to_edit = form.save(commit=False) - shift_to_edit.job = job - shift_to_edit.save() - return HttpResponseRedirect(reverse('shift:list_shifts', args=(shift.job.id,))) - else: - if (shift_date < start_date_job or shift_date > end_date_job): - messages.add_message(request, messages.INFO, 'Shift date should lie within Job dates') - if shift_end_time <= shift_start_time: - messages.add_message(request, messages.INFO, 'Shift end time should be greater than start time') - return render( - request, - 'shift/edit.html', - {'form': form, 'shift': shift, 'job': shift.job} - ) - else: - return render( - request, - 'shift/edit.html', - {'form': form, 'shift': shift, 'job': shift.job} - ) - else: - raise Http404 + def get_object(self, queryset=None): + shift_id = self.kwargs['shift_id'] + obj = Shift.objects.get(pk=shift_id) + return obj + def form_valid(self, form): + shift_id = self.kwargs['shift_id'] + shift = get_shift_by_id(shift_id) + job = shift.job + start_date_job = job.start_date + end_date_job = job.end_date + shift_date = form.cleaned_data['date'] + shift_start_time = form.cleaned_data['start_time'] + shift_end_time = form.cleaned_data['end_time'] + + # save when all conditions satisfied + if (shift_date >= start_date_job and shift_date <= end_date_job and shift_end_time > shift_start_time): + shift_to_edit = form.save(commit=False) + shift_to_edit.job = job + shift_to_edit.save() + return HttpResponseRedirect(reverse('shift:list_shifts', args=(shift.job.id,))) else: - form = ShiftForm(instance=shift) + if (shift_date < start_date_job or shift_date > end_date_job): + messages.add_message(self.request, messages.INFO, 'Shift date should lie within Job dates') + if shift_end_time <= shift_start_time: + messages.add_message(self.request, messages.INFO, 'Shift end time should be greater than start time') return render( - request, + self.request, 'shift/edit.html', {'form': form, 'shift': shift, 'job': shift.job} - ) + ) -@login_required -def edit_hours(request, shift_id, volunteer_id): +class EditHoursView(LoginRequiredMixin, FormView): + template_name = 'shift/edit_hours.html' + form_class = HoursForm - if shift_id and volunteer_id: - volunteer_shift = get_volunteer_shift_by_id(volunteer_id, shift_id) + def get_context_data(self, **kwargs): + context = super(EditHoursView, self).get_context_data(**kwargs) + volunteer_id = self.kwargs['volunteer_id'] + shift_id = self.kwargs['shift_id'] + context['volunteer_shift'] = get_volunteer_shift_by_id(volunteer_id, shift_id) + context['shift'] = get_shift_by_id(shift_id) + return context + + def form_valid(self, form): + volunteer_id = self.kwargs['volunteer_id'] + shift_id = self.kwargs['shift_id'] shift = get_shift_by_id(shift_id) - user = request.user - if int(user.volunteer.id) == int(volunteer_id): - if volunteer_shift: - if request.method == 'POST': - form = HoursForm(request.POST) - if form.is_valid(): - start_time = form.cleaned_data['start_time'] - end_time = form.cleaned_data['end_time'] - shift_start_time=shift.start_time - shift_end_time=shift.end_time - try: - if(end_time>start_time): - if(start_time >= shift_start_time and end_time<=shift_end_time): - edit_shift_hours( - volunteer_id, - shift_id, - start_time, - end_time - ) - return HttpResponseRedirect(reverse('shift:view_hours', args=(volunteer_id,))) - else: - messages.add_message(request, messages.INFO, 'Logged hours should be between shift hours') - return render( - request, - 'shift/edit_hours.html', - {'form': form, 'shift_id': shift_id, 'volunteer_id': volunteer_id} - ) - - else: - messages.add_message(request, messages.INFO, 'End time should be greater than start time') - return render( - request, - 'shift/edit_hours.html', - {'form': form, 'shift_id': shift_id, 'volunteer_id': volunteer_id} - ) - - - except: - raise Http404 - else: - return render( - request, - 'shift/edit_hours.html', - {'form': form, 'shift_id': shift_id, 'volunteer_id': volunteer_id} - ) + start_time = form.cleaned_data['start_time'] + end_time = form.cleaned_data['end_time'] + shift_start_time = shift.start_time + shift_end_time = shift.end_time + try: + if (end_time > start_time): + if (start_time >= shift_start_time and end_time <= shift_end_time): + edit_shift_hours( + volunteer_id, + shift_id, + start_time, + end_time + ) + return HttpResponseRedirect(reverse('shift:view_hours', args=(volunteer_id,))) else: - form = HoursForm(initial={'start_time': volunteer_shift.start_time, 'end_time': volunteer_shift.end_time}) + messages.add_message(self.request, messages.INFO, 'Logged hours should be between shift hours') return render( - request, + self.request, 'shift/edit_hours.html', {'form': form, 'shift_id': shift_id, 'volunteer_id': volunteer_id} - ) + ) + else: - raise Http404 - else: - return HttpResponse(status=403) - else: - raise Http404 + messages.add_message(self.request, messages.INFO, 'End time should be greater than start time') + return render( + self.request, + 'shift/edit_hours.html', + {'form': form, 'shift_id': shift_id, 'volunteer_id': volunteer_id} + ) + except: + raise Http404 -def edit_hours_manager(request, shift_id, volunteer_id): - user = request.user - admin = None +class EditHoursManagerView(AdministratorLoginRequiredMixin, FormView): + template_name = 'shift/edit_hours_manager.html' + form_class = HoursForm - try: - admin = user.administrator - except ObjectDoesNotExist: - pass + def get_context_data(self, **kwargs): + context = super(EditHoursManagerView, self).get_context_data(**kwargs) + volunteer_id = self.kwargs['volunteer_id'] + shift_id = self.kwargs['shift_id'] + context['volunteer_shift'] = get_volunteer_shift_by_id(volunteer_id, shift_id) + context['shift'] = get_shift_by_id(shift_id) + return context - # check that an admin is logged in - if not admin: - return render(request, 'vms/no_admin_rights.html') - else: - if shift_id and volunteer_id: - volunteer_shift = get_volunteer_shift_by_id(volunteer_id, shift_id) - shift = get_shift_by_id(shift_id) - if volunteer_shift: - if request.method == 'POST': - form = HoursForm(request.POST) - if form.is_valid(): - start_time = form.cleaned_data['start_time'] - end_time = form.cleaned_data['end_time'] - shift_start_time=shift.start_time - shift_end_time=shift.end_time - try: - if(end_time>start_time): - if(start_time >= shift_start_time and end_time<=shift_end_time): - edit_shift_hours(volunteer_id, shift_id, start_time, end_time) - return HttpResponseRedirect(reverse('shift:manage_volunteer_shifts', args=(volunteer_id,))) - else: - messages.add_message(request, messages.INFO, 'Logged hours should be between shift hours') - return render( - request, - 'shift/edit_hours_manager.html', - {'form': form, 'shift_id': shift_id, 'volunteer_id': volunteer_id} - ) - - else: - messages.add_message(request, messages.INFO, 'End time should be greater than start time') - return render( - request, - 'shift/edit_hours_manager.html', - {'form': form, 'shift_id': shift_id, 'volunteer_id': volunteer_id} - ) - - except: - raise Http404 - else: - return render( - request, - 'shift/edit_hours_manager.html', - {'form': form, 'shift_id': shift_id, 'volunteer_id': volunteer_id} - ) + def form_valid(self, form): + volunteer_id = self.kwargs['volunteer_id'] + shift_id = self.kwargs['shift_id'] + shift = get_shift_by_id(shift_id) + start_time = form.cleaned_data['start_time'] + end_time = form.cleaned_data['end_time'] + shift_start_time = shift.start_time + shift_end_time = shift.end_time + try: + if (end_time > start_time): + if (start_time >= shift_start_time and end_time <= shift_end_time): + edit_shift_hours(volunteer_id, shift_id, start_time, end_time) + return HttpResponseRedirect(reverse('shift:manage_volunteer_shifts', args=(volunteer_id,))) else: - form = HoursForm(initial={'start_time': volunteer_shift.start_time, 'end_time': volunteer_shift.end_time}) + messages.add_message(self.request, messages.INFO, 'Logged hours should be between shift hours') return render( - request, + self.request, 'shift/edit_hours_manager.html', {'form': form, 'shift_id': shift_id, 'volunteer_id': volunteer_id} - ) + ) + else: - raise Http404 - else: + messages.add_message(self.request, messages.INFO, 'End time should be greater than start time') + return render( + self.request, + 'shift/edit_hours_manager.html', + {'form': form, 'shift_id': shift_id, 'volunteer_id': volunteer_id} + ) + + except: raise Http404 -@login_required -def list_jobs(request): - user = request.user - admin = None +class JobListView(AdministratorLoginRequiredMixin, ListView): #Replaced by list_jobs + template_name = 'shift/list_jobs.html' + model_form = Job - try: - admin = user.administrator - except ObjectDoesNotExist: - pass + def get_queryset(self): + job = Job.objects.all().order_by('name') + return job - #check that an admin is logged in - if not admin: - return render(request, 'vms/no_admin_rights.html') - else: - job_list = get_jobs_ordered_by_title() - return render( - request, - 'shift/list_jobs.html', - {'job_list' : job_list} - ) +class ShiftListView(AdministratorLoginRequiredMixin, TemplateView): #Replaced by list_shifts + template_name = 'shift/list_shifts.html' - -@login_required -def list_shifts(request, job_id): - user = request.user - admin = None - - try: - admin = user.administrator - except ObjectDoesNotExist: - pass - - #check that an admin is logged in - if not admin: - return render(request, 'vms/no_admin_rights.html') - else: - if job_id: - job = get_job_by_id(job_id) - if job: - shift_list = get_shifts_ordered_by_date(job_id) - return render( - request, - 'shift/list_shifts.html', - {'shift_list': shift_list, 'job_id': job_id} - ) - else: - raise Http404 - else: - raise Http404 + def get_context_data(self, **kwargs): + context = super(ShiftListView, self).get_context_data(**kwargs) + job_id = self.kwargs['job_id'] + context['shift_list'] = get_shifts_ordered_by_date(job_id) + return context @login_required @@ -619,36 +499,16 @@ def list_shifts_sign_up(request, job_id, volunteer_id): raise Http404 -@login_required -def manage_volunteer_shifts(request, volunteer_id): - user = request.user - admin = None - - try: - admin = user.administrator - except ObjectDoesNotExist: - pass +class ManageVolunteerShiftView(AdministratorLoginRequiredMixin, TemplateView): + template_name = 'shift/manage_volunteer_shifts.html' - # check that an admin is logged in - if not admin: - return render(request, 'vms/no_admin_rights.html') - else: - if volunteer_id: - volunteer = get_volunteer_by_id(volunteer_id) - if volunteer: - # show only shifts that have no hours logged yet - # (since it doesn't make sense be able to cancel shifts that have already been logged) - shift_list = get_unlogged_shifts_by_volunteer_id(volunteer_id) - shift_list_with_hours = get_volunteer_shifts_with_hours(volunteer_id) - return render( - request, - 'shift/manage_volunteer_shifts.html', - {'shift_list': shift_list,'shift_list_with_hours': shift_list_with_hours, 'volunteer_id': volunteer_id} - ) - else: - raise Http404 - else: - raise Http404 + def get_context_data(self, **kwargs): + context = super(ManageVolunteerShiftView, self).get_context_data(**kwargs) + volunteer_id = self.kwargs['volunteer_id'] + context['volunteer'] = get_volunteer_by_id(volunteer_id) + context['shift_list'] = get_unlogged_shifts_by_volunteer_id(volunteer_id) + context['shift_list_with_hours'] = get_volunteer_shifts_with_hours(volunteer_id) + return context @login_required @@ -705,25 +565,15 @@ def sign_up(request, shift_id, volunteer_id): raise Http404 -@login_required -def view_hours(request, volunteer_id): - if volunteer_id: - volunteer = get_volunteer_by_id(volunteer_id) - if volunteer: - user = request.user - if int(user.volunteer.id) == int(volunteer_id): - volunteer_shift_list = get_volunteer_shifts_with_hours(volunteer_id) - return render( - request, - 'shift/hours_list.html', - {'volunteer_shift_list': volunteer_shift_list, } - ) - else: - return HttpResponse(status=403) - else: - raise Http404 - else: - raise Http404 +class ViewHoursView(LoginRequiredMixin, FormView, TemplateView): + template_name = 'shift/hours_list.html' + + def get_context_data(self, **kwargs): + context = super(ViewHoursView, self).get_context_data(**kwargs) + volunteer_id = self.kwargs['volunteer_id'] + context['volunteer'] = get_volunteer_by_id(volunteer_id) + context['volunteer_shift_list'] = get_volunteer_shifts_with_hours(volunteer_id) + return context @login_required @@ -759,54 +609,38 @@ def view_volunteer_shifts(request, volunteer_id): return HttpResponse(status=403) -@login_required -def volunteer_search(request): - user = request.user - admin = None - - try: - admin = user.administrator - except ObjectDoesNotExist: - pass - - # check that an admin is logged in - if not admin: - return render(request, 'vms/no_admin_rights.html') - else: - if request.method == 'POST': - form = SearchVolunteerForm(request.POST) - if form.is_valid(): - - first_name = form.cleaned_data['first_name'] - last_name = form.cleaned_data['last_name'] - city = form.cleaned_data['city'] - state = form.cleaned_data['state'] - country = form.cleaned_data['country'] - organization = form.cleaned_data['organization'] - - volunteer_list = search_volunteers( - first_name, - last_name, - city, - state, - country, - organization - ) - return render( - request, - 'shift/volunteer_search.html', - {'form': form, 'has_searched': True, 'volunteer_list': volunteer_list} - ) - else: - form = SearchVolunteerForm() - volunteer_list = get_all_volunteers() - +class VolunteerSearchView(AdministratorLoginRequiredMixin, FormView): + template_name = 'shift/volunteer_search.html' + form_class = SearchVolunteerForm + success_url = 'volunteer_list' + + def get_context_data(self, **kwargs): + context = super(VolunteerSearchView, self).get_context_data(**kwargs) + context['volunteer_list'] = get_all_volunteers() + context['has_searched'] = False + return context + + def form_valid(self, form): + first_name = form.cleaned_data['first_name'] + last_name = form.cleaned_data['last_name'] + city = form.cleaned_data['city'] + state = form.cleaned_data['state'] + country = form.cleaned_data['country'] + organization = form.cleaned_data['organization'] + + volunteer_list = search_volunteers( + first_name, + last_name, + city, + state, + country, + organization + ) return render( - request, + self.request, 'shift/volunteer_search.html', - {'form': form, 'has_searched': False, 'volunteer_list': volunteer_list} - ) - + {'form': form, 'has_searched': True, 'volunteer_list': volunteer_list} + ) @login_required def view_volunteers(request, shift_id): @@ -836,4 +670,4 @@ def view_volunteers(request, shift_id): else: raise Http404 else: - raise Http404 \ No newline at end of file + raise Http404