diff options
| author | RomainLibert <rli@odoo.com> | 2018-10-19 10:09:56 +0200 |
|---|---|---|
| committer | Yannick Tivisse <yti@odoo.com> | 2018-12-04 11:55:07 +0100 |
| commit | ad99f09a062cd190c9863d49486e60899daed2a6 (patch) | |
| tree | 9902ae2601dd1bcefc1a0b7b1a4439a93b06e7e4 /addons/lunch/models | |
| parent | a2c5b44df8c538b0a43b86502849e72c55c2d2ff (diff) | |
[REF] lunch: split lunch.py into smaller files
We should basically have one model per file, as this will facilitate the
reading and maintenance of the code
Diffstat (limited to 'addons/lunch/models')
| -rw-r--r-- | addons/lunch/models/__init__.py | 5 | ||||
| -rw-r--r-- | addons/lunch/models/lunch_alert.py | 85 | ||||
| -rw-r--r-- | addons/lunch/models/lunch_cashmove.py | 23 | ||||
| -rw-r--r-- | addons/lunch/models/lunch_order.py (renamed from addons/lunch/models/lunch.py) | 156 | ||||
| -rw-r--r-- | addons/lunch/models/lunch_product.py | 63 |
5 files changed, 175 insertions, 157 deletions
diff --git a/addons/lunch/models/__init__.py b/addons/lunch/models/__init__.py index 9824b4ad3b0..f99750c87d0 100644 --- a/addons/lunch/models/__init__.py +++ b/addons/lunch/models/__init__.py @@ -1,3 +1,6 @@ # -*- coding: utf-8 -*- -from . import lunch +from . import lunch_alert +from . import lunch_cashmove +from . import lunch_order +from . import lunch_product diff --git a/addons/lunch/models/lunch_alert.py b/addons/lunch/models/lunch_alert.py new file mode 100644 index 00000000000..325a64f4385 --- /dev/null +++ b/addons/lunch/models/lunch_alert.py @@ -0,0 +1,85 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +import datetime + +from odoo import api, fields, models, _ + + +class LunchAlert(models.Model): + """ Alerts to display during a lunch order. An alert can be specific to a + given day, weekly or daily. The alert is displayed from start to end hour. """ + _name = 'lunch.alert' + _description = 'Lunch Alert' + _rec_name = 'message' + + display = fields.Boolean(compute='_compute_display_get') + message = fields.Text('Message', required=True) + alert_type = fields.Selection([('specific', 'Specific Day'), + ('week', 'Every Week'), + ('days', 'Every Day')], + string='Recurrence', required=True, index=True, default='specific') + partner_id = fields.Many2one('res.partner', string="Vendor", + help="If specified, the selected vendor can be ordered only on selected days") + specific_day = fields.Date('Day', default=fields.Date.context_today) + monday = fields.Boolean('Monday') + tuesday = fields.Boolean('Tuesday') + wednesday = fields.Boolean('Wednesday') + thursday = fields.Boolean('Thursday') + friday = fields.Boolean('Friday') + saturday = fields.Boolean('Saturday') + sunday = fields.Boolean('Sunday') + start_hour = fields.Float('Between', oldname='active_from', required=True, default=7) + end_hour = fields.Float('And', oldname='active_to', required=True, default=23) + active = fields.Boolean(default=True) + + @api.multi + def name_get(self): + return [(alert.id, '%s %s' % (_('Alert'), '#%d' % alert.id)) for alert in self] + + @api.depends('alert_type', 'specific_day', 'monday', 'tuesday', 'thursday', + 'friday', 'saturday', 'sunday', 'start_hour', 'end_hour') + def _compute_display_get(self): + """ + This method check if the alert can be displayed today + if alert type is specific : compare specific_day(date) with today's date + if alert type is week : check today is set as alert (checkbox true) eg. self['monday'] + if alert type is day : True + return : Message if can_display_alert is True else False + """ + days_codes = {'0': 'sunday', + '1': 'monday', + '2': 'tuesday', + '3': 'wednesday', + '4': 'thursday', + '5': 'friday', + '6': 'saturday'} + fullday = False + now = datetime.datetime.now() + if self.env.context.get('lunch_date'): + # lunch_date is a fields.Date -> 00:00:00 + lunch_date = fields.Datetime.from_string(self.env.context['lunch_date']) + # if lunch_date is in the future, planned lunch, ignore hours + fullday = lunch_date > now + now = max(lunch_date, now) + mynow = fields.Datetime.context_timestamp(self, now) + + for alert in self: + can_display_alert = { + 'specific': (str(alert.specific_day) == fields.Date.to_string(mynow)), + 'week': alert[days_codes[mynow.strftime('%w')]], + 'days': True + } + + if can_display_alert[alert.alert_type]: + hour_to = int(alert.end_hour) + min_to = int((alert.end_hour - hour_to) * 60) + to_alert = datetime.time(hour_to, min_to) + hour_from = int(alert.start_hour) + min_from = int((alert.start_hour - hour_from) * 60) + from_alert = datetime.time(hour_from, min_from) + + if fullday or (from_alert <= mynow.time() <= to_alert): + alert.display = True + else: + alert.display = False diff --git a/addons/lunch/models/lunch_cashmove.py b/addons/lunch/models/lunch_cashmove.py new file mode 100644 index 00000000000..7d4a6b56ae4 --- /dev/null +++ b/addons/lunch/models/lunch_cashmove.py @@ -0,0 +1,23 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +from odoo import api, fields, models, _ + + +class LunchCashMove(models.Model): + """ Two types of cashmoves: payment (credit) or order (debit) """ + _name = 'lunch.cashmove' + _description = 'Lunch Cashmove' + + user_id = fields.Many2one('res.users', 'User', + default=lambda self: self.env.uid) + date = fields.Date('Date', required=True, default=fields.Date.context_today) + amount = fields.Float('Amount', required=True, help='Can be positive (payment) or negative (order or payment if user wants to get his money back)') + description = fields.Text('Description', help='Can be an order or a payment') + order_id = fields.Many2one('lunch.order.line', 'Order', ondelete='cascade') + state = fields.Selection([('order', 'Order'), ('payment', 'Payment')], + 'Is an order or a payment', default='payment') + + @api.multi + def name_get(self): + return [(cashmove.id, '%s %s' % (_('Lunch Cashmove'), '#%d' % cashmove.id)) for cashmove in self] diff --git a/addons/lunch/models/lunch.py b/addons/lunch/models/lunch_order.py index 19aad72ef22..f4354913db8 100644 --- a/addons/lunch/models/lunch.py +++ b/addons/lunch/models/lunch_order.py @@ -4,12 +4,10 @@ from collections import OrderedDict import json -import datetime from odoo import api, fields, models, _ from odoo.exceptions import AccessError, ValidationError from odoo.addons import decimal_precision as dp -from odoo.osv import expression class LunchOrder(models.Model): @@ -315,157 +313,3 @@ class LunchOrderLine(models.Model): self.cashmove.unlink() else: raise AccessError(_("Only your lunch manager cancels the orders.")) - - -class LunchProduct(models.Model): - """ Products available to order. A product is linked to a specific vendor. """ - _name = 'lunch.product' - _description = 'Lunch Product' - - name = fields.Char('Product', required=True) - category_id = fields.Many2one('lunch.product.category', 'Product Category', required=True) - description = fields.Text('Description') - price = fields.Float('Price', digits=dp.get_precision('Account')) - supplier = fields.Many2one('res.partner', 'Vendor') - active = fields.Boolean(default=True) - available = fields.Boolean(compute='_get_available_product', search='_search_available_products') - - @api.depends('supplier') - def _get_available_product(self): - for product in self: - if not product.supplier: - product.available = True - else: - alerts = self.env['lunch.alert'].search([ - ('partner_id', '=', self.supplier.id) - ]) - if alerts and not any(alert.display for alert in alerts): - # every alert is not available - product.available = False - else: - # no alert for the supplier or at least one is not available - product.available = True - - def _search_available_products(self, operator, value): - alerts = self.env['lunch.alert'].search([]) - supplier_w_alerts = alerts.mapped('partner_id') - available_suppliers = alerts.filtered(lambda a: a.display).mapped('partner_id') - available_products = self.search([ - '|', - ('supplier', 'not in', supplier_w_alerts.ids), - ('supplier', 'in', available_suppliers.ids) - ]) - - if (operator in expression.NEGATIVE_TERM_OPERATORS and value) or \ - (operator not in expression.NEGATIVE_TERM_OPERATORS and not value): - # e.g. (available = False) or (available != True) - return [('id', 'not in', available_products.ids)] - else: - # e.g. (available = True) or (available != False) - return [('id', 'in', available_products.ids)] - - -class LunchProductCategory(models.Model): - """ Category of the product such as pizza, sandwich, pasta, chinese, burger... """ - _name = 'lunch.product.category' - _description = 'Lunch Product Category' - - name = fields.Char('Product Category', required=True) - - -class LunchCashMove(models.Model): - """ Two types of cashmoves: payment (credit) or order (debit) """ - _name = 'lunch.cashmove' - _description = 'Lunch Cashmove' - - user_id = fields.Many2one('res.users', 'User', - default=lambda self: self.env.uid) - date = fields.Date('Date', required=True, default=fields.Date.context_today) - amount = fields.Float('Amount', required=True, help='Can be positive (payment) or negative (order or payment if user wants to get his money back)') - description = fields.Text('Description', help='Can be an order or a payment') - order_id = fields.Many2one('lunch.order.line', 'Order', ondelete='cascade') - state = fields.Selection([('order', 'Order'), ('payment', 'Payment')], - 'Is an order or a payment', default='payment') - - @api.multi - def name_get(self): - return [(cashmove.id, '%s %s' % (_('Lunch Cashmove'), '#%d' % cashmove.id)) for cashmove in self] - - -class LunchAlert(models.Model): - """ Alerts to display during a lunch order. An alert can be specific to a - given day, weekly or daily. The alert is displayed from start to end hour. """ - _name = 'lunch.alert' - _description = 'Lunch Alert' - _rec_name = 'message' - - display = fields.Boolean(compute='_compute_display_get') - message = fields.Text('Message', required=True) - alert_type = fields.Selection([('specific', 'Specific Day'), - ('week', 'Every Week'), - ('days', 'Every Day')], - string='Recurrence', required=True, index=True, default='specific') - partner_id = fields.Many2one('res.partner', string="Vendor", - help="If specified, the selected vendor can be ordered only on selected days") - specific_day = fields.Date('Day', default=fields.Date.context_today) - monday = fields.Boolean('Monday') - tuesday = fields.Boolean('Tuesday') - wednesday = fields.Boolean('Wednesday') - thursday = fields.Boolean('Thursday') - friday = fields.Boolean('Friday') - saturday = fields.Boolean('Saturday') - sunday = fields.Boolean('Sunday') - start_hour = fields.Float('Between', oldname='active_from', required=True, default=7) - end_hour = fields.Float('And', oldname='active_to', required=True, default=23) - active = fields.Boolean(default=True) - - @api.multi - def name_get(self): - return [(alert.id, '%s %s' % (_('Alert'), '#%d' % alert.id)) for alert in self] - - @api.depends('alert_type', 'specific_day', 'monday', 'tuesday', 'thursday', - 'friday', 'saturday', 'sunday', 'start_hour', 'end_hour') - def _compute_display_get(self): - """ - This method check if the alert can be displayed today - if alert type is specific : compare specific_day(date) with today's date - if alert type is week : check today is set as alert (checkbox true) eg. self['monday'] - if alert type is day : True - return : Message if can_display_alert is True else False - """ - days_codes = {'0': 'sunday', - '1': 'monday', - '2': 'tuesday', - '3': 'wednesday', - '4': 'thursday', - '5': 'friday', - '6': 'saturday'} - fullday = False - now = datetime.datetime.now() - if self.env.context.get('lunch_date'): - # lunch_date is a fields.Date -> 00:00:00 - lunch_date = fields.Datetime.from_string(self.env.context['lunch_date']) - # if lunch_date is in the future, planned lunch, ignore hours - fullday = lunch_date > now - now = max(lunch_date, now) - mynow = fields.Datetime.context_timestamp(self, now) - - for alert in self: - can_display_alert = { - 'specific': (str(alert.specific_day) == fields.Date.to_string(mynow)), - 'week': alert[days_codes[mynow.strftime('%w')]], - 'days': True - } - - if can_display_alert[alert.alert_type]: - hour_to = int(alert.end_hour) - min_to = int((alert.end_hour - hour_to) * 60) - to_alert = datetime.time(hour_to, min_to) - hour_from = int(alert.start_hour) - min_from = int((alert.start_hour - hour_from) * 60) - from_alert = datetime.time(hour_from, min_from) - - if fullday or (from_alert <= mynow.time() <= to_alert): - alert.display = True - else: - alert.display = False diff --git a/addons/lunch/models/lunch_product.py b/addons/lunch/models/lunch_product.py new file mode 100644 index 00000000000..8ded108fc81 --- /dev/null +++ b/addons/lunch/models/lunch_product.py @@ -0,0 +1,63 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +from odoo import api, fields, models, _ + +from odoo.addons import decimal_precision as dp +from odoo.osv import expression + + +class LunchProductCategory(models.Model): + """ Category of the product such as pizza, sandwich, pasta, chinese, burger... """ + _name = 'lunch.product.category' + _description = 'Lunch Product Category' + + name = fields.Char('Product Category', required=True) + + +class LunchProduct(models.Model): + """ Products available to order. A product is linked to a specific vendor. """ + _name = 'lunch.product' + _description = 'Lunch Product' + + name = fields.Char('Product', required=True) + category_id = fields.Many2one('lunch.product.category', 'Product Category', required=True) + description = fields.Text('Description') + price = fields.Float('Price', digits=dp.get_precision('Account')) + supplier = fields.Many2one('res.partner', 'Vendor') + active = fields.Boolean(default=True) + available = fields.Boolean(compute='_get_available_product', search='_search_available_products') + + @api.depends('supplier') + def _get_available_product(self): + for product in self: + if not product.supplier: + product.available = True + else: + alerts = self.env['lunch.alert'].search([ + ('partner_id', '=', self.supplier.id) + ]) + if alerts and not any(alert.display for alert in alerts): + # every alert is not available + product.available = False + else: + # no alert for the supplier or at least one is not available + product.available = True + + def _search_available_products(self, operator, value): + alerts = self.env['lunch.alert'].search([]) + supplier_w_alerts = alerts.mapped('partner_id') + available_suppliers = alerts.filtered(lambda a: a.display).mapped('partner_id') + available_products = self.search([ + '|', + ('supplier', 'not in', supplier_w_alerts.ids), + ('supplier', 'in', available_suppliers.ids) + ]) + + if (operator in expression.NEGATIVE_TERM_OPERATORS and value) or \ + (operator not in expression.NEGATIVE_TERM_OPERATORS and not value): + # e.g. (available = False) or (available != True) + return [('id', 'not in', available_products.ids)] + else: + # e.g. (available = True) or (available != False) + return [('id', 'in', available_products.ids)] |
