Add ability to clear current schedule when editing

This commit is contained in:
Lance Edgar 2016-10-14 14:45:04 -05:00
parent 9e7cb532c8
commit c2503977ea
2 changed files with 41 additions and 5 deletions

View file

@ -190,6 +190,15 @@
location.href = '${url('schedule.edit')}'; location.href = '${url('schedule.edit')}';
}); });
$('.clear-schedule').click(function() {
if (confirm("This will remove all shifts from the schedule you're " +
"currently viewing.\n\nAre you sure you wish to do this?")) {
$(this).button('disable').button('option', 'label', "Clearing...");
okay_to_leave = true;
$('#clear-schedule-form').submit();
}
});
}); });
</script> </script>
<style type="text/css"> <style type="text/css">
@ -236,21 +245,26 @@
% endfor % endfor
</%def> </%def>
<%def name="edit_form()">
${h.form(url('schedule.edit'), id='schedule-form')}
</%def>
<%def name="edit_tools()"> <%def name="edit_tools()">
<div class="buttons"> <div class="buttons">
<button type="button" class="save-changes" disabled="disabled">Save Changes</button> <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="undo-changes" disabled="disabled">Undo Changes</button>
<button type="button" class="clear-schedule">Clear Schedule</button>
</div> </div>
</%def> </%def>
<%def name="edit_form()">
${h.form(url('schedule.edit'), id='schedule-form')}
</%def>
${self.timesheet(edit_form=edit_form, edit_tools=edit_tools)} ${self.timesheet(edit_form=edit_form, edit_tools=edit_tools)}
${edit_tools()} ${edit_tools()}
${h.form(url('schedule.edit'), id="clear-schedule-form")}
${h.hidden('clear-schedule', value='clear')}
${h.end_form()}
<div id="day-editor" style="display: none;"> <div id="day-editor" style="display: none;">
<div class="shifts"></div> <div class="shifts"></div>
<button type="button" id="add-shift">Add Shift</button> <button type="button" id="add-shift">Add Shift</button>

View file

@ -48,7 +48,13 @@ class ScheduleView(TimeSheetView):
""" """
View for editing (full) schedule. View for editing (full) schedule.
""" """
# first process filters; will redirect if any were received # first check if we should clear the schedule
if self.request.method == 'POST' and self.request.POST.get('clear-schedule') == 'clear':
count = self.clear_schedule()
self.request.session.flash("Removed {} shifts from current schedule.".format(count))
return self.redirect(self.request.route_url('schedule.edit'))
# okay then, process filters; redirect if any were received
form = Form(self.request, schema=ShiftFilter) form = Form(self.request, schema=ShiftFilter)
self.process_filter_form(form) self.process_filter_form(form)
@ -107,6 +113,22 @@ class ScheduleView(TimeSheetView):
context['page_title'] = "Edit Schedule" context['page_title'] = "Edit Schedule"
return self.render_full(**context) return self.render_full(**context)
def clear_schedule(self):
deleted = 0
context = self.get_timesheet_context()
if context['employees']:
sunday = datetime.datetime.combine(context['date'], 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.delete(shift)
deleted += 1
return deleted
@classmethod @classmethod
def defaults(cls, config): def defaults(cls, config):
cls._defaults(config) cls._defaults(config)