o
    g                     @   s   d dl mZ ddlmZmZmZmZmZ d dlm	Z	m
Z
 d dlmZ G dd dejZG dd	 d	ejZG d
d dejZG dd dejZG dd dejZG dd dejZG dd dejZG dd dejZdS )    )forms   )CartShippingAddressOrder	OrderItemShippingMethod)ProductCategory)Userc                       sL   e Zd ZdZejejdZejddejddddd	Z	 fd
dZ
  ZS )AddToCartFormzV
    Form for adding products to the cart with validation for stock availability.
    )widgetr   Quantityform-controlzEnter quantityclassplaceholderattrs)	min_valuelabelr   c                    s`   t   }|d}|d}tjj|d }|std||j	kr.td|j	 d|S )N
product_idquantity)idz$The selected product does not exist.zOnly z units available.)
supercleangetr	   objectsfilterfirstr   ValidationErrorstock_quantity)selfcleaned_datar   r   product	__class__ &/var/www/html/hyperkenya/shop/forms.pyr      s   




zAddToCartForm.clean)__name__
__module____qualname____doc__r   IntegerFieldHiddenInputr   NumberInputr   r   __classcell__r'   r'   r%   r(   r      s    r   c                   @   s6   e Zd ZdZejdejddddddZd	d
 ZdS )GuestCheckoutFormzg
    Form for guest users to provide an email during checkout, with validation for existing users.
    zEmail Addressr   zEnter your emailr   r   T)r   r   requiredc                 C   s*   | j d }tjj|d rtd|S )Nemail)r3   zAThis email is associated with an existing account. Please log in.)r#   r   r   r   existsr   r    )r"   r3   r'   r'   r(   clean_email,   s   

zGuestCheckoutForm.clean_emailN)	r)   r*   r+   r,   r   
EmailField
EmailInputr3   r5   r'   r'   r'   r(   r1   "   s    r1   c                   @      e Zd ZdZG dd dZdS )ShippingAddressFormz>
    Form for capturing shipping details during checkout.
    c                   @   sl   e Zd ZeZg dZejddddejddddejddddejddddejd	diddZ	d
S )zShippingAddressForm.Meta)address_line_1address_line_2citypostal_codecountryr   zAddress Line 1r   r   zAddress Line 2 (optional)CityzPostal Coder   N)
r)   r*   r+   r   modelfieldsr   	TextInputSelectwidgetsr'   r'   r'   r(   Meta7   s    
rE   Nr)   r*   r+   r,   rE   r'   r'   r'   r(   r9   3       r9   c                   @   s.   e Zd ZdZejejjddej	dddZ
dS )ShippingMethodFormz?
    Form for selecting a shipping method during checkout.
    T)	is_activezShipping MethodN)querysetr   r   empty_label)r)   r*   r+   r,   r   ModelChoiceFieldr   r   r   RadioSelectshipping_methodr'   r'   r'   r(   rH   C   s    
rH   c                   @   s.   e Zd ZdZG dd dZdd Zdd ZdS )	OrderItemFormzV
    Form for managing items in an order, with validation for quantity and price.
    c                   @   s>   e Zd ZeZg dZejddddejdddddZdS )	zOrderItemForm.Meta)r$   r   pricer   r   r   r   Price)r   rP   N)	r)   r*   r+   r   r@   rA   r   r/   rD   r'   r'   r'   r(   rE   T   s    
rE   c                 C   s"   | j d}|dkrtd|S )Nr   r   zQuantity must be at least 1.)r#   r   r   r    )r"   r   r'   r'   r(   clean_quantity\   s   
zOrderItemForm.clean_quantityc                 C   s4   | j d}| j d}|r||jkrtd|S )NrP   r$   z'Invalid price for the selected product.)r#   r   selling_pricer   r    )r"   rP   r$   r'   r'   r(   clean_priceb   s
   
zOrderItemForm.clean_priceN)r)   r*   r+   r,   rE   rR   rT   r'   r'   r'   r(   rO   P   s
    rO   c                   @   r8   )	OrderFormz.
    Form for creating or editing orders.
    c                   @   sH   e Zd ZeZg dZejddidejddidejddiddZdS )zOrderForm.Meta)shipping_addressrN   statusr   r   r   N)	r)   r*   r+   r   r@   rA   r   rC   rD   r'   r'   r'   r(   rE   n   s    
rE   NrF   r'   r'   r'   r(   rU   j   rG   rU   c                       s   e Zd ZdZejddejdddddZejd	dg ej	d
diddZ
ejdddejdddddZejdddejdddddZ fddZ fddZ  ZS )
SearchFormz<
    Form for searching products with optional filters.
    SearchFr   zSearch for products...r   r   )r   r2   r   r
   r   )r   r2   choicesr   z	Min Pricer   zMinimum price)r   r2   r   r   z	Max PricezMaximum pricec                    s8   t  j|i | dgdd tj D  | jd _dS )z8
        Dynamically populate category choices.
        ) zAll Categoriesc                 S   s   g | ]}|j |jfqS r'   )r   name).0categoryr'   r'   r(   
<listcomp>   s    z'SearchForm.__init__.<locals>.<listcomp>r^   N)r   __init__r
   r   allrA   rZ   )r"   argskwargsr%   r'   r(   r`      s   
zSearchForm.__init__c                    sD   t   }|d}|d}|d ur |d ur ||kr td|S )N	min_price	max_pricez3Minimum price cannot be greater than maximum price.)r   r   r   r   r    )r"   r#   rd   re   r%   r'   r(   r      s   



zSearchForm.clean)r)   r*   r+   r,   r   	CharFieldrB   queryChoiceFieldrC   r^   DecimalFieldr/   rd   re   r`   r   r0   r'   r'   r%   r(   rX   y   s6    	rX   c                   @   r8   )rH   z:
    Form for creating and updating shipping methods.
    c                   @   s   e Zd ZeZg dZdS )zShippingMethodForm.Meta)r\   descriptioncostrI   N)r)   r*   r+   r   r@   rA   r'   r'   r'   r(   rE      s    rE   NrF   r'   r'   r'   r(   rH      rG   N)djangor   modelsr   r   r   r   r   inventory.modelsr	   r
   django.contrib.auth.modelsr   Formr   r1   	ModelFormr9   rH   rO   rU   rX   r'   r'   r'   r(   <module>   s    0