3

\                 @   s\   d dl Z d dlmZ d dlmZmZ e jdZeddZG dd de	Z
G d	d
 d
eZdS )    N)chain)	get_class	get_modelzoscar.offerszoffer.resultsOfferApplicationsc               @   s   e Zd ZdS )OfferApplicationErrorN)__name__
__module____qualname__ r
   r
   6/var/www/html/oscar2019/oscar/apps/offer/applicator.pyr   
   s   r   c               @   sH   e Zd ZdddZdd ZdddZdd	 Zd
d Zdd Zdd Z	dS )
ApplicatorNc             C   s   | j |||}| j|| dS )z
        Apply all relevant offers to the given basket.

        The request is passed too as sometimes the available offers
        are dependent on the user (eg session-based offers).
        N)
get_offersapply_offers)selfbasketuserrequestoffersr
   r
   r   apply   s    zApplicator.applyc             C   sf   t  }xT|D ]L}d}xB||j|jk rV|j|}|d7 }|js@P |j|| |jrP qW qW ||_d S )Nr      )r   Zget_max_applicationsownerZapply_benefitis_successfuladdis_finaloffer_applications)r   r   r   applicationsofferZnum_applicationsresultr
   r
   r   r      s    


zApplicator.apply_offersc             C   sH   | j  }| j||}| j|}| j|}ttt||||dd ddS )z
        Return all offers to apply to the basket.

        This method should be subclassed and extended to provide more
        sophisticated behaviour.  For instance, you could load extra offers
        based on the session or the user type.
        c             S   s   | j S )N)priority)or
   r
   r   <lambda>=   s    z'Applicator.get_offers.<locals>.<lambda>T)keyreverse)get_site_offersget_basket_offersget_user_offersget_session_offerslistsortedr   )r   r   r   r   Zsite_offersbasket_offersZuser_offersZsession_offersr
   r
   r   r   .   s    


zApplicator.get_offersc             C   s&   t dd}|jj|jd}|jddS )zD
        Return site offers that are available to all users
        r   ConditionalOffer)Z
offer_type	conditionZbenefit)r   activefilterZSITEselect_related)r   r*   qsr
   r
   r   r#   ?   s    
zApplicator.get_site_offersc       	      C   sz   g }|j  s| r|S x^|jj D ]P}|j|d\}}|j r"|r"|jj }x|D ]}|j| qRW tt||}q"W |S )zb
        Return basket-linked offers such as those associated with a voucher
        code
        )r   )	idvouchersallZis_available_to_user	is_activer   Zset_voucherr'   r   )	r   r   r   r   voucherZavailable_to_user__r)   r   r
   r
   r   r$   J   s    

zApplicator.get_basket_offersc             C   s   g S )zm
        Returns offers linked to this particular user.

        Eg: student users might get 25% off
        r
   )r   r   r
   r
   r   r%   \   s    zApplicator.get_user_offersc             C   s   g S )z
        Returns temporary offers linked to the current session.

        Eg: visitors coming from an affiliate site get a 10% discount
        r
   )r   r   r
   r
   r   r&   d   s    zApplicator.get_session_offers)NN)NN)
r   r   r	   r   r   r   r#   r$   r%   r&   r
   r
   r
   r   r      s   


r   )logging	itertoolsr   oscar.core.loadingr   r   	getLoggerloggerr   	Exceptionr   objectr   r
   r
   r
   r   <module>   s   

