- ${week_of}
+
+
+
+
+
+ ${form.select('store', store_options, selected_value=store.uuid if store else None)}
+
+
+
+
+
+ ${form.select('department', department_options, selected_value=department.uuid if department else None)}
+
+
+
+
+
+
+ ${week_of}
+
+
+
diff --git a/tailbone/templates/shifts/schedule.mako b/tailbone/templates/shifts/schedule.mako
index c1796a47..82be920f 100644
--- a/tailbone/templates/shifts/schedule.mako
+++ b/tailbone/templates/shifts/schedule.mako
@@ -3,4 +3,9 @@
<%def name="title()">Schedule: ${sunday}%def>
+
+
${self.timesheet(employees)}
diff --git a/tailbone/views/shifts/lib.py b/tailbone/views/shifts/lib.py
index 1668409c..e8e8bf29 100644
--- a/tailbone/views/shifts/lib.py
+++ b/tailbone/views/shifts/lib.py
@@ -28,31 +28,94 @@ from __future__ import unicode_literals, absolute_import
import datetime
-from rattail.db import model
+from rattail import enum
+from rattail.db import model, api
from rattail.time import localtime, get_sunday
+import formencode as fe
+from pyramid_simpleform import Form
+
+from tailbone import forms
from tailbone.db import Session
from tailbone.views import View
+class ShiftFilter(fe.Schema):
+ allow_extra_fields = True
+ filter_extra_fields = True
+ store = forms.validators.ValidStore()
+ department = forms.validators.ValidDepartment()
+
+
class TimeSheetView(View):
"""
Base view for time sheets.
"""
model_class = None
+ # Set this to False to avoid the default behavior of auto-filtering by
+ # current store.
+ default_filter_store = True
+
+ def __call__(self):
+ date = self.get_date()
+ store = None
+ department = None
+ employees = Session.query(model.Employee)\
+ .filter(model.Employee.status == enum.EMPLOYEE_STATUS_CURRENT)
+
+ form = Form(self.request, schema=ShiftFilter)
+ if form.validate():
+ store = form.data['store']
+ department = form.data['department']
+
+ elif self.request.method != 'POST' and self.default_filter_store:
+ store = self.rattail_config.get('rattail', 'store')
+ if store:
+ store = api.get_store(Session(), store)
+
+ # TODO:
+ # store = Session.query(model.Store).filter_by(id='003').one()
+ # department = Session.query(model.Department).filter_by(number=6).one()
+
+ if store:
+ employees = employees.join(model.EmployeeStore)\
+ .filter(model.EmployeeStore.store == store)
+
+ if department:
+ employees = employees.join(model.EmployeeDepartment)\
+ .filter(model.EmployeeDepartment.department == department)
+
+ return self.render(date, employees.all(), store=store, department=department, form=form)
+
def get_date(self):
date = None
- if 'date' in self.request.GET:
+ if 'date' in self.request.params:
try:
- date = datetime.datetime.strptime(self.request.GET['date'], '%Y-%m-%d').date()
+ date = datetime.datetime.strptime(self.request.params['date'], '%Y-%m-%d').date()
except ValueError:
- self.request.session.flash("The specified date is not valid: {}".format(self.request.GET['date']), 'error')
+ self.request.session.flash("The specified date is not valid: {}".format(self.request.params['date']), 'error')
if not date:
date = localtime(self.rattail_config).date()
return date
- def render(self, date, employees):
+ def get_stores(self):
+ return Session.query(model.Store).order_by(model.Store.id).all()
+
+ def get_store_options(self, stores):
+ options = [(s.uuid, "{} - {}".format(s.id, s.name)) for s in stores]
+ options.insert(0, (None, "(all)"))
+ return options
+
+ def get_departments(self):
+ return Session.query(model.Department).order_by(model.Department.name).all()
+
+ def get_department_options(self, departments):
+ options = [(d.uuid, d.name) for d in departments]
+ options.insert(0, (None, "(all)"))
+ return options
+
+ def render(self, date, employees, store=None, department=None, form=None):
"""
Render a time sheet for one or more employees, for the week which
includes the specified date.
@@ -69,8 +132,22 @@ class TimeSheetView(View):
week_of = '{} - {}'.format(sunday.strftime('%a %b %d, Y'), saturday.strftime('%a %b %d, %Y'))
self.modify_employees(employees, weekdays)
+
+ stores = self.get_stores()
+ store_options = self.get_store_options(stores)
+
+ departments = self.get_departments()
+ department_options = self.get_department_options(departments)
+
return {
+ 'form': forms.FormRenderer(form) if form else None,
'employees': employees,
+ 'stores': stores,
+ 'store_options': store_options,
+ 'store': store,
+ 'departments': departments,
+ 'department_options': department_options,
+ 'department': department,
'week_of': week_of,
'sunday': sunday,
'prev_sunday': sunday - datetime.timedelta(days=7),
diff --git a/tailbone/views/shifts/schedule.py b/tailbone/views/shifts/schedule.py
index f5dce073..233fe351 100644
--- a/tailbone/views/shifts/schedule.py
+++ b/tailbone/views/shifts/schedule.py
@@ -39,24 +39,6 @@ class ScheduleView(TimeSheetView):
"""
model_class = model.ScheduledShift
- def __call__(self):
- date = self.get_date()
- employees = Session.query(model.Employee)\
- .filter(model.Employee.status == enum.EMPLOYEE_STATUS_CURRENT)
-
- # TODO:
- # store = Session.query(model.Store).filter_by(id='003').one()
- # department = Session.query(model.Department).filter_by(number=6).one()
-
- # if store:
- # employees = employees.join(model.EmployeeStore)\
- # .filter(model.EmployeeStore.store == store)
- # if department:
- # employees = employees.join(model.EmployeeDepartment)\
- # .filter(model.EmployeeDepartment.department == department)
-
- return self.render(date, employees.all())
-
def includeme(config):