3

\                 @   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dddg\ZZZZe j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_classeszanalytics.models
UserSearch
UserRecordProductRecordUserProductViewzoscar.analytics   c             C   sj   yD| j jf |}|jf |t|| i}|sB|||< | j jf | W n  tk
rd   tj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_namefilter_kwargs	incrementrecordaffected r   9/var/www/html/oscar2019/oscar/apps/analytics/receivers.py_update_counter   s    r   c             C   s.   x(| j j D ]}ttdd|ji|j qW d S )Nnum_purchasesproduct)linesallr   r   r   quantity)orderliner   r   r   _record_products_in_order/   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j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   	num_lines	num_itemstotal_incl_taxdate_placedr   r   r   r   )r&   r#   r   r   r   r   r   _record_user_order7   s"    

r0   c             K   sN   |j ddrd S ttdd|i |rJ|jrJttdd|i tjj||d d S )NrawF	num_viewsr   num_product_viewsr&   )r   r&   )getr   r   is_authenticatedr   r   r   r   )senderr   r&   kwargsr   r   r   receive_product_viewM   s    
r8   c             K   s,   |r(|j r(|jdd r(tjj||d d S )Nr1   F)r&   query)r5   r4   r
   _default_managerr   )r6   r9   r&   r7   r   r   r   receive_product_searchW   s    r;   c             K   s>   |j ddrd S ttdd|i |r:|jr:ttdd|i d S )Nr1   Fnum_basket_additionsr   r&   )r4   r   r   r5   r   )r6   r   r&   r7   r   r   r   receive_basket_addition]   s    
r=   c             K   s0   |j ddrd S t| |r,|jr,t|| d S )Nr1   F)r4   r%   r5   r0   )r6   r#   r&   r7   r   r   r   receive_order_placedg   s
    
r>   )r   )logging	django.dbr   django.db.modelsr   django.dispatchr   oscar.apps.basket.signalsr   oscar.apps.catalogue.signalsr   oscar.apps.order.signalsr   Zoscar.apps.search.signalsr   Zoscar.core.loadingr	   r
   r   r   r   	getLoggerr   r   r%   r0   r8   r;   r=   r>   r   r   r   r   <module>   s&   



