Add ability to clone schedule data from another week

This commit is contained in:
Lance Edgar 2016-10-14 16:02:15 -05:00
parent c2503977ea
commit 3980886356
2 changed files with 94 additions and 1 deletions

View file

@ -199,6 +199,38 @@
}
});
$('#copy-week').datepicker({
dateFormat: 'mm/dd/yy'
});
$('.copy-schedule').click(function() {
$('#copy-details').dialog({
modal: true,
title: "Copy from Another Week",
width: '500px',
buttons: [
{
text: "Copy Schedule",
click: function(event) {
if (! $('#copy-week').val()) {
alert("You must specify the week from which to copy shift data.");
$('#copy-week').focus();
return;
}
$(event.target).button('disable').button('option', 'label', "Copying Schedule...");
$('#copy-schedule-form').submit();
}
},
{
text: "Cancel",
click: function() {
$('#copy-details').dialog('close');
}
}
]
});
});
});
</script>
<style type="text/css">
@ -254,6 +286,7 @@
<button type="button" class="save-changes" disabled="disabled">Save Changes</button>
<button type="button" class="undo-changes" disabled="disabled">Undo Changes</button>
<button type="button" class="clear-schedule">Clear Schedule</button>
<button type="button" class="copy-schedule">Copy Schedule From...</button>
</div>
</%def>
@ -270,6 +303,22 @@ ${h.end_form()}
<button type="button" id="add-shift">Add Shift</button>
</div>
<div id="copy-details" style="display: none;">
<p>
This tool will replace the currently visible schedule, with one from
another week.
</p>
<p>
<strong>NOTE:</strong>&nbsp; If you do this, all shifts in the current
schedule will be <em>removed</em>,
and then new shifts will be created based on the week you specify.
</p>
${h.form(url('schedule.edit'), id='copy-schedule-form')}
<label for="copy-week">Copy from week:</label>
${h.text('copy-week')}
${h.end_form()}
</div>
<div id="snippets">
<div class="shift" data-uuid="">
${h.text('edit_start_time')} thru ${h.text('edit_end_time')}

View file

@ -29,7 +29,7 @@ from __future__ import unicode_literals, absolute_import
import datetime
from rattail.db import model
from rattail.time import localtime, make_utc
from rattail.time import localtime, make_utc, get_sunday
from pyramid_simpleform import Form
@ -54,6 +54,12 @@ class ScheduleView(TimeSheetView):
self.request.session.flash("Removed {} shifts from current schedule.".format(count))
return self.redirect(self.request.route_url('schedule.edit'))
# okay then, check if we should copy data from another week
if self.request.method == 'POST' and self.request.POST.get('copy-week'):
sunday, copied = self.copy_schedule()
self.request.session.flash("Copied {} shifts from week of {}".format(copied, sunday.strftime('%m/%d/%Y')))
return self.redirect(self.request.route_url('schedule.edit'))
# okay then, process filters; redirect if any were received
form = Form(self.request, schema=ShiftFilter)
self.process_filter_form(form)
@ -129,6 +135,44 @@ class ScheduleView(TimeSheetView):
deleted += 1
return deleted
def copy_schedule(self):
"""
Clear current schedule, then copy shift data from another week.
"""
try:
sunday = datetime.datetime.strptime(self.request.POST['copy-week'], '%m/%d/%Y').date()
except ValueError as error:
self.request.session.flash("Invalid date specified: {}: {}".format(type(error), error), 'error')
raise self.redirect(self.request.route_url('schedule.edit'))
sunday = get_sunday(sunday)
context = self.get_timesheet_context()
if sunday == context['date']:
self.request.session.flash("Cannot copy schedule from same week; please specify a different week.", 'error')
raise self.redirect(self.request.route_url('schedule.edit'))
self.clear_schedule()
copied = 0
if context['employees']:
offset = context['date'] - sunday
sunday = datetime.datetime.combine(sunday, datetime.time(0))
start_time = localtime(self.rattail_config, sunday)
end_time = localtime(self.rattail_config, sunday + datetime.timedelta(days=7))
shifts = Session.query(model.ScheduledShift)\
.filter(model.ScheduledShift.employee_uuid.in_([e.uuid for e in context['employees']]))\
.filter(model.ScheduledShift.start_time >= make_utc(start_time))\
.filter(model.ScheduledShift.end_time < make_utc(end_time))
for shift in shifts:
Session.add(model.ScheduledShift(
employee_uuid=shift.employee_uuid,
store_uuid=shift.store_uuid,
start_time=shift.start_time + offset,
end_time=shift.end_time + offset,
))
copied += 1
return sunday, copied
@classmethod
def defaults(cls, config):
cls._defaults(config)