2020-05-13 15:54:40 +02:00
|
|
|
from django.shortcuts import render, redirect
|
|
|
|
|
|
|
|
from django.views.generic.list import ListView
|
|
|
|
from django.contrib.auth.mixins import LoginRequiredMixin
|
|
|
|
from django.contrib.auth.decorators import login_required
|
2020-05-19 11:20:09 +02:00
|
|
|
from django.forms.models import model_to_dict
|
2020-05-13 15:54:40 +02:00
|
|
|
|
|
|
|
from .models import Schedule
|
|
|
|
from .forms import ScheduleForm
|
|
|
|
|
|
|
|
import json
|
|
|
|
|
|
|
|
# Create your views here.
|
|
|
|
class ScheduleListView(LoginRequiredMixin,ListView):
|
2020-05-27 09:44:51 +02:00
|
|
|
"""
|
|
|
|
This view will give a list of all entered schedules. The list is filtered on the logged in employee.
|
|
|
|
|
|
|
|
Only the schedules owned by the logged in user are shown.
|
|
|
|
|
|
|
|
The results are shown with 10 items per page. A pager will be shown when there are more then 10 schedules
|
|
|
|
"""
|
2020-05-13 15:54:40 +02:00
|
|
|
|
|
|
|
model = Schedule
|
|
|
|
paginate_by = 10
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
return Schedule.objects.filter(employee=self.request.user.employee).order_by('-created_at')
|
|
|
|
|
|
|
|
@login_required
|
2020-05-19 11:20:09 +02:00
|
|
|
def new_or_update_schedule(request, schedule_id = None):
|
2020-05-27 09:44:51 +02:00
|
|
|
"""
|
|
|
|
This view will create or update an existing schedule. At the moment there is not a real update, but a clone functionality.
|
|
|
|
So every schedule that is updated will be stored as a new schedule.
|
|
|
|
|
|
|
|
Only schedules owned by the logged in user can be loaded here and can be cloned.
|
|
|
|
|
|
|
|
The data of the form is stored as a JSON object in a single database field for more flexibility.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
request HttpRequest -- This is the HTTP request from the Django framework. This will hold the current logged in user info.
|
|
|
|
|
|
|
|
Keyword Arguments:
|
|
|
|
schedule_id Schedule -- This is the schedule to be edited. When none, a new schedule will be created (default: {None})
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
A view with an empty form to create a new schedule or a prefilled form for cloning.
|
|
|
|
"""
|
2020-05-13 15:54:40 +02:00
|
|
|
template_name = 'schedule/schedule_new.html'
|
|
|
|
|
2020-05-19 11:20:09 +02:00
|
|
|
schedule = None
|
|
|
|
if schedule_id is not None:
|
|
|
|
try:
|
2020-05-27 09:44:51 +02:00
|
|
|
schedule = Schedule.objects.get(pk=schedule_id,employee=request.user.employee)
|
2020-05-19 11:20:09 +02:00
|
|
|
except Schedule.DoesNotExist:
|
|
|
|
pass
|
|
|
|
|
2020-05-13 15:54:40 +02:00
|
|
|
if request.method == 'POST':
|
|
|
|
schedule_form = ScheduleForm(request.POST)
|
|
|
|
|
|
|
|
if schedule_form.is_valid():
|
|
|
|
new_schedule = Schedule()
|
|
|
|
|
|
|
|
try:
|
|
|
|
new_schedule.planning_source = json.loads(schedule_form.cleaned_data['json'])
|
|
|
|
except json.JSONDecodeError as ex:
|
2020-05-18 12:19:01 +02:00
|
|
|
new_schedule.planning_source = json.loads(json.dumps({'error': str(ex)}))
|
2020-05-18 16:53:40 +02:00
|
|
|
new_schedule.status = Schedule.ScheduleStatus.INVALID
|
2020-05-13 15:54:40 +02:00
|
|
|
|
|
|
|
new_schedule.employee = request.user.employee
|
|
|
|
new_schedule.name = schedule_form.cleaned_data['name']
|
|
|
|
new_schedule.email = schedule_form.cleaned_data['email']
|
|
|
|
|
|
|
|
new_schedule.save()
|
|
|
|
|
|
|
|
return redirect('schedule:list')
|
|
|
|
|
|
|
|
else:
|
2020-05-19 11:20:09 +02:00
|
|
|
if schedule is not None:
|
|
|
|
schedule = json.dumps(schedule.planning_source)
|
|
|
|
|
|
|
|
schedule_form = ScheduleForm(initial={'json' : schedule})
|
2020-05-13 15:54:40 +02:00
|
|
|
|
|
|
|
return render(request, template_name, {
|
|
|
|
'form' : schedule_form,
|
|
|
|
})
|