61 lines
1.9 KiB
Python
61 lines
1.9 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):
|
|
|
|
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):
|
|
template_name = 'schedule/schedule_new.html'
|
|
|
|
schedule = None
|
|
if schedule_id is not None:
|
|
try:
|
|
schedule = Schedule.objects.get(pk=schedule_id)
|
|
except Schedule.DoesNotExist:
|
|
pass
|
|
|
|
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:
|
|
new_schedule.planning_source = json.loads(json.dumps({'error': str(ex)}))
|
|
new_schedule.status = Schedule.ScheduleStatus.INVALID
|
|
|
|
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:
|
|
if schedule is not None:
|
|
schedule = json.dumps(schedule.planning_source)
|
|
|
|
schedule_form = ScheduleForm(initial={'json' : schedule})
|
|
|
|
return render(request, template_name, {
|
|
'form' : schedule_form,
|
|
}) |