50 lines
1.4 KiB
Python
50 lines
1.4 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 .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_study(request, schedule = None):
|
|
template_name = 'schedule/schedule_new.html'
|
|
|
|
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:
|
|
# For now, sorry, ignore
|
|
pass
|
|
|
|
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:
|
|
schedule_form = ScheduleForm()
|
|
|
|
return render(request, template_name, {
|
|
'form' : schedule_form,
|
|
}) |