From b4483cc32fdaffba85b2130dddb8ee3a9dda87a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Taymans?= Date: Thu, 4 Jan 2018 18:29:54 +0100 Subject: [PATCH] [IMP] website_shift: Refactor templates Refactor templates to avoid dupplicate code. Refactor the controller to match with new templates. Add license. --- beesdoo_website_shift/__openerp__.py | 10 +- beesdoo_website_shift/controllers/main.py | 252 +++++-- .../data/res_config_data.xml | 4 + beesdoo_website_shift/models/res_config.py | 3 + .../views/my_shift_website_templates.xml | 532 +++++++++++++++ .../views/res_config_views.xml | 14 +- .../views/shift_website_templates.xml | 637 ++---------------- 7 files changed, 779 insertions(+), 673 deletions(-) create mode 100644 beesdoo_website_shift/views/my_shift_website_templates.xml diff --git a/beesdoo_website_shift/__openerp__.py b/beesdoo_website_shift/__openerp__.py index 24d1d7b..b07357f 100644 --- a/beesdoo_website_shift/__openerp__.py +++ b/beesdoo_website_shift/__openerp__.py @@ -1,6 +1,10 @@ # -*- coding: utf-8 -*- + +# Copyright 2017-2018 Rémy Taymans +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + { - 'name': 'Beescoop Shift Website', + 'name': 'BEES coop Shift Website', 'summary': """ Show available shifts for regular and irregular workers in @@ -10,16 +14,18 @@ """, 'author': 'Rémy Taymans', + 'license': 'AGPL-3', + 'version': '9.0.1.0', 'website': "https://github.com/beescoop/Obeesdoo", 'category': 'Cooperative management', - 'version': '0.1', 'depends': ['website', 'beesdoo_shift'], 'data': [ 'data/res_config_data.xml', 'views/shift_website_templates.xml', + 'views/my_shift_website_templates.xml', 'views/res_config_views.xml', ] } diff --git a/beesdoo_website_shift/controllers/main.py b/beesdoo_website_shift/controllers/main.py index 7690f4c..f95e8a8 100644 --- a/beesdoo_website_shift/controllers/main.py +++ b/beesdoo_website_shift/controllers/main.py @@ -1,4 +1,9 @@ # -*- coding: utf8 -*- + +# Copyright 2017-2018 Rémy Taymans +# Copyright 2017-2018 Thibault François +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + from ast import literal_eval from datetime import datetime, timedelta from itertools import groupby @@ -11,26 +16,57 @@ from openerp.addons.beesdoo_shift.models.planning import float_to_time class WebsiteShiftController(http.Controller): - @http.route('/shift', auth='user', website=True) - def shift(self, **kwargs): - cur_user = request.env['res.users'].browse(request.uid) - working_mode = cur_user.partner_id.working_mode - if working_mode == 'irregular': - return self.shift_irregular_worker() - if working_mode == 'regular': - return self.shift_template_regular_worker() - if working_mode == 'exempt': - return self.shift_exempted_worker() + def is_user_irregular(self): + user = request.env['res.users'].browse(request.uid) + working_mode = user.partner_id.working_mode + return working_mode == 'irregular' + + def is_user_regular(self): + user = request.env['res.users'].browse(request.uid) + working_mode = user.partner_id.working_mode + return working_mode == 'regular' + + def is_user_exempted(self): + user = request.env['res.users'].browse(request.uid) + working_mode = user.partner_id.working_mode + return working_mode == 'exempt' + + @http.route('/my/shift', auth='user', website=True) + def my_shift(self, **kw): + """ + Personnal page for managing your shifts + """ + if self.is_user_irregular(): + return request.render( + 'beesdoo_website_shift.my_shift_irregular_worker', + self.my_shift_irregular_worker(nexturl='/my/shift') + ) + if self.is_user_regular(): + return request.render( + 'beesdoo_website_shift.my_shift_regular_worker', + self.my_shift_regular_worker() + ) + if self.is_user_exempted(): + return request.render( + 'beesdoo_website_shift.my_shift_exempted_worker', + self.my_shift_exempted_worker() + ) return request.render( - 'beesdoo_website_shift.shift', - { - 'user': cur_user, - } + 'beesdoo_website_shift.my_shift_non_worker', + {} ) @http.route('/shift//subscribe', auth='user', website=True) - def subscribe_to_shift(self, shift=None, **kwargs): + def subscribe_to_shift(self, shift=None, **kw): + """ + Subscribe the current connected user into the given shift + This is done only if : + * shift sign up is authorised via configuration panel + * the current connected user is an irregular worker + * the given shift exist + * the shift is free for subscription + """ # Get current user cur_user = request.env['res.users'].browse(request.uid) # Get config @@ -42,13 +78,112 @@ class WebsiteShiftController(http.Controller): and shift and not shift.worker_id): shift.worker_id = cur_user.partner_id - return request.redirect(kwargs['nexturl']) + return request.redirect(kw['nexturl']) + + @http.route('/shift_irregular_worker', auth='public', website=True) + def public_shift_irregular_worker(self, **kw): + """ + Show a public access page that show all the available shifts for irregular worker. + """ + nexturl = '/shift_irregular_worker' + irregular_enable_sign_up = False + + # Create template context + template_context = {} + template_context.update(self.available_shift_irregular_worker( + irregular_enable_sign_up, nexturl + )) + + return request.render( + 'beesdoo_website_shift.public_shift_irregular_worker', + template_context + ) + + @http.route('/shift_template_regular_worker', auth='public', website=True) + def public_shift_template_regular_worker(self, **kw): + """ + Show a public access page that show all the available shift templates for regular worker. + """ + # Get all the task template + template = request.env['beesdoo.shift.template'] + task_templates = template.sudo().search([], order="planning_id, day_nb_id, start_time") + + return request.render( + 'beesdoo_website_shift.public_shift_template_regular_worker', + { + 'task_templates': task_templates, + 'float_to_time': float_to_time, + } + ) - def shift_irregular_worker(self, **kwargs): + def my_shift_irregular_worker(self, nexturl=""): + """ + Return template variables for 'beesdoo_website_shift.my_shift_irregular_worker' template + """ # Get current user cur_user = request.env['res.users'].browse(request.uid) cur_cooperative_status = cur_user.partner_id.cooperative_status_ids + # Get config + irregular_enable_sign_up = literal_eval(request.env['ir.config_parameter'].get_param( + 'beesdoo_website_shift.irregular_enable_sign_up')) + + # Create template context + template_context = {} + + template_context.update(self.my_shift_worker_status()) + template_context.update(self.my_shift_next_shifts()) + template_context.update(self.available_shift_irregular_worker( + irregular_enable_sign_up, nexturl + )) + + # Compute date before which the worker is up to date + today_date = fields.Date.from_string(cur_cooperative_status.today) + delta = (today_date - fields.Date.from_string(cur_cooperative_status.irregular_start_date)).days + date_before_last_shift = today_date + timedelta(days=(cur_cooperative_status.sr + 1) * 28 - delta % 28) + date_before_last_shift = date_before_last_shift.strftime('%Y-%m-%d') + + template_context.update( + { + 'date_before_last_shift': date_before_last_shift, + } + ) + return template_context + + def my_shift_regular_worker(self): + """ + Return template variables for 'beesdoo_website_shift.my_shift_regular_worker' template + """ + # Create template context + template_context = {} + + # Get all the task template + template = request.env['beesdoo.shift.template'] + task_templates = template.sudo().search([], order="planning_id, day_nb_id, start_time") + + template_context.update(self.my_shift_worker_status()) + template_context.update(self.my_shift_next_shifts()) + template_context.update( + { + 'task_templates': task_templates, + 'float_to_time': float_to_time, + } + ) + return template_context + + def my_shift_exempted_worker(self): + """ + Return template variables for 'beesdoo_website_shift.my_shift_exempted_worker' template + """ + return self.my_shift_worker_status() + + def available_shift_irregular_worker(self, irregular_enable_sign_up=False, nexturl=""): + """ + Return template variables for 'beesdoo_website_shift.available_shift_irregular_worker' template + """ + # Get current user + cur_user = request.env['res.users'].browse(request.uid) + # Get all the shifts in the future with no worker now = datetime.now() shifts = request.env['beesdoo.shift.shift'].sudo().search( @@ -64,12 +199,6 @@ class WebsiteShiftController(http.Controller): order="start_time, task_template_id, task_type_id", ) - # Compute date before which the worker is up to date - today_date = fields.Date.from_string(cur_cooperative_status.today) - delta = (today_date - fields.Date.from_string(cur_cooperative_status.irregular_start_date)).days - date_before_last_shift = today_date + timedelta(days=(cur_cooperative_status.sr + 1) * 28 - delta % 28) - date_before_last_shift = date_before_last_shift.strftime('%Y-%m-%d') - # Get config irregular_shift_limit = int(request.env['ir.config_parameter'].get_param( 'beesdoo_website_shift.irregular_shift_limit')) @@ -77,22 +206,23 @@ class WebsiteShiftController(http.Controller): 'beesdoo_website_shift.highlight_rule')) hide_rule = int(request.env['ir.config_parameter'].get_param( 'beesdoo_website_shift.hide_rule')) / 100.0 - irregular_enable_sign_up = literal_eval(request.env['ir.config_parameter'].get_param( - 'beesdoo_website_shift.irregular_enable_sign_up')) # Grouby task_template_id, if no task_template_id is specified - # then group by start_time - groupby_func = lambda s: (s.task_template_id, s.start_time, s.task_type_id) - groupby_iter = groupby(shifts, groupby_func) + # then group by start_time, if no start_time specified sort by + # task_type + groupby_iter = groupby( + shifts, + lambda s: (s.task_template_id, s.start_time, s.task_type_id) + ) shifts_count_subscribed = [] nb_displayed_shift = 0 # Number of shift displayed for (keys, grouped_shifts) in groupby_iter: (task_template, start_time, task_type) = keys nb_displayed_shift = nb_displayed_shift + 1 - s = list(grouped_shifts) + shift_list = list(grouped_shifts) # Compute available space - free_space = len(s) + free_space = len(shift_list) # Is the current user subscribed to this task_template is_subscribed = any( (sub_shift.task_template_id == task_template and @@ -100,33 +230,24 @@ class WebsiteShiftController(http.Controller): sub_shift.task_type_id == task_type) for sub_shift in subscribed_shifts) if free_space >= task_template.worker_nb * hide_rule: - shifts_count_subscribed.append([s[0], free_space, is_subscribed]) + shifts_count_subscribed.append([shift_list[0], free_space, is_subscribed]) # Stop showing shifts if the limit is reached if irregular_shift_limit > 0 and nb_displayed_shift >= irregular_shift_limit: break - return request.render( - 'beesdoo_website_shift.irregular_worker', - { - 'partner': cur_user.partner_id, - 'status': cur_cooperative_status, - 'date_before_last_shift': date_before_last_shift, - 'shift_templates': shifts_count_subscribed, - 'highlight_rule': highlight_rule, - 'nexturl': '/shift', - 'subscribed_shifts': subscribed_shifts, - 'irregular_enable_sign_up': irregular_enable_sign_up, - } - ) + return { + 'shift_templates': shifts_count_subscribed, + 'highlight_rule': highlight_rule, + 'nexturl': nexturl, + 'irregular_enable_sign_up': irregular_enable_sign_up, + } - def shift_template_regular_worker(self, **kwargs): + def my_shift_next_shifts(self): + """ + Return template variables for 'beesdoo_website_shift.my_shift_next_shifts' template + """ # Get current user cur_user = request.env['res.users'].browse(request.uid) - - # Get all the task template - template = request.env['beesdoo.shift.template'] - task_templates = template.sudo().search([], order="planning_id, day_nb_id, start_time") - # Get shifts where user is subscribed now = datetime.now() subscribed_shifts = request.env['beesdoo.shift.shift'].sudo().search( @@ -134,26 +255,15 @@ class WebsiteShiftController(http.Controller): ('worker_id', '=', cur_user.partner_id.id)], order="start_time, task_template_id, task_type_id", ) + return { + 'subscribed_shifts': subscribed_shifts, + } - return request.render( - 'beesdoo_website_shift.regular_worker', - { - 'partner': cur_user.partner_id, - 'status': cur_user.partner_id.cooperative_status_ids, - 'task_templates': task_templates, - 'float_to_time': float_to_time, - 'subscribed_shifts': subscribed_shifts, - } - ) - - def shift_exempted_worker(self, **kwargs): - # Get current user + def my_shift_worker_status(self): + """ + Return template variables for 'beesdoo_website_shift.my_shift_worker_status_*' template + """ cur_user = request.env['res.users'].browse(request.uid) - - return request.render( - 'beesdoo_website_shift.exempted_worker', - { - 'partner': cur_user.partner_id, - 'status': cur_user.partner_id.cooperative_status_ids, - } - ) + return { + 'status': cur_user.partner_id.cooperative_status_ids, + } diff --git a/beesdoo_website_shift/data/res_config_data.xml b/beesdoo_website_shift/data/res_config_data.xml index 7765da1..1defe4f 100644 --- a/beesdoo_website_shift/data/res_config_data.xml +++ b/beesdoo_website_shift/data/res_config_data.xml @@ -1,4 +1,8 @@ + diff --git a/beesdoo_website_shift/models/res_config.py b/beesdoo_website_shift/models/res_config.py index 23f9add..8cf3a7a 100644 --- a/beesdoo_website_shift/models/res_config.py +++ b/beesdoo_website_shift/models/res_config.py @@ -1,5 +1,8 @@ # -*- coding: utf-8 -*- +# Copyright 2017-2018 Rémy Taymans +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + from ast import literal_eval from openerp import fields, models, api diff --git a/beesdoo_website_shift/views/my_shift_website_templates.xml b/beesdoo_website_shift/views/my_shift_website_templates.xml new file mode 100644 index 0000000..80337ab --- /dev/null +++ b/beesdoo_website_shift/views/my_shift_website_templates.xml @@ -0,0 +1,532 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/beesdoo_website_shift/views/res_config_views.xml b/beesdoo_website_shift/views/res_config_views.xml index 352e0cd..ce6b5dc 100644 --- a/beesdoo_website_shift/views/res_config_views.xml +++ b/beesdoo_website_shift/views/res_config_views.xml @@ -1,12 +1,16 @@ + - Website Shift Settings + Website Shift Settings Irregular Worker beesdoo.website.shift.config.settings -
+