B
    `                 @   s   d dl Z d dlmZ d dlmZ d dlmZ d dlmZ d dl	m
Z
 d dlmZ d dlmZ d d	lmZ ed
dZed
dZed
dZed
dZe dZdddZdd Zdd Zee
dd Zeedd Zeedd Zeedd ZdS )     N)IntegrityError)F)receiver)basket_addition)product_viewed)order_placed)user_search)	get_modelZ	analyticsProductRecordUserProductView
UserRecord
UserSearchzoscar.analytics   c             C   sj   yD| j jf |}|jf |t|| i}|sB|||< | j jf | W n  tk
rd   td|  Y nX dS )a  
    Efficiently updates a counter field by a given increment. Uses Django's
    update() call to fetch and update in one query.

    TODO: This has a race condition, we should use UPSERT here

    :param model: The model class of the recording model
    :param field_name: The name of the field to update
    :param filter_kwargs: Parameters to the ORM's filter() function to get the
                          correct instance
    z5IntegrityError when updating analytics counter for %sN)objectsfilterupdater   creater   loggererror)model
field_nameZfilter_kwargs	incrementrecordaffected r   M/home/dcms/DCMS/lib/python3.7/site-packages/oscar/apps/analytics/receivers.py_update_counter   s    r   c             C   s.   x(| j  D ]}ttdd|ji|j qW d S )NZnum_purchasesproduct)linesallr   r
   r   Zquantity)orderliner   r   r   _record_products_in_order0   s    r"   c             C   s   yrt jj| d}|jtdd td|j td|j td|j |jd}|spt jj	| d|j|j|j|jd W n t
k
r   td	 Y nX d S )
N)user
num_ordersr   num_order_linesnum_order_itemstotal_spent)r$   r%   r&   r'   date_last_order)r#   r$   r%   r&   r'   r(   z8IntegrityError in analytics when recording a user order.)r   r   r   r   r   Z	num_linesZ	num_itemsZtotal_incl_taxZdate_placedr   r   r   r   )r#   r    r   r   r   r   r   _record_user_order8   s"    

r)   c             K   sN   | ddrd S ttdd|i |rJ|jrJttdd|i tjj||d d S )NrawFZ	num_viewsr   Znum_product_viewsr#   )r   r#   )getr   r
   is_authenticatedr   r   r   r   )senderr   r#   kwargsr   r   r   receive_product_viewN   s    
r/   c             K   s*   |r&|j r&|dds&tjj||d d S )Nr*   F)r#   query)r,   r+   r   Z_default_managerr   )r-   r0   r#   r.   r   r   r   receive_product_searchX   s    r1   c             K   s>   | ddrd S ttdd|i |r:|jr:ttdd|i d S )Nr*   FZnum_basket_additionsr   r#   )r+   r   r
   r,   r   )r-   r   r#   r.   r   r   r   receive_basket_addition^   s    
r2   c             K   s0   | ddrd S t| |r,|jr,t|| d S )Nr*   F)r+   r"   r,   r)   )r-   r    r#   r.   r   r   r   receive_order_placedh   s
    
r3   )r   )loggingZ	django.dbr   Zdjango.db.modelsr   Zdjango.dispatchr   Zoscar.apps.basket.signalsr   Zoscar.apps.catalogue.signalsr   Zoscar.apps.order.signalsr   Zoscar.apps.search.signalsr   Zoscar.core.loadingr	   r
   r   r   r   	getLoggerr   r   r"   r)   r/   r1   r2   r3   r   r   r   r   <module>   s(   







