Add clone option for schedules

This commit is contained in:
Joshua Rubingh 2020-05-19 11:20:09 +02:00
parent 485307b78e
commit ed6879b7f1
4 changed files with 66 additions and 17 deletions

View File

@ -14,6 +14,7 @@
<th>{% trans "Updated" %}</th> <th>{% trans "Updated" %}</th>
<th>{% trans "Status" %}</th> <th>{% trans "Status" %}</th>
<th>{% trans "Report" %}</th> <th>{% trans "Report" %}</th>
<th>{% trans "Actions" %}</th>
</tr> </tr>
{% for schedule in object_list %} {% for schedule in object_list %}
<tr> <tr>
@ -23,6 +24,7 @@
<td>{{ schedule.updated_at|date:"SHORT_DATETIME_FORMAT" }}</td> <td>{{ schedule.updated_at|date:"SHORT_DATETIME_FORMAT" }}</td>
<td>{{ schedule.get_status_display }}</td> <td>{{ schedule.get_status_display }}</td>
<td>None</td> <td>None</td>
<td><a href="{% url 'schedule:clone' schedule_id=schedule.id %}">Clone</a></td>
</tr> </tr>
{% empty %} {% empty %}
<tr> <tr>

View File

@ -2,8 +2,8 @@
{% load i18n %} {% load i18n %}
{% load static %} {% load static %}
{% block title %}{% trans "New schedule" %}{% endblock %} {% block title %}{% if form.json.value %}{% trans "Clone schedule" %}{% else %}{% trans "New schedule" %}{% endif %}{% endblock %}
{% block pagetitle %}{% trans "New schedule" %}{% endblock %} {% block pagetitle %}{% if form.json.value %}{% trans "Clone schedule" %}{% else %}{% trans "New schedule" %}{% endif %}{% endblock %}
{% block content %} {% block content %}
<form method="POST" action="{% url 'schedule:new' %}" id="new_schedule_form"> <form method="POST" action="{% url 'schedule:new' %}" id="new_schedule_form">
{% csrf_token %} {% csrf_token %}
@ -464,7 +464,7 @@
var clone = jQuery('div.poliekliniek_specialisatie:first').clone() var clone = jQuery('div.poliekliniek_specialisatie:first').clone()
clone.find('input').val(''); clone.find('input').val('');
clone.find('option:disabled').removeAttr('disabled'); clone.find('option:disabled').removeAttr('disabled');
clone.find('small').show(); clone.find('small.text-muted').show();
clone.insertAfter('div.poliekliniek_specialisatie:last'); clone.insertAfter('div.poliekliniek_specialisatie:last');
updateSpecialisaties(); updateSpecialisaties();
loadRemoveSpecialisatie(); loadRemoveSpecialisatie();
@ -476,6 +476,7 @@
jQuery(this).parentsUntil('.poliekliniek_specialisatie').parent().remove(); jQuery(this).parentsUntil('.poliekliniek_specialisatie').parent().remove();
updateSpecialisaties(); updateSpecialisaties();
updateSpecialisatieValues(); updateSpecialisatieValues();
calculateBehandelingen();
}); });
} }
@ -502,7 +503,7 @@
}); });
jQuery('div.werktijden select').off('change').on('change',function(event){ jQuery('div.werktijden select').off('change').on('change',function(event){
check_working_hours(this); check_working_hours(jQuery(this).parentsUntil('.werktijden').parent());
}); });
} }
@ -536,11 +537,7 @@
clone.insertAfter('tr.behandeling:last'); clone.insertAfter('tr.behandeling:last');
jQuery('tr.behandeling input.behandeling_calc').off('change keyup').on('change keyup',function(){ jQuery('tr.behandeling :input').off('change keyup').on('change keyup',function(){
calculateBehandelingen();
});
jQuery('tr.behandeling select').off('change').on('change',function(){
calculateBehandelingen(); calculateBehandelingen();
}); });
@ -554,6 +551,7 @@
jQuery(this).parentsUntil('.behandeling').parent().remove(); jQuery(this).parentsUntil('.behandeling').parent().remove();
updateBehandelingen(); updateBehandelingen();
calculateBehandelingen();
}); });
} }
@ -811,8 +809,11 @@
} }
function check_working_hours(html, showerror) { function check_working_hours(html, showerror) {
html = jQuery(html).parentsUntil('.werktijden').parent(); if (Array.isArray(html)) {
html = jQuery(html[0]); html = html[0];
}
html = jQuery(html);
let day_start = html.find('select[name^="werkdag_start_afdeling_"]'); let day_start = html.find('select[name^="werkdag_start_afdeling_"]');
let day_end = html.find('select[name^="werkdag_end_afdeling_"]'); let day_end = html.find('select[name^="werkdag_end_afdeling_"]');
@ -917,6 +918,35 @@
} }
} }
function load_clone_data() {
if (jQuery('input#id_json').val() != '') {
let form = jQuery('form#new_schedule_form');
let json_data = JSON.parse(form.find('input#id_json').val());
jQuery.each(json_data,function(key,value){
let input = form.find(':input[name="' + key + '"]');
if (input.length == 0) {
// check for second or more specilism or treatment
if (key.indexOf('behandeling') >= 0) {
addBehandeling();
} else {
addSpecialisatie();
}
input = form.find(':input[name="' + key + '"]');
}
input.val(value);
updateSpecialisatieValues();
});
validate_working_hours();
calculateBehandelingen();
validate_behandeling_hours();
}
}
jQuery(function() { jQuery(function() {
jQuery('a.add_specialisatie').on('click',function(event){ jQuery('a.add_specialisatie').on('click',function(event){
event.preventDefault(); event.preventDefault();
@ -939,7 +969,7 @@
}); });
jQuery('div.werktijden select').off('change').on('change',function(event){ jQuery('div.werktijden select').off('change').on('change',function(event){
check_working_hours(this); check_working_hours(jQuery(this).parentsUntil('.werktijden').parent());
calculateBehandelingen(); calculateBehandelingen();
}); });
@ -949,7 +979,7 @@
}); });
}); });
jQuery('input.behandeling_calc').off('change keyup').on('change keyup',function(event){ jQuery('tr.behandeling :input').off('change keyup').on('change keyup',function(event){
calculateBehandelingen(); calculateBehandelingen();
}); });
@ -966,10 +996,11 @@
return true; return true;
} else { } else {
alert('Er zijn wat problemen met het formulier. Controleer de invoer velden'); alert('Er zijn wat problemen met het formulier. Controleer de invoer velden');
} }
return false; return false;
}); });
load_clone_data();
}); });
</script> </script>
{% endblock %} {% endblock %}

View File

@ -5,5 +5,8 @@ from . import views
app_name = 'schedule' app_name = 'schedule'
urlpatterns = [ urlpatterns = [
path('', views.ScheduleListView.as_view(), name='list'), path('', views.ScheduleListView.as_view(), name='list'),
path('new', views.new_or_update_study, name='new'), path('new', views.new_or_update_schedule, name='new'),
#path('<int:pk>', views.ScheduleDetailView.as_view(), name='detail'),
path('<int:schedule_id>/clone', views.new_or_update_schedule, name='clone'),
] ]

View File

@ -3,6 +3,7 @@ from django.shortcuts import render, redirect
from django.views.generic.list import ListView from django.views.generic.list import ListView
from django.contrib.auth.mixins import LoginRequiredMixin from django.contrib.auth.mixins import LoginRequiredMixin
from django.contrib.auth.decorators import login_required from django.contrib.auth.decorators import login_required
from django.forms.models import model_to_dict
from .models import Schedule from .models import Schedule
from .forms import ScheduleForm from .forms import ScheduleForm
@ -19,9 +20,16 @@ class ScheduleListView(LoginRequiredMixin,ListView):
return Schedule.objects.filter(employee=self.request.user.employee).order_by('-created_at') return Schedule.objects.filter(employee=self.request.user.employee).order_by('-created_at')
@login_required @login_required
def new_or_update_study(request, schedule = None): def new_or_update_schedule(request, schedule_id = None):
template_name = 'schedule/schedule_new.html' 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': if request.method == 'POST':
schedule_form = ScheduleForm(request.POST) schedule_form = ScheduleForm(request.POST)
@ -43,7 +51,12 @@ def new_or_update_study(request, schedule = None):
return redirect('schedule:list') return redirect('schedule:list')
else: else:
schedule_form = ScheduleForm() if schedule is not None:
print(dir(schedule.planning_source))
schedule = json.dumps(schedule.planning_source)
print(schedule)
schedule_form = ScheduleForm(initial={'json' : schedule})
return render(request, template_name, { return render(request, template_name, {
'form' : schedule_form, 'form' : schedule_form,