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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mZmZ d dlmZ eddk\ZZZZZ Z!Z"Z#eddl\Z$Z%Z&Z'Z(Z)eddm\Z*Z+Z,Z-ed"dn\Z.Z/Z0ed&d'Z1ed&d(Z2ed&d)Z3ed&d*Z4ed&d+Z5ed,d-Z6ed,d.Z7ed,d/Z8ed&d0Z9ed&d1Z:d2d3 Z;G d4d5 d5eZ<G d6d7 d7ej=Z>G d8d9 d9ej?Z@G d:d; d;ejAZBG d<d= d=ejCZDG d>d? d?eZEG d@dA dAeejFZGG dBdC dCeHZIG dDdE dEeIejJZKG dFdG dGeIej?ZLG dHdI dIeIejAZMG dJdK dKeZNG dLdM dMej?ZOG dNdO dOeOZPG dPdQ dQeOZQG dRdS dSejCZRG dTdU dUejAZSG dVdW dWej?ZTG dXdY dYe.eTZUG dZd[ d[e/eTZVG d\d] d]eZWG d^d_ d_e0ejAZXG d`da daeZYG dbdc dcej?ZZG ddde deeZZ[G dfdg dgeZZ\G dhdi diejAZ]djS )o    )settings)messages)Q)HttpResponseRedirect)get_object_or_404redirect)render_to_string)reverse)gettext_lazy)generic)SingleTableMixinSingleTableView)get_classes	get_model)ObjectLookupViewzdashboard.catalogue.formsProductFormProductClassSelectFormProductSearchFormProductClassFormCategoryFormStockAlertSearchFormAttributeOptionGroupForm
OptionFormzdashboard.catalogue.formsetsStockRecordFormSetProductCategoryFormSetProductImageFormSetProductRecommendationFormSetProductAttributesFormSetAttributeOptionFormSetzdashboard.catalogue.tablesProductTableCategoryTableAttributeOptionGroupTableOptionTablezdashboard.viewsPopUpWindowCreateMixinPopUpWindowUpdateMixinPopUpWindowDeleteMixin	catalogueProductCategoryProductImageProductCategoryProductClasspartnerStockRecord
StockAlertPartnerAttributeOptionGroupOptionc             C   s   |j r
| S | j|jdj S )z
    Restrict the queryset to products the given user has access to.
    A staff user is allowed to access all Products.
    A non-staff user is only allowed access to a product if they are in at
    least one stock record's partner user list.
    )Z stockrecords__partner__users__pk)is_stafffilterpkdistinct)querysetuser r8   ?/var/www/html/oscar2019/oscar/apps/dashboard/catalogue/views.pyfilter_productsE   s    r:   c                   sh   e Zd ZdZdZeZeZe	Z
dZ fddZdd Z fdd	Zd
d Zdd Zdd Zdd Z  ZS )ProductListViewzZ
    Dashboard view of the product list.
    Supports the permission-based dashboard.
    z%dashboard/catalogue/product_list.htmlproductsc                s(   t  jf |}| j|d< | j |d< |S )NformZproductclass_form)superget_context_datar=   productclass_form_class)selfkwargsctx)	__class__r8   r9   r?   _   s    
z ProductListView.get_context_datac             C   s&   |j  rt|jj rtdS tdS )NzProduct search resultsProducts)is_validanycleaned_datavalues_)rA   r=   r8   r8   r9   get_descriptione   s    zProductListView.get_descriptionc                s<   d| j jkr|jtdd t jf |}| j| j|_|S )NZrecently_editedF)	orderable)	requestGETupdatedictr>   	get_tablerK   r=   caption)rA   rB   table)rD   r8   r9   rQ   j   s
    zProductListView.get_tablec             C   s
   t ddS )N   )per_page)rP   )rA   rS   r8   r8   r9   get_table_paginationr   s    z$ProductListView.get_table_paginationc             C   s   t || jjS )zT
        Apply any filters to restrict the products that appear on the list
        )r:   rM   r7   )rA   r6   r8   r8   r9   filter_querysetu   s    zProductListView.filter_querysetc             C   s&   t jj j }| j|}| j|}|S )z2
        Build the queryset for this list
        )r'   objects	browsablebase_querysetrW   apply_search)rA   r6   r8   r8   r9   get_queryset{   s    

zProductListView.get_querysetc             C   s   | j | jj| _| jj s|S | jj}|jdrtjj	|d d}|j	t
|jddt
|jddB }|j rv|}n8tjj	|d d}|j	t
|jddt
|jddB }|jdr|j	|d d}|S )	zn
        Filter the queryset and set the description according to the search
        parameters given
        upc)r]   id)id__in	parent_id)Zupc__icontainstitle)title__icontains)
form_classrM   rN   r=   rF   rH   getr'   rX   r3   r   rI   exists)rA   r6   dataZmatches_upcZqs_matchr8   r8   r9   r[      s"    

"
zProductListView.apply_search)__name__
__module____qualname____doc__template_namer   rc   r   r@   r   table_classcontext_table_namer?   rK   rQ   rV   rW   r\   r[   __classcell__r8   r8   )rD   r9   r;   R   s   	r;   c               @   s,   e Zd ZdZeZdd Zdd Zdd ZdS )	ProductCreateRedirectViewFc             C   s   t dd|jidS )z" Allow site to provide custom URL z"dashboard:catalogue-product-createproduct_class_slug)rB   )r	   slug)rA   product_classr8   r8   r9   get_product_create_url   s    z0ProductCreateRedirectView.get_product_create_urlc             C   s   t j| jtd tdS )NzPlease choose a product typez dashboard:catalogue-product-list)r   errorrM   rJ   r	   )rA   r8   r8   r9   get_invalid_product_class_url   s    z7ProductCreateRedirectView.get_invalid_product_class_urlc             K   s6   | j | jj}|j r*|jd }| j|S | j S d S )Nrr   )r@   rM   rN   rF   rH   rs   ru   )rA   rB   r=   rr   r8   r8   r9   get_redirect_url   s
    

z*ProductCreateRedirectView.get_redirect_urlN)	rg   rh   ri   	permanentr   r@   rs   ru   rv   r8   r8   r8   r9   ro      s
   ro   c                   s   e Zd ZdZdZeZdZeZ	e
ZeZeZeZ fddZ fddZdd	 Zd
d Zd# fdd	Z fddZdd Z fddZdd Ze ZZdd Zdd Zdd Zdd Z dd  Z!d!d" Z"  Z#S )$ProductCreateUpdateViewai  
    Dashboard view that is can both create and update products of all kinds.
    It can be used in three different ways, each of them with a unique URL
    pattern:
    - When creating a new standalone product, this view is called with the
      desired product class
    - When editing an existing product, this view is called with the product's
      primary key. If the product is a child product, the template considerably
      reduces the available form fields.
    - When creating a new child product, this view is called with the parent's
      primary key.

    Supports the permission-based dashboard.
    z'dashboard/catalogue/product_update.htmlproductc                s*   t  j|| | j| j| j| jd| _d S )N)category_formsetimage_formsetZrecommended_formsetstockrecord_formset)r>   __init__rz   r{   recommendations_formsetr|   formsets)rA   argsrB   )rD   r8   r9   r}      s
    z ProductCreateUpdateView.__init__c                s    t  j|f||}| j p|S )N)r>   dispatchcheck_objects_or_redirect)rA   rM   r   rB   resp)rD   r8   r9   r      s    z ProductCreateUpdateView.dispatchc             C   s@   | j r<| jdk	r<| jjdd\}}|s<tj| j| tdS dS )a  
        Allows checking the objects fetched by get_object and redirect
        if they don't satisfy our needs.
        Is used to redirect when create a new variant and the specified
        parent product can't actually be turned into a parent product.
        NT)give_reasonz dashboard:catalogue-product-list)creatingparentcan_be_parentr   rt   rM   r   )rA   rF   reasonr8   r8   r9   r      s
    z1ProductCreateUpdateView.check_objects_or_redirectc             C   s   t tjj | jjS )zQ
        Filter products that the user doesn't have permission to update
        )r:   r'   rX   allrM   r7   )rA   r8   r8   r9   r\      s    z$ProductCreateUpdateView.get_querysetNc                s   d| j k| _| jrd| j jd}|dkrHd| _| j jd}tt|d| _ntt|d| _| jj| _dS t j	|}|j
 | _|j| _|S dS )a?  
        This parts allows generic.UpdateView to handle creating products as
        well. The only distinction between an UpdateView and a CreateView
        is that self.object is None. We emulate this behavior.

        This method is also responsible for setting self.product_class and
        self.parent.
        r4   	parent_pkNrp   )rq   )r4   )rB   r   rd   r   r   r+   rr   r'   r>   
get_objectget_product_class)rA   r6   r   rp   ry   )rD   r8   r9   r      s    	

z"ProductCreateUpdateView.get_objectc                sl   t  jf |}| j|d< | j|d< | j |d< x8| jj D ]*\}}||kr:|| j| jj| j	d||< q:W |S )Nrr   r   ra   )instance)
r>   r?   rr   r   get_page_titler   itemsrM   r7   object)rA   rB   rC   ctx_nameformset_class)rD   r8   r9   r?     s    

z(ProductCreateUpdateView.get_context_datac             C   sj   | j r:| jd kr$tdd| jji S tdd| jji S n,| jjsJ| j rR| jjS tdd| jji S d S )Nz$Create new %(product_class)s productrr   z(Create new variant of %(parent_product)sparent_productz%Editing variant of %(parent_product)s)r   r   rJ   rr   namera   r   )rA   r8   r8   r9   r   #  s    
z&ProductCreateUpdateView.get_page_titlec                s"   t  j }| j|d< | j|d< |S )Nrr   r   )r>   get_form_kwargsrr   r   )rA   rB   )rD   r8   r9   r   2  s    


z'ProductCreateUpdateView.get_form_kwargsc             C   s   | j r|j r|j | _i }x<| jj D ].\}}|| j| jj| jj	| jj
| jd||< q(W |j ovtdd |j D }| j||}|r|r| j||S | j||S dS )zq
        Short-circuits the regular logic to have one place to have our
        logic to check all forms
        )r   c             S   s   g | ]}|j  qS r8   )rF   ).0formsetr8   r8   r9   
<listcomp>J  s   z=ProductCreateUpdateView.process_all_forms.<locals>.<listcomp>N)r   rF   saver   r   r   rr   rM   r7   POSTFILESr   rI   cleanforms_validforms_invalid)rA   r=   r   r   r   rF   Zcross_form_validation_resultr8   r8   r9   process_all_forms8  s    
z)ProductCreateUpdateView.process_all_formsc             C   s   dS )a@  
        Perform any cross-form/formset validation. If there are errors, attach
        errors to a form or a form field so that they are displayed to the user
        and return False. If everything is valid, return True. This method will
        be called regardless of whether the individual forms are valid.
        Tr8   )rA   r=   r   r8   r8   r9   r   [  s    zProductCreateUpdateView.cleanc             C   sD   | j r| j| j n
|j | _x|j D ]}|j  q(W t| j S )z
        Save all changes and display a success url.
        When creating the first child product, this method also sets the new
        parent's structure accordingly.
        )r   handle_adding_childr   r   r   rI   r   get_success_url)rA   r=   r   r   r8   r8   r9   r   d  s    
z#ProductCreateUpdateView.forms_validc             C   s   |dk	rt j|_|j  dS )z
        When creating the first child product, the parent product needs
        to be implicitly converted from a standalone product to a
        parent product.
        N)r'   PARENT	structurer   )rA   r   r8   r8   r9   r   v  s    	z+ProductCreateUpdateView.handle_adding_childc             C   sX   | j r(| jr(| jjd k	r(| jj  d | _tj| jtd | jf d|i|}| j	|S )NzCYour submitted data was not valid - please correct the errors belowr=   )
r   r   r4   deleter   rt   rM   rJ   r?   render_to_response)rA   r=   r   rC   r8   r8   r9   r     s    

z%ProductCreateUpdateView.forms_invalidc             C   s.   |g}| j jj r$|| j jj g7 }dj|S )N?)rM   rN   	urlencodejoin)rA   url	url_partsr8   r8   r9   get_url_with_querystring  s    z0ProductCreateUpdateView.get_url_with_querystringc             C   s   t d| j| j| jd}tj| j|dd | jjjd}|dkrVtdd| jj	id	}nJ|d
krz| j
rztdd| j
jid	}n&|dkrtdd| jjid	}ntd}| j|S )aV  
        Renders a success message and redirects depending on the button:
        - Standard case is pressing "Save"; redirects to the product list
        - When "Save and continue" is pressed, we stay on the same page
        - When "Create (another) child product" is pressed, it redirects
          to a new product creation page
        z/dashboard/catalogue/messages/product_saved.html)ry   r   rM   zsafe noicon)
extra_tagsactioncontinuezdashboard:catalogue-productr4   )rB   zcreate-another-childz(dashboard:catalogue-product-create-childr   zcreate-childz dashboard:catalogue-product-list)r   r   r   rM   r   successr   rd   r	   r^   r   r4   r   )rA   msgr   r   r8   r8   r9   r     s(    z'ProductCreateUpdateView.get_success_url)N)$rg   rh   ri   rj   rk   r'   modelcontext_object_namer   rc   r   rz   r   r{   r   r~   r   r|   r}   r   r   r\   r   r?   r   r   r   
form_validform_invalidr   r   r   r   r   r   rn   r8   r8   )rD   r9   rx      s2    !	rx   c                   sL   e Zd ZdZdZeZdZdd Z fddZ	dd	 Z
d
d Zdd Z  ZS )ProductDeleteViewz
    Dashboard view to delete a product. Has special logic for deleting the
    last child product.
    Supports the permission-based dashboard.
    z'dashboard/catalogue/product_delete.htmlry   c             C   s   t tjj | jjS )zQ
        Filter products that the user doesn't have permission to update
        )r:   r'   rX   r   rM   r7   )rA   r8   r8   r9   r\     s    zProductDeleteView.get_querysetc                s4   t  jf |}| jjr$td|d< ntd|d< |S )NzDelete product variant?ra   zDelete product?)r>   r?   r   is_childrJ   )rA   rB   rC   )rD   r8   r9   r?     s
    z"ProductDeleteView.get_context_datac             O   sP   | j  | _d}| jjr,| jj}|jj dk}| jj  |rD| j| t| j	 S )NF   )
r   r   r   r   childrencountr   handle_deleting_last_childr   r   )rA   rM   r   rB   is_last_childr   r8   r8   r9   r     s    


zProductDeleteView.deletec             C   s   |j |_|j  d S )N)
STANDALONEr   r   )rA   r   r8   r8   r9   r     s    z,ProductDeleteView.handle_deleting_last_childc             C   sf   | j jr<td| j j  }tj| j| tdd| j jidS td| j j	 }tj| j| tdS dS )z
        When deleting child products, this view redirects to editing the
        parent product. When deleting any other product, it redirects to the
        product list view.
        zDeleted product variant '%s'zdashboard:catalogue-productr4   )rB   zDeleted product '%s'z dashboard:catalogue-product-listN)
r   r   rJ   	get_titler   r   rM   r	   r`   ra   )rA   r   r8   r8   r9   r     s    z!ProductDeleteView.get_success_url)rg   rh   ri   rj   rk   r'   r   r   r\   r?   r   r   r   rn   r8   r8   )rD   r9   r     s   	r   c                   s6   e Zd ZdZeZdZejZ	 fddZ
dd Z  ZS )StockAlertListViewz(dashboard/catalogue/stockalert_list.htmlalertsc                s&   t  jf |}| j|d< | j|d< |S )Nr=   description)r>   r?   r=   r   )rA   rB   rC   )rD   r8   r9   r?   	  s    

z#StockAlertListView.get_context_datac             C   sn   d| j jkrPt| j j| _| jj rb| jjd }td| | _| jj	j
|dS ntd| _t | _| jj	j S )NstatuszAlerts with status "%s")r   z
All alerts)rM   rN   r   r=   rF   rH   rJ   r   r   rX   r3   r   )rA   r   r8   r8   r9   r\     s    

zStockAlertListView.get_queryset)rg   rh   ri   rk   r.   r   r   r   OSCAR_STOCK_ALERTS_PER_PAGEpaginate_byr?   r\   rn   r8   r8   )rD   r9   r     s   r   c                   s0   e Zd ZdZeZdZdd Z fddZ  Z	S )CategoryListViewz&dashboard/catalogue/category_list.html
categoriesc             C   s   t j S )N)r(   get_root_nodes)rA   r8   r8   r9   r\   !  s    zCategoryListView.get_querysetc                s   t  j||}tj |d< |S )Nchild_categories)r>   r?   r(   r   )rA   r   rB   rC   )rD   r8   r9   r?   $  s    z!CategoryListView.get_context_data)
rg   rh   ri   rk   r    rl   rm   r\   r?   rn   r8   r8   )rD   r9   r     s
   r   c                   s8   e Zd ZdZeZdZeZdZ	dd Z
 fddZ  ZS )CategoryDetailListViewz&dashboard/catalogue/category_list.htmlcategoryr   c             C   s
   | j j S )N)r   get_children)rA   r8   r8   r9   get_table_data1  s    z%CategoryDetailListView.get_table_datac                s.   t  j||}| jj |d< | jj |d< |S )Nr   	ancestors)r>   r?   r   r   get_ancestors_and_self)rA   r   rB   rC   )rD   r8   r9   r?   4  s    z'CategoryDetailListView.get_context_data)rg   rh   ri   rk   r(   r   r   r    rl   rm   r   r?   rn   r8   r8   )rD   r9   r   *  s   r   c               @   s   e Zd Zdd ZdS )CategoryListMixinc             C   s.   | j j }|d krtdS td|jfdS d S )Nz!dashboard:catalogue-category-listz(dashboard:catalogue-category-detail-list)r   )r   
get_parentr	   r4   )rA   r   r8   r8   r9   r   =  s
    
z!CategoryListMixin.get_success_urlN)rg   rh   ri   r   r8   r8   r8   r9   r   ;  s   r   c                   s@   e Zd ZdZeZeZ fddZ fddZ	 fddZ
  ZS )CategoryCreateViewz&dashboard/catalogue/category_form.htmlc                s   t  jf |}td|d< |S )NzAdd a new categoryra   )r>   r?   rJ   )rA   rB   rC   )rD   r8   r9   r?   K  s    z#CategoryCreateView.get_context_datac                s   t j| jtd t j S )NzCategory created successfully)r   inforM   rJ   r>   r   )rA   )rD   r8   r9   r   P  s    z"CategoryCreateView.get_success_urlc                s&   t  j }d| jkr"| jd |d< |S )Nr   Z_ref_node_id)r>   get_initialrB   )rA   initial)rD   r8   r9   r   T  s    

zCategoryCreateView.get_initial)rg   rh   ri   rk   r(   r   r   rc   r?   r   r   rn   r8   r8   )rD   r9   r   F  s   r   c                   s4   e Zd ZdZeZeZ fddZ fddZ	  Z
S )CategoryUpdateViewz&dashboard/catalogue/category_form.htmlc                s&   t  jf |}td| jj |d< |S )NzUpdate category '%s'ra   )r>   r?   rJ   r   r   )rA   rB   rC   )rD   r8   r9   r?   a  s    z#CategoryUpdateView.get_context_datac                s   t j| jtd t j S )NzCategory updated successfully)r   r   rM   rJ   r>   r   )rA   )rD   r8   r9   r   f  s    z"CategoryUpdateView.get_success_url)rg   rh   ri   rk   r(   r   r   rc   r?   r   rn   r8   r8   )rD   r9   r   \  s
   r   c                   s0   e Zd ZdZeZ fddZ fddZ  ZS )CategoryDeleteViewz(dashboard/catalogue/category_delete.htmlc                s    t  j||}| jj |d< |S )Nr   )r>   r?   r   r   )rA   r   rB   rC   )rD   r8   r9   r?   o  s    z#CategoryDeleteView.get_context_datac                s   t j| jtd t j S )NzCategory deleted successfully)r   r   rM   rJ   r>   r   )rA   )rD   r8   r9   r   t  s    z"CategoryDeleteView.get_success_url)	rg   rh   ri   rk   r(   r   r?   r   rn   r8   r8   )rD   r9   r   k  s   r   c               @   s    e Zd ZeZdd Zdd ZdS )ProductLookupViewc             C   s   | j jj S )N)r   rY   r   )rA   r8   r8   r9   r\   |  s    zProductLookupView.get_querysetc             C   s   |j t|dt|dB S )N)rb   )parent__title__icontains)r3   r   )rA   qstermr8   r8   r9   lookup_filter  s    zProductLookupView.lookup_filterN)rg   rh   ri   r'   r   r\   r   r8   r8   r8   r9   r   y  s   r   c                   sL   e Zd ZdZeZeZeZ	dd Z
dd Zdd Ze
 ZZ fdd	Z  ZS )
ProductClassCreateUpdateViewz+dashboard/catalogue/product_class_form.htmlc             C   sf   | j r|j r|jdd| _| j| jj| jj| jd}|j oD|j }|rV| j||S | j	||S dS )z
        This validates both the ProductClass form and the
        ProductClassAttributes formset at once
        making it possible to display all their errors at once.
        F)commit)r   N)
r   rF   r   r   product_attributes_formsetrM   r   r   r   r   )rA   r=   attributes_formsetrF   r8   r8   r9   r     s    z.ProductClassCreateUpdateView.process_all_formsc             C   s   |j   |j   t| j S )N)r   r   r   )rA   r=   r   r8   r8   r9   r     s    z(ProductClassCreateUpdateView.forms_validc             C   s*   t j| jtd | j||d}| j|S )NzCYour submitted data was not valid - please correct the errors below)r=   r   )r   rt   rM   rJ   r?   r   )rA   r=   r   rC   r8   r8   r9   r     s
    
z*ProductClassCreateUpdateView.forms_invalidc                s8   t  j||}d|kr(| j| jd|d< | j |d< |S )Nr   )r   ra   )r>   r?   r   r   r   )rA   r   rB   rC   )rD   r8   r9   r?     s    z-ProductClassCreateUpdateView.get_context_data)rg   rh   ri   rk   r+   r   r   rc   r   r   r   r   r   r   r   r?   rn   r8   r8   )rD   r9   r     s   r   c               @   s(   e Zd ZdZdd Zdd Zdd ZdS )	ProductClassCreateViewTc             C   s   d S )Nr8   )rA   r8   r8   r9   r     s    z!ProductClassCreateView.get_objectc             C   s   t dS )NzAdd a new product type)rJ   )rA   r8   r8   r9   r     s    z ProductClassCreateView.get_titlec             C   s   t j| jtd tdS )Nz!Product type created successfullyzdashboard:catalogue-class-list)r   r   rM   rJ   r	   )rA   r8   r8   r9   r     s    z&ProductClassCreateView.get_success_urlN)rg   rh   ri   r   r   r   r   r8   r8   r8   r9   r     s   r   c               @   s(   e Zd ZdZdd Zdd Zdd ZdS )	ProductClassUpdateViewFc             C   s   t d| jj S )NzUpdate product type '%s')rJ   r   r   )rA   r8   r8   r9   r     s    z ProductClassUpdateView.get_titlec             C   s   t j| jtd tdS )Nz!Product type updated successfullyzdashboard:catalogue-class-list)r   r   rM   rJ   r	   )rA   r8   r8   r9   r     s    z&ProductClassUpdateView.get_success_urlc             C   s   t t| jd d}|S )Nr4   )r4   )r   r+   rB   )rA   rr   r8   r8   r9   r     s    z!ProductClassUpdateView.get_objectN)rg   rh   ri   r   r   r   r   r8   r8   r8   r9   r     s   r   c                   s(   e Zd ZdZdZeZ fddZ  ZS )ProductClassListViewz+dashboard/catalogue/product_class_list.htmlclassesc                s   t  j||}td|d< |S )NzProduct Typesra   )r>   r?   rJ   )rA   r   rB   rC   )rD   r8   r9   r?     s    z%ProductClassListView.get_context_data)	rg   rh   ri   rk   r   r+   r   r?   rn   r8   r8   )rD   r9   r     s   r   c                   s0   e Zd ZdZeZeZ fddZdd Z	  Z
S )ProductClassDeleteViewz-dashboard/catalogue/product_class_delete.htmlc                sl   t  j||}td| jj |d< | jjj }|dkrhd|d< td| jj |d< tj| j	td|  |S )NzDelete product type '%s'ra   r   TdisallowzUnable to delete '%s'z+%i products are still assigned to this type)
r>   r?   rJ   r   r   r<   r   r   rt   rM   )rA   r   rB   rC   Zproduct_count)rD   r8   r9   r?     s    z'ProductClassDeleteView.get_context_datac             C   s   t j| jtd tdS )Nz!Product type deleted successfullyzdashboard:catalogue-class-list)r   r   rM   rJ   r	   )rA   r8   r8   r9   r     s    z&ProductClassDeleteView.get_success_url)rg   rh   ri   rk   r+   r   r   rc   r?   r   rn   r8   r8   )rD   r9   r     s
   r   c                   sT   e Zd ZdZeZeZeZ	dd Z
dd Zdd Ze
 ZZ fdd	Zd
d Z  ZS )$AttributeOptionGroupCreateUpdateViewz4dashboard/catalogue/attribute_option_group_form.htmlc             C   sf   | j r|j r|jdd| _| j| jj| jj| jd}|j oD|j }|rV| j||S | j	||S dS )z
        This validates both the AttributeOptionGroup form and the
        AttributeOptions formset at once making it possible to display all their
        errors at once.
        F)r   )r   N)
r   rF   r   r   attribute_option_formsetrM   r   r   r   r   )rA   r=   r   rF   r8   r8   r9   r     s    z6AttributeOptionGroupCreateUpdateView.process_all_formsc             C   s   |j   |j   t| j S )N)r   r   r   )rA   r=   r   r8   r8   r9   r     s    z0AttributeOptionGroupCreateUpdateView.forms_validc             C   s*   t j| jtd | j||d}| j|S )NzCYour submitted data was not valid - please correct the errors below)r=   r   )r   rt   rM   rJ   r?   r   )rA   r=   r   rC   r8   r8   r9   r   %  s
    
z2AttributeOptionGroupCreateUpdateView.forms_invalidc                s4   t  jf |}|jd| j| jd | j |d< |S )Nr   )r   ra   )r>   r?   
setdefaultr   r   r   )rA   rB   rC   )rD   r8   r9   r?   6  s    z5AttributeOptionGroupCreateUpdateView.get_context_datac             C   s.   |g}| j jj r$|| j jj g7 }dj|S )Nr   )rM   rN   r   r   )rA   r   r   r8   r8   r9   r   <  s    z=AttributeOptionGroupCreateUpdateView.get_url_with_querystring)rg   rh   ri   rk   r0   r   r   rc   r   r   r   r   r   r   r   r?   r   rn   r8   r8   )rD   r9   r     s   r   c               @   s(   e Zd ZdZdd Zdd Zdd ZdS )	AttributeOptionGroupCreateViewTc             C   s   d S )Nr8   )rA   r8   r8   r9   r   G  s    z)AttributeOptionGroupCreateView.get_objectc             C   s   t dS )Nz Add a new Attribute Option Group)rJ   )rA   r8   r8   r9   r   J  s    z(AttributeOptionGroupCreateView.get_titlec             C   s*   | j stj| jtd td}| j|S )Nz+Attribute Option Group created successfullyz/dashboard:catalogue-attribute-option-group-list)is_popupr   r   rM   rJ   r	   r   )rA   r   r8   r8   r9   r   M  s    z.AttributeOptionGroupCreateView.get_success_urlN)rg   rh   ri   r   r   r   r   r8   r8   r8   r9   r   C  s   r   c               @   s(   e Zd ZdZdd Zdd Zdd ZdS )	AttributeOptionGroupUpdateViewFc             C   s   t t| jd d}|S )Nr4   )r4   )r   r0   rB   )rA   attribute_option_groupr8   r8   r9   r   X  s    z)AttributeOptionGroupUpdateView.get_objectc             C   s   t d| jj S )Nz"Update Attribute Option Group '%s')rJ   r   r   )rA   r8   r8   r9   r   \  s    z(AttributeOptionGroupUpdateView.get_titlec             C   s*   | j stj| jtd td}| j|S )Nz+Attribute Option Group updated successfullyz/dashboard:catalogue-attribute-option-group-list)r   r   r   rM   rJ   r	   r   )rA   r   r8   r8   r9   r   _  s    z.AttributeOptionGroupUpdateView.get_success_urlN)rg   rh   ri   r   r   r   r   r8   r8   r8   r9   r   T  s   r   c                   s,   e Zd ZdZeZeZdZ fddZ	  Z
S )AttributeOptionGroupListViewz4dashboard/catalogue/attribute_option_group_list.htmlZattribute_option_groupsc                s"   t  jf |}| jjj |d< |S )Nquerystring)r>   r?   rM   rN   r   )rA   rB   rC   )rD   r8   r9   r?   m  s    z-AttributeOptionGroupListView.get_context_data)rg   rh   ri   rk   r0   r   r!   rl   rm   r?   rn   r8   r8   )rD   r9   r   f  s
   r   c                   s8   e Zd ZdZeZeZ fddZdd Z	dd Z
  ZS )AttributeOptionGroupDeleteViewz6dashboard/catalogue/attribute_option_group_delete.htmlc                sx   t  jf |}td| jj |d< | jjj }|dkrhd|d< td| jj |d< tj| j	td|  | j	j
|d< |S )	Nz"Delete Attribute Option Group '%s'ra   r   Tr   zUnable to delete '%s'zG%i product attributes are still assigned to this attribute option groupZhttp_get_params)r>   r?   rJ   r   r   product_attributesr   r   rt   rM   rN   )rA   rB   rC   Zproduct_attribute_count)rD   r8   r9   r?   y  s    z/AttributeOptionGroupDeleteView.get_context_datac             C   sR   |g}| j jj }y
|d= W n tk
r0   Y nX |j rH||j g7 }dj|S )Ncsrfmiddlewaretokenr   )rM   r   copyKeyErrorr   r   )rA   r   r   Zhttp_post_paramsr8   r8   r9   r     s    
z7AttributeOptionGroupDeleteView.get_url_with_querystringc             C   s*   | j stj| jtd td}| j|S )Nz+Attribute Option Group deleted successfullyz/dashboard:catalogue-attribute-option-group-list)r   r   r   rM   rJ   r	   r   )rA   r   r8   r8   r9   r     s    z.AttributeOptionGroupDeleteView.get_success_url)rg   rh   ri   rk   r0   r   r   rc   r?   r   r   rn   r8   r8   )rD   r9   r   s  s   r   c               @   s   e Zd ZdZeZeZdZdS )OptionListViewz$dashboard/catalogue/option_list.htmloptionsN)	rg   rh   ri   rk   r1   r   r"   rl   rm   r8   r8   r8   r9   r     s   r   c                   s4   e Zd ZdZeZeZ fddZ fddZ	  Z
S )OptionCreateUpdateViewz$dashboard/catalogue/option_form.htmlc                s    t j| jtd t j||S )NzCYour submitted data was not valid - please correct the errors below)r   rt   rM   rJ   r>   r   )rA   r   rB   )rD   r8   r9   r     s    
z$OptionCreateUpdateView.forms_invalidc                s   t  jf |}| j |d< |S )Nra   )r>   r?   r   )rA   rB   rC   )rD   r8   r9   r?     s    z'OptionCreateUpdateView.get_context_data)rg   rh   ri   rk   r1   r   r   rc   r   r?   rn   r8   r8   )rD   r9   r     s
   r   c               @   s$   e Zd Zdd Zdd Zdd ZdS )OptionCreateViewc             C   s   d S )Nr8   )rA   r8   r8   r9   r     s    zOptionCreateView.get_objectc             C   s   t dS )NzAdd a new Option)rJ   )rA   r8   r8   r9   r     s    zOptionCreateView.get_titlec             C   s   t j| jtd td}|S )NzOption created successfullyzdashboard:catalogue-option-list)r   r   rM   rJ   r	   )rA   r   r8   r8   r9   r     s    z OptionCreateView.get_success_urlN)rg   rh   ri   r   r   r   r8   r8   r8   r9   r     s   r   c               @   s$   e Zd Zdd Zdd Zdd ZdS )OptionUpdateViewc             C   s   t t| jd d}|S )Nr4   )r4   )r   r1   rB   )rA   r   r8   r8   r9   r     s    zOptionUpdateView.get_objectc             C   s   t d| jj S )NzUpdate Option '%s')rJ   r   r   )rA   r8   r8   r9   r     s    zOptionUpdateView.get_titlec             C   s   t j| jtd td}|S )NzOption updated successfullyzdashboard:catalogue-option-list)r   r   rM   rJ   r	   )rA   r   r8   r8   r9   r     s    z OptionUpdateView.get_success_urlN)rg   rh   ri   r   r   r   r8   r8   r8   r9   r     s   r   c                   s,   e Zd ZdZeZ fddZdd Z  ZS )OptionDeleteViewz&dashboard/catalogue/option_delete.htmlc                s   t  jf |}td| jj |d< | jjj }| jjj }t||grd|d< td| jj |d< |r|t	j
| jtd|  |rt	j
| jtd|  |S )NzDelete Option '%s'ra   Tr   zUnable to delete '%s'z-%i products are still assigned to this optionz4%i product classes are still assigned to this option)r>   r?   rJ   r   r   product_setr   productclass_setrG   r   rt   rM   )rA   rB   rC   r<   Zproduct_classes)rD   r8   r9   r?     s     z!OptionDeleteView.get_context_datac             C   s   t j| jtd td}|S )NzOption deleted successfullyzdashboard:catalogue-option-list)r   r   rM   rJ   r	   )rA   r   r8   r8   r9   r     s    z OptionDeleteView.get_success_url)	rg   rh   ri   rk   r1   r   r?   r   rn   r8   r8   )rD   r9   r     s   r   N)r   r   r   r   r   r   r   r   )r   r   r   r   r   r   )r   r    r!   r"   )r#   r$   r%   )^django.confr   django.contribr   django.db.modelsr   django.httpr   django.shortcutsr   r   django.template.loaderr   django.urlsr	   django.utils.translationr
   rJ   Zdjango.viewsr   django_tables2r   r   oscar.core.loadingr   r   oscar.views.genericr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r'   r(   r)   r*   r+   r-   r.   r/   r0   r1   r:   r;   RedirectViewro   
UpdateViewrx   
DeleteViewr   ListViewr   r   
DetailViewr   r   r   
CreateViewr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r8   r8   r8   r9   <module>   s                  









T {L??)