| 1 |
from datetime import datetime, timedelta |
| 2 |
|
| 3 |
from odoo import api, fields, models |
| 4 |
from odoo.http import request |
| 5 |
|
| 6 |
|
| 7 |
class ProductWishlist(models.Model): |
| 8 |
_name = 'product.wishlist' |
| 9 |
_description = 'Product Wishlist' |
| 10 |
_product_unique_partner_id = models.Constraint( |
| 11 |
'UNIQUE(product_id, partner_id)', |
| 12 |
'Duplicated wishlisted product for this partner.', |
| 13 |
) |
| 14 |
|
| 15 |
partner_id = fields.Many2one('res.partner', string='Owner', index='btree_not_null') |
| 16 |
product_id = fields.Many2one('product.product', string='Product', required=True) |
| 17 |
currency_id = fields.Many2one('res.currency', related='website_id.currency_id', readonly=True) |
| 18 |
pricelist_id = fields.Many2one('product.pricelist', string='Pricelist', help='Pricelist when added') |
| 19 |
price = fields.Monetary(currency_field='currency_id', string='Price', help='Price of the product when it has been added in the wishlist') |
| 20 |
website_id = fields.Many2one('website', ondelete='cascade', required=True) |
| 21 |
active = fields.Boolean(default=True, required=True) |
| 22 |
|
| 23 |
@api.model |
| 24 |
def current(self): |
| 25 |
"""Get all wishlist items that belong to current user or session, |
| 26 |
filter products that are unpublished.""" |
| 27 |
if not request: |
| 28 |
return self |
| 29 |
|
| 30 |
if request.website.is_public_user(): |
| 31 |
wish = self.sudo().search([('id', 'in', request.session.get('wishlist_ids', []))]) |
| 32 |
else: |
| 33 |
wish = self.search([("partner_id", "=", self.env.user.partner_id.id), ('website_id', '=', request.website.id)]) |
| 34 |
|
| 35 |
return wish.filtered( |
| 36 |
lambda wish: |
| 37 |
wish.sudo().product_id.product_tmpl_id.website_published |
| 38 |
and wish.sudo().product_id.product_tmpl_id._is_add_to_cart_possible() |
| 39 |
) |
| 40 |
|
| 41 |
@api.model |
| 42 |
def _add_to_wishlist(self, pricelist_id, currency_id, website_id, price, product_id, partner_id=False): |
| 43 |
wish = self.env['product.wishlist'].create({ |
| 44 |
'partner_id': partner_id, |
| 45 |
'product_id': product_id, |
| 46 |
'currency_id': currency_id, |
| 47 |
'pricelist_id': pricelist_id, |
| 48 |
'price': price, |
| 49 |
'website_id': website_id, |
| 50 |
}) |
| 51 |
return wish |
| 52 |
|
| 53 |
@api.model |
| 54 |
def _check_wishlist_from_session(self): |
| 55 |
"""Assign all wishlist withtout partner from this the current session""" |
| 56 |
session_wishes = self.sudo().search([('id', 'in', request.session.get('wishlist_ids', []))]) |
| 57 |
partner_wishes = self.sudo().search([("partner_id", "=", self.env.user.partner_id.id)]) |
| 58 |
partner_products = partner_wishes.mapped("product_id") |
| 59 |
|
| 60 |
duplicated_wishes = session_wishes.filtered(lambda wish: wish.product_id <= partner_products) |
| 61 |
session_wishes -= duplicated_wishes |
| 62 |
duplicated_wishes.unlink() |
| 63 |
|
| 64 |
session_wishes.write({"partner_id": self.env.user.partner_id.id}) |
| 65 |
request.session.pop('wishlist_ids') |
| 66 |
|
| 67 |
@api.autovacuum |
| 68 |
def _gc_sessions(self, *args, **kwargs): |
| 69 |
"""Remove wishlists for unexisting sessions.""" |
| 70 |
self.with_context(active_test=False).search([ |
| 71 |
("create_date", "<", fields.Datetime.to_string(datetime.now() - timedelta(weeks=kwargs.get('wishlist_week', 5)))), |
| 72 |
("partner_id", "=", False), |
| 73 |
]).unlink() |
| 74 |
|
| 75 |
|
| 76 |
class ResPartner(models.Model): |
| 77 |
_inherit = 'res.partner' |
| 78 |
|
| 79 |
wishlist_ids = fields.One2many('product.wishlist', 'partner_id', string='Wishlist', domain=[('active', '=', True)]) |
| 80 |
|
| 81 |
|
| 82 |
class ProductTemplate(models.Model): |
| 83 |
_inherit = 'product.template' |
| 84 |
|
| 85 |
def _is_in_wishlist(self): |
| 86 |
self.ensure_one() |
| 87 |
return self in self.env['product.wishlist'].current().mapped('product_id.product_tmpl_id') |
| 88 |
|
| 89 |
|
| 90 |
class ProductProduct(models.Model): |
| 91 |
_inherit = 'product.product' |
| 92 |
|
| 93 |
def _is_in_wishlist(self): |
| 94 |
self.ensure_one() |
| 95 |
return self in self.env['product.wishlist'].current().mapped('product_id') |