3

\+                 @   s   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	m
Z
 d dlZd dlZd dlmZ edd	Zedd
ZeddZeddZeddZeddZG dd deZG dd deZdS )    )View)messages)	get_model)settings)JsonResponseHttpResponseRedirectN)reverse	catalogueProductProductAttributeProductAttributeValueProductClasspartnerStockRecordPartnerc               @   s"   e Zd ZdZdddZdd ZdS )	CreateVariantsViewzp
    Create all variants for the given product.
    Create the stock records for all difference currencies.
    Fc       
      C   s   t jj|j||jd|d}x0|D ](}tjj|jjj|j	dj
 ||d q W tjjdj
 }tdd ttdD }x*|D ]"}tj }	tjj||||	jd	 qzW d S )
N   )	structureparentslugis_discountablesubscription)product_class)	attributeproductvalue_optionpkc             S   s   g | ]}|d  qS )currency ).0cr   r   9/var/www/html/oscar2019/apps/dashboard/catalogue/views.py
<listcomp>-   s    z0CreateVariantsView.make_baby.<locals>.<listcomp>SUPPORTED_COUNTRIES)r   r   price_currencypartner_sku)r
   objectscreateCHILDr   r   groupproduct_attributesfilterr   firstr   order_bysetgetattrr   uuiduuid4r   hex)
selfr   combinationr   childoptionr   
currenciesr   skur   r   r!   	make_baby   s(    

zCreateVariantsView.make_babyc             O   s  t jj| jjdd}tjj|jdjdddjddj j	 }g }d	}x|D ]}|j
|jjj	  qNW ttj| }yx|D ]}	| j||	 q~W W nD tk
r }
 z(tj| jt|
 ttd
d|jidS d }
~
X nX tj| jdj| ttd
d|jidS )Nr   )r   )r   TF)use_for_variationsZoption_group__isnulloption_groupZoption_group__optionsr   zdashboard:catalogue-product)kwargsz{} babies were made !)r
   r&   getr<   r   r+   r   prefetch_relateddistinctallappendr;   optionslist	itertoolsr   r9   	Exceptionr   errorrequeststrr   r   r   successformat)r3   argsr<   r   
attributes	attr_listZchildren_bornr   combinationsr4   er   r   r!   r=   8   s"    

"zCreateVariantsView.getN)F)__name__
__module____qualname____doc__r9   r=   r   r   r   r!   r      s   
r   c               @   s   e Zd ZdZdd ZdS )CreateSubscriptionVariantz7
    Create a clone of the given product or child.
    c             O   s   | j jd}tjj|d}d |_d|_|j  x.tjj|dj	 D ]}d |_||_
|j  qBW x:tjj|dj	 D ]$}||_
d |_tj j|_|j  qrW tj| jd ttdd|jidS )Nr   )r   T)
product_idSuccesszdashboard:catalogue-product)r<   )r<   r=   r
   r&   r   r   saver   r+   r@   r   r   r0   r1   r2   r%   r   rI   rG   r   r   	parent_id)r3   rK   r<   rU   r   attrrecordr   r   r!   r=   V   s     zCreateSubscriptionVariant.getN)rP   rQ   rR   rS   r=   r   r   r   r!   rT   Q   s   rT   )Zdjango.views.genericr   django.contribr   oscar.core.loadingr   django.confr   django.http.responser   r   rD   r0   django.shortcutsr   r
   r   r   r   r   r   r   rT   r   r   r   r!   <module>   s   





>