108 lines
4.5 KiB
Python
108 lines
4.5 KiB
Python
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
|
|
from django.forms.models import model_to_dict
|
|
|
|
from .models import Schedule
|
|
from .forms import ScheduleForm
|
|
|
|
import json
|
|
|
|
# Create your views here.
|
|
class ScheduleListView(LoginRequiredMixin,ListView):
|
|
"""
|
|
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
|
|
"""
|
|
|
|
model = Schedule
|
|
paginate_by = 10
|
|
|
|
def get_queryset(self):
|
|
return Schedule.objects.filter(employee=self.request.user.employee).order_by('-created_at')
|
|
|
|
@login_required
|
|
def new_or_update_schedule(request, schedule_id = None):
|
|
"""
|
|
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.
|
|
"""
|
|
template_name = 'schedule/schedule_new.html'
|
|
|
|
schedule = None
|
|
if schedule_id is not None:
|
|
try:
|
|
# Try loading an existing schedule. Make sure you only load a schedule that is owned by the logged in user!
|
|
schedule = Schedule.objects.get(pk = schedule_id, employee = request.user.employee)
|
|
except Schedule.DoesNotExist:
|
|
# Schedule does not exist, or you do not own the schedule you try to load. Stop playing around on the url bar ;)
|
|
pass
|
|
|
|
if request.method == 'POST':
|
|
# Load the form data
|
|
schedule_form = ScheduleForm(request.POST)
|
|
|
|
# Check if minimal input fields are correct
|
|
if schedule_form.is_valid():
|
|
# First time saving. Create a new, empty schedule
|
|
# Or when the existing schedule has NOT 'draft' status, which means we are cloning an existing schedule
|
|
if schedule is None or schedule.status != Schedule.ScheduleStatus.DRAFT:
|
|
schedule = Schedule()
|
|
|
|
# Set the schedule status based on the form pressed button. Either new or draft status
|
|
schedule.status = Schedule.ScheduleStatus.DRAFT if schedule_form.cleaned_data['status'] == 'draft' else Schedule.ScheduleStatus.NEW
|
|
|
|
try:
|
|
# Try loading the JSON data from the form
|
|
schedule.planning_source = json.loads(schedule_form.cleaned_data['json'])
|
|
except json.JSONDecodeError as ex:
|
|
# Something when wrong. The error is saved instead, and can be read out in the admin area
|
|
schedule.planning_source = json.loads(json.dumps({'error': str(ex)}))
|
|
schedule.status = Schedule.ScheduleStatus.INVALID
|
|
|
|
# Make sure that the logged in user that is creating the schedule does become the owner
|
|
schedule.employee = request.user.employee
|
|
# Store name of the schedule
|
|
schedule.name = schedule_form.cleaned_data['name']
|
|
# Store email address for the results
|
|
schedule.email = schedule_form.cleaned_data['email']
|
|
# Save the schedule to the database
|
|
schedule.save()
|
|
|
|
# Return to overview
|
|
return redirect('schedule:list')
|
|
|
|
else:
|
|
# We start with a new form. Either new of clone/edit
|
|
status = 'new'
|
|
json_form = ''
|
|
if schedule is not None:
|
|
# The schedule is an existing one. So we are in clone/edit mode. So load the current status and JSON data
|
|
status = schedule.status
|
|
json_form = json.dumps(schedule.planning_source)
|
|
|
|
# Create the from logic
|
|
schedule_form = ScheduleForm(initial={'json' : json_form, 'status' : status})
|
|
|
|
# Show the form in the browser
|
|
return render(request, template_name, {
|
|
'form' : schedule_form,
|
|
}) |