
    h                         d dl 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  G d
 de      Z G d de      Zy)    N)	CMSPlugin)DjangoJSONEncoder)models)conditional_escape	mark_safe)gettext)gettext_lazy)TagTypeField)FRAMEWORK_PLUGIN_INFOc                        e Zd ZdZ G d d      Z ej                  d      Z ed      Z	 ej                  ee      Z fd	Z fd
Zd Zd ZddZd Zd Z fdZddZd Zed        Z xZS )AbstractFrontendUIItema  
    The `AbstractFrontendUIItem` class is an abstract base class that provides common functionality
    for frontend UI items in a CMS plugin. It is a subclass of `CMSPlugin` class.

    Use this class as a base class for custom plugins that add their own database fields.

    Attributes:
        - ui_item: A CharField that represents the UI item name (max length 30).
        - tag_type: A TagTypeField (custom field) that represents the type of HTML tag for the UI item.
        - config: A JSONField that stores additional configuration for the UI item.

    Methods:
        - __init__(*args, **kwargs): Constructor method that initializes the object and sets additional classes.
        - __getattr__(item): Allows properties of the plugin config to be accessed as plugin properties.
        - __str__(): Returns a string representation of the UI item.
        - add_classes(*args): Adds additional classes to the UI item.
        - add_attribute(attr, value=None): Adds an attribute to the configuration attributes.
        - get_attributes(): Returns the attributes as a string for rendering the UI item.
        - save(*args, **kwargs): Saves the UI item to the database.
        - initialize_from_form(form=None): Populates the config JSON field based on initial values from a form.
        - get_short_description(): Returns a plugin-specific short description.
        - framework_info: Returns the framework information for the UI item.

    Note: This is an abstract base class and should not be used directly.
    c                        e Zd ZdZ ed      Zy)AbstractFrontendUIItem.MetaTUI itemN)__name__
__module____qualname__abstract_verbose_name     I/home/dcms/DCMS/lib/python3.12/site-packages/djangocms_frontend/models.pyMetar   )   s    |r   r      )
max_lengthT)blank)defaultencoderc                 v    g | _         t        t        j                               | _        t	        |   |i | y N)_additional_classesstruuiduuid4super__init__selfargskwargs	__class__s      r   r'   zAbstractFrontendUIItem.__init__1   s/    #% 

%	$)&)r   c                 l    |d   dk7  r|| j                   v r| j                   |   S t        | 	  |      S )zAMakes properties of plugin config available as plugin properties.r   r   )configr&   __getattribute__)r)   itemr,   s     r   __getattr__z"AbstractFrontendUIItem.__getattr__6   s8    7c>ddkk1;;t$$w'--r   c                     d| j                   v r| j                   d   S t        | j                         dt        | j                         dS )N__str__z ())r.   r   ui_itemr#   pkr)   s    r   r3   zAbstractFrontendUIItem.__str__<   sB    #;;y))$,,'(3tww<.::r   c                     |D ]:  }|s| xj                   t        |t              r|j                         n|z  c_         < y r!   )r"   
isinstancer#   split)r)   r*   args      r   add_classesz"AbstractFrontendUIItem.add_classesA   s9     	YC((:c3;OCIIKUXX(	Yr   c                     | j                   j                  di       }|dk(  r||v r|||   z  }|j                  ||i       || j                   d<   y )N
attributesstyle)r.   getupdate)r)   attrvalueattrss       r   add_attributez$AbstractFrontendUIItem.add_attributeF   sO    b17?tu}U4[ EdE]#$)L!r   c                    | j                   j                  di       }| j                         }|rd| dnd}d |j                         D        }|dj	                  |      z   j                         }|rt        d|z         S dS )Nr>   zclass="" c              3   X   K   | ]"  \  }}|d k7  r|r| dt        |       dn|  $ yw)classz="rG   N)r   ).0r0   rC   s      r   	<genexpr>z8AbstractFrontendUIItem.get_attributes.<locals>.<genexpr>Q   s@      
ew 8=tfB)%013TFK
s   (* )r.   r@   get_classesitemsjoinstripr   )r)   r>   classespartsattributes_strings        r   get_attributesz%AbstractFrontendUIItem.get_attributesM   s    [[__\26
""$,3WWIQ'
)//1

 %sxx6==?5Fy001NBNr   c                     | j                   j                  di       }t        |j                  dd      j                               }|j	                  | j
                         t        dj                  |            S )Nr>   rJ   rH   rM   )r.   r@   setr:   rA   r"   r   rP   )r)   r>   rR   s      r   rN   z"AbstractFrontendUIItem.get_classesY   sZ    [[__\26
jnnWb1779:t//0!#((7"344r   c                 X    | j                   j                  | _        t        |   |i |S r!   )r,   r   r5   r&   saver(   s      r   rY   zAbstractFrontendUIItem.save_   s(    ~~..w|T,V,,r   c                    || j                         j                  }t        |t              r9t	        |j
                  dd      s| j                  |j
                  _         |       }t	        t	        |dd      di       j                  dd      }|D ]6  }| j                  j                  ||dk(  ri n||   j                  xs d       8 | S )	zVPopulates the config JSON field based on initial values provided by the fields of formNmodel_metaentangled_fieldsr.   r   r>   rH   )get_plugin_classformr9   typegetattrr\   r,   r[   r@   r.   
setdefaultinitial)r)   r_   r]   fields       r   initialize_from_formz+AbstractFrontendUIItem.initialize_from_formc   s    <((*//DdD!4::w5#'>>

 6D"74$#?ASUWX\\]egij% 	fEKK""50E"4PU;K^K^Kdbde	fr   c                 :    | j                   j                  dd      S )z[Plugin-specific short description (to be defined by subclasses). Try title attribute first.titlerH   )r.   r@   r7   s    r   get_short_descriptionz,AbstractFrontendUIItem.get_short_descriptionp   s    {{w++r   c                 V    t        j                  | j                  j                  d       S r!   )r   r@   r,   r   r7   s    r   framework_infoz%AbstractFrontendUIItem.framework_infot   s    $(()@)@$GGr   r!   )r   r   r   __doc__r   r   	CharFieldr5   r
   tag_type	JSONFielddictr   r.   r'   r1   r3   r<   rE   rU   rN   rY   re   rh   propertyrj   __classcell__)r,   s   @r   r   r      s    4$ $ f"-G$'HVd4EFF*
.;
Y
*
O5-, H Hr   r   c                   $    e Zd ZdZ G d d      Zy)FrontendUIItema  

    Class: FrontendUIItem

    Inherits From: AbstractFrontendUIItem

    Description:
    This class represents a UI item in the frontend. It is used to define the behavior and attributes
    of a UI item in the user interface.

    Use this class as a base class for custom plugins that do not add their own database fields but
    use the entangled form fields instead.

    Attributes:
    - verbose_name (str): The verbose name of the UI item.

    Methods:
    None

    c                       e Zd Z ed      Zy)FrontendUIItem.Metar   N)r   r   r   r   r   r   r   r   r   ru      s    |r   r   N)r   r   r   rk   r   r   r   r   rs   rs   y   s    *$ $r   rs   )r$   
cms.modelsr   django.core.serializers.jsonr   	django.dbr   django.utils.htmlr   r   django.utils.translationr   r	   r   djangocms_frontend.fieldsr
   djangocms_frontend.settingsr   r   rs   r   r   r   <module>r}      s>       :  ; , 6 2 =hHY hHV$+ $r   