Browse Source

[REVIEW] Make the code much simplier, use groupby for irregular worker and use float_to_time for regular worker

pull/24/head
Thibault Francois 7 years ago
parent
commit
dcac5953e2
  1. 94
      beesdoo_portal_shift/controllers/main.py
  2. 18
      beesdoo_portal_shift/views/shift_portal_templates.xml

94
beesdoo_portal_shift/controllers/main.py

@ -1,10 +1,11 @@
# -*- coding: utf8 -*-
from datetime import datetime
from math import floor
from itertools import groupby
from openerp import http
from openerp.http import request
from openerp.addons.beesdoo_shift.models.planning import float_to_time
class ShiftPortalController(http.Controller):
@http.route('/shift_irregular_worker', auth='public', website=True)
@ -16,84 +17,25 @@ class ShiftPortalController(http.Controller):
('worker_id', '=', False)],
order="start_time, task_template_id, task_type_id",
)
# Loop on all the shifts
shift_templates = []
current_template = None
current_shift_template = None
current_remaining_space = 0
for shift in shifts:
# For a planning id, count the number of shift that don't
# have a worker.
if shift.task_template_id == current_template:
# If we are in the same template then update the number
# of available sapce
current_remaining_space = current_remaining_space + 1
else:
if current_shift_template:
# Save the old current_shift_template
current_shift_template.remaining_space = current_remaining_space
shift_templates.append(current_shift_template)
# Initiate the new current_shift_template
current_template = shift.task_template_id
current_remaining_space = 1
current_shift_template = ShiftTemplate(shift,
shift.start_time,
shift.end_time,
current_template.name,
current_template.task_type_id.name)
shifts_and_count = []
for _, val in groupby(shifts, lambda s: s.task_template_id):
s = [v for v in val]
shifts_and_count.append([len(s), s[0]])
return request.render(
'beesdoo_portal_shift.shift_template',
{'shift_templates': shift_templates}
return request.render('beesdoo_portal_shift.shift_template',
{'shift_templates': shifts_and_count}
)
@http.route('/shift_template_regular_worker', auth='public', website=True)
def shift_template_regular_worker(self, **kwargs):
# Get all the task template
task_templates = request.env['beesdoo.shift.template'].sudo().search(
[],
order="planning_id, day_nb_id, start_time",
)
# Compute start_time and end_time
task_template_times = []
cur_start_hour = 0
cur_start_minute = 0
cur_end_hour = 0
cur_end_minute = 0
for template in task_templates:
cur_start_hour = floor(template.start_time)
cur_start_minute = floor((template.start_time -
cur_start_hour) * 60)
cur_end_hour = floor(template.end_time)
cur_end_minute = floor((template.end_time -
cur_end_hour) * 60)
task_template_times.append(
{"start_hour": "%02d" % cur_start_hour,
"start_minute": "%02d" % cur_start_minute,
"end_hour": "%02d" % cur_end_hour,
"end_minute": "%02d" % cur_end_minute}
)
return request.render(
'beesdoo_portal_shift.task_template',
{'task_templates': task_templates,
'task_template_times': task_template_times}
)
class ShiftTemplate(object):
shift = None
start_time = None
end_time = None
name = ''
task_type = ''
remaining_space = 0
def __init__(self, shift, start_time, end_time, name, task_type):
self.shift = shift
self.start_time = start_time
self.end_time = end_time
self.name = name
self.task_type = task_type
template = request.env['beesdoo.shift.template']
task_templates = template.sudo().search([], order="planning_id, day_nb_id, start_time")
return request.render('beesdoo_portal_shift.task_template',
{
'task_templates': task_templates,
'float_to_time': float_to_time
}
)

18
beesdoo_portal_shift/views/shift_portal_templates.xml

@ -44,8 +44,8 @@
<t t-esc="template.day_nb_id.name"/>
</td>
<td>
<t t-esc='task_template_times[template_index]["start_hour"]'/>:<t t-esc='task_template_times[template_index]["start_minute"]'/> -
<t t-esc='task_template_times[template_index]["end_hour"]'/>:<t t-esc='task_template_times[template_index]["end_minute"]'/>
<t t-esc='float_to_time(template.start_time)' /> -
<t t-esc='float_to_time(template.end_time)'/>
</td>
<td>
<t t-esc="template.task_type_id.name"/>
@ -92,7 +92,9 @@
</tr>
</thead>
<tbody>
<t t-foreach="shift_templates" t-as="shift">
<t t-foreach="shift_templates" t-as="shift_and_count">
<t t-set="count" t-value="shift_and_count[0]" />
<t t-set="shift" t-value="shift_and_count[1]" />
<tr>
<td>
<t t-esc="time.strftime('%A', time.strptime(shift.start_time, '%Y-%m-%d %H:%M:%S'))"/>
@ -101,17 +103,17 @@
<t t-esc="time.strftime('%d %B %Y', time.strptime(shift.start_time, '%Y-%m-%d %H:%M:%S'))"/>
</td>
<td>
<span t-field="shift.shift.start_time" t-field-options='{"format": "HH:mm"}'/> -
<span t-field="shift.shift.end_time" t-field-options='{"format": "HH:mm"}'/>
<span t-field="shift.start_time" t-field-options='{"format": "HH:mm"}'/> -
<span t-field="shift.end_time" t-field-options='{"format": "HH:mm"}'/>
</td>
<td>
<t t-esc="shift.name"/>
<t t-esc="shift.task_template_id.name"/>
</td>
<td>
<t t-esc="shift.task_type"/>
<t t-esc="shift.task_type_id.name"/>
</td>
<td class="text-center">
<t t-esc="shift.remaining_space"/>
<t t-esc="count"/>
</td>
</tr>
</t>

Loading…
Cancel
Save