class ProductTemplate(models.Model): _inherit = "product.template" quotation_only_description = fields.Html( 'Quotation Only Description', sanitize_attributes=False, translate=html_translate, help="The quotation description (not used on eCommerce)") quotation_description = fields.Html( 'Quotation Description', compute='_compute_quotation_description', help= "This field uses the Quotation Only Description if it is defined, otherwise it will try to read the eCommerce Description." ) @api.multi def _compute_quotation_description(self): for record in self: if record.quotation_only_description: record.quotation_description = record.quotation_only_description elif hasattr(record, 'website_description') and record.website_description: record.quotation_description = record.website_description else: record.quotation_description = ''
class Job(models.Model): _name = 'hr.job' _inherit = [ 'hr.job', 'website.seo.metadata', 'website.published.multi.mixin' ] def _get_default_website_description(self): default_description = self.env["ir.model.data"].xmlid_to_object( "website_hr_recruitment.default_website_description") return (default_description.render() if default_description else "") website_description = fields.Html('Website description', translate=html_translate, sanitize_attributes=False, default=_get_default_website_description, prefetch=False) @api.multi def _compute_website_url(self): super(Job, self)._compute_website_url() for job in self: job.website_url = "/jobs/detail/%s" % job.id @api.multi def set_open(self): self.write({'website_published': False}) return super(Job, self).set_open()
class SaleOrderTemplateLine(models.Model): _inherit = "sale.order.template.line" website_description = fields.Html( 'Website Description', related='product_id.product_tmpl_id.quotation_only_description', translate=html_translate, readonly=False) @api.onchange('product_id') def _onchange_product_id(self): ret = super(SaleOrderTemplateLine, self)._onchange_product_id() if self.product_id: self.website_description = self.product_id.quotation_description return ret @api.model def create(self, values): values = self._inject_quotation_description(values) return super(SaleOrderTemplateLine, self).create(values) @api.multi def write(self, values): values = self._inject_quotation_description(values) return super(SaleOrderTemplateLine, self).write(values) def _inject_quotation_description(self, values): values = dict(values or {}) if not values.get('website_description') and values.get('product_id'): product = self.env['product.product'].browse(values['product_id']) values['website_description'] = product.quotation_description return values
class ConverterTest(models.Model): _name = 'web_editor.converter.test' _description = 'Web Editor Converter Test' # disable translation export for those brilliant field labels and values _translate = False char = fields.Char() integer = fields.Integer() float = fields.Float() numeric = fields.Float(digits=(16, 2)) many2one = fields.Many2one('web_editor.converter.test.sub') binary = fields.Binary() date = fields.Date() datetime = fields.Datetime() selection = fields.Selection([ (1, "réponse A"), (2, "réponse B"), (3, "réponse C"), (4, "réponse <D>"), ]) selection_str = fields.Selection([ ('A', "Qu'il n'est pas arrivé à Toronto"), ('B', "Qu'il était supposé arriver à Toronto"), ('C', "Qu'est-ce qu'il fout ce maudit pancake, tabernacle ?"), ('D', "La réponse D"), ], string=u"Lorsqu'un pancake prend l'avion à destination de Toronto et " u"qu'il fait une escale technique à St Claude, on dit:") html = fields.Html() text = fields.Text()
class WebsiteResPartner(models.Model): _name = 'res.partner' _inherit = ['res.partner', 'website.seo.metadata', 'website.published.mixin'] website_description = fields.Html('Website Partner Full Description', strip_style=True, translate=html_translate) website_short_description = fields.Text('Website Partner Short Description', translate=True) @api.multi def _compute_website_url(self): super(WebsiteResPartner, self)._compute_website_url() for partner in self: partner.website_url = "/partners/%s" % slug(partner)
class SaleOrderTemplateOption(models.Model): _inherit = "sale.order.template.option" website_description = fields.Html('Website Description', translate=html_translate, sanitize_attributes=False) @api.onchange('product_id') def _onchange_product_id(self): ret = super(SaleOrderTemplateOption, self)._onchange_product_id() if self.product_id: self.website_description = self.product_id.quotation_description return ret
class SaleOrderTemplate(models.Model): _inherit = "sale.order.template" website_description = fields.Html('Website Description', translate=html_translate, sanitize_attributes=False) @api.multi def open_template(self): self.ensure_one() return { 'type': 'ir.actions.act_url', 'target': 'self', 'url': '/sale_quotation_builder/template/%d' % self.id }
class MassMailingList(models.Model): _inherit = 'mail.mass_mailing.list' def _default_popup_content(self): return """<div class="modal-header text-center"> <h3 class="modal-title mt8">Swerp Presents</h3> </div> <div class="o_popup_message"> <font>7</font> <strong>Business Hacks</strong> <span> to<br/>boost your marketing</span> </div> <p class="o_message_paragraph">Join our Marketing newsletter and get <strong>this white paper instantly</strong></p>""" popup_content = fields.Html(string="Website Popup Content", translate=True, sanitize_attributes=False, default=_default_popup_content) popup_redirect_url = fields.Char(string="Website Popup Redirect URL", default='/')
class DigestTip(models.Model): _name = 'digest.tip' _description = 'Digest Tips' _order = 'sequence' sequence = fields.Integer( 'Sequence', default=1, help='Used to display digest tip in email template base on order') user_ids = fields.Many2many('res.users', string='Recipients', help='Users having already received this tip') tip_description = fields.Html('Tip description', translate=html_translate) group_id = fields.Many2one( 'res.groups', string='Authorized Group', default=lambda self: self.env.ref('base.group_user'))
class ImLivechatChannel(models.Model): _name = 'im_livechat.channel' _inherit = ['im_livechat.channel', 'website.published.mixin'] @api.multi def _compute_website_url(self): super(ImLivechatChannel, self)._compute_website_url() for channel in self: channel.website_url = "/livechat/channel/%s" % (slug(channel), ) website_description = fields.Html( "Website description", default=False, help="Description of the channel displayed on the website page", sanitize_attributes=False, translate=html_translate)
class SaleOrderOption(models.Model): _inherit = "sale.order.option" website_description = fields.Html('Website Description', sanitize_attributes=False, translate=html_translate) @api.onchange('product_id', 'uom_id') def _onchange_product_id(self): ret = super(SaleOrderOption, self)._onchange_product_id() if self.product_id: product = self.product_id.with_context( lang=self.order_id.partner_id.lang) self.website_description = product.quotation_description return ret @api.multi def _get_values_to_add_to_order(self): values = super(SaleOrderOption, self)._get_values_to_add_to_order() values.update(website_description=self.website_description) return values
class SaleOrderLine(models.Model): _inherit = "sale.order.line" website_description = fields.Html('Website Description', sanitize=False, translate=html_translate) @api.model def create(self, values): values = self._inject_quotation_description(values) return super(SaleOrderLine, self).create(values) @api.multi def write(self, values): values = self._inject_quotation_description(values) return super(SaleOrderLine, self).write(values) def _inject_quotation_description(self, values): values = dict(values or {}) if not values.get('website_description') and values.get('product_id'): product = self.env['product.product'].browse(values['product_id']) values.update(website_description=product.quotation_description) return values
class SaleOrder(models.Model): _inherit = 'sale.order' website_description = fields.Html('Website Description', sanitize_attributes=False, translate=html_translate) @api.onchange('partner_id') def onchange_update_description_lang(self): if not self.sale_order_template_id: return else: template = self.sale_order_template_id.with_context( lang=self.partner_id.lang) self.website_description = template.website_description def _compute_line_data_for_template_change(self, line): vals = super(SaleOrder, self)._compute_line_data_for_template_change(line) vals.update(website_description=line.website_description) return vals def _compute_option_data_for_template_change(self, option): vals = super(SaleOrder, self)._compute_option_data_for_template_change(option) vals.update(website_description=option.website_description) return vals @api.onchange('sale_order_template_id') def onchange_sale_order_template_id(self): ret = super(SaleOrder, self).onchange_sale_order_template_id() if self.sale_order_template_id: template = self.sale_order_template_id.with_context( lang=self.partner_id.lang) self.website_description = template.website_description return ret
class IrActions(models.Model): _name = 'ir.actions.actions' _description = 'Actions' _table = 'ir_actions' _order = 'name' name = fields.Char(required=True) type = fields.Char(string='Action Type', required=True) xml_id = fields.Char(compute='_compute_xml_id', string="External ID") help = fields.Html( string='Action Description', help= 'Optional help text for the users with a description of the target view, such as its usage and purpose.', translate=True) binding_model_id = fields.Many2one( 'ir.model', ondelete='cascade', help= "Setting a value makes this action available in the sidebar for the given model." ) binding_type = fields.Selection([('action', 'Action'), ('action_form_only', "Form-only"), ('report', 'Report')], required=True, default='action') def _compute_xml_id(self): res = self.get_external_id() for record in self: record.xml_id = res.get(record.id) @api.model_create_multi def create(self, vals_list): res = super(IrActions, self).create(vals_list) # self.get_bindings() depends on action records self.clear_caches() return res @api.multi def write(self, vals): res = super(IrActions, self).write(vals) # self.get_bindings() depends on action records self.clear_caches() return res @api.multi def unlink(self): """unlink ir.action.todo which are related to actions which will be deleted. NOTE: ondelete cascade will not work on ir.actions.actions so we will need to do it manually.""" todos = self.env['ir.actions.todo'].search([('action_id', 'in', self.ids)]) todos.unlink() res = super(IrActions, self).unlink() # self.get_bindings() depends on action records self.clear_caches() return res @api.model def _get_eval_context(self, action=None): """ evaluation context to pass to safe_eval """ return { 'uid': self._uid, 'user': self.env.user, 'time': time, 'datetime': datetime, 'dateutil': dateutil, 'timezone': timezone, 'float_compare': float_compare, 'b64encode': base64.b64encode, 'b64decode': base64.b64decode, } @api.model @tools.ormcache('frozenset(self.env.user.groups_id.ids)', 'model_name') def get_bindings(self, model_name): """ Retrieve the list of actions bound to the given model. :return: a dict mapping binding types to a list of dict describing actions, where the latter is given by calling the method ``read`` on the action record. """ cr = self.env.cr query = """ SELECT a.id, a.type, a.binding_type FROM ir_actions a, ir_model m WHERE a.binding_model_id=m.id AND m.model=%s ORDER BY a.id """ cr.execute(query, [model_name]) # discard unauthorized actions, and read action definitions result = defaultdict(list) user_groups = self.env.user.groups_id for action_id, action_model, binding_type in cr.fetchall(): try: action = self.env[action_model].browse(action_id) action_groups = getattr(action, 'groups_id', ()) if action_groups and not action_groups & user_groups: # the user may not perform this action continue result[binding_type].append(action.read()[0]) except (AccessError, MissingError): continue return result
class PosConfig(models.Model): _name = 'pos.config' _description = 'Point of Sale Configuration' def _default_sale_journal(self): journal = self.env.ref('point_of_sale.pos_sale_journal', raise_if_not_found=False) if journal and journal.sudo().company_id == self.env.user.company_id: return journal return self._default_invoice_journal() def _default_invoice_journal(self): return self.env['account.journal'].search( [('type', '=', 'sale'), ('company_id', '=', self.env.user.company_id.id)], limit=1) def _default_pricelist(self): return self.env['product.pricelist'].search( [('company_id', 'in', (False, self.env.user.company_id.id)), ('currency_id', '=', self.env.user.company_id.currency_id.id)], limit=1) def _get_default_location(self): return self.env['stock.warehouse'].search( [('company_id', '=', self.env.user.company_id.id)], limit=1).lot_stock_id def _get_group_pos_manager(self): return self.env.ref('point_of_sale.group_pos_manager') def _get_group_pos_user(self): return self.env.ref('point_of_sale.group_pos_user') def _compute_default_customer_html(self): return self.env['ir.qweb'].render( 'point_of_sale.customer_facing_display_html') name = fields.Char(string='Point of Sale Name', index=True, required=True, help="An internal identification of the point of sale.") is_installed_account_accountant = fields.Boolean( string="Is the Full Accounting Installed", compute="_compute_is_installed_account_accountant") journal_ids = fields.Many2many( 'account.journal', 'pos_config_journal_rel', 'pos_config_id', 'journal_id', string='Available Payment Methods', domain= "[('journal_user', '=', True ), ('type', 'in', ['bank', 'cash'])]", ) picking_type_id = fields.Many2one('stock.picking.type', string='Operation Type') use_existing_lots = fields.Boolean( related='picking_type_id.use_existing_lots', readonly=False) stock_location_id = fields.Many2one('stock.location', string='Stock Location', domain=[('usage', '=', 'internal')], required=True, default=_get_default_location) journal_id = fields.Many2one( 'account.journal', string='Sales Journal', domain=[('type', '=', 'sale')], help="Accounting journal used to post sales entries.", default=_default_sale_journal) invoice_journal_id = fields.Many2one( 'account.journal', string='Invoice Journal', domain=[('type', '=', 'sale')], help="Accounting journal used to create invoices.", default=_default_invoice_journal) currency_id = fields.Many2one('res.currency', compute='_compute_currency', string="Currency") iface_cashdrawer = fields.Boolean( string='Cashdrawer', help="Automatically open the cashdrawer.") iface_payment_terminal = fields.Boolean( string='Payment Terminal', help="Enables Payment Terminal integration.") iface_electronic_scale = fields.Boolean( string='Electronic Scale', help="Enables Electronic Scale integration.") iface_vkeyboard = fields.Boolean( string='Virtual KeyBoard', help= u"Don’t turn this option on if you take orders on smartphones or tablets. \n Such devices already benefit from a native keyboard." ) iface_customer_facing_display = fields.Boolean( string='Customer Facing Display', help="Show checkout to customers with a remotely-connected screen.") iface_print_via_proxy = fields.Boolean( string='Print via Proxy', help="Bypass browser printing and prints via the hardware proxy.") iface_scan_via_proxy = fields.Boolean( string='Scan via Proxy', help= "Enable barcode scanning with a remotely connected barcode scanner.") iface_big_scrollbars = fields.Boolean( 'Large Scrollbars', help='For imprecise industrial touchscreens.') iface_print_auto = fields.Boolean( string='Automatic Receipt Printing', default=False, help= 'The receipt will automatically be printed at the end of each order.') iface_print_skip_screen = fields.Boolean( string='Skip Preview Screen', default=True, help= 'The receipt screen will be skipped if the receipt can be printed automatically.' ) iface_precompute_cash = fields.Boolean( string='Prefill Cash Payment', help= 'The payment input will behave similarily to bank payment input, and will be prefilled with the exact due amount.' ) iface_tax_included = fields.Selection([('subtotal', 'Tax-Excluded Price'), ('total', 'Tax-Included Price')], string="Tax Display", default='subtotal', required=True) iface_start_categ_id = fields.Many2one( 'pos.category', string='Initial Category', help= 'The point of sale will display this product category by default. If no category is specified, all available products will be shown.' ) iface_display_categ_images = fields.Boolean( string='Display Category Pictures', help="The product categories will be displayed with pictures.") restrict_price_control = fields.Boolean( string='Restrict Price Modifications to Managers', help= "Only users with Manager access rights for PoS app can modify the product prices on orders." ) cash_control = fields.Boolean( string='Cash Control', help="Check the amount of the cashbox at opening and closing.") receipt_header = fields.Text( string='Receipt Header', help= "A short text that will be inserted as a header in the printed receipt." ) receipt_footer = fields.Text( string='Receipt Footer', help= "A short text that will be inserted as a footer in the printed receipt." ) proxy_ip = fields.Char( string='IP Address', size=45, help= 'The hostname or ip address of the hardware proxy, Will be autodetected if left empty.' ) active = fields.Boolean(default=True) uuid = fields.Char( readonly=True, default=lambda self: str(uuid4()), help= 'A globally unique identifier for this pos configuration, used to prevent conflicts in client-generated data.' ) sequence_id = fields.Many2one( 'ir.sequence', string='Order IDs Sequence', readonly=True, help= "This sequence is automatically created by Swerp but you can change it " "to customize the reference numbers of your orders.", copy=False) sequence_line_id = fields.Many2one( 'ir.sequence', string='Order Line IDs Sequence', readonly=True, help= "This sequence is automatically created by Swerp but you can change it " "to customize the reference numbers of your orders lines.", copy=False) session_ids = fields.One2many('pos.session', 'config_id', string='Sessions') current_session_id = fields.Many2one('pos.session', compute='_compute_current_session', string="Current Session") current_session_state = fields.Char(compute='_compute_current_session') last_session_closing_cash = fields.Float(compute='_compute_last_session') last_session_closing_date = fields.Date(compute='_compute_last_session') pos_session_username = fields.Char(compute='_compute_current_session_user') pos_session_state = fields.Char(compute='_compute_current_session_user') pos_session_duration = fields.Char(compute='_compute_current_session_user') group_by = fields.Boolean( string='Group Journal Items', default=True, help= "Check this if you want to group the Journal Items by Product while closing a Session." ) pricelist_id = fields.Many2one( 'product.pricelist', string='Default Pricelist', required=True, default=_default_pricelist, help= "The pricelist used if no customer is selected or if the customer has no Sale Pricelist configured." ) available_pricelist_ids = fields.Many2many( 'product.pricelist', string='Available Pricelists', default=_default_pricelist, help= "Make several pricelists available in the Point of Sale. You can also apply a pricelist to specific customers from their contact form (in Sales tab). To be valid, this pricelist must be listed here as an available pricelist. Otherwise the default pricelist will apply." ) company_id = fields.Many2one('res.company', string='Company', required=True, default=lambda self: self.env.user.company_id) barcode_nomenclature_id = fields.Many2one( 'barcode.nomenclature', string='Barcode Nomenclature', help= 'Defines what kind of barcodes are available and how they are assigned to products, customers and cashiers.' ) group_pos_manager_id = fields.Many2one( 'res.groups', string='Point of Sale Manager Group', default=_get_group_pos_manager, help= 'This field is there to pass the id of the pos manager group to the point of sale client.' ) group_pos_user_id = fields.Many2one( 'res.groups', string='Point of Sale User Group', default=_get_group_pos_user, help= 'This field is there to pass the id of the pos user group to the point of sale client.' ) iface_tipproduct = fields.Boolean(string="Product tips") tip_product_id = fields.Many2one( 'product.product', string='Tip Product', help="This product is used as reference on customer receipts.") fiscal_position_ids = fields.Many2many( 'account.fiscal.position', string='Fiscal Positions', help= 'This is useful for restaurants with onsite and take-away services that imply specific tax rates.' ) default_fiscal_position_id = fields.Many2one( 'account.fiscal.position', string='Default Fiscal Position') default_cashbox_lines_ids = fields.One2many('account.cashbox.line', 'default_pos_id', string='Default Balance') customer_facing_display_html = fields.Html( string='Customer facing display content', translate=True, default=_compute_default_customer_html) use_pricelist = fields.Boolean("Use a pricelist.") tax_regime = fields.Boolean("Tax Regime") tax_regime_selection = fields.Boolean("Tax Regime Selection value") barcode_scanner = fields.Boolean("Barcode Scanner") start_category = fields.Boolean("Set Start Category") module_account = fields.Boolean( string='Invoicing', help='Enables invoice generation from the Point of Sale.') module_pos_restaurant = fields.Boolean("Is a Bar/Restaurant") module_pos_discount = fields.Boolean("Global Discounts") module_pos_loyalty = fields.Boolean("Loyalty Program") module_pos_mercury = fields.Boolean(string="Integrated Card Payments") module_pos_reprint = fields.Boolean(string="Reprint Receipt") is_posbox = fields.Boolean("PosBox") is_header_or_footer = fields.Boolean("Header & Footer") def _compute_is_installed_account_accountant(self): account_accountant = self.env['ir.module.module'].sudo().search([ ('name', '=', 'account_accountant'), ('state', '=', 'installed') ]) for pos_config in self: pos_config.is_installed_account_accountant = account_accountant and account_accountant.id @api.depends('journal_id.currency_id', 'journal_id.company_id.currency_id') def _compute_currency(self): for pos_config in self: if pos_config.journal_id: pos_config.currency_id = pos_config.journal_id.currency_id.id or pos_config.journal_id.company_id.currency_id.id else: pos_config.currency_id = self.env.user.company_id.currency_id.id @api.depends('session_ids') def _compute_current_session(self): for pos_config in self: session = pos_config.session_ids.filtered(lambda r: r.user_id.id == self.env.uid and \ not r.state == 'closed' and \ not r.rescue) # sessions ordered by id desc pos_config.current_session_id = session and session[0].id or False pos_config.current_session_state = session and session[ 0].state or False @api.depends('session_ids') def _compute_last_session(self): PosSession = self.env['pos.session'] for pos_config in self: session = PosSession.search_read( [('config_id', '=', pos_config.id), ('state', '=', 'closed')], ['cash_register_balance_end_real', 'stop_at'], order="stop_at desc", limit=1) if session: pos_config.last_session_closing_cash = session[0][ 'cash_register_balance_end_real'] utc = pytz.timezone('UTC') timezone = pytz.timezone( self._context.get('tz') or self.env.user.tz or 'UTC') pos_config.last_session_closing_date = utc.localize( session[0]['stop_at']).astimezone(timezone).date() else: pos_config.last_session_closing_cash = 0 pos_config.last_session_closing_date = False @api.depends('session_ids') def _compute_current_session_user(self): for pos_config in self: session = pos_config.session_ids.filtered(lambda s: s.state in [ 'opening_control', 'opened', 'closing_control' ] and not s.rescue) if session: pos_config.pos_session_username = session[0].user_id.sudo( ).name pos_config.pos_session_state = session[0].state pos_config.pos_session_duration = ( datetime.now() - session[0].start_at).days if session[0].start_at else 0 else: pos_config.pos_session_username = False pos_config.pos_session_state = False pos_config.pos_session_duration = 0 @api.constrains('company_id', 'stock_location_id') def _check_company_location(self): if self.stock_location_id.company_id and self.stock_location_id.company_id.id != self.company_id.id: raise ValidationError( _("The stock location and the point of sale must belong to the same company." )) @api.constrains('company_id', 'journal_id') def _check_company_journal(self): if self.journal_id and self.journal_id.company_id.id != self.company_id.id: raise ValidationError( _("The sales journal and the point of sale must belong to the same company." )) @api.constrains('company_id', 'invoice_journal_id') def _check_company_invoice_journal(self): if self.invoice_journal_id and self.invoice_journal_id.company_id.id != self.company_id.id: raise ValidationError( _("The invoice journal and the point of sale must belong to the same company." )) @api.constrains('company_id', 'journal_ids') def _check_company_payment(self): if self.env['account.journal'].search_count([ ('id', 'in', self.journal_ids.ids), ('company_id', '!=', self.company_id.id) ]): raise ValidationError( _("The method payments and the point of sale must belong to the same company." )) @api.constrains('pricelist_id', 'available_pricelist_ids', 'journal_id', 'invoice_journal_id', 'journal_ids') def _check_currencies(self): if self.pricelist_id not in self.available_pricelist_ids: raise ValidationError( _("The default pricelist must be included in the available pricelists." )) if any( self.available_pricelist_ids.mapped( lambda pricelist: pricelist.currency_id != self.currency_id )): raise ValidationError( _("All available pricelists must be in the same currency as the company or" " as the Sales Journal set on this point of sale if you use" " the Accounting application.")) if self.invoice_journal_id.currency_id and self.invoice_journal_id.currency_id != self.currency_id: raise ValidationError( _("The invoice journal must be in the same currency as the Sales Journal or the company currency if that is not set." )) if any( self.journal_ids.mapped( lambda journal: self.currency_id not in (journal.company_id.currency_id, journal.currency_id))): raise ValidationError( _("All payment methods must be in the same currency as the Sales Journal or the company currency if that is not set." )) @api.constrains('company_id', 'available_pricelist_ids') def _check_companies(self): if any( self.available_pricelist_ids.mapped( lambda pl: pl.company_id.id not in (False, self.company_id.id))): raise ValidationError( _("The selected pricelists must belong to no company or the company of the point of sale." )) @api.onchange('iface_print_via_proxy') def _onchange_iface_print_via_proxy(self): self.iface_print_auto = self.iface_print_via_proxy @api.onchange('module_account') def _onchange_module_account(self): if self.module_account: self.invoice_journal_id = self.env.ref( 'point_of_sale.pos_sale_journal') @api.onchange('picking_type_id') def _onchange_picking_type_id(self): if self.picking_type_id.default_location_src_id.usage == 'internal' and self.picking_type_id.default_location_dest_id.usage == 'customer': self.stock_location_id = self.picking_type_id.default_location_src_id.id @api.onchange('use_pricelist') def _onchange_use_pricelist(self): """ If the 'pricelist' box is unchecked, we reset the pricelist_id to stop using a pricelist for this iotbox. """ if not self.use_pricelist: self.pricelist_id = self._default_pricelist() @api.onchange('available_pricelist_ids') def _onchange_available_pricelist_ids(self): if self.pricelist_id not in self.available_pricelist_ids: self.pricelist_id = False @api.onchange('iface_scan_via_proxy') def _onchange_iface_scan_via_proxy(self): if self.iface_scan_via_proxy: self.barcode_scanner = True else: self.barcode_scanner = False @api.onchange('barcode_scanner') def _onchange_barcode_scanner(self): if self.barcode_scanner: self.barcode_nomenclature_id = self.env.user.company_id.nomenclature_id else: self.barcode_nomenclature_id = False @api.onchange('is_posbox') def _onchange_is_posbox(self): if not self.is_posbox: self.proxy_ip = False self.iface_scan_via_proxy = False self.iface_electronic_scale = False self.iface_cashdrawer = False self.iface_print_via_proxy = False self.iface_customer_facing_display = False @api.onchange('tax_regime') def _onchange_tax_regime(self): if not self.tax_regime: self.default_fiscal_position_id = False @api.onchange('tax_regime_selection') def _onchange_tax_regime_selection(self): if not self.tax_regime_selection: self.fiscal_position_ids = [(5, 0, 0)] @api.onchange('start_category') def _onchange_start_category(self): if not self.start_category: self.iface_start_categ_id = False @api.onchange('is_header_or_footer') def _onchange_header_footer(self): if not self.is_header_or_footer: self.receipt_header = False self.receipt_footer = False @api.multi def name_get(self): result = [] for config in self: last_session = self.env['pos.session'].search( [('config_id', '=', config.id)], limit=1) if (not last_session) or (last_session.state == 'closed'): result.append( (config.id, config.name + ' (' + _('not used') + ')')) continue result.append( (config.id, config.name + ' (' + last_session.user_id.name + ')')) return result @api.model def create(self, values): if values.get('is_posbox') and values.get( 'iface_customer_facing_display'): if values.get('customer_facing_display_html') and not values[ 'customer_facing_display_html'].strip(): values[ 'customer_facing_display_html'] = self._compute_default_customer_html( ) IrSequence = self.env['ir.sequence'].sudo() val = { 'name': _('POS Order %s') % values['name'], 'padding': 4, 'prefix': "%s/" % values['name'], 'code': "pos.order", 'company_id': values.get('company_id', False), } # force sequence_id field to new pos.order sequence values['sequence_id'] = IrSequence.create(val).id val.update(name=_('POS order line %s') % values['name'], code='pos.order.line') values['sequence_line_id'] = IrSequence.create(val).id pos_config = super(PosConfig, self).create(values) pos_config.sudo()._check_modules_to_install() pos_config.sudo()._check_groups_implied() # If you plan to add something after this, use a new environment. The one above is no longer valid after the modules install. return pos_config @api.multi def write(self, vals): result = super(PosConfig, self).write(vals) config_display = self.filtered( lambda c: c.is_posbox and c.iface_customer_facing_display and not ( c.customer_facing_display_html or '').strip()) if config_display: super(PosConfig, config_display).write({ 'customer_facing_display_html': self._compute_default_customer_html() }) self.sudo()._set_fiscal_position() self.sudo()._check_modules_to_install() self.sudo()._check_groups_implied() return result @api.multi def unlink(self): for pos_config in self.filtered( lambda pos_config: pos_config.sequence_id or pos_config. sequence_line_id): pos_config.sequence_id.unlink() pos_config.sequence_line_id.unlink() return super(PosConfig, self).unlink() def _set_fiscal_position(self): for config in self: if config.tax_regime and config.default_fiscal_position_id.id not in config.fiscal_position_ids.ids: config.fiscal_position_ids = [ (4, config.default_fiscal_position_id.id) ] elif not config.tax_regime_selection and not config.tax_regime and config.fiscal_position_ids.ids: config.fiscal_position_ids = [(5, 0, 0)] def _check_modules_to_install(self): module_installed = False for pos_config in self: for field_name in [ f for f in pos_config.fields_get_keys() if f.startswith('module_') ]: module_name = field_name.split('module_')[1] module_to_install = self.env['ir.module.module'].sudo().search( [('name', '=', module_name)]) if getattr(pos_config, field_name) and module_to_install.state not in ( 'installed', 'to install', 'to upgrade'): module_to_install.button_immediate_install() module_installed = True # just in case we want to do something if we install a module. (like a refresh ...) return module_installed def _check_groups_implied(self): for pos_config in self: for field_name in [ f for f in pos_config.fields_get_keys() if f.startswith('group_') ]: field = pos_config._fields[field_name] if field.type in ('boolean', 'selection') and hasattr( field, 'implied_group'): field_group_xmlids = getattr(field, 'group', 'base.group_user').split(',') field_groups = self.env['res.groups'].concat( *(self.env.ref(it) for it in field_group_xmlids)) field_groups.write({ 'implied_ids': [(4, self.env.ref(field.implied_group).id)] }) def execute(self): return { 'type': 'ir.actions.client', 'tag': 'reload', 'params': { 'wait': True } } # Methods to open the POS @api.multi def open_ui(self): """ open the pos interface """ self.ensure_one() # check all constraints, raises if any is not met self._validate_fields(self._fields) return { 'type': 'ir.actions.act_url', 'url': '/pos/web/', 'target': 'self', } @api.multi def open_session_cb(self): """ new session button create one if none exist access cash control interface if enabled or start a session """ self.ensure_one() if not self.current_session_id: self.current_session_id = self.env['pos.session'].create({ 'user_id': self.env.uid, 'config_id': self.id }) if self.current_session_id.state == 'opened': return self.open_ui() return self._open_session(self.current_session_id.id) return self._open_session(self.current_session_id.id) @api.multi def open_existing_session_cb(self): """ close session button access session form to validate entries """ self.ensure_one() return self._open_session(self.current_session_id.id) def _open_session(self, session_id): return { 'name': _('Session'), 'view_type': 'form', 'view_mode': 'form,tree', 'res_model': 'pos.session', 'res_id': session_id, 'view_id': False, 'type': 'ir.actions.act_window', }
class ServerActions(models.Model): """ Add email option in server actions. """ _name = 'ir.actions.server' _description = 'Server Action' _inherit = ['ir.actions.server'] state = fields.Selection(selection_add=[ ('email', 'Send Email'), ('followers', 'Add Followers'), ('next_activity', 'Create Next Activity'), ]) # Followers partner_ids = fields.Many2many('res.partner', string='Add Followers') channel_ids = fields.Many2many('mail.channel', string='Add Channels') # Template template_id = fields.Many2one( 'mail.template', 'Email Template', ondelete='set null', domain="[('model_id', '=', model_id)]", ) # Next Activity activity_type_id = fields.Many2one( 'mail.activity.type', string='Activity', domain= "['|', ('res_model_id', '=', False), ('res_model_id', '=', model_id)]") activity_summary = fields.Char('Summary') activity_note = fields.Html('Note') activity_date_deadline_range = fields.Integer(string='Due Date In') activity_date_deadline_range_type = fields.Selection([ ('days', 'Days'), ('weeks', 'Weeks'), ('months', 'Months'), ], string='Due type', default='days') activity_user_type = fields.Selection( [('specific', 'Specific User'), ('generic', 'Generic User From Record')], default="specific", required=True, help= "Use 'Specific User' to always assign the same user on the next activity. Use 'Generic User From Record' to specify the field name of the user to choose on the record." ) activity_user_id = fields.Many2one('res.users', string='Responsible') activity_user_field_name = fields.Char( 'User field name', help="Technical name of the user on the record", default="user_id") @api.onchange('activity_date_deadline_range') def _onchange_activity_date_deadline_range(self): if self.activity_date_deadline_range < 0: raise UserError(_("The 'Due Date In' value can't be negative.")) @api.onchange('template_id') def on_change_template_id(self): # TODO: remove in master pass @api.constrains('state', 'model_id') def _check_mail_thread(self): for action in self: if action.state == 'followers' and not action.model_id.is_mail_thread: raise ValidationError( _("Add Followers can only be done on a mail thread model")) @api.constrains('state', 'model_id') def _check_activity_mixin(self): for action in self: if action.state == 'next_activity' and not issubclass( self.pool[action.model_id.model], self.pool['mail.thread']): raise ValidationError( _("A next activity can only be planned on models that use the chatter" )) @api.model def run_action_followers_multi(self, action, eval_context=None): Model = self.env[action.model_name] if self.partner_ids or self.channel_ids and hasattr( Model, 'message_subscribe'): records = Model.browse( self._context.get('active_ids', self._context.get('active_id'))) records.message_subscribe(self.partner_ids.ids, self.channel_ids.ids) return False @api.model def _is_recompute(self, action): """When an activity is set on update of a record, update might be triggered many times by recomputes. When need to know it to skip these steps. Except if the computed field is supposed to trigger the action """ records = self.env[action.model_name].browse( self._context.get('active_ids', self._context.get('active_id'))) old_values = action._context.get('old_values') if old_values: domain_post = action._context.get('domain_post') tracked_fields = [] if domain_post: for leaf in domain_post: if isinstance(leaf, (tuple, list)): tracked_fields.append(leaf[0]) fields_to_check = [ field for record, field_names in old_values.items() for field in field_names if field not in tracked_fields ] if fields_to_check: field = records._fields[fields_to_check[0]] # Pick an arbitrary field; if it is marked to be recomputed, # it means we are in an extraneous write triggered by the recompute. # In this case, we should not create a new activity. if records._recompute_check(field): return True return False @api.model def run_action_email(self, action, eval_context=None): # TDE CLEANME: when going to new api with server action, remove action if not action.template_id or not self._context.get( 'active_id') or self._is_recompute(action): return False # Clean context from default_type to avoid making attachment # with wrong values in subsequent operations cleaned_ctx = dict(self.env.context) cleaned_ctx.pop('default_type', None) cleaned_ctx.pop('default_parent_id', None) action.template_id.with_context(cleaned_ctx).send_mail( self._context.get('active_id'), force_send=False, raise_exception=False) return False @api.model def run_action_next_activity(self, action, eval_context=None): if not action.activity_type_id or not self._context.get( 'active_id') or self._is_recompute(action): return False records = self.env[action.model_name].browse( self._context.get('active_ids', self._context.get('active_id'))) vals = { 'summary': action.activity_summary or '', 'note': action.activity_note or '', 'activity_type_id': action.activity_type_id.id, } if action.activity_date_deadline_range > 0: vals['date_deadline'] = fields.Date.context_today( action) + relativedelta( **{ action.activity_date_deadline_range_type: action.activity_date_deadline_range }) for record in records: user = False if action.activity_user_type == 'specific': user = action.activity_user_id elif action.activity_user_type == 'generic' and action.activity_user_field_name in record: user = record[action.activity_user_field_name] if user: vals['user_id'] = user.id record.activity_schedule(**vals) return False @api.model def _get_eval_context(self, action=None): """ Override the method giving the evaluation context but also the context used in all subsequent calls. Add the mail_notify_force_send key set to False in the context. This way all notification emails linked to the currently executed action will be set in the queue instead of sent directly. This will avoid possible break in transactions. """ eval_context = super(ServerActions, self)._get_eval_context(action=action) ctx = dict(eval_context['env'].context) ctx['mail_notify_force_send'] = False eval_context['env'].context = ctx return eval_context
class CrmLeadForwardToPartner(models.TransientModel): """ Forward info history to partners. """ _name = 'crm.lead.forward.to.partner' _description = 'Lead forward to partner' @api.model def _convert_to_assignation_line(self, lead, partner): lead_location = [] partner_location = [] if lead.country_id: lead_location.append(lead.country_id.name) if lead.city: lead_location.append(lead.city) if partner: if partner.country_id: partner_location.append(partner.country_id.name) if partner.city: partner_location.append(partner.city) return { 'lead_id': lead.id, 'lead_location': ", ".join(lead_location), 'partner_assigned_id': partner and partner.id or False, 'partner_location': ", ".join(partner_location), 'lead_link': self.get_lead_portal_url(lead.id, lead.type), } @api.model def default_get(self, fields): template = self.env.ref( 'website_crm_partner_assign.email_template_lead_forward_mail', False) res = super(CrmLeadForwardToPartner, self).default_get(fields) active_ids = self.env.context.get('active_ids') default_composition_mode = self.env.context.get( 'default_composition_mode') res['assignation_lines'] = [] if template: res['body'] = template.body_html if active_ids: leads = self.env['crm.lead'].browse(active_ids) if default_composition_mode == 'mass_mail': partner_assigned_dict = leads.search_geo_partner() else: partner_assigned_dict = { lead.id: lead.partner_assigned_id.id for lead in leads } res['partner_id'] = leads[0].partner_assigned_id.id for lead in leads: partner_id = partner_assigned_dict.get(lead.id) or False partner = self.env['res.partner'].browse(partner_id) res['assignation_lines'].append( (0, 0, self._convert_to_assignation_line(lead, partner))) return res @api.multi def action_forward(self): self.ensure_one() template = self.env.ref( 'website_crm_partner_assign.email_template_lead_forward_mail', False) if not template: raise UserError( _('The Forward Email Template is not in the database')) portal_group = self.env.ref('base.group_portal') local_context = self.env.context.copy() if not (self.forward_type == 'single'): no_email = set() for lead in self.assignation_lines: if lead.partner_assigned_id and not lead.partner_assigned_id.email: no_email.add(lead.partner_assigned_id.name) if no_email: raise UserError( _('Set an email address for the partner(s): %s') % ", ".join(no_email)) if self.forward_type == 'single' and not self.partner_id.email: raise UserError( _('Set an email address for the partner %s') % self.partner_id.name) partners_leads = {} for lead in self.assignation_lines: partner = self.forward_type == 'single' and self.partner_id or lead.partner_assigned_id lead_details = { 'lead_link': lead.lead_link, 'lead_id': lead.lead_id, } if partner: partner_leads = partners_leads.get(partner.id) if partner_leads: partner_leads['leads'].append(lead_details) else: partners_leads[partner.id] = { 'partner': partner, 'leads': [lead_details] } for partner_id, partner_leads in partners_leads.items(): in_portal = False if portal_group: for contact in ( partner.child_ids or partner).filtered(lambda contact: contact.user_ids): in_portal = portal_group.id in [ g.id for g in contact.user_ids[0].groups_id ] local_context['partner_id'] = partner_leads['partner'] local_context['partner_leads'] = partner_leads['leads'] local_context['partner_in_portal'] = in_portal template.with_context(local_context).send_mail(self.id) leads = self.env['crm.lead'] for lead_data in partner_leads['leads']: leads |= lead_data['lead_id'] values = { 'partner_assigned_id': partner_id, 'user_id': partner_leads['partner'].user_id.id } leads.with_context(mail_auto_subscribe_no_notify=1).write(values) self.env['crm.lead'].message_subscribe([partner_id]) return True def get_lead_portal_url(self, lead_id, type): action = type == 'opportunity' and 'action_portal_opportunities' or 'action_portal_leads' action_ref = self.env.ref('website_crm_partner_assign.%s' % (action, ), False) portal_link = "%s/?db=%s#id=%s&action=%s&view_type=form" % ( self.env['ir.config_parameter'].sudo().get_param('web.base.url'), self.env.cr.dbname, lead_id, action_ref and action_ref.id or False) return portal_link def get_portal_url(self): portal_link = "%s/?db=%s" % (self.env['ir.config_parameter'].sudo( ).get_param('web.base.url'), self.env.cr.dbname) return portal_link forward_type = fields.Selection([ ('single', 'a single partner: manual selection of partner'), ('assigned', "several partners: automatic assignation, using GPS coordinates and partner's grades" ) ], 'Forward selected leads to', default=lambda self: self.env.context.get( 'forward_type') or 'single') partner_id = fields.Many2one('res.partner', 'Forward Leads To') assignation_lines = fields.One2many('crm.lead.assignation', 'forward_id', 'Partner Assignation') body = fields.Html('Contents', help='Automatically sanitized HTML contents')
class StockRule(models.Model): """ A rule describe what a procurement should do; produce, buy, move, ... """ _name = 'stock.rule' _description = "Stock Rule" _order = "sequence, id" name = fields.Char( 'Name', required=True, translate=True, help="This field will fill the packing origin and the name of its moves") active = fields.Boolean( 'Active', default=True, help="If unchecked, it will allow you to hide the rule without removing it.") group_propagation_option = fields.Selection([ ('none', 'Leave Empty'), ('propagate', 'Propagate'), ('fixed', 'Fixed')], string="Propagation of Procurement Group", default='propagate') group_id = fields.Many2one('procurement.group', 'Fixed Procurement Group') action = fields.Selection( selection=[('pull', 'Pull From'), ('push', 'Push To'), ('pull_push', 'Pull & Push')], string='Action', required=True) sequence = fields.Integer('Sequence', default=20) company_id = fields.Many2one('res.company', 'Company', default=lambda self: self.env.user.company_id) location_id = fields.Many2one('stock.location', 'Destination Location', required=True) location_src_id = fields.Many2one('stock.location', 'Source Location') route_id = fields.Many2one('stock.location.route', 'Route', required=True, ondelete='cascade') procure_method = fields.Selection([ ('make_to_stock', 'Take From Stock'), ('make_to_order', 'Trigger Another Rule')], string='Move Supply Method', default='make_to_stock', required=True, help="""Create Procurement: A procurement will be created in the source location and the system will try to find a rule to resolve it. The available stock will be ignored. Take from Stock: The products will be taken from the available stock.""") route_sequence = fields.Integer('Route Sequence', related='route_id.sequence', store=True, readonly=False, compute_sudo=True) picking_type_id = fields.Many2one( 'stock.picking.type', 'Operation Type', required=True) delay = fields.Integer('Delay', default=0, help="The expected date of the created transfer will be computed based on this delay.") partner_address_id = fields.Many2one('res.partner', 'Partner Address', help="Address where goods should be delivered. Optional.") propagate = fields.Boolean( 'Propagate cancel and split', default=True, help="When ticked, if the move is splitted or cancelled, the next move will be too.") warehouse_id = fields.Many2one('stock.warehouse', 'Warehouse') propagate_warehouse_id = fields.Many2one( 'stock.warehouse', 'Warehouse to Propagate', help="The warehouse to propagate on the created move/procurement, which can be different of the warehouse this rule is for (e.g for resupplying rules from another warehouse)") auto = fields.Selection([ ('manual', 'Manual Operation'), ('transparent', 'Automatic No Step Added')], string='Automatic Move', default='manual', index=True, required=True, help="The 'Manual Operation' value will create a stock move after the current one. " "With 'Automatic No Step Added', the location is replaced in the original move.") rule_message = fields.Html(compute='_compute_action_message') @api.onchange('picking_type_id') def _onchange_picking_type(self): """ Modify locations to the default picking type's locations source and destination. """ self.location_src_id = self.picking_type_id.default_location_src_id.id self.location_id = self.picking_type_id.default_location_dest_id.id @api.onchange('route_id', 'company_id') def _onchange_route(self): """ Ensure that the rule's company is the same than the route's company. """ if self.route_id.company_id: self.company_id = self.route_id.company_id if self.picking_type_id.warehouse_id.company_id != self.route_id.company_id: self.picking_type_id = False domain = {'company_id': self.route_id.company_id and [('id', '=', self.route_id.company_id.id)] or []} return {'domain': domain} def _get_message_values(self): """ Return the source, destination and picking_type applied on a stock rule. The purpose of this function is to avoid code duplication in _get_message_dict functions since it often requires those data. """ source = self.location_src_id and self.location_src_id.display_name or _('Source Location') destination = self.location_id and self.location_id.display_name or _('Destination Location') operation = self.picking_type_id and self.picking_type_id.name or _('Operation Type') return source, destination, operation def _get_message_dict(self): """ Return a dict with the different possible message used for the rule message. It should return one message for each stock.rule action (except push and pull). This function is override in mrp and purchase_stock in order to complete the dictionary. """ message_dict = {} source, destination, operation = self._get_message_values() if self.action in ('push', 'pull', 'pull_push'): suffix = "" if self.procure_method == 'make_to_order' and self.location_src_id: suffix = _("<br>A need is created in <b>%s</b> and a rule will be triggered to fulfill it.") % (source) message_dict = { 'pull': _('When products are needed in <b>%s</b>, <br/> <b>%s</b> are created from <b>%s</b> to fulfill the need.') % (destination, operation, source) + suffix, 'push': _('When products arrive in <b>%s</b>, <br/> <b>%s</b> are created to send them in <b>%s</b>.') % (source, operation, destination) } return message_dict @api.depends('action', 'location_id', 'location_src_id', 'picking_type_id', 'procure_method') def _compute_action_message(self): """ Generate dynamicaly a message that describe the rule purpose to the end user. """ for rule in self.filtered(lambda rule: rule.action): message_dict = rule._get_message_dict() message = message_dict.get(rule.action) and message_dict[rule.action] or "" if rule.action == 'pull_push': message = message_dict['pull'] + "<br/><br/>" + message_dict['push'] rule.rule_message = message def _run_push(self, move): """ Apply a push rule on a move. If the rule is 'no step added' it will modify the destination location on the move. If the rule is 'manual operation' it will generate a new move in order to complete the section define by the rule. Care this function is not call by method run. It is called explicitely in stock_move.py inside the method _push_apply """ new_date = fields.Datetime.to_string(move.date_expected + relativedelta(days=self.delay)) if self.auto == 'transparent': old_dest_location = move.location_dest_id move.write({ 'date': new_date, 'date_expected': new_date, 'location_dest_id': self.location_id.id}) # avoid looping if a push rule is not well configured; otherwise call again push_apply to see if a next step is defined if self.location_id != old_dest_location: # TDE FIXME: should probably be done in the move model IMO move._push_apply() else: new_move_vals = self._push_prepare_move_copy_values(move, new_date) new_move = move.sudo().copy(new_move_vals) move.write({'move_dest_ids': [(4, new_move.id)]}) new_move._action_confirm() def _push_prepare_move_copy_values(self, move_to_copy, new_date): company_id = self.company_id.id if not company_id: company_id = self.sudo().warehouse_id and self.sudo().warehouse_id.company_id.id or self.sudo().picking_type_id.warehouse_id.company_id.id new_move_vals = { 'origin': move_to_copy.origin or move_to_copy.picking_id.name or "/", 'location_id': move_to_copy.location_dest_id.id, 'location_dest_id': self.location_id.id, 'date': new_date, 'date_expected': new_date, 'company_id': company_id, 'picking_id': False, 'picking_type_id': self.picking_type_id.id, 'propagate': self.propagate, 'warehouse_id': self.warehouse_id.id, } return new_move_vals def _run_pull(self, product_id, product_qty, product_uom, location_id, name, origin, values): if not self.location_src_id: msg = _('No source location defined on stock rule: %s!') % (self.name, ) raise UserError(msg) # create the move as SUPERUSER because the current user may not have the rights to do it (mto product launched by a sale for example) # Search if picking with move for it exists already: group_id = False if self.group_propagation_option == 'propagate': group_id = values.get('group_id', False) and values['group_id'].id elif self.group_propagation_option == 'fixed': group_id = self.group_id.id data = self._get_stock_move_values(product_id, product_qty, product_uom, location_id, name, origin, values, group_id) # Since action_confirm launch following procurement_group we should activate it. move = self.env['stock.move'].sudo().with_context(force_company=data.get('company_id', False)).create(data) move._action_confirm() return True def _get_custom_move_fields(self): """ The purpose of this method is to be override in order to easily add fields from procurement 'values' argument to move data. """ return [] def _get_stock_move_values(self, product_id, product_qty, product_uom, location_id, name, origin, values, group_id): ''' Returns a dictionary of values that will be used to create a stock move from a procurement. This function assumes that the given procurement has a rule (action == 'pull' or 'pull_push') set on it. :param procurement: browse record :rtype: dictionary ''' date_expected = fields.Datetime.to_string( fields.Datetime.from_string(values['date_planned']) - relativedelta(days=self.delay or 0) ) # it is possible that we've already got some move done, so check for the done qty and create # a new move with the correct qty qty_left = product_qty move_values = { 'name': name[:2000], 'company_id': self.company_id.id or self.location_src_id.company_id.id or self.location_id.company_id.id or values['company_id'].id, 'product_id': product_id.id, 'product_uom': product_uom.id, 'product_uom_qty': qty_left, 'partner_id': self.partner_address_id.id or (values.get('group_id', False) and values['group_id'].partner_id.id) or False, 'location_id': self.location_src_id.id, 'location_dest_id': location_id.id, 'move_dest_ids': values.get('move_dest_ids', False) and [(4, x.id) for x in values['move_dest_ids']] or [], 'rule_id': self.id, 'procure_method': self.procure_method, 'origin': origin, 'picking_type_id': self.picking_type_id.id, 'group_id': group_id, 'route_ids': [(4, route.id) for route in values.get('route_ids', [])], 'warehouse_id': self.propagate_warehouse_id.id or self.warehouse_id.id, 'date': date_expected, 'date_expected': date_expected, 'propagate': self.propagate, 'priority': values.get('priority', "1"), } for field in self._get_custom_move_fields(): if field in values: move_values[field] = values.get(field) return move_values def _log_next_activity(self, product_id, note): existing_activity = self.env['mail.activity'].search([('res_id', '=', product_id.product_tmpl_id.id), ('res_model_id', '=', self.env.ref('product.model_product_template').id), ('note', '=', note)]) if not existing_activity: # If the user deleted todo activity type. try: activity_type_id = self.env.ref('mail.mail_activity_data_todo').id except: activity_type_id = False self.env['mail.activity'].create({ 'activity_type_id': activity_type_id, 'note': note, 'user_id': product_id.responsible_id.id, 'res_id': product_id.product_tmpl_id.id, 'res_model_id': self.env.ref('product.model_product_template').id, }) def _make_po_get_domain(self, values, partner): return ()
class Task(models.Model): _name = "project.task" _description = "Task" _date_name = "date_start" _inherit = ['portal.mixin', 'mail.thread', 'mail.activity.mixin', 'rating.mixin'] _mail_post_access = 'read' _order = "priority desc, sequence, id desc" @api.model def default_get(self, fields_list): result = super(Task, self).default_get(fields_list) # force some parent values, if needed if 'parent_id' in result and result['parent_id']: result.update(self._subtask_values_from_parent(result['parent_id'])) return result @api.model def _get_default_partner(self): if 'default_project_id' in self.env.context: default_project_id = self.env['project.project'].browse(self.env.context['default_project_id']) return default_project_id.exists().partner_id def _get_default_stage_id(self): """ Gives default stage_id """ project_id = self.env.context.get('default_project_id') if not project_id: return False return self.stage_find(project_id, [('fold', '=', False)]) @api.model def _default_company_id(self): if self._context.get('default_project_id'): return self.env['project.project'].browse(self._context['default_project_id']).company_id return self.env['res.company']._company_default_get() @api.model def _read_group_stage_ids(self, stages, domain, order): search_domain = [('id', 'in', stages.ids)] if 'default_project_id' in self.env.context: search_domain = ['|', ('project_ids', '=', self.env.context['default_project_id'])] + search_domain stage_ids = stages._search(search_domain, order=order, access_rights_uid=SUPERUSER_ID) return stages.browse(stage_ids) active = fields.Boolean(default=True) name = fields.Char(string='Title', track_visibility='always', required=True, index=True) description = fields.Html(string='Description') priority = fields.Selection([ ('0', 'Low'), ('1', 'Normal'), ], default='0', index=True, string="Priority") sequence = fields.Integer(string='Sequence', index=True, default=10, help="Gives the sequence order when displaying a list of tasks.") stage_id = fields.Many2one('project.task.type', string='Stage', ondelete='restrict', track_visibility='onchange', index=True, default=_get_default_stage_id, group_expand='_read_group_stage_ids', domain="[('project_ids', '=', project_id)]", copy=False) tag_ids = fields.Many2many('project.tags', string='Tags', oldname='categ_ids') kanban_state = fields.Selection([ ('normal', 'Grey'), ('done', 'Green'), ('blocked', 'Red')], string='Kanban State', copy=False, default='normal', required=True) kanban_state_label = fields.Char(compute='_compute_kanban_state_label', string='Kanban State Label', track_visibility='onchange') create_date = fields.Datetime("Created On", readonly=True, index=True) write_date = fields.Datetime("Last Updated On", readonly=True, index=True) date_start = fields.Datetime(string='Starting Date', default=fields.Datetime.now, index=True, copy=False) date_end = fields.Datetime(string='Ending Date', index=True, copy=False) date_assign = fields.Datetime(string='Assigning Date', index=True, copy=False, readonly=True) date_deadline = fields.Date(string='Deadline', index=True, copy=False, track_visibility='onchange') date_last_stage_update = fields.Datetime(string='Last Stage Update', index=True, copy=False, readonly=True) project_id = fields.Many2one('project.project', string='Project', default=lambda self: self.env.context.get('default_project_id'), index=True, track_visibility='onchange', change_default=True) notes = fields.Text(string='Notes') planned_hours = fields.Float("Planned Hours", help='It is the time planned to achieve the task. If this document has sub-tasks, it means the time needed to achieve this tasks and its childs.',track_visibility='onchange') subtask_planned_hours = fields.Float("Subtasks", compute='_compute_subtask_planned_hours', help="Computed using sum of hours planned of all subtasks created from main task. Usually these hours are less or equal to the Planned Hours (of main task).") user_id = fields.Many2one('res.users', string='Assigned to', default=lambda self: self.env.uid, index=True, track_visibility='always') partner_id = fields.Many2one('res.partner', string='Customer', default=lambda self: self._get_default_partner()) manager_id = fields.Many2one('res.users', string='Project Manager', related='project_id.user_id', readonly=True, related_sudo=False) company_id = fields.Many2one('res.company', string='Company', required=True, default=_default_company_id) color = fields.Integer(string='Color Index') user_email = fields.Char(related='user_id.email', string='User Email', readonly=True, related_sudo=False) attachment_ids = fields.One2many('ir.attachment', compute='_compute_attachment_ids', string="Main Attachments", help="Attachment that don't come from message.") # In the domain of displayed_image_id, we couln't use attachment_ids because a one2many is represented as a list of commands so we used res_model & res_id displayed_image_id = fields.Many2one('ir.attachment', domain="[('res_model', '=', 'project.task'), ('res_id', '=', id), ('mimetype', 'ilike', 'image')]", string='Cover Image') legend_blocked = fields.Char(related='stage_id.legend_blocked', string='Kanban Blocked Explanation', readonly=True, related_sudo=False) legend_done = fields.Char(related='stage_id.legend_done', string='Kanban Valid Explanation', readonly=True, related_sudo=False) legend_normal = fields.Char(related='stage_id.legend_normal', string='Kanban Ongoing Explanation', readonly=True, related_sudo=False) parent_id = fields.Many2one('project.task', string='Parent Task', index=True) child_ids = fields.One2many('project.task', 'parent_id', string="Sub-tasks", context={'active_test': False}) subtask_project_id = fields.Many2one('project.project', related="project_id.subtask_project_id", string='Sub-task Project', readonly=True) subtask_count = fields.Integer("Sub-task count", compute='_compute_subtask_count') email_from = fields.Char(string='Email', help="These people will receive email.", index=True) email_cc = fields.Char(string='Watchers Emails', help="""These email addresses will be added to the CC field of all inbound and outbound emails for this record before being sent. Separate multiple email addresses with a comma""") # Computed field about working time elapsed between record creation and assignation/closing. working_hours_open = fields.Float(compute='_compute_elapsed', string='Working hours to assign', store=True, group_operator="avg") working_hours_close = fields.Float(compute='_compute_elapsed', string='Working hours to close', store=True, group_operator="avg") working_days_open = fields.Float(compute='_compute_elapsed', string='Working days to assign', store=True, group_operator="avg") working_days_close = fields.Float(compute='_compute_elapsed', string='Working days to close', store=True, group_operator="avg") # customer portal: include comment and incoming emails in communication history website_message_ids = fields.One2many(domain=lambda self: [('model', '=', self._name), ('message_type', 'in', ['email', 'comment'])]) _constraints = [(models.BaseModel._check_recursion, 'Circular references are not permitted between tasks and sub-tasks', ['parent_id'])] def _compute_attachment_ids(self): for task in self: attachment_ids = self.env['ir.attachment'].search([('res_id', '=', task.id), ('res_model', '=', 'project.task')]).ids message_attachment_ids = task.mapped('message_ids.attachment_ids').ids # from mail_thread task.attachment_ids = list(set(attachment_ids) - set(message_attachment_ids)) @api.multi @api.depends('create_date', 'date_end', 'date_assign') def _compute_elapsed(self): task_linked_to_calendar = self.filtered( lambda task: task.project_id.resource_calendar_id and task.create_date ) for task in task_linked_to_calendar: dt_create_date = fields.Datetime.from_string(task.create_date) if task.date_assign: dt_date_assign = fields.Datetime.from_string(task.date_assign) task.working_hours_open = task.project_id.resource_calendar_id.get_work_hours_count( dt_create_date, dt_date_assign, compute_leaves=True) task.working_days_open = task.working_hours_open / 24.0 if task.date_end: dt_date_end = fields.Datetime.from_string(task.date_end) task.working_hours_close = task.project_id.resource_calendar_id.get_work_hours_count( dt_create_date, dt_date_end, compute_leaves=True) task.working_days_close = task.working_hours_close / 24.0 (self - task_linked_to_calendar).update(dict.fromkeys( ['working_hours_open', 'working_hours_close', 'working_days_open', 'working_days_close'], 0.0)) @api.depends('stage_id', 'kanban_state') def _compute_kanban_state_label(self): for task in self: if task.kanban_state == 'normal': task.kanban_state_label = task.legend_normal elif task.kanban_state == 'blocked': task.kanban_state_label = task.legend_blocked else: task.kanban_state_label = task.legend_done def _compute_access_url(self): super(Task, self)._compute_access_url() for task in self: task.access_url = '/my/task/%s' % task.id def _compute_access_warning(self): super(Task, self)._compute_access_warning() for task in self.filtered(lambda x: x.project_id.privacy_visibility != 'portal'): task.access_warning = _( "The task cannot be shared with the recipient(s) because the privacy of the project is too restricted. Set the privacy of the project to 'Visible by following customers' in order to make it accessible by the recipient(s).") @api.depends('child_ids.planned_hours') def _compute_subtask_planned_hours(self): for task in self: task.subtask_planned_hours = sum(task.child_ids.mapped('planned_hours')) @api.depends('child_ids') def _compute_subtask_count(self): """ Note: since we accept only one level subtask, we can use a read_group here """ task_data = self.env['project.task'].read_group([('parent_id', 'in', self.ids)], ['parent_id'], ['parent_id']) mapping = dict((data['parent_id'][0], data['parent_id_count']) for data in task_data) for task in self: task.subtask_count = mapping.get(task.id, 0) @api.onchange('partner_id') def _onchange_partner_id(self): self.email_from = self.partner_id.email @api.onchange('parent_id') def _onchange_parent_id(self): if self.parent_id: for field_name in self._subtask_implied_fields(): self[field_name] = self.parent_id[field_name] @api.onchange('project_id') def _onchange_project(self): if self.project_id: if not self.parent_id and self.project_id.partner_id: self.partner_id = self.project_id.partner_id if self.project_id not in self.stage_id.project_ids: self.stage_id = self.stage_find(self.project_id.id, [('fold', '=', False)]) # keep multi company consistency self.company_id = self.project_id.company_id else: self.stage_id = False @api.onchange('user_id') def _onchange_user(self): if self.user_id: self.date_start = fields.Datetime.now() @api.constrains('parent_id', 'child_ids') def _check_subtask_level(self): for task in self: if task.parent_id and task.child_ids: raise ValidationError(_('Task %s cannot have several subtask levels.' % (task.name,))) @api.multi @api.returns('self', lambda value: value.id) def copy(self, default=None): if default is None: default = {} if not default.get('name'): default['name'] = _("%s (copy)") % self.name return super(Task, self).copy(default) @api.constrains('parent_id') def _check_parent_id(self): for task in self: if not task._check_recursion(): raise ValidationError(_('Error! You cannot create recursive hierarchy of task(s).')) @api.model def get_empty_list_help(self, help): tname = _("task") project_id = self.env.context.get('default_project_id', False) if project_id: name = self.env['project.project'].browse(project_id).label_tasks if name: tname = name.lower() self = self.with_context( empty_list_help_id=self.env.context.get('default_project_id'), empty_list_help_model='project.project', empty_list_help_document_name=tname, ) return super(Task, self).get_empty_list_help(help) # ---------------------------------------- # Case management # ---------------------------------------- def stage_find(self, section_id, domain=[], order='sequence'): """ Override of the base.stage method Parameter of the stage search taken from the lead: - section_id: if set, stages must belong to this section or be a default stage; if not set, stages must be default stages """ # collect all section_ids section_ids = [] if section_id: section_ids.append(section_id) section_ids.extend(self.mapped('project_id').ids) search_domain = [] if section_ids: search_domain = [('|')] * (len(section_ids) - 1) for section_id in section_ids: search_domain.append(('project_ids', '=', section_id)) search_domain += list(domain) # perform search, return the first found return self.env['project.task.type'].search(search_domain, order=order, limit=1).id # ------------------------------------------------ # CRUD overrides # ------------------------------------------------ @api.model def create(self, vals): # context: no_log, because subtype already handle this context = dict(self.env.context, mail_create_nolog=True) # force some parent values, if needed if 'parent_id' in vals and vals['parent_id']: vals.update(self._subtask_values_from_parent(vals['parent_id'])) context.pop('default_parent_id', None) # for default stage if vals.get('project_id') and not context.get('default_project_id'): context['default_project_id'] = vals.get('project_id') # user_id change: update date_assign if vals.get('user_id'): vals['date_assign'] = fields.Datetime.now() # Stage change: Update date_end if folded stage and date_last_stage_update if vals.get('stage_id'): vals.update(self.update_date_end(vals['stage_id'])) vals['date_last_stage_update'] = fields.Datetime.now() task = super(Task, self.with_context(context)).create(vals) return task @api.multi def write(self, vals): now = fields.Datetime.now() # subtask: force some parent values, if needed if 'parent_id' in vals and vals['parent_id']: vals.update(self._subtask_values_from_parent(vals['parent_id'])) # stage change: update date_last_stage_update if 'stage_id' in vals: vals.update(self.update_date_end(vals['stage_id'])) vals['date_last_stage_update'] = now # reset kanban state when changing stage if 'kanban_state' not in vals: vals['kanban_state'] = 'normal' # user_id change: update date_assign if vals.get('user_id') and 'date_assign' not in vals: vals['date_assign'] = now result = super(Task, self).write(vals) # rating on stage if 'stage_id' in vals and vals.get('stage_id'): self.filtered(lambda x: x.project_id.rating_status == 'stage')._send_task_rating_mail(force_send=True) # subtask: update subtask according to parent values subtask_values_to_write = self._subtask_write_values(vals) if subtask_values_to_write: subtasks = self.filtered(lambda task: not task.parent_id).mapped('child_ids') if subtasks: subtasks.write(subtask_values_to_write) return result def update_date_end(self, stage_id): project_task_type = self.env['project.task.type'].browse(stage_id) if project_task_type.fold: return {'date_end': fields.Datetime.now()} return {'date_end': False} # --------------------------------------------------- # Subtasks # --------------------------------------------------- @api.model def _subtask_implied_fields(self): """ Return the list of field name to apply on subtask when changing parent_id or when updating parent task. """ return ['partner_id', 'email_from'] @api.multi def _subtask_write_values(self, values): """ Return the values to write on subtask when `values` is written on parent tasks :param values: dict of values to write on parent """ result = {} for field_name in self._subtask_implied_fields(): if field_name in values: result[field_name] = values[field_name] return result def _subtask_values_from_parent(self, parent_id): """ Get values for substask implied field of the given""" result = {} parent_task = self.env['project.task'].browse(parent_id) for field_name in self._subtask_implied_fields(): result[field_name] = parent_task[field_name] return self._convert_to_write(result) # --------------------------------------------------- # Mail gateway # --------------------------------------------------- @api.multi def _track_template(self, tracking): res = super(Task, self)._track_template(tracking) test_task = self[0] changes, tracking_value_ids = tracking[test_task.id] if 'stage_id' in changes and test_task.stage_id.mail_template_id: res['stage_id'] = (test_task.stage_id.mail_template_id, { 'auto_delete_message': True, 'subtype_id': self.env['ir.model.data'].xmlid_to_res_id('mail.mt_note'), 'notif_layout': 'mail.mail_notification_light' }) return res @api.multi def _track_subtype(self, init_values): self.ensure_one() if 'kanban_state_label' in init_values and self.kanban_state == 'blocked': return 'project.mt_task_blocked' elif 'kanban_state_label' in init_values and self.kanban_state == 'done': return 'project.mt_task_ready' elif 'stage_id' in init_values and self.stage_id and self.stage_id.sequence <= 1: # start stage -> new return 'project.mt_task_new' elif 'stage_id' in init_values: return 'project.mt_task_stage' return super(Task, self)._track_subtype(init_values) @api.multi def _notify_get_groups(self, message, groups): """ Handle project users and managers recipients that can assign tasks and create new one directly from notification emails. Also give access button to portal users and portal customers. If they are notified they should probably have access to the document. """ groups = super(Task, self)._notify_get_groups(message, groups) self.ensure_one() project_user_group_id = self.env.ref('project.group_project_user').id new_group = ( 'group_project_user', lambda pdata: pdata['type'] == 'user' and project_user_group_id in pdata['groups'], {}, ) if not self.user_id and not self.stage_id.fold: take_action = self._notify_get_action_link('assign') project_actions = [{'url': take_action, 'title': _('I take it')}] new_group[2]['actions'] = project_actions groups = [new_group] + groups for group_name, group_method, group_data in groups: if group_name == 'customer': continue group_data['has_button_access'] = True return groups @api.multi def _notify_get_reply_to(self, default=None, records=None, company=None, doc_names=None): """ Override to set alias of tasks to their project if any. """ aliases = self.sudo().mapped('project_id')._notify_get_reply_to(default=default, records=None, company=company, doc_names=None) res = {task.id: aliases.get(task.project_id.id) for task in self} leftover = self.filtered(lambda rec: not rec.project_id) if leftover: res.update(super(Task, leftover)._notify_get_reply_to(default=default, records=None, company=company, doc_names=doc_names)) return res @api.multi def email_split(self, msg): email_list = tools.email_split((msg.get('to') or '') + ',' + (msg.get('cc') or '')) # check left-part is not already an alias aliases = self.mapped('project_id.alias_name') return [x for x in email_list if x.split('@')[0] not in aliases] @api.model def message_new(self, msg, custom_values=None): """ Overrides mail_thread message_new that is called by the mailgateway through message_process. This override updates the document according to the email. """ # remove default author when going through the mail gateway. Indeed we # do not want to explicitly set user_id to False; however we do not # want the gateway user to be responsible if no other responsible is # found. create_context = dict(self.env.context or {}) create_context['default_user_id'] = False if custom_values is None: custom_values = {} defaults = { 'name': msg.get('subject') or _("No Subject"), 'email_from': msg.get('from'), 'email_cc': msg.get('cc'), 'planned_hours': 0.0, 'partner_id': msg.get('author_id') } defaults.update(custom_values) task = super(Task, self.with_context(create_context)).message_new(msg, custom_values=defaults) email_list = task.email_split(msg) partner_ids = [p for p in task._find_partner_from_emails(email_list, force_create=False) if p] task.message_subscribe(partner_ids) return task @api.multi def message_update(self, msg, update_vals=None): """ Override to update the task according to the email. """ email_list = self.email_split(msg) partner_ids = [p for p in self._find_partner_from_emails(email_list, force_create=False) if p] self.message_subscribe(partner_ids) return super(Task, self).message_update(msg, update_vals=update_vals) @api.multi def message_get_suggested_recipients(self): recipients = super(Task, self).message_get_suggested_recipients() for task in self: if task.partner_id: reason = _('Customer Email') if task.partner_id.email else _('Customer') task._message_add_suggested_recipient(recipients, partner=task.partner_id, reason=reason) elif task.email_from: task._message_add_suggested_recipient(recipients, email=task.email_from, reason=_('Customer Email')) return recipients @api.multi def _notify_specific_email_values(self, message): res = super(Task, self)._notify_specific_email_values(message) try: headers = safe_eval(res.get('headers', dict())) except Exception: headers = {} if self.project_id: current_objects = [h for h in headers.get('X-Swerp-Objects', '').split(',') if h] current_objects.insert(0, 'project.project-%s, ' % self.project_id.id) headers['X-Swerp-Objects'] = ','.join(current_objects) if self.tag_ids: headers['X-Swerp-Tags'] = ','.join(self.tag_ids.mapped('name')) res['headers'] = repr(headers) return res def _message_post_after_hook(self, message, *args, **kwargs): if self.email_from and not self.partner_id: # we consider that posting a message with a specified recipient (not a follower, a specific one) # on a document without customer means that it was created through the chatter using # suggested recipients. This heuristic allows to avoid ugly hacks in JS. new_partner = message.partner_ids.filtered(lambda partner: partner.email == self.email_from) if new_partner: self.search([ ('partner_id', '=', False), ('email_from', '=', new_partner.email), ('stage_id.fold', '=', False)]).write({'partner_id': new_partner.id}) return super(Task, self)._message_post_after_hook(message, *args, **kwargs) def action_assign_to_me(self): self.write({'user_id': self.env.user.id}) def action_open_parent_task(self): return { 'name': _('Parent Task'), 'view_type': 'form', 'view_mode': 'form', 'res_model': 'project.task', 'res_id': self.parent_id.id, 'type': 'ir.actions.act_window' } def action_subtask(self): action = self.env.ref('project.project_task_action_sub_task').read()[0] ctx = self.env.context.copy() ctx.update({ 'default_parent_id': self.id, 'default_project_id': self.env.context.get('project_id', self.project_id.id), 'default_name': self.env.context.get('name', self.name) + ':', 'default_partner_id': self.env.context.get('partner_id', self.partner_id.id), 'search_default_project_id': self.env.context.get('project_id', self.project_id.id), }) action['context'] = ctx action['domain'] = [('id', 'child_of', self.id), ('id', '!=', self.id)] return action # --------------------------------------------------- # Rating business # --------------------------------------------------- def _send_task_rating_mail(self, force_send=False): for task in self: rating_template = task.stage_id.rating_template_id if rating_template: task.rating_send_request(rating_template, lang=task.partner_id.lang, force_send=force_send) def rating_get_partner_id(self): res = super(Task, self).rating_get_partner_id() if not res and self.project_id.partner_id: return self.project_id.partner_id return res @api.multi def rating_apply(self, rate, token=None, feedback=None, subtype=None): return super(Task, self).rating_apply(rate, token=token, feedback=feedback, subtype="project.mt_task_rating") def rating_get_parent(self): return 'project_id'
class BlogPost(models.Model): _name = "blog.post" _description = "Blog Post" _inherit = [ 'mail.thread', 'website.seo.metadata', 'website.published.multi.mixin' ] _order = 'id DESC' _mail_post_access = 'read' @api.multi def _compute_website_url(self): super(BlogPost, self)._compute_website_url() for blog_post in self: blog_post.website_url = "/blog/%s/post/%s" % (slug( blog_post.blog_id), slug(blog_post)) @api.multi @api.depends('post_date', 'visits') def _compute_ranking(self): res = {} for blog_post in self: if blog_post.id: # avoid to rank one post not yet saved and so withtout post_date in case of an onchange. age = datetime.now() - fields.Datetime.from_string( blog_post.post_date) res[blog_post.id] = blog_post.visits * ( 0.5 + random.random()) / max(3, age.days) return res def _default_content(self): return ''' <section class="s_text_block"> <div class="container"> <div class="row"> <div class="col-lg-12 mb16 mt16"> <p class="o_default_snippet_text">''' + _( "Start writing here...") + '''</p> </div> </div> </div> </section> ''' name = fields.Char('Title', required=True, translate=True, default='') subtitle = fields.Char('Sub Title', translate=True) author_id = fields.Many2one('res.partner', 'Author', default=lambda self: self.env.user.partner_id) active = fields.Boolean('Active', default=True) cover_properties = fields.Text( 'Cover Properties', default= '{"background-image": "none", "background-color": "oe_black", "opacity": "0.2", "resize_class": ""}' ) blog_id = fields.Many2one('blog.blog', 'Blog', required=True, ondelete='cascade') tag_ids = fields.Many2many('blog.tag', string='Tags') content = fields.Html('Content', default=_default_content, translate=html_translate, sanitize=False) teaser = fields.Text('Teaser', compute='_compute_teaser', inverse='_set_teaser') teaser_manual = fields.Text(string='Teaser Content') website_message_ids = fields.One2many( domain=lambda self: [('model', '=', self._name), ('message_type', '=', 'comment')]) # creation / update stuff create_date = fields.Datetime('Created on', index=True, readonly=True) published_date = fields.Datetime('Published Date') post_date = fields.Datetime( 'Publishing date', compute='_compute_post_date', inverse='_set_post_date', store=True, help= "The blog post will be visible for your visitors as of this date on the website if it is set as published." ) create_uid = fields.Many2one('res.users', 'Created by', index=True, readonly=True) write_date = fields.Datetime('Last Updated on', index=True, readonly=True) write_uid = fields.Many2one('res.users', 'Last Contributor', index=True, readonly=True) author_avatar = fields.Binary(related='author_id.image_small', string="Avatar", readonly=False) visits = fields.Integer('No of Views', copy=False) ranking = fields.Float(compute='_compute_ranking', string='Ranking') website_id = fields.Many2one(related='blog_id.website_id', readonly=True) @api.multi @api.depends('content', 'teaser_manual') def _compute_teaser(self): for blog_post in self: if blog_post.teaser_manual: blog_post.teaser = blog_post.teaser_manual else: content = html2plaintext(blog_post.content).replace('\n', ' ') blog_post.teaser = content[:150] + '...' @api.multi def _set_teaser(self): for blog_post in self: blog_post.teaser_manual = blog_post.teaser @api.multi @api.depends('create_date', 'published_date') def _compute_post_date(self): for blog_post in self: if blog_post.published_date: blog_post.post_date = blog_post.published_date else: blog_post.post_date = blog_post.create_date @api.multi def _set_post_date(self): for blog_post in self: blog_post.published_date = blog_post.post_date if not blog_post.published_date: blog_post._write(dict(post_date=blog_post.create_date) ) # dont trigger inverse function def _check_for_publication(self, vals): if vals.get('website_published'): for post in self: post.blog_id.message_post_with_view( 'website_blog.blog_post_template_new_post', subject=post.name, values={'post': post}, subtype_id=self.env['ir.model.data'].xmlid_to_res_id( 'website_blog.mt_blog_blog_published')) return True return False @api.model def create(self, vals): post_id = super(BlogPost, self.with_context(mail_create_nolog=True)).create(vals) post_id._check_for_publication(vals) return post_id @api.multi def write(self, vals): result = True for post in self: copy_vals = dict(vals) if ('website_published' in vals and 'published_date' not in vals and (not post.published_date or post.published_date <= fields.Datetime.now())): copy_vals['published_date'] = vals[ 'website_published'] and fields.Datetime.now() or False result &= super(BlogPost, self).write(copy_vals) self._check_for_publication(vals) return result @api.multi def get_access_action(self, access_uid=None): """ Instead of the classic form view, redirect to the post on website directly if user is an employee or if the post is published. """ self.ensure_one() user = access_uid and self.env['res.users'].sudo().browse( access_uid) or self.env.user if user.share and not self.sudo().website_published: return super(BlogPost, self).get_access_action(access_uid) return { 'type': 'ir.actions.act_url', 'url': self.website_url, 'target': 'self', 'target_type': 'public', 'res_id': self.id, } @api.multi def _notify_get_groups(self, message, groups): """ Add access button to everyone if the document is published. """ groups = super(BlogPost, self)._notify_get_groups(message, groups) if self.website_published: for group_name, group_method, group_data in groups: group_data['has_button_access'] = True return groups @api.multi def _notify_customize_recipients(self, message, msg_vals, recipients_vals): """ Override to avoid keeping all notified recipients of a comment. We avoid tracking needaction on post comments. Only emails should be sufficient. """ msg_type = msg_vals.get('message_type') or message.message_type if msg_type == 'comment': return {'needaction_partner_ids': []} return {} def _default_website_meta(self): res = super(BlogPost, self)._default_website_meta() res['default_opengraph']['og:description'] = res['default_twitter'][ 'twitter:description'] = self.subtitle blog_post_cover_properties = json.loads(self.cover_properties) res['default_opengraph']['og:image'] = res['default_twitter'][ 'twitter:image'] = blog_post_cover_properties.get( 'background-image', 'none')[4:-1] res['default_opengraph']['og:title'] = res['default_twitter'][ 'twitter:title'] = self.name return res
class Module(models.Model): _name = "ir.module.module" _rec_name = "shortdesc" _description = "Module" _order = 'sequence,name' @api.model def fields_view_get(self, view_id=None, view_type='form', toolbar=False, submenu=False): res = super(Module, self).fields_view_get(view_id, view_type, toolbar=toolbar, submenu=False) if view_type == 'form' and res.get('toolbar',False): install_id = self.env.ref('base.action_server_module_immediate_install').id action = [rec for rec in res['toolbar']['action'] if rec.get('id', False) != install_id] res['toolbar'] = {'action': action} return res @classmethod def get_module_info(cls, name): try: return modules.load_information_from_description_file(name) except Exception: _logger.debug('Error when trying to fetch information for module %s', name, exc_info=True) return {} @api.depends('name', 'description') def _get_desc(self): for module in self: path = modules.get_module_resource(module.name, 'static/description/index.html') if path: with tools.file_open(path, 'rb') as desc_file: doc = desc_file.read() html = lxml.html.document_fromstring(doc) for element, attribute, link, pos in html.iterlinks(): if element.get('src') and not '//' in element.get('src') and not 'static/' in element.get('src'): element.set('src', "/%s/static/description/%s" % (module.name, element.get('src'))) module.description_html = tools.html_sanitize(lxml.html.tostring(html)) else: overrides = { 'embed_stylesheet': False, 'doctitle_xform': False, 'output_encoding': 'unicode', 'xml_declaration': False, 'file_insertion_enabled': False, } output = publish_string(source=module.description if not module.application and module.description else '', settings_overrides=overrides, writer=MyWriter()) module.description_html = tools.html_sanitize(output) @api.depends('name') def _get_latest_version(self): default_version = modules.adapt_version('1.0') for module in self: module.installed_version = self.get_module_info(module.name).get('version', default_version) @api.depends('name', 'state') def _get_views(self): IrModelData = self.env['ir.model.data'].with_context(active_test=True) dmodels = ['ir.ui.view', 'ir.actions.report', 'ir.ui.menu'] for module in self: # Skip uninstalled modules below, no data to find anyway. if module.state not in ('installed', 'to upgrade', 'to remove'): module.views_by_module = "" module.reports_by_module = "" module.menus_by_module = "" continue # then, search and group ir.model.data records imd_models = defaultdict(list) imd_domain = [('module', '=', module.name), ('model', 'in', tuple(dmodels))] for data in IrModelData.sudo().search(imd_domain): imd_models[data.model].append(data.res_id) def browse(model): # as this method is called before the module update, some xmlid # may be invalid at this stage; explictly filter records before # reading them return self.env[model].browse(imd_models[model]).exists() def format_view(v): return '%s%s (%s)' % (v.inherit_id and '* INHERIT ' or '', v.name, v.type) module.views_by_module = "\n".join(sorted(format_view(v) for v in browse('ir.ui.view'))) module.reports_by_module = "\n".join(sorted(r.name for r in browse('ir.actions.report'))) module.menus_by_module = "\n".join(sorted(m.complete_name for m in browse('ir.ui.menu'))) @api.depends('icon') def _get_icon_image(self): for module in self: module.icon_image = '' if module.icon: path_parts = module.icon.split('/') path = modules.get_module_resource(path_parts[1], *path_parts[2:]) else: path = modules.module.get_module_icon(module.name) if path: with tools.file_open(path, 'rb') as image_file: module.icon_image = base64.b64encode(image_file.read()) name = fields.Char('Technical Name', readonly=True, required=True, index=True) category_id = fields.Many2one('ir.module.category', string='Category', readonly=True, index=True) shortdesc = fields.Char('Module Name', readonly=True, translate=True) summary = fields.Char('Summary', readonly=True, translate=True) description = fields.Text('Description', readonly=True, translate=True) description_html = fields.Html('Description HTML', compute='_get_desc') author = fields.Char("Author", readonly=True) maintainer = fields.Char('Maintainer', readonly=True) contributors = fields.Text('Contributors', readonly=True) website = fields.Char("Website", readonly=True) # attention: Incorrect field names !! # installed_version refers the latest version (the one on disk) # latest_version refers the installed version (the one in database) # published_version refers the version available on the repository installed_version = fields.Char('Latest Version', compute='_get_latest_version') latest_version = fields.Char('Installed Version', readonly=True) published_version = fields.Char('Published Version', readonly=True) url = fields.Char('URL', readonly=True) sequence = fields.Integer('Sequence', default=100) dependencies_id = fields.One2many('ir.module.module.dependency', 'module_id', string='Dependencies', readonly=True) exclusion_ids = fields.One2many('ir.module.module.exclusion', 'module_id', string='Exclusions', readonly=True) auto_install = fields.Boolean('Automatic Installation', help='An auto-installable module is automatically installed by the ' 'system when all its dependencies are satisfied. ' 'If the module has no dependency, it is always installed.') state = fields.Selection(STATES, string='Status', default='uninstallable', readonly=True, index=True) demo = fields.Boolean('Demo Data', default=False, readonly=True) license = fields.Selection([ ('GPL-2', 'GPL Version 2'), ('GPL-2 or any later version', 'GPL-2 or later version'), ('GPL-3', 'GPL Version 3'), ('GPL-3 or any later version', 'GPL-3 or later version'), ('AGPL-3', 'Affero GPL-3'), ('LGPL-3', 'LGPL Version 3'), ('Other OSI approved licence', 'Other OSI Approved Licence'), ('OEEL-1', 'swerp Enterprise Edition License v1.0'), ('OPL-1', 'swerp Proprietary License v1.0'), ('Other proprietary', 'Other Proprietary') ], string='License', default='LGPL-3', readonly=True) menus_by_module = fields.Text(string='Menus', compute='_get_views', store=True) reports_by_module = fields.Text(string='Reports', compute='_get_views', store=True) views_by_module = fields.Text(string='Views', compute='_get_views', store=True) application = fields.Boolean('Application', readonly=True) icon = fields.Char('Icon URL') icon_image = fields.Binary(string='Icon', compute='_get_icon_image') to_buy = fields.Boolean('swerp Enterprise Module', default=False) _sql_constraints = [ ('name_uniq', 'UNIQUE (name)', 'The name of the module must be unique!'), ] @api.multi def unlink(self): if not self: return True for module in self: if module.state in ('installed', 'to upgrade', 'to remove', 'to install'): raise UserError(_('You are trying to remove a module that is installed or will be installed.')) self.clear_caches() return super(Module, self).unlink() @staticmethod def _check_external_dependencies(terp): depends = terp.get('external_dependencies') if not depends: return for pydep in depends.get('python', []): try: importlib.import_module(pydep) except ImportError: raise ImportError('No module named %s' % (pydep,)) for binary in depends.get('bin', []): try: tools.find_in_path(binary) except IOError: raise Exception('Unable to find %r in path' % (binary,)) @classmethod def check_external_dependencies(cls, module_name, newstate='to install'): terp = cls.get_module_info(module_name) try: cls._check_external_dependencies(terp) except Exception as e: if newstate == 'to install': msg = _('Unable to install module "%s" because an external dependency is not met: %s') elif newstate == 'to upgrade': msg = _('Unable to upgrade module "%s" because an external dependency is not met: %s') else: msg = _('Unable to process module "%s" because an external dependency is not met: %s') raise UserError(msg % (module_name, e.args[0])) @api.multi def _state_update(self, newstate, states_to_update, level=100): if level < 1: raise UserError(_('Recursion error in modules dependencies !')) # whether some modules are installed with demo data demo = False for module in self: # determine dependency modules to update/others update_mods, ready_mods = self.browse(), self.browse() for dep in module.dependencies_id: if dep.state == 'unknown': raise UserError(_("You try to install module '%s' that depends on module '%s'.\nBut the latter module is not available in your system.") % (module.name, dep.name,)) if dep.depend_id.state == newstate: ready_mods += dep.depend_id else: update_mods += dep.depend_id # update dependency modules that require it, and determine demo for module update_demo = update_mods._state_update(newstate, states_to_update, level=level-1) module_demo = module.demo or update_demo or any(mod.demo for mod in ready_mods) demo = demo or module_demo if module.state in states_to_update: # check dependencies and update module itself self.check_external_dependencies(module.name, newstate) module.write({'state': newstate, 'demo': module_demo}) return demo @assert_log_admin_access @api.multi def button_install(self): # domain to select auto-installable (but not yet installed) modules auto_domain = [('state', '=', 'uninstalled'), ('auto_install', '=', True)] # determine whether an auto-install module must be installed: # - all its dependencies are installed or to be installed, # - at least one dependency is 'to install' install_states = frozenset(('installed', 'to install', 'to upgrade')) def must_install(module): states = set(dep.state for dep in module.dependencies_id) return states <= install_states and 'to install' in states modules = self while modules: # Mark the given modules and their dependencies to be installed. modules._state_update('to install', ['uninstalled']) # Determine which auto-installable modules must be installed. modules = self.search(auto_domain).filtered(must_install) # the modules that are installed/to install/to upgrade install_mods = self.search([('state', 'in', list(install_states))]) # check individual exclusions install_names = {module.name for module in install_mods} for module in install_mods: for exclusion in module.exclusion_ids: if exclusion.name in install_names: msg = _('Modules "%s" and "%s" are incompatible.') raise UserError(msg % (module.shortdesc, exclusion.exclusion_id.shortdesc)) # check category exclusions def closure(module): todo = result = module while todo: result |= todo todo = todo.mapped('dependencies_id.depend_id') return result exclusives = self.env['ir.module.category'].search([('exclusive', '=', True)]) for category in exclusives: # retrieve installed modules in category and sub-categories categories = category.search([('id', 'child_of', category.ids)]) modules = install_mods.filtered(lambda mod: mod.category_id in categories) # the installation is valid if all installed modules in categories # belong to the transitive dependencies of one of them if modules and not any(modules <= closure(module) for module in modules): msg = _('You are trying to install incompatible modules in category "%s":') labels = dict(self.fields_get(['state'])['state']['selection']) raise UserError("\n".join([msg % category.name] + [ "- %s (%s)" % (module.shortdesc, labels[module.state]) for module in modules ])) return dict(ACTION_DICT, name=_('Install')) @assert_log_admin_access @api.multi def button_immediate_install(self): """ Installs the selected module(s) immediately and fully, returns the next res.config action to execute :returns: next res.config item to execute :rtype: dict[str, object] """ _logger.info('User #%d triggered module installation', self.env.uid) return self._button_immediate_function(type(self).button_install) @assert_log_admin_access @api.multi def button_install_cancel(self): self.write({'state': 'uninstalled', 'demo': False}) return True @assert_log_admin_access @api.multi def module_uninstall(self): """ Perform the various steps required to uninstall a module completely including the deletion of all database structures created by the module: tables, columns, constraints, etc. """ modules_to_remove = self.mapped('name') self.env['ir.model.data']._module_data_uninstall(modules_to_remove) # we deactivate prefetching to not try to read a column that has been deleted self.with_context(prefetch_fields=False).write({'state': 'uninstalled', 'latest_version': False}) return True @api.multi def _remove_copied_views(self): """ Remove the copies of the views installed by the modules in `self`. Those copies do not have an external id so they will not be cleaned by `_module_data_uninstall`. This is why we rely on `key` instead. It is important to remove these copies because using them will crash if they rely on data that don't exist anymore if the module is removed. """ domain = expression.OR([[('key', '=like', m.name + '.%')] for m in self]) orphans = self.env['ir.ui.view'].with_context(**{'active_test': False, MODULE_UNINSTALL_FLAG: True}).search(domain) orphans.unlink() @api.multi @api.returns('self') def downstream_dependencies(self, known_deps=None, exclude_states=('uninstalled', 'uninstallable', 'to remove')): """ Return the modules that directly or indirectly depend on the modules in `self`, and that satisfy the `exclude_states` filter. """ if not self: return self known_deps = known_deps or self.browse() query = """ SELECT DISTINCT m.id FROM ir_module_module_dependency d JOIN ir_module_module m ON (d.module_id=m.id) WHERE d.name IN (SELECT name from ir_module_module where id in %s) AND m.state NOT IN %s AND m.id NOT IN %s """ self._cr.execute(query, (tuple(self.ids), tuple(exclude_states), tuple(known_deps.ids or self.ids))) new_deps = self.browse([row[0] for row in self._cr.fetchall()]) missing_mods = new_deps - known_deps known_deps |= new_deps if missing_mods: known_deps |= missing_mods.downstream_dependencies(known_deps, exclude_states) return known_deps @api.multi @api.returns('self') def upstream_dependencies(self, known_deps=None, exclude_states=('installed', 'uninstallable', 'to remove')): """ Return the dependency tree of modules of the modules in `self`, and that satisfy the `exclude_states` filter. """ if not self: return self known_deps = known_deps or self.browse() query = """ SELECT DISTINCT m.id FROM ir_module_module_dependency d JOIN ir_module_module m ON (d.module_id=m.id) WHERE m.name IN (SELECT name from ir_module_module_dependency where module_id in %s) AND m.state NOT IN %s AND m.id NOT IN %s """ self._cr.execute(query, (tuple(self.ids), tuple(exclude_states), tuple(known_deps.ids or self.ids))) new_deps = self.browse([row[0] for row in self._cr.fetchall()]) missing_mods = new_deps - known_deps known_deps |= new_deps if missing_mods: known_deps |= missing_mods.upstream_dependencies(known_deps, exclude_states) return known_deps def next(self): """ Return the action linked to an ir.actions.todo is there exists one that should be executed. Otherwise, redirect to /web """ Todos = self.env['ir.actions.todo'] _logger.info('getting next %s', Todos) active_todo = Todos.search([('state', '=', 'open')], limit=1) if active_todo: _logger.info('next action is "%s"', active_todo.name) return active_todo.action_launch() return { 'type': 'ir.actions.act_url', 'target': 'self', 'url': '/web', } @api.multi def _button_immediate_function(self, function): try: # This is done because the installation/uninstallation/upgrade can modify a currently # running cron job and prevent it from finishing, and since the ir_cron table is locked # during execution, the lock won't be released until timeout. self._cr.execute("SELECT * FROM ir_cron FOR UPDATE NOWAIT") except psycopg2.OperationalError: raise UserError(_("The server is busy right now, module operations are not possible at" " this time, please try again later.")) function(self) self._cr.commit() api.Environment.reset() modules.registry.Registry.new(self._cr.dbname, update_module=True) self._cr.commit() env = api.Environment(self._cr, self._uid, self._context) # pylint: disable=next-method-called config = env['ir.module.module'].next() or {} if config.get('type') not in ('ir.actions.act_window_close',): return config # reload the client; open the first available root menu menu = env['ir.ui.menu'].search([('parent_id', '=', False)])[:1] return { 'type': 'ir.actions.client', 'tag': 'reload', 'params': {'menu_id': menu.id}, } @assert_log_admin_access @api.multi def button_immediate_uninstall(self): """ Uninstall the selected module(s) immediately and fully, returns the next res.config action to execute """ _logger.info('User #%d triggered module uninstallation', self.env.uid) return self._button_immediate_function(type(self).button_uninstall) @assert_log_admin_access @api.multi def button_uninstall(self): if 'base' in self.mapped('name'): raise UserError(_("The `base` module cannot be uninstalled")) if not all(state in ('installed', 'to upgrade') for state in self.mapped('state')): raise UserError(_( "One or more of the selected modules have already been uninstalled, if you " "believe this to be an error, you may try again later or contact support." )) deps = self.downstream_dependencies() (self + deps).write({'state': 'to remove'}) return dict(ACTION_DICT, name=_('Uninstall')) @assert_log_admin_access @api.multi def button_uninstall_wizard(self): """ Launch the wizard to uninstall the given module. """ return { 'type': 'ir.actions.act_window', 'target': 'new', 'name': _('Uninstall module'), 'view_mode': 'form', 'res_model': 'base.module.uninstall', 'context': {'default_module_id': self.id}, } @api.multi def button_uninstall_cancel(self): self.write({'state': 'installed'}) return True @assert_log_admin_access @api.multi def button_immediate_upgrade(self): """ Upgrade the selected module(s) immediately and fully, return the next res.config action to execute """ return self._button_immediate_function(type(self).button_upgrade) @assert_log_admin_access @api.multi def button_upgrade(self): Dependency = self.env['ir.module.module.dependency'] self.update_list() todo = list(self) i = 0 while i < len(todo): module = todo[i] i += 1 if module.state not in ('installed', 'to upgrade'): raise UserError(_("Can not upgrade module '%s'. It is not installed.") % (module.name,)) self.check_external_dependencies(module.name, 'to upgrade') for dep in Dependency.search([('name', '=', module.name)]): if dep.module_id.state == 'installed' and dep.module_id not in todo: todo.append(dep.module_id) self.browse(module.id for module in todo).write({'state': 'to upgrade'}) to_install = [] for module in todo: for dep in module.dependencies_id: if dep.state == 'unknown': raise UserError(_('You try to upgrade the module %s that depends on the module: %s.\nBut this module is not available in your system.') % (module.name, dep.name,)) if dep.state == 'uninstalled': to_install += self.search([('name', '=', dep.name)]).ids self.browse(to_install).button_install() return dict(ACTION_DICT, name=_('Apply Schedule Upgrade')) @assert_log_admin_access @api.multi def button_upgrade_cancel(self): self.write({'state': 'installed'}) return True @staticmethod def get_values_from_terp(terp): return { 'description': terp.get('description', ''), 'shortdesc': terp.get('name', ''), 'author': terp.get('author', 'Unknown'), 'maintainer': terp.get('maintainer', False), 'contributors': ', '.join(terp.get('contributors', [])) or False, 'website': terp.get('website', ''), 'license': terp.get('license', 'LGPL-3'), 'sequence': terp.get('sequence', 100), 'application': terp.get('application', False), 'auto_install': terp.get('auto_install', False), 'icon': terp.get('icon', False), 'summary': terp.get('summary', ''), 'url': terp.get('url') or terp.get('live_test_url', ''), 'to_buy': False } @api.model def create(self, vals): new = super(Module, self).create(vals) module_metadata = { 'name': 'module_%s' % vals['name'], 'model': 'ir.module.module', 'module': 'base', 'res_id': new.id, 'noupdate': True, } self.env['ir.model.data'].create(module_metadata) return new # update the list of available packages @assert_log_admin_access @api.model def update_list(self): res = [0, 0] # [update, add] default_version = modules.adapt_version('1.0') known_mods = self.with_context(lang=None).search([]) known_mods_names = {mod.name: mod for mod in known_mods} # iterate through detected modules and update/create them in db for mod_name in modules.get_modules(): mod = known_mods_names.get(mod_name) terp = self.get_module_info(mod_name) values = self.get_values_from_terp(terp) if mod: updated_values = {} for key in values: old = getattr(mod, key) updated = tools.ustr(values[key]) if isinstance(values[key], pycompat.string_types) else values[key] if (old or updated) and updated != old: updated_values[key] = values[key] if terp.get('installable', True) and mod.state == 'uninstallable': updated_values['state'] = 'uninstalled' if parse_version(terp.get('version', default_version)) > parse_version(mod.latest_version or default_version): res[0] += 1 if updated_values: mod.write(updated_values) else: mod_path = modules.get_module_path(mod_name) if not mod_path or not terp: continue state = "uninstalled" if terp.get('installable', True) else "uninstallable" mod = self.create(dict(name=mod_name, state=state, **values)) res[1] += 1 mod._update_dependencies(terp.get('depends', [])) mod._update_exclusions(terp.get('excludes', [])) mod._update_category(terp.get('category', 'Uncategorized')) return res @assert_log_admin_access @api.multi def download(self, download=True): return [] @assert_log_admin_access @api.model def install_from_urls(self, urls): if not self.env.user.has_group('base.group_system'): raise AccessDenied() # One-click install is opt-in - cfr Issue #15225 ad_dir = tools.config.addons_data_dir if not os.access(ad_dir, os.W_OK): msg = (_("Automatic install of downloaded Apps is currently disabled.") + "\n\n" + _("To enable it, make sure this directory exists and is writable on the server:") + "\n%s" % ad_dir) _logger.warning(msg) raise UserError(msg) apps_server = urls.url_parse(self.get_apps_server()) OPENERP = swerp.release.product_name.lower() tmp = tempfile.mkdtemp() _logger.debug('Install from url: %r', urls) try: # 1. Download & unzip missing modules for module_name, url in urls.items(): if not url: continue # nothing to download, local version is already the last one up = urls.url_parse(url) if up.scheme != apps_server.scheme or up.netloc != apps_server.netloc: raise AccessDenied() try: _logger.info('Downloading module `%s` from OpenERP Apps', module_name) response = requests.get(url) response.raise_for_status() content = response.content except Exception: _logger.exception('Failed to fetch module %s', module_name) raise UserError(_('The `%s` module appears to be unavailable at the moment, please try again later.') % module_name) else: zipfile.ZipFile(io.BytesIO(content)).extractall(tmp) assert os.path.isdir(os.path.join(tmp, module_name)) # 2a. Copy/Replace module source in addons path for module_name, url in urls.items(): if module_name == OPENERP or not url: continue # OPENERP is special case, handled below, and no URL means local module module_path = modules.get_module_path(module_name, downloaded=True, display_warning=False) bck = backup(module_path, False) _logger.info('Copy downloaded module `%s` to `%s`', module_name, module_path) shutil.move(os.path.join(tmp, module_name), module_path) if bck: shutil.rmtree(bck) # 2b. Copy/Replace server+base module source if downloaded if urls.get(OPENERP): # special case. it contains the server and the base module. # extract path is not the same base_path = os.path.dirname(modules.get_module_path('base')) # copy all modules in the SERVER/swerp/addons directory to the new "swerp" module (except base itself) for d in os.listdir(base_path): if d != 'base' and os.path.isdir(os.path.join(base_path, d)): destdir = os.path.join(tmp, OPENERP, 'addons', d) # XXX 'swerp' subdirectory ? shutil.copytree(os.path.join(base_path, d), destdir) # then replace the server by the new "base" module server_dir = tools.config['root_path'] # XXX or dirname() bck = backup(server_dir) _logger.info('Copy downloaded module `swerp` to `%s`', server_dir) shutil.move(os.path.join(tmp, OPENERP), server_dir) #if bck: # shutil.rmtree(bck) self.update_list() with_urls = [module_name for module_name, url in urls.items() if url] downloaded = self.search([('name', 'in', with_urls)]) installed = self.search([('id', 'in', downloaded.ids), ('state', '=', 'installed')]) to_install = self.search([('name', 'in', list(urls)), ('state', '=', 'uninstalled')]) post_install_action = to_install.button_immediate_install() if installed or to_install: # in this case, force server restart to reload python code... self._cr.commit() swerp.service.server.restart() return { 'type': 'ir.actions.client', 'tag': 'home', 'params': {'wait': True}, } return post_install_action finally: shutil.rmtree(tmp) @api.model def get_apps_server(self): return tools.config.get('apps_server', 'https://apps.swerp.it/apps') def _update_dependencies(self, depends=None): existing = set(dep.name for dep in self.dependencies_id) needed = set(depends or []) for dep in (needed - existing): self._cr.execute('INSERT INTO ir_module_module_dependency (module_id, name) values (%s, %s)', (self.id, dep)) for dep in (existing - needed): self._cr.execute('DELETE FROM ir_module_module_dependency WHERE module_id = %s and name = %s', (self.id, dep)) self.invalidate_cache(['dependencies_id'], self.ids) def _update_exclusions(self, excludes=None): existing = set(excl.name for excl in self.exclusion_ids) needed = set(excludes or []) for name in (needed - existing): self._cr.execute('INSERT INTO ir_module_module_exclusion (module_id, name) VALUES (%s, %s)', (self.id, name)) for name in (existing - needed): self._cr.execute('DELETE FROM ir_module_module_exclusion WHERE module_id=%s AND name=%s', (self.id, name)) self.invalidate_cache(['exclusion_ids'], self.ids) def _update_category(self, category='Uncategorized'): current_category = self.category_id current_category_path = [] while current_category: current_category_path.insert(0, current_category.name) current_category = current_category.parent_id categs = category.split('/') if categs != current_category_path: cat_id = modules.db.create_categories(self._cr, categs) self.write({'category_id': cat_id}) @api.multi def _update_translations(self, filter_lang=None): if not filter_lang: langs = self.env['res.lang'].search([('translatable', '=', True)]) filter_lang = [lang.code for lang in langs] elif not isinstance(filter_lang, (list, tuple)): filter_lang = [filter_lang] update_mods = self.filtered(lambda r: r.state in ('installed', 'to install', 'to upgrade')) mod_dict = { mod.name: mod.dependencies_id.mapped('name') for mod in update_mods } mod_names = topological_sort(mod_dict) self.env['ir.translation'].load_module_terms(mod_names, filter_lang) @api.multi def _check(self): for module in self: if not module.description_html: _logger.warning('module %s: description is empty !', module.name) @api.model @tools.ormcache() def _installed(self): """ Return the set of installed modules as a dictionary {name: id} """ return { module.name: module.id for module in self.sudo().search([('state', '=', 'installed')]) }
class MailTemplate(models.Model): "Templates for sending email" _name = "mail.template" _description = 'Email Templates' _order = 'name' @api.model def default_get(self, fields): res = super(MailTemplate, self).default_get(fields) if res.get('model'): res['model_id'] = self.env['ir.model']._get(res.pop('model')).id return res name = fields.Char('Name') model_id = fields.Many2one('ir.model', 'Applies to', help="The type of document this template can be used with") model = fields.Char('Related Document Model', related='model_id.model', index=True, store=True, readonly=True) lang = fields.Char('Language', help="Optional translation language (ISO code) to select when sending out an email. " "If not set, the english version will be used. " "This should usually be a placeholder expression " "that provides the appropriate language, e.g. " "${object.partner_id.lang}.", placeholder="${object.partner_id.lang}") user_signature = fields.Boolean('Add Signature', help="If checked, the user's signature will be appended to the text version " "of the message") subject = fields.Char('Subject', translate=True, help="Subject (placeholders may be used here)") email_from = fields.Char('From', help="Sender address (placeholders may be used here). If not set, the default " "value will be the author's email alias if configured, or email address.") use_default_to = fields.Boolean( 'Default recipients', help="Default recipients of the record:\n" "- partner (using id on a partner or the partner_id field) OR\n" "- email (using email_from or email field)") email_to = fields.Char('To (Emails)', help="Comma-separated recipient addresses (placeholders may be used here)") partner_to = fields.Char('To (Partners)', oldname='email_recipients', help="Comma-separated ids of recipient partners (placeholders may be used here)") email_cc = fields.Char('Cc', help="Carbon copy recipients (placeholders may be used here)") reply_to = fields.Char('Reply-To', help="Preferred response address (placeholders may be used here)") mail_server_id = fields.Many2one('ir.mail_server', 'Outgoing Mail Server', readonly=False, help="Optional preferred server for outgoing mails. If not set, the highest " "priority one will be used.") body_html = fields.Html('Body', translate=True, sanitize=False) report_name = fields.Char('Report Filename', translate=True, help="Name to use for the generated report file (may contain placeholders)\n" "The extension can be omitted and will then come from the report type.") report_template = fields.Many2one('ir.actions.report', 'Optional report to print and attach') ref_ir_act_window = fields.Many2one('ir.actions.act_window', 'Sidebar action', readonly=True, copy=False, help="Sidebar action to make this template available on records " "of the related document model") attachment_ids = fields.Many2many('ir.attachment', 'email_template_attachment_rel', 'email_template_id', 'attachment_id', 'Attachments', help="You may attach files to this template, to be added to all " "emails created from this template") auto_delete = fields.Boolean('Auto Delete', default=True, help="Permanently delete this email after sending it, to save space") # Fake fields used to implement the placeholder assistant model_object_field = fields.Many2one('ir.model.fields', string="Field", help="Select target field from the related document model.\n" "If it is a relationship field you will be able to select " "a target field at the destination of the relationship.") sub_object = fields.Many2one('ir.model', 'Sub-model', readonly=True, help="When a relationship field is selected as first field, " "this field shows the document model the relationship goes to.") sub_model_object_field = fields.Many2one('ir.model.fields', 'Sub-field', help="When a relationship field is selected as first field, " "this field lets you select the target field within the " "destination document model (sub-model).") null_value = fields.Char('Default Value', help="Optional value to use if the target field is empty") copyvalue = fields.Char('Placeholder Expression', help="Final placeholder expression, to be copy-pasted in the desired template field.") scheduled_date = fields.Char('Scheduled Date', help="If set, the queue manager will send the email after the date. If not set, the email will be send as soon as possible. Jinja2 placeholders may be used.") @api.onchange('model_id') def onchange_model_id(self): # TDE CLEANME: should'nt it be a stored related ? if self.model_id: self.model = self.model_id.model else: self.model = False def build_expression(self, field_name, sub_field_name, null_value): """Returns a placeholder expression for use in a template field, based on the values provided in the placeholder assistant. :param field_name: main field name :param sub_field_name: sub field name (M2O) :param null_value: default value if the target value is empty :return: final placeholder expression """ expression = '' if field_name: expression = "${object." + field_name if sub_field_name: expression += "." + sub_field_name if null_value: expression += " or '''%s'''" % null_value expression += "}" return expression @api.onchange('model_object_field', 'sub_model_object_field', 'null_value') def onchange_sub_model_object_value_field(self): if self.model_object_field: if self.model_object_field.ttype in ['many2one', 'one2many', 'many2many']: model = self.env['ir.model']._get(self.model_object_field.relation) if model: self.sub_object = model.id self.copyvalue = self.build_expression(self.model_object_field.name, self.sub_model_object_field and self.sub_model_object_field.name or False, self.null_value or False) else: self.sub_object = False self.sub_model_object_field = False self.copyvalue = self.build_expression(self.model_object_field.name, False, self.null_value or False) else: self.sub_object = False self.copyvalue = False self.sub_model_object_field = False self.null_value = False @api.multi def unlink(self): self.unlink_action() return super(MailTemplate, self).unlink() @api.multi @api.returns('self', lambda value: value.id) def copy(self, default=None): default = dict(default or {}, name=_("%s (copy)") % self.name) return super(MailTemplate, self).copy(default=default) @api.multi def unlink_action(self): for template in self: if template.ref_ir_act_window: template.ref_ir_act_window.unlink() return True @api.multi def create_action(self): ActWindow = self.env['ir.actions.act_window'] view = self.env.ref('mail.email_compose_message_wizard_form') for template in self: button_name = _('Send Mail (%s)') % template.name action = ActWindow.create({ 'name': button_name, 'type': 'ir.actions.act_window', 'res_model': 'mail.compose.message', 'src_model': template.model_id.model, 'view_type': 'form', 'context': "{'default_composition_mode': 'mass_mail', 'default_template_id' : %d, 'default_use_template': True}" % (template.id), 'view_mode': 'form,tree', 'view_id': view.id, 'target': 'new', 'binding_model_id': template.model_id.id, }) template.write({'ref_ir_act_window': action.id}) return True # ---------------------------------------- # RENDERING # ---------------------------------------- @api.model def render_post_process(self, html): html = self.env['mail.thread']._replace_local_links(html) return html @api.model def _render_template(self, template_txt, model, res_ids, post_process=False): """ Render the given template text, replace mako expressions ``${expr}`` with the result of evaluating these expressions with an evaluation context containing: - ``user``: Model of the current user - ``object``: record of the document record this mail is related to - ``context``: the context passed to the mail composition wizard :param str template_txt: the template text to render :param str model: model name of the document record this mail is related to. :param int res_ids: list of ids of document records those mails are related to. """ multi_mode = True if isinstance(res_ids, pycompat.integer_types): multi_mode = False res_ids = [res_ids] results = dict.fromkeys(res_ids, u"") # try to load the template try: mako_env = mako_safe_template_env if self.env.context.get('safe') else mako_template_env template = mako_env.from_string(tools.ustr(template_txt)) except Exception: _logger.info("Failed to load template %r", template_txt, exc_info=True) return multi_mode and results or results[res_ids[0]] # prepare template variables records = self.env[model].browse(it for it in res_ids if it) # filter to avoid browsing [None] res_to_rec = dict.fromkeys(res_ids, None) for record in records: res_to_rec[record.id] = record variables = { 'format_date': lambda date, format=False, context=self._context: format_date(self.env, date, format), 'format_tz': lambda dt, tz=False, format=False, context=self._context: format_tz(self.env, dt, tz, format), 'format_amount': lambda amount, currency, context=self._context: format_amount(self.env, amount, currency), 'user': self.env.user, 'ctx': self._context, # context kw would clash with mako internals } for res_id, record in res_to_rec.items(): variables['object'] = record try: render_result = template.render(variables) except Exception as e: _logger.info("Failed to render template %r using values %r" % (template, variables), exc_info=True) raise UserError(_("Failed to render template %r using values %r") % (template, variables) + "\n\n%s: %s" % (type(e).__name__, str(e))) if render_result == u"False": render_result = u"" results[res_id] = render_result if post_process: for res_id, result in results.items(): results[res_id] = self.render_post_process(result) return multi_mode and results or results[res_ids[0]] @api.multi def get_email_template(self, res_ids): multi_mode = True if isinstance(res_ids, pycompat.integer_types): res_ids = [res_ids] multi_mode = False if res_ids is None: res_ids = [None] results = dict.fromkeys(res_ids, False) if not self.ids: return results self.ensure_one() langs = self._render_template(self.lang, self.model, res_ids) for res_id, lang in langs.items(): if lang: template = self.with_context(lang=lang) else: template = self results[res_id] = template return multi_mode and results or results[res_ids[0]] @api.multi def generate_recipients(self, results, res_ids): """Generates the recipients of the template. Default values can ben generated instead of the template values if requested by template or context. Emails (email_to, email_cc) can be transformed into partners if requested in the context. """ self.ensure_one() if self.use_default_to or self._context.get('tpl_force_default_to'): default_recipients = self.env['mail.thread'].message_get_default_recipients(res_model=self.model, res_ids=res_ids) for res_id, recipients in default_recipients.items(): results[res_id].pop('partner_to', None) results[res_id].update(recipients) records_company = None if self._context.get('tpl_partners_only') and self.model and results and 'company_id' in self.env[self.model]._fields: records = self.env[self.model].browse(results.keys()).read(['company_id']) records_company = {rec['id']: (rec['company_id'][0] if rec['company_id'] else None) for rec in records} for res_id, values in results.items(): partner_ids = values.get('partner_ids', list()) if self._context.get('tpl_partners_only'): mails = tools.email_split(values.pop('email_to', '')) + tools.email_split(values.pop('email_cc', '')) Partner = self.env['res.partner'] if records_company: Partner = Partner.with_context(default_company_id=records_company[res_id]) for mail in mails: partner_id = Partner.find_or_create(mail) partner_ids.append(partner_id) partner_to = values.pop('partner_to', '') if partner_to: # placeholders could generate '', 3, 2 due to some empty field values tpl_partner_ids = [int(pid) for pid in partner_to.split(',') if pid] partner_ids += self.env['res.partner'].sudo().browse(tpl_partner_ids).exists().ids results[res_id]['partner_ids'] = partner_ids return results @api.multi def generate_email(self, res_ids, fields=None): """Generates an email from the template for given the given model based on records given by res_ids. :param res_id: id of the record to use for rendering the template (model is taken from template definition) :returns: a dict containing all relevant fields for creating a new mail.mail entry, with one extra key ``attachments``, in the format [(report_name, data)] where data is base64 encoded. """ self.ensure_one() multi_mode = True if isinstance(res_ids, pycompat.integer_types): res_ids = [res_ids] multi_mode = False if fields is None: fields = ['subject', 'body_html', 'email_from', 'email_to', 'partner_to', 'email_cc', 'reply_to', 'scheduled_date'] res_ids_to_templates = self.get_email_template(res_ids) # templates: res_id -> template; template -> res_ids templates_to_res_ids = {} for res_id, template in res_ids_to_templates.items(): templates_to_res_ids.setdefault(template, []).append(res_id) results = dict() for template, template_res_ids in templates_to_res_ids.items(): Template = self.env['mail.template'] # generate fields value for all res_ids linked to the current template if template.lang: Template = Template.with_context(lang=template._context.get('lang')) for field in fields: Template = Template.with_context(safe=field in {'subject'}) generated_field_values = Template._render_template( getattr(template, field), template.model, template_res_ids, post_process=(field == 'body_html')) for res_id, field_value in generated_field_values.items(): results.setdefault(res_id, dict())[field] = field_value # compute recipients if any(field in fields for field in ['email_to', 'partner_to', 'email_cc']): results = template.generate_recipients(results, template_res_ids) # update values for all res_ids for res_id in template_res_ids: values = results[res_id] # body: add user signature, sanitize if 'body_html' in fields and template.user_signature: signature = self.env.user.signature if signature: values['body_html'] = tools.append_content_to_html(values['body_html'], signature, plaintext=False) if values.get('body_html'): values['body'] = tools.html_sanitize(values['body_html']) # technical settings values.update( mail_server_id=template.mail_server_id.id or False, auto_delete=template.auto_delete, model=template.model, res_id=res_id or False, attachment_ids=[attach.id for attach in template.attachment_ids], ) # Add report in attachments: generate once for all template_res_ids if template.report_template: for res_id in template_res_ids: attachments = [] report_name = self._render_template(template.report_name, template.model, res_id) report = template.report_template report_service = report.report_name if report.report_type in ['qweb-html', 'qweb-pdf']: result, format = report.render_qweb_pdf([res_id]) else: res = report.render([res_id]) if not res: raise UserError(_('Unsupported report type %s found.') % report.report_type) result, format = res # TODO in trunk, change return format to binary to match message_post expected format result = base64.b64encode(result) if not report_name: report_name = 'report.' + report_service ext = "." + format if not report_name.endswith(ext): report_name += ext attachments.append((report_name, result)) results[res_id]['attachments'] = attachments return multi_mode and results or results[res_ids[0]] @api.multi def send_mail(self, res_id, force_send=False, raise_exception=False, email_values=None, notif_layout=False): """ Generates a new mail.mail. Template is rendered on record given by res_id and model coming from template. :param int res_id: id of the record to render the template :param bool force_send: send email immediately; otherwise use the mail queue (recommended); :param dict email_values: update generated mail with those values to further customize the mail; :param str notif_layout: optional notification layout to encapsulate the generated email; :returns: id of the mail.mail that was created """ self.ensure_one() Mail = self.env['mail.mail'] Attachment = self.env['ir.attachment'] # TDE FIXME: should remove dfeault_type from context # create a mail_mail based on values, without attachments values = self.generate_email(res_id) values['recipient_ids'] = [(4, pid) for pid in values.get('partner_ids', list())] values.update(email_values or {}) attachment_ids = values.pop('attachment_ids', []) attachments = values.pop('attachments', []) # add a protection against void email_from if 'email_from' in values and not values.get('email_from'): values.pop('email_from') # encapsulate body if notif_layout and values['body_html']: try: template = self.env.ref(notif_layout, raise_if_not_found=True) except ValueError: _logger.warning('QWeb template %s not found when sending template %s. Sending without layouting.' % (notif_layout, self.name)) else: record = self.env[self.model].browse(res_id) lang = self._render_template(self.lang, self.model, res_id) model = self.env['ir.model']._get(record._name) if lang: template = template.with_context(lang=lang) model = model.with_context(lang=lang) template_ctx = { 'message': self.env['mail.message'].sudo().new(dict(body=values['body_html'], record_name=record.display_name)), 'model_description': model.display_name, 'company': 'company_id' in record and record['company_id'] or self.env.user.company_id, 'record': record, } body = template.render(template_ctx, engine='ir.qweb', minimal_qcontext=True) values['body_html'] = self.env['mail.thread']._replace_local_links(body) mail = Mail.create(values) # manage attachments for attachment in attachments: attachment_data = { 'name': attachment[0], 'datas_fname': attachment[0], 'datas': attachment[1], 'type': 'binary', 'res_model': 'mail.message', 'res_id': mail.mail_message_id.id, } attachment_ids.append(Attachment.create(attachment_data).id) if attachment_ids: values['attachment_ids'] = [(6, 0, attachment_ids)] mail.write({'attachment_ids': [(6, 0, attachment_ids)]}) if force_send: mail.send(raise_exception=raise_exception) return mail.id # TDE CLEANME: return mail + api.returns ?
class Note(models.Model): _name = 'note.note' _inherit = ['mail.thread', 'mail.activity.mixin'] _description = "Note" _order = 'sequence' def _get_default_stage_id(self): return self.env['note.stage'].search([('user_id', '=', self.env.uid)], limit=1) name = fields.Text(compute='_compute_name', string='Note Summary', store=True) user_id = fields.Many2one('res.users', string='Owner', default=lambda self: self.env.uid) memo = fields.Html('Note Content') sequence = fields.Integer('Sequence') stage_id = fields.Many2one('note.stage', compute='_compute_stage_id', inverse='_inverse_stage_id', string='Stage', default=_get_default_stage_id) stage_ids = fields.Many2many('note.stage', 'note_stage_rel', 'note_id', 'stage_id', string='Stages of Users', default=_get_default_stage_id) open = fields.Boolean(string='Active', default=True) date_done = fields.Date('Date done') color = fields.Integer(string='Color Index') tag_ids = fields.Many2many('note.tag', 'note_tags_rel', 'note_id', 'tag_id', string='Tags') @api.depends('memo') def _compute_name(self): """ Read the first line of the memo to determine the note name """ for note in self: text = html2plaintext(note.memo) if note.memo else '' note.name = text.strip().replace('*', '').split("\n")[0] @api.multi def _compute_stage_id(self): first_user_stage = self.env['note.stage'].search( [('user_id', '=', self.env.uid)], limit=1) for note in self: for stage in note.stage_ids.filtered( lambda stage: stage.user_id == self.env.user): note.stage_id = stage # note without user's stage if not note.stage_id: note.stage_id = first_user_stage @api.multi def _inverse_stage_id(self): for note in self.filtered('stage_id'): note.stage_ids = note.stage_id + note.stage_ids.filtered( lambda stage: stage.user_id != self.env.user) @api.model def name_create(self, name): return self.create({'memo': name}).name_get()[0] @api.model def read_group(self, domain, fields, groupby, offset=0, limit=None, orderby=False, lazy=True): if groupby and groupby[0] == "stage_id": stages = self.env['note.stage'].search([('user_id', '=', self.env.uid)]) if stages: # if the user has some stages result = [{ # notes by stage for stages user '__context': {'group_by': groupby[1:]}, '__domain': domain + [('stage_ids.id', '=', stage.id)], 'stage_id': (stage.id, stage.name), 'stage_id_count': self.search_count(domain + [('stage_ids', '=', stage.id)]), '__fold': stage.fold, } for stage in stages] # note without user's stage nb_notes_ws = self.search_count(domain + [('stage_ids', 'not in', stages.ids)]) if nb_notes_ws: # add note to the first column if it's the first stage dom_not_in = ('stage_ids', 'not in', stages.ids) if result and result[0]['stage_id'][0] == stages[0].id: dom_in = result[0]['__domain'].pop() result[0]['__domain'] = domain + [ '|', dom_in, dom_not_in ] result[0]['stage_id_count'] += nb_notes_ws else: # add the first stage column result = [{ '__context': { 'group_by': groupby[1:] }, '__domain': domain + [dom_not_in], 'stage_id': (stages[0].id, stages[0].name), 'stage_id_count': nb_notes_ws, '__fold': stages[0].name, }] + result else: # if stage_ids is empty, get note without user's stage nb_notes_ws = self.search_count(domain) if nb_notes_ws: result = [{ # notes for unknown stage '__context': { 'group_by': groupby[1:] }, '__domain': domain, 'stage_id': False, 'stage_id_count': nb_notes_ws }] else: result = [] return result return super(Note, self).read_group(domain, fields, groupby, offset=offset, limit=limit, orderby=orderby, lazy=lazy) @api.multi def action_close(self): return self.write({'open': False, 'date_done': fields.date.today()}) @api.multi def action_open(self): return self.write({'open': True})
class ProductTemplate(models.Model): _inherit = [ "product.template", "website.seo.metadata", 'website.published.multi.mixin', 'rating.mixin' ] _name = 'product.template' _mail_post_access = 'read' website_description = fields.Html('Description for the website', sanitize_attributes=False, translate=html_translate) alternative_product_ids = fields.Many2many( 'product.template', 'product_alternative_rel', 'src_id', 'dest_id', string='Alternative Products', help='Suggest alternatives to your customer' '(upsell strategy).Those product show up on the product page.') accessory_product_ids = fields.Many2many( 'product.product', 'product_accessory_rel', 'src_id', 'dest_id', string='Accessory Products', help='Accessories show up when the customer' 'reviews the cart before payment (cross-sell strategy).') website_size_x = fields.Integer('Size X', default=1) website_size_y = fields.Integer('Size Y', default=1) website_style_ids = fields.Many2many('product.style', string='Styles') website_sequence = fields.Integer( 'Website Sequence', help="Determine the display order in the Website E-commerce", default=lambda self: self._default_website_sequence(), copy=False) public_categ_ids = fields.Many2many( 'product.public.category', string='Website Product Category', help= "The product will be available in each mentioned e-commerce category. Go to" "Shop > Customize and enable 'E-commerce categories' to view all e-commerce categories." ) product_image_ids = fields.One2many('product.image', 'product_tmpl_id', string='Images') # website_price deprecated, directly use _get_combination_info instead website_price = fields.Float('Website price', compute='_website_price', digits=dp.get_precision('Product Price')) # website_public_price deprecated, directly use _get_combination_info instead website_public_price = fields.Float( 'Website public price', compute='_website_price', digits=dp.get_precision('Product Price')) # website_price_difference deprecated, directly use _get_combination_info instead website_price_difference = fields.Boolean('Website price difference', compute='_website_price') def _website_price(self): current_website = self.env['website'].get_current_website() for template in self.with_context(website_id=current_website.id): res = template._get_combination_info() template.website_price = res.get('price') template.website_public_price = res.get('list_price') template.website_price_difference = res.get('has_discounted_price') @api.multi def _has_no_variant_attributes(self): """Return whether this `product.template` has at least one no_variant attribute. :return: True if at least one no_variant attribute, False otherwise :rtype: bool """ self.ensure_one() return any(a.create_variant == 'no_variant' for a in self._get_valid_product_attributes()) @api.multi def _has_is_custom_values(self): self.ensure_one() """Return whether this `product.template` has at least one is_custom attribute value. :return: True if at least one is_custom attribute value, False otherwise :rtype: bool """ return any(v.is_custom for v in self._get_valid_product_attribute_values()) @api.multi def _is_quick_add_to_cart_possible(self, parent_combination=None): """ It's possible to quickly add to cart if there's no optional product, there's only one possible combination and no value is set to is_custom. Attributes set to dynamic or no_variant don't have to be tested specifically because they will be taken into account when checking for the possible combinations. :param parent_combination: combination from which `self` is an optional or accessory product :type parent_combination: recordset `product.template.attribute.value` :return: True if it's possible to quickly add to cart, else False :rtype: bool """ self.ensure_one() if not self._is_add_to_cart_possible(parent_combination): return False gen = self._get_possible_combinations(parent_combination) first_possible_combination = next(gen) if next(gen, False) is not False: # there are at least 2 possible combinations. return False if self._has_is_custom_values(): return False if self.optional_product_ids.filtered( lambda p: p._is_add_to_cart_possible(first_possible_combination )): return False return True @api.multi def _get_possible_variants_sorted(self, parent_combination=None): """Return the sorted recordset of variants that are possible. The order is based on the order of the attributes and their values. See `_get_possible_variants` for the limitations of this method with dynamic or no_variant attributes, and also for a warning about performances. :param parent_combination: combination from which `self` is an optional or accessory product :type parent_combination: recordset `product.template.attribute.value` :return: the sorted variants that are possible :rtype: recordset of `product.product` """ self.ensure_one() def _sort_key_attribute_value(value): # if you change this order, keep it in sync with _order from `product.attribute` return (value.attribute_id.sequence, value.attribute_id.id) def _sort_key_variant(variant): """ We assume all variants will have the same attributes, with only one value for each. - first level sort: same as "product.attribute"._order - second level sort: same as "product.attribute.value"._order """ keys = [] for attribute in variant.attribute_value_ids.sorted( _sort_key_attribute_value): # if you change this order, keep it in sync with _order from `product.attribute.value` keys.append(attribute.sequence) keys.append(attribute.id) return keys return self._get_possible_variants(parent_combination).sorted( _sort_key_variant) @api.multi def _get_combination_info(self, combination=False, product_id=False, add_qty=1, pricelist=False, parent_combination=False, only_template=False): """Override for website, where we want to: - take the website pricelist if no pricelist is set - apply the b2b/b2c setting to the result This will work when adding website_id to the context, which is done automatically when called from routes with website=True. """ self.ensure_one() current_website = False if self.env.context.get('website_id'): current_website = self.env['website'].get_current_website() if not pricelist: pricelist = current_website.get_current_pricelist() combination_info = super(ProductTemplate, self)._get_combination_info( combination=combination, product_id=product_id, add_qty=add_qty, pricelist=pricelist, parent_combination=parent_combination, only_template=only_template) if self.env.context.get('website_id'): partner = self.env.user.partner_id company_id = current_website.company_id product = self.env['product.product'].browse( combination_info['product_id']) or self tax_display = self.env.user.has_group( 'account.group_show_line_subtotals_tax_excluded' ) and 'total_excluded' or 'total_included' Fpos_sudo = self.env['account.fiscal.position'].sudo() fpos_id = Fpos_sudo.with_context( force_company=company_id.id).get_fiscal_position(partner.id) taxes = Fpos_sudo.browse(fpos_id).map_tax( product.sudo().taxes_id.filtered( lambda x: x.company_id == company_id), product, partner) # The list_price is always the price of one. quantity_1 = 1 combination_info['price'] = self.env[ 'account.tax']._fix_tax_included_price_company( combination_info['price'], product.sudo().taxes_id, taxes, company_id) price = taxes.compute_all(combination_info['price'], pricelist.currency_id, quantity_1, product, partner)[tax_display] if pricelist.discount_policy == 'without_discount': combination_info['list_price'] = self.env[ 'account.tax']._fix_tax_included_price_company( combination_info['list_price'], product.sudo().taxes_id, taxes, company_id) list_price = taxes.compute_all(combination_info['list_price'], pricelist.currency_id, quantity_1, product, partner)[tax_display] else: list_price = price has_discounted_price = pricelist.currency_id.compare_amounts( list_price, price) == 1 combination_info.update( price=price, list_price=list_price, has_discounted_price=has_discounted_price, ) return combination_info @api.multi def _create_first_product_variant(self, log_warning=False): """Create if necessary and possible and return the first product variant for this template. :param log_warning: whether a warning should be logged on fail :type log_warning: bool :return: the first product variant or none :rtype: recordset of `product.product` """ return self._create_product_variant( self._get_first_possible_combination(), log_warning) @api.multi def _get_current_company_fallback(self, **kwargs): """Override: if a website is set on the product or given, fallback to the company of the website. Otherwise use the one from parent method.""" res = super(ProductTemplate, self)._get_current_company_fallback(**kwargs) website = self.website_id or kwargs.get('website') return website and website.company_id or res def _default_website_sequence(self): self._cr.execute("SELECT MIN(website_sequence) FROM %s" % self._table) min_sequence = self._cr.fetchone()[0] return min_sequence and min_sequence - 1 or 10 def set_sequence_top(self): self.website_sequence = self.sudo().search( [], order='website_sequence desc', limit=1).website_sequence + 1 def set_sequence_bottom(self): self.website_sequence = self.sudo().search( [], order='website_sequence', limit=1).website_sequence - 1 def set_sequence_up(self): previous_product_tmpl = self.sudo().search( [('website_sequence', '>', self.website_sequence), ('website_published', '=', self.website_published)], order='website_sequence', limit=1) if previous_product_tmpl: previous_product_tmpl.website_sequence, self.website_sequence = self.website_sequence, previous_product_tmpl.website_sequence else: self.set_sequence_top() def set_sequence_down(self): next_prodcut_tmpl = self.search( [('website_sequence', '<', self.website_sequence), ('website_published', '=', self.website_published)], order='website_sequence desc', limit=1) if next_prodcut_tmpl: next_prodcut_tmpl.website_sequence, self.website_sequence = self.website_sequence, next_prodcut_tmpl.website_sequence else: return self.set_sequence_bottom() def _default_website_meta(self): res = super(ProductTemplate, self)._default_website_meta() res['default_opengraph']['og:description'] = res['default_twitter'][ 'twitter:description'] = self.description_sale res['default_opengraph']['og:title'] = res['default_twitter'][ 'twitter:title'] = self.name res['default_opengraph']['og:image'] = res['default_twitter'][ 'twitter:image'] = "/web/image/product.template/%s/image" % ( self.id) return res @api.multi def _compute_website_url(self): super(ProductTemplate, self)._compute_website_url() for product in self: product.website_url = "/shop/product/%s" % (product.id, )
class Track(models.Model): _name = "event.track" _description = 'Event Track' _order = 'priority, date' _inherit = [ 'mail.thread', 'mail.activity.mixin', 'website.seo.metadata', 'website.published.mixin' ] @api.model def _get_default_stage_id(self): return self.env['event.track.stage'].search([], limit=1).id name = fields.Char('Title', required=True, translate=True) active = fields.Boolean(default=True) user_id = fields.Many2one('res.users', 'Responsible', track_visibility='onchange', default=lambda self: self.env.user) partner_id = fields.Many2one('res.partner', 'Speaker') partner_name = fields.Char('Speaker Name') partner_email = fields.Char('Speaker Email') partner_phone = fields.Char('Speaker Phone') partner_biography = fields.Html('Speaker Biography') tag_ids = fields.Many2many('event.track.tag', string='Tags') stage_id = fields.Many2one('event.track.stage', string='Stage', ondelete='restrict', index=True, copy=False, default=_get_default_stage_id, group_expand='_read_group_stage_ids', required=True, track_visibility='onchange') kanban_state = fields.Selection( [('normal', 'Grey'), ('done', 'Green'), ('blocked', 'Red')], string='Kanban State', copy=False, default='normal', required=True, track_visibility='onchange', help= "A track's kanban state indicates special situations affecting it:\n" " * Grey is the default situation\n" " * Red indicates something is preventing the progress of this track\n" " * Green indicates the track is ready to be pulled to the next stage") description = fields.Html('Track Description', translate=html_translate, sanitize_attributes=False) date = fields.Datetime('Track Date') duration = fields.Float('Duration', default=1.5) location_id = fields.Many2one('event.track.location', 'Room') event_id = fields.Many2one('event.event', 'Event', required=True) color = fields.Integer('Color Index') priority = fields.Selection([('0', 'Low'), ('1', 'Medium'), ('2', 'High'), ('3', 'Highest')], 'Priority', required=True, default='1') image = fields.Binary('Image', related='partner_id.image_medium', store=True, attachment=True, readonly=False) @api.multi @api.depends('name') def _compute_website_url(self): super(Track, self)._compute_website_url() for track in self: if not isinstance(track.id, models.NewId): track.website_url = '/event/%s/track/%s' % (slug( track.event_id), slug(track)) @api.onchange('partner_id') def _onchange_partner_id(self): if self.partner_id: self.partner_name = self.partner_id.name self.partner_email = self.partner_id.email self.partner_phone = self.partner_id.phone self.partner_biography = self.partner_id.website_description @api.model def create(self, vals): track = super(Track, self).create(vals) track.event_id.message_post_with_view( 'website_event_track.event_track_template_new', values={'track': track}, subject=track.name, subtype_id=self.env.ref('website_event_track.mt_event_track').id, ) return track @api.multi def write(self, vals): if 'stage_id' in vals and 'kanban_state' not in vals: vals['kanban_state'] = 'normal' res = super(Track, self).write(vals) if vals.get('partner_id'): self.message_subscribe([vals['partner_id']]) return res @api.model def _read_group_stage_ids(self, stages, domain, order): """ Always display all stages """ return stages.search([], order=order) @api.multi def _track_template(self, tracking): res = super(Track, self)._track_template(tracking) track = self[0] changes, tracking_value_ids = tracking[track.id] if 'stage_id' in changes and track.stage_id.mail_template_id: res['stage_id'] = (track.stage_id.mail_template_id, { 'composition_mode': 'comment', 'auto_delete_message': True, 'subtype_id': self.env['ir.model.data'].xmlid_to_res_id('mail.mt_note'), 'notif_layout': 'mail.mail_notification_light' }) return res @api.multi def _track_subtype(self, init_values): self.ensure_one() if 'kanban_state' in init_values and self.kanban_state == 'blocked': return 'website_event_track.mt_track_blocked' elif 'kanban_state' in init_values and self.kanban_state == 'done': return 'website_event_track.mt_track_ready' return super(Track, self)._track_subtype(init_values) @api.multi def message_get_suggested_recipients(self): recipients = super(Track, self).message_get_suggested_recipients() for track in self: if track.partner_email and track.partner_email != track.partner_id.email: track._message_add_suggested_recipient( recipients, email=track.partner_email, reason=_('Speaker Email')) return recipients def _message_post_after_hook(self, message, *args, **kwargs): if self.partner_email and not self.partner_id: # we consider that posting a message with a specified recipient (not a follower, a specific one) # on a document without customer means that it was created through the chatter using # suggested recipients. This heuristic allows to avoid ugly hacks in JS. new_partner = message.partner_ids.filtered( lambda partner: partner.email == self.partner_email) if new_partner: self.search([ ('partner_id', '=', False), ('partner_email', '=', new_partner.email), ('stage_id.is_cancel', '=', False), ]).write({'partner_id': new_partner.id}) return super(Track, self)._message_post_after_hook(message, *args, **kwargs) @api.multi def open_track_speakers_list(self): return { 'name': _('Speakers'), 'domain': [('id', 'in', self.mapped('partner_id').ids)], 'view_type': 'form', 'view_mode': 'kanban,form', 'res_model': 'res.partner', 'view_id': False, 'type': 'ir.actions.act_window', }
class Invite(models.TransientModel): """ Wizard to invite partners (or channels) and make them followers. """ _name = 'mail.wizard.invite' _description = 'Invite wizard' @api.model def default_get(self, fields): result = super(Invite, self).default_get(fields) if self._context.get('mail_invite_follower_channel_only'): result['send_mail'] = False if 'message' not in fields: return result user_name = self.env.user.name_get()[0][1] model = result.get('res_model') res_id = result.get('res_id') if model and res_id: document = self.env['ir.model']._get(model).display_name title = self.env[model].browse(res_id).display_name msg_fmt = _( '%(user_name)s invited you to follow %(document)s document: %(title)s' ) else: msg_fmt = _('%(user_name)s invited you to follow a new document.') text = msg_fmt % locals() message = html.DIV(html.P(_('Hello,')), html.P(text)) result['message'] = etree.tostring(message) return result res_model = fields.Char('Related Document Model', required=True, index=True, help='Model of the followed resource') res_id = fields.Integer('Related Document ID', index=True, help='Id of the followed resource') partner_ids = fields.Many2many( 'res.partner', string='Recipients', help= "List of partners that will be added as follower of the current document." ) channel_ids = fields.Many2many( 'mail.channel', string='Channels', help= 'List of channels that will be added as listeners of the current document.', domain=[('channel_type', '=', 'channel')]) message = fields.Html('Message') send_mail = fields.Boolean( 'Send Email', default=True, help= "If checked, the partners will receive an email warning they have been added in the document's followers." ) @api.multi def add_followers(self): email_from = self.env['mail.message']._get_default_from() for wizard in self: Model = self.env[wizard.res_model] document = Model.browse(wizard.res_id) # filter partner_ids to get the new followers, to avoid sending email to already following partners new_partners = wizard.partner_ids - document.message_partner_ids new_channels = wizard.channel_ids - document.message_channel_ids document.message_subscribe(new_partners.ids, new_channels.ids) model_name = self.env['ir.model']._get( wizard.res_model).display_name # send an email if option checked and if a message exists (do not send void emails) if wizard.send_mail and wizard.message and not wizard.message == '<br>': # when deleting the message, cleditor keeps a <br> message = self.env['mail.message'].create({ 'subject': _('Invitation to follow %s: %s') % (model_name, document.name_get()[0][1]), 'body': wizard.message, 'record_name': document.name_get()[0][1], 'email_from': email_from, 'reply_to': email_from, 'model': wizard.res_model, 'res_id': wizard.res_id, 'no_auto_thread': True, 'add_sign': True, }) partners_data = [] recipient_data = self.env[ 'mail.followers']._get_recipient_data( document, False, pids=new_partners.ids) for pid, cid, active, pshare, ctype, notif, groups in recipient_data: pdata = { 'id': pid, 'share': pshare, 'active': active, 'notif': 'email', 'groups': groups or [] } if not pshare and notif: # has an user and is not shared, is therefore user partners_data.append(dict(pdata, type='user')) elif pshare and notif: # has an user and is shared, is therefore portal partners_data.append(dict(pdata, type='portal')) else: # has no user, is therefore customer partners_data.append(dict(pdata, type='customer')) self.env['res.partner'].with_context(auto_delete=True)._notify( message, partners_data, document, force_send=True, send_after_commit=False) message.unlink() return {'type': 'ir.actions.act_window_close'}
class EventEvent(models.Model): """Event""" _name = 'event.event' _description = 'Event' _inherit = ['mail.thread'] _order = 'date_begin' name = fields.Char(string='Event', translate=True, required=True, readonly=False, states={'done': [('readonly', True)]}) active = fields.Boolean(default=True) user_id = fields.Many2one('res.users', string='Responsible', default=lambda self: self.env.user, track_visibility="onchange", readonly=False, states={'done': [('readonly', True)]}) company_id = fields.Many2one('res.company', string='Company', change_default=True, default=lambda self: self.env['res.company']. _company_default_get('event.event'), required=False, readonly=False, states={'done': [('readonly', True)]}) organizer_id = fields.Many2one( 'res.partner', string='Organizer', track_visibility="onchange", default=lambda self: self.env.user.company_id.partner_id) event_type_id = fields.Many2one('event.type', string='Category', readonly=False, states={'done': [('readonly', True)]}, oldname='type') color = fields.Integer('Kanban Color Index') event_mail_ids = fields.One2many('event.mail', 'event_id', string='Mail Schedule', copy=True) # Seats and computation seats_max = fields.Integer( string='Maximum Attendees Number', oldname='register_max', readonly=True, states={ 'draft': [('readonly', False)], 'confirm': [('readonly', False)] }, help= "For each event you can define a maximum registration of seats(number of attendees), above this numbers the registrations are not accepted." ) seats_availability = fields.Selection([('limited', 'Limited'), ('unlimited', 'Unlimited')], 'Maximum Attendees', required=True, default='unlimited') seats_min = fields.Integer( string='Minimum Attendees', oldname='register_min', help= "For each event you can define a minimum reserved seats (number of attendees), if it does not reach the mentioned registrations the event can not be confirmed (keep 0 to ignore this rule)" ) seats_reserved = fields.Integer(oldname='register_current', string='Reserved Seats', store=True, readonly=True, compute='_compute_seats') seats_available = fields.Integer(oldname='register_avail', string='Available Seats', store=True, readonly=True, compute='_compute_seats') seats_unconfirmed = fields.Integer(oldname='register_prospect', string='Unconfirmed Seat Reservations', store=True, readonly=True, compute='_compute_seats') seats_used = fields.Integer(oldname='register_attended', string='Number of Participants', store=True, readonly=True, compute='_compute_seats') seats_expected = fields.Integer(string='Number of Expected Attendees', readonly=True, compute='_compute_seats_expected') # Registration fields registration_ids = fields.One2many('event.registration', 'event_id', string='Attendees', readonly=False, states={'done': [('readonly', True)]}) # Date fields date_tz = fields.Selection('_tz_get', string='Timezone', required=True, default=lambda self: self.env.user.tz or 'UTC') date_begin = fields.Datetime(string='Start Date', required=True, track_visibility='onchange', states={'done': [('readonly', True)]}) date_end = fields.Datetime(string='End Date', required=True, track_visibility='onchange', states={'done': [('readonly', True)]}) date_begin_located = fields.Char(string='Start Date Located', compute='_compute_date_begin_tz') date_end_located = fields.Char(string='End Date Located', compute='_compute_date_end_tz') state = fields.Selection( [('draft', 'Unconfirmed'), ('cancel', 'Cancelled'), ('confirm', 'Confirmed'), ('done', 'Done')], string='Status', default='draft', readonly=True, required=True, copy=False, help= "If event is created, the status is 'Draft'. If event is confirmed for the particular dates the status is set to 'Confirmed'. If the event is over, the status is set to 'Done'. If event is cancelled the status is set to 'Cancelled'." ) auto_confirm = fields.Boolean(string='Autoconfirm Registrations') is_online = fields.Boolean('Online Event') address_id = fields.Many2one( 'res.partner', string='Location', default=lambda self: self.env.user.company_id.partner_id, readonly=False, states={'done': [('readonly', True)]}, track_visibility="onchange") country_id = fields.Many2one('res.country', 'Country', related='address_id.country_id', store=True, readonly=False) twitter_hashtag = fields.Char('Twitter Hashtag') description = fields.Html(string='Description', oldname='note', translate=html_translate, sanitize_attributes=False, readonly=False, states={'done': [('readonly', True)]}) # badge fields badge_front = fields.Html(string='Badge Front') badge_back = fields.Html(string='Badge Back') badge_innerleft = fields.Html(string='Badge Inner Left') badge_innerright = fields.Html(string='Badge Inner Right') event_logo = fields.Html(string='Event Logo') @api.multi @api.depends('seats_max', 'registration_ids.state') def _compute_seats(self): """ Determine reserved, available, reserved but unconfirmed and used seats. """ # initialize fields to 0 for event in self: event.seats_unconfirmed = event.seats_reserved = event.seats_used = event.seats_available = 0 # aggregate registrations by event and by state if self.ids: state_field = { 'draft': 'seats_unconfirmed', 'open': 'seats_reserved', 'done': 'seats_used', } query = """ SELECT event_id, state, count(event_id) FROM event_registration WHERE event_id IN %s AND state IN ('draft', 'open', 'done') GROUP BY event_id, state """ self._cr.execute(query, (tuple(self.ids), )) for event_id, state, num in self._cr.fetchall(): event = self.browse(event_id) event[state_field[state]] += num # compute seats_available for event in self: if event.seats_max > 0: event.seats_available = event.seats_max - ( event.seats_reserved + event.seats_used) @api.multi @api.depends('seats_unconfirmed', 'seats_reserved', 'seats_used') def _compute_seats_expected(self): for event in self: event.seats_expected = event.seats_unconfirmed + event.seats_reserved + event.seats_used @api.model def _tz_get(self): return [(x, x) for x in pytz.all_timezones] @api.one @api.depends('date_tz', 'date_begin') def _compute_date_begin_tz(self): if self.date_begin: self.date_begin_located = format_tz( self.with_context(use_babel=True).env, self.date_begin, tz=self.date_tz) else: self.date_begin_located = False @api.one @api.depends('date_tz', 'date_end') def _compute_date_end_tz(self): if self.date_end: self.date_end_located = format_tz( self.with_context(use_babel=True).env, self.date_end, tz=self.date_tz) else: self.date_end_located = False @api.onchange('is_online') def _onchange_is_online(self): if self.is_online: self.address_id = False @api.onchange('event_type_id') def _onchange_type(self): if self.event_type_id: self.seats_min = self.event_type_id.default_registration_min self.seats_max = self.event_type_id.default_registration_max if self.event_type_id.default_registration_max: self.seats_availability = 'limited' if self.event_type_id.auto_confirm: self.auto_confirm = self.event_type_id.auto_confirm if self.event_type_id.use_hashtag: self.twitter_hashtag = self.event_type_id.default_hashtag if self.event_type_id.use_timezone: self.date_tz = self.event_type_id.default_timezone self.is_online = self.event_type_id.is_online if self.event_type_id.event_type_mail_ids: self.event_mail_ids = [ (5, 0, 0) ] + [{ 'template_id': line.template_id, 'interval_nbr': line.interval_nbr, 'interval_unit': line.interval_unit, 'interval_type': line.interval_type } for line in self.event_type_id.event_type_mail_ids] @api.constrains('seats_min', 'seats_max', 'seats_availability') def _check_seats_min_max(self): if any(event.seats_availability == 'limited' and event.seats_min > event.seats_max for event in self): raise ValidationError( _('Maximum attendees number should be greater than minimum attendees number.' )) @api.constrains('seats_max', 'seats_available') def _check_seats_limit(self): if any(event.seats_availability == 'limited' and event.seats_max and event.seats_available < 0 for event in self): raise ValidationError(_('No more available seats.')) @api.one @api.constrains('date_begin', 'date_end') def _check_closing_date(self): if self.date_end < self.date_begin: raise ValidationError( _('The closing date cannot be earlier than the beginning date.' )) @api.multi @api.depends('name', 'date_begin', 'date_end') def name_get(self): result = [] for event in self: date_begin = fields.Datetime.from_string(event.date_begin) date_end = fields.Datetime.from_string(event.date_end) dates = [ fields.Date.to_string( fields.Datetime.context_timestamp(event, dt)) for dt in [date_begin, date_end] if dt ] dates = sorted(set(dates)) result.append( (event.id, '%s (%s)' % (event.name, ' - '.join(dates)))) return result @api.model def create(self, vals): res = super(EventEvent, self).create(vals) if res.organizer_id: res.message_subscribe([res.organizer_id.id]) if res.auto_confirm: res.button_confirm() return res @api.multi def write(self, vals): res = super(EventEvent, self).write(vals) if vals.get('organizer_id'): self.message_subscribe([vals['organizer_id']]) return res @api.multi @api.returns('self', lambda value: value.id) def copy(self, default=None): self.ensure_one() default = dict(default or {}, name=_("%s (copy)") % (self.name)) return super(EventEvent, self).copy(default) @api.one def button_draft(self): self.state = 'draft' @api.multi def button_cancel(self): if any('done' in event.mapped('registration_ids.state') for event in self): raise UserError( _("There are already attendees who attended this event. Please reset it to draft if you want to cancel this event." )) self.registration_ids.write({'state': 'cancel'}) self.state = 'cancel' @api.one def button_done(self): self.state = 'done' @api.one def button_confirm(self): self.state = 'confirm' @api.one def mail_attendees(self, template_id, force_send=False, filter_func=lambda self: self.state != 'cancel'): for attendee in self.registration_ids.filtered(filter_func): self.env['mail.template'].browse(template_id).send_mail( attendee.id, force_send=force_send) @api.multi def _is_event_registrable(self): return True
class MailActivity(models.Model): """ An actual activity to perform. Activities are linked to documents using res_id and res_model_id fields. Activities have a deadline that can be used in kanban view to display a status. Once done activities are unlinked and a message is posted. This message has a new activity_type_id field that indicates the activity linked to the message. """ _name = 'mail.activity' _description = 'Activity' _order = 'date_deadline ASC' _rec_name = 'summary' @api.model def default_get(self, fields): res = super(MailActivity, self).default_get(fields) if not fields or 'res_model_id' in fields and res.get('res_model'): res['res_model_id'] = self.env['ir.model']._get( res['res_model']).id return res # owner res_id = fields.Integer('Related Document ID', index=True, required=True) res_model_id = fields.Many2one('ir.model', 'Document Model', index=True, ondelete='cascade', required=True) res_model = fields.Char('Related Document Model', index=True, related='res_model_id.model', store=True, readonly=True) res_name = fields.Char('Document Name', compute='_compute_res_name', store=True, help="Display name of the related document.", readonly=True) # activity activity_type_id = fields.Many2one( 'mail.activity.type', 'Activity', domain= "['|', ('res_model_id', '=', False), ('res_model_id', '=', res_model_id)]", ondelete='restrict') activity_category = fields.Selection(related='activity_type_id.category', readonly=True) activity_decoration = fields.Selection( related='activity_type_id.decoration_type', readonly=True) icon = fields.Char('Icon', related='activity_type_id.icon', readonly=True) summary = fields.Char('Summary') note = fields.Html('Note', sanitize_style=True) feedback = fields.Html('Feedback') date_deadline = fields.Date('Due Date', index=True, required=True, default=fields.Date.context_today) automated = fields.Boolean( 'Automated activity', readonly=True, help= 'Indicates this activity has been created automatically and not by any user.' ) # description user_id = fields.Many2one('res.users', 'Assigned to', default=lambda self: self.env.user, index=True, required=True) create_user_id = fields.Many2one('res.users', 'Creator', default=lambda self: self.env.user, index=True) state = fields.Selection([('overdue', 'Overdue'), ('today', 'Today'), ('planned', 'Planned')], 'State', compute='_compute_state') recommended_activity_type_id = fields.Many2one( 'mail.activity.type', string="Recommended Activity Type") previous_activity_type_id = fields.Many2one( 'mail.activity.type', string='Previous Activity Type', readonly=True) has_recommended_activities = fields.Boolean( 'Next activities available', compute='_compute_has_recommended_activities', help='Technical field for UX purpose') mail_template_ids = fields.Many2many( related='activity_type_id.mail_template_ids', readonly=False) force_next = fields.Boolean(related='activity_type_id.force_next', readonly=False) @api.multi @api.onchange('previous_activity_type_id') def _compute_has_recommended_activities(self): for record in self: record.has_recommended_activities = bool( record.previous_activity_type_id.next_type_ids) @api.multi @api.onchange('previous_activity_type_id') def _onchange_previous_activity_type_id(self): for record in self: if record.previous_activity_type_id.default_next_type_id: record.activity_type_id = record.previous_activity_type_id.default_next_type_id @api.depends('res_model', 'res_id') def _compute_res_name(self): for activity in self: activity.res_name = self.env[activity.res_model].browse( activity.res_id).name_get()[0][1] @api.depends('date_deadline') def _compute_state(self): for record in self.filtered(lambda activity: activity.date_deadline): tz = record.user_id.sudo().tz date_deadline = record.date_deadline record.state = self._compute_state_from_date(date_deadline, tz) @api.model def _compute_state_from_date(self, date_deadline, tz=False): date_deadline = fields.Date.from_string(date_deadline) today_default = date.today() today = today_default if tz: today_utc = pytz.UTC.localize(datetime.utcnow()) today_tz = today_utc.astimezone(pytz.timezone(tz)) today = date(year=today_tz.year, month=today_tz.month, day=today_tz.day) diff = (date_deadline - today) if diff.days == 0: return 'today' elif diff.days < 0: return 'overdue' else: return 'planned' @api.onchange('activity_type_id') def _onchange_activity_type_id(self): if self.activity_type_id: self.summary = self.activity_type_id.summary # Date.context_today is correct because date_deadline is a Date and is meant to be # expressed in user TZ base = fields.Date.context_today(self) if self.activity_type_id.delay_from == 'previous_activity' and 'activity_previous_deadline' in self.env.context: base = fields.Date.from_string( self.env.context.get('activity_previous_deadline')) self.date_deadline = base + relativedelta( **{ self.activity_type_id.delay_unit: self.activity_type_id.delay_count }) @api.onchange('recommended_activity_type_id') def _onchange_recommended_activity_type_id(self): if self.recommended_activity_type_id: self.activity_type_id = self.recommended_activity_type_id @api.multi def _check_access(self, operation): """ Rule to access activities * create: check write rights on related document; * write: rule OR write rights on document; * unlink: rule OR write rights on document; """ self.check_access_rights( operation, raise_exception=True) # will raise an AccessError if operation in ('write', 'unlink'): try: self.check_access_rule(operation) except exceptions.AccessError: pass else: return doc_operation = 'read' if operation == 'read' else 'write' activity_to_documents = dict() for activity in self.sudo(): activity_to_documents.setdefault(activity.res_model, list()).append(activity.res_id) for model, res_ids in activity_to_documents.items(): self.env[model].check_access_rights(doc_operation, raise_exception=True) try: self.env[model].browse(res_ids).check_access_rule( doc_operation) except exceptions.AccessError: raise exceptions.AccessError( _('The requested operation cannot be completed due to security restrictions. Please contact your system administrator.\n\n(Document type: %s, Operation: %s)' ) % (self._description, operation) + ' - ({} {}, {} {})'.format(_('Records:'), res_ids[:6], _('User:'******'read') except exceptions.AccessError: raise exceptions.UserError( _('Assigned user %s has no access to the document and is not able to handle this activity.' ) % activity.user_id.display_name) else: try: target_user = activity.user_id target_record = self.env[activity.res_model].browse( activity.res_id) if hasattr(target_record, 'company_id') and ( target_record.company_id != target_user.company_id and (len(target_user.sudo().company_ids) > 1)): return # in that case we skip the check, assuming it would fail because of the company model.browse(activity.res_id).check_access_rule('read') except exceptions.AccessError: raise exceptions.UserError( _('Assigned user %s has no access to the document and is not able to handle this activity.' ) % activity.user_id.display_name) @api.model def create(self, values): # already compute default values to be sure those are computed using the current user values_w_defaults = self.default_get(self._fields.keys()) values_w_defaults.update(values) # continue as sudo because activities are somewhat protected activity = super(MailActivity, self.sudo()).create(values_w_defaults) activity_user = activity.sudo(self.env.user) activity_user._check_access('create') need_sudo = False try: # in multicompany, reading the partner might break partner_id = activity_user.user_id.partner_id.id except exceptions.AccessError: need_sudo = True partner_id = activity_user.user_id.sudo().partner_id.id # send a notification to assigned user; in case of manually done activity also check # target has rights on document otherwise we prevent its creation. Automated activities # are checked since they are integrated into business flows that should not crash. if activity_user.user_id != self.env.user: if not activity_user.automated: activity_user._check_access_assignation() if not self.env.context.get('mail_activity_quick_update', False): if need_sudo: activity_user.sudo().action_notify() else: activity_user.action_notify() self.env[activity_user.res_model].browse( activity_user.res_id).message_subscribe(partner_ids=[partner_id]) if activity.date_deadline <= fields.Date.today(): self.env['bus.bus'].sendone((self._cr.dbname, 'res.partner', activity.user_id.partner_id.id), { 'type': 'activity_updated', 'activity_created': True }) return activity_user @api.multi def write(self, values): self._check_access('write') if values.get('user_id'): pre_responsibles = self.mapped('user_id.partner_id') res = super(MailActivity, self.sudo()).write(values) if values.get('user_id'): if values['user_id'] != self.env.uid: to_check = self.filtered(lambda act: not act.automated) to_check._check_access_assignation() if not self.env.context.get('mail_activity_quick_update', False): self.action_notify() for activity in self: self.env[activity.res_model].browse( activity.res_id).message_subscribe( partner_ids=[activity.user_id.partner_id.id]) if activity.date_deadline <= fields.Date.today(): self.env['bus.bus'].sendone( (self._cr.dbname, 'res.partner', activity.user_id.partner_id.id), { 'type': 'activity_updated', 'activity_created': True }) for activity in self: if activity.date_deadline <= fields.Date.today(): for partner in pre_responsibles: self.env['bus.bus'].sendone( (self._cr.dbname, 'res.partner', partner.id), { 'type': 'activity_updated', 'activity_deleted': True }) return res @api.multi def unlink(self): self._check_access('unlink') for activity in self: if activity.date_deadline <= fields.Date.today(): self.env['bus.bus'].sendone( (self._cr.dbname, 'res.partner', activity.user_id.sudo().partner_id.id), { 'type': 'activity_updated', 'activity_deleted': True }) return super(MailActivity, self.sudo()).unlink() @api.multi def name_get(self): res = [] for record in self: name = record.summary or record.activity_type_id.display_name res.append((record.id, name)) return res @api.multi def action_notify(self): body_template = self.env.ref('mail.message_activity_assigned') for activity in self: model_description = self.env['ir.model']._get( activity.res_model).display_name body = body_template.render(dict( activity=activity, model_description=model_description), engine='ir.qweb', minimal_qcontext=True) self.env['mail.thread'].message_notify( partner_ids=activity.user_id.partner_id.ids, body=body, subject=_('%s: %s assigned to you') % (activity.res_name, activity.summary or activity.activity_type_id.name), record_name=activity.res_name, model_description=model_description, notif_layout='mail.mail_notification_light') @api.multi def action_done(self): """ Wrapper without feedback because web button add context as parameter, therefore setting context to feedback """ return self.action_feedback() def action_feedback(self, feedback=False): message = self.env['mail.message'] if feedback: self.write(dict(feedback=feedback)) # Search for all attachments linked to the activities we are about to unlink. This way, we # can link them to the message posted and prevent their deletion. attachments = self.env['ir.attachment'].search_read([ ('res_model', '=', self._name), ('res_id', 'in', self.ids), ], ['id', 'res_id']) activity_attachments = defaultdict(list) for attachment in attachments: activity_id = attachment['res_id'] activity_attachments[activity_id].append(attachment['id']) for activity in self: record = self.env[activity.res_model].browse(activity.res_id) record.message_post_with_view( 'mail.message_activity_done', values={'activity': activity}, subtype_id=self.env['ir.model.data'].xmlid_to_res_id( 'mail.mt_activities'), mail_activity_type_id=activity.activity_type_id.id, ) # Moving the attachments in the message # TODO: Fix void res_id on attachment when you create an activity with an image # directly, see route /web_editor/attachment/add activity_message = record.message_ids[0] message_attachments = self.env['ir.attachment'].browse( activity_attachments[activity.id]) if message_attachments: message_attachments.write({ 'res_id': activity_message.id, 'res_model': activity_message._name, }) activity_message.attachment_ids = message_attachments message |= activity_message self.unlink() return message.ids and message.ids[0] or False def action_done_schedule_next(self): """ Wrapper without feedback because web button add context as parameter, therefore setting context to feedback """ return self.action_feedback_schedule_next() @api.multi def action_feedback_schedule_next(self, feedback=False): ctx = dict( clean_context(self.env.context), default_previous_activity_type_id=self.activity_type_id.id, activity_previous_deadline=self.date_deadline, default_res_id=self.res_id, default_res_model=self.res_model, ) force_next = self.force_next self.action_feedback( feedback) # will unlink activity, dont access self after that if force_next: Activity = self.env['mail.activity'].with_context(ctx) res = Activity.new(Activity.default_get(Activity.fields_get())) res._onchange_previous_activity_type_id() res._onchange_activity_type_id() Activity.create(res._convert_to_write(res._cache)) return False else: return { 'name': _('Schedule an Activity'), 'context': ctx, 'view_type': 'form', 'view_mode': 'form', 'res_model': 'mail.activity', 'views': [(False, 'form')], 'type': 'ir.actions.act_window', 'target': 'new', } @api.multi def action_close_dialog(self): return {'type': 'ir.actions.act_window_close'} @api.multi def activity_format(self): activities = self.read() mail_template_ids = set([ template_id for activity in activities for template_id in activity["mail_template_ids"] ]) mail_template_info = self.env["mail.template"].browse( mail_template_ids).read(['id', 'name']) mail_template_dict = dict([(mail_template['id'], mail_template) for mail_template in mail_template_info]) for activity in activities: activity['mail_template_ids'] = [ mail_template_dict[mail_template_id] for mail_template_id in activity['mail_template_ids'] ] return activities @api.model def get_activity_data(self, res_model, domain): activity_domain = [('res_model', '=', res_model)] if domain: res = self.env[res_model].search(domain) activity_domain.append(('res_id', 'in', res.ids)) grouped_activities = self.env['mail.activity'].read_group( activity_domain, [ 'res_id', 'activity_type_id', 'ids:array_agg(id)', 'date_deadline:min(date_deadline)' ], ['res_id', 'activity_type_id'], lazy=False) # filter out unreadable records if not domain: res_ids = tuple(a['res_id'] for a in grouped_activities) res = self.env[res_model].search([('id', 'in', res_ids)]) grouped_activities = [ a for a in grouped_activities if a['res_id'] in res.ids ] activity_type_ids = self.env['mail.activity.type'] res_id_to_deadline = {} activity_data = defaultdict(dict) for group in grouped_activities: res_id = group['res_id'] activity_type_id = (group.get('activity_type_id') or (False, False))[0] activity_type_ids |= self.env['mail.activity.type'].browse( activity_type_id ) # we will get the name when reading mail_template_ids res_id_to_deadline[res_id] = group['date_deadline'] if ( res_id not in res_id_to_deadline or group['date_deadline'] < res_id_to_deadline[res_id]) else res_id_to_deadline[res_id] state = self._compute_state_from_date(group['date_deadline'], self.user_id.sudo().tz) activity_data[res_id][activity_type_id] = { 'count': group['__count'], 'ids': group['ids'], 'state': state, 'o_closest_deadline': group['date_deadline'], } res_ids_sorted = sorted(res_id_to_deadline, key=lambda item: res_id_to_deadline[item]) res_id_to_name = dict( self.env[res_model].browse(res_ids_sorted).name_get()) activity_type_infos = [] for elem in sorted(activity_type_ids, key=lambda item: item.sequence): mail_template_info = [] for mail_template_id in elem.mail_template_ids: mail_template_info.append({ "id": mail_template_id.id, "name": mail_template_id.name }) activity_type_infos.append( [elem.id, elem.name, mail_template_info]) return { 'activity_types': activity_type_infos, 'res_ids': [(rid, res_id_to_name[rid]) for rid in res_ids_sorted], 'grouped_activities': activity_data, 'model': res_model, }
class Channel(models.Model): """ A channel is a container of slides. It has group-based access configuration allowing to configure slide upload and access. Slides can be promoted in channels. """ _name = 'slide.channel' _description = 'Slide Channel' _inherit = [ 'mail.thread', 'website.seo.metadata', 'website.published.multi.mixin' ] _order = 'sequence, id' _order_by_strategy = { 'most_viewed': 'total_views desc', 'most_voted': 'likes desc', 'latest': 'date_published desc', } name = fields.Char('Name', translate=True, required=True) active = fields.Boolean(default=True) description = fields.Html('Description', translate=html_translate, sanitize_attributes=False) sequence = fields.Integer(default=10, help='Display order') category_ids = fields.One2many('slide.category', 'channel_id', string="Categories") slide_ids = fields.One2many('slide.slide', 'channel_id', string="Slides") promote_strategy = fields.Selection([('none', 'No Featured Presentation'), ('latest', 'Latest Published'), ('most_voted', 'Most Voted'), ('most_viewed', 'Most Viewed'), ('custom', 'Featured Presentation')], string="Featuring Policy", default='most_voted', required=True) custom_slide_id = fields.Many2one('slide.slide', string='Slide to Promote') promoted_slide_id = fields.Many2one('slide.slide', string='Featured Slide', compute='_compute_promoted_slide_id', store=True) @api.depends('custom_slide_id', 'promote_strategy', 'slide_ids.likes', 'slide_ids.total_views', "slide_ids.date_published") def _compute_promoted_slide_id(self): for record in self: if record.promote_strategy == 'none': record.promoted_slide_id = False elif record.promote_strategy == 'custom': record.promoted_slide_id = record.custom_slide_id elif record.promote_strategy: slides = self.env['slide.slide'].search( [('website_published', '=', True), ('channel_id', '=', record.id)], limit=1, order=self._order_by_strategy[record.promote_strategy]) record.promoted_slide_id = slides and slides[0] or False nbr_presentations = fields.Integer('Number of Presentations', compute='_count_presentations', store=True) nbr_documents = fields.Integer('Number of Documents', compute='_count_presentations', store=True) nbr_videos = fields.Integer('Number of Videos', compute='_count_presentations', store=True) nbr_infographics = fields.Integer('Number of Infographics', compute='_count_presentations', store=True) total = fields.Integer(compute='_count_presentations', store=True) @api.depends('slide_ids.slide_type', 'slide_ids.website_published') def _count_presentations(self): result = dict.fromkeys(self.ids, dict()) res = self.env['slide.slide'].read_group( [('website_published', '=', True), ('channel_id', 'in', self.ids)], ['channel_id', 'slide_type'], ['channel_id', 'slide_type'], lazy=False) for res_group in res: result[res_group['channel_id'][0]][res_group[ 'slide_type']] = result[res_group['channel_id'][0]].get( res_group['slide_type'], 0) + res_group['__count'] for record in self: record.nbr_presentations = result[record.id].get('presentation', 0) record.nbr_documents = result[record.id].get('document', 0) record.nbr_videos = result[record.id].get('video', 0) record.nbr_infographics = result[record.id].get('infographic', 0) record.total = record.nbr_presentations + record.nbr_documents + record.nbr_videos + record.nbr_infographics publish_template_id = fields.Many2one( 'mail.template', string='Published Template', help="Email template to send slide publication through email", default=lambda self: self.env['ir.model.data'].xmlid_to_res_id( 'website_slides.slide_template_published')) share_template_id = fields.Many2one( 'mail.template', string='Shared Template', help="Email template used when sharing a slide", default=lambda self: self.env['ir.model.data'].xmlid_to_res_id( 'website_slides.slide_template_shared')) visibility = fields.Selection( [('public', 'Public'), ('private', 'Private'), ('partial', 'Show channel but restrict presentations')], default='public', required=True) group_ids = fields.Many2many( 'res.groups', 'rel_channel_groups', 'channel_id', 'group_id', string='Channel Groups', help="Groups allowed to see presentations in this channel") access_error_msg = fields.Html( 'Error Message', help="Message to display when not accessible due to access rights", default=lambda s: _("<p>This channel is private and its content is restricted to some users.</p>" ), translate=html_translate, sanitize_attributes=False) upload_group_ids = fields.Many2many( 'res.groups', 'rel_upload_groups', 'channel_id', 'group_id', string='Upload Groups', help= "Groups allowed to upload presentations in this channel. If void, every user can upload." ) # not stored access fields, depending on each user can_see = fields.Boolean('Can See', compute='_compute_access', search='_search_can_see') can_see_full = fields.Boolean('Full Access', compute='_compute_access') can_upload = fields.Boolean('Can Upload', compute='_compute_access') def _search_can_see(self, operator, value): if operator not in ('=', '!=', '<>'): raise ValueError('Invalid operator: %s' % (operator, )) if not value: operator = operator == "=" and '!=' or '=' # Won't impact sitemap, search() in converter is forced as public user if self.env.user._is_admin(): return [(1, '=', 1)] # Better perfs to split request and use inner join that left join req = """ SELECT id FROM slide_channel WHERE visibility='public' UNION SELECT c.id FROM slide_channel c INNER JOIN rel_channel_groups rg on c.id = rg.channel_id INNER JOIN res_groups g on g.id = rg.group_id INNER JOIN res_groups_users_rel u on g.id = u.gid and uid = %s """ op = operator == "=" and "inselect" or "not inselect" # don't use param named because orm will add other param (test_active, ...) return [('id', op, (req, (self._uid, )))] @api.one @api.depends('visibility', 'group_ids', 'upload_group_ids') def _compute_access(self): self.can_see = self.visibility in [ 'public', 'private' ] or bool(self.group_ids & self.env.user.groups_id) self.can_see_full = self.visibility == 'public' or bool( self.group_ids & self.env.user.groups_id) self.can_upload = self.can_see and (not self.upload_group_ids or bool(self.upload_group_ids & self.env.user.groups_id)) @api.multi def get_base_url(self): self.ensure_one() icp = self.env['ir.config_parameter'].sudo().get_param('web.base.url') return self.website_id and self.website_id._get_http_domain() or icp @api.multi @api.depends('name', 'website_id.domain') def _compute_website_url(self): super(Channel, self)._compute_website_url() for channel in self: if channel.id: # avoid to perform a slug on a not yet saved record in case of an onchange. base_url = channel.get_base_url() channel.website_url = '%s/slides/%s' % (base_url, slug(channel)) @api.onchange('visibility') def change_visibility(self): if self.visibility == 'public': self.group_ids = False @api.multi def write(self, vals): res = super(Channel, self).write(vals) if 'active' in vals: # archiving/unarchiving a channel does it on its slides, too self.with_context(active_test=False).mapped('slide_ids').write( {'active': vals['active']}) return res @api.multi @api.returns('mail.message', lambda value: value.id) def message_post(self, parent_id=False, subtype=None, **kwargs): """ Temporary workaround to avoid spam. If someone replies on a channel through the 'Presentation Published' email, it should be considered as a note as we don't want all channel followers to be notified of this answer. """ self.ensure_one() if parent_id: parent_message = self.env['mail.message'].sudo().browse(parent_id) if parent_message.subtype_id and parent_message.subtype_id == self.env.ref( 'website_slides.mt_channel_slide_published'): if kwargs.get('subtype_id'): kwargs['subtype_id'] = False subtype = 'mail.mt_note' return super(Channel, self).message_post(parent_id=parent_id, subtype=subtype, **kwargs) def list_all(self): return { 'channels': [{ 'id': channel.id, 'name': channel.name, 'website_url': channel.website_url } for channel in self.search([])] }