
    \j9                         d dl mZ d dlmZ d dlmZ ddlmZ  G d d      Z G d d	ee      Z	 G d
 d      Z
 G d de
ee	      Zy)    )ImproperlyConfigured)Model)	ModelBase   )add_permc                   "     e Zd ZdZ fdZ xZS )RulesModelBaseMixina  
    Mixin for the metaclass of Django's Model that allows declaring object-level
    permissions in the model's Meta options.

    If set, the Meta attribute "rules_permissions" has to be a dictionary with
    permission types (like "add" or "change") as keys and predicates (like
    rules.is_staff) as values. Permissions are then registered with the rules
    framework automatically upon Model creation.

    This mixin can be used for creating custom metaclasses.
    c                    |j                  d      }t        |d      rJ|j                  }|`t        |t              st        d|dt        |      d      |j                         }ni }t        
| $  | |||fi |}||j                  _        |j                  |       |j                         D ]   \  }}	t        |j                  |      |	       " |S )NMetarules_permissionsz%The rules_permissions Meta option of z must be a dict, not .)gethasattrr   
isinstancedictr   typecopysuper__new___metapreprocess_rules_permissionsitemsr   get_perm)clsnamebasesattrskwargs
model_metaperms	new_class	perm_type	predicate	__class__s             >/root/env/lib/python3.12/site-packages/rules/contrib/models.pyr   zRulesModelBaseMixin.__new__   s    YYv&
:2300E,eT**T%[*  JJLEEGOCueFvF	,1	)..u5$)KKM 	? IyY''	2I>	?    )__name__
__module____qualname____doc__r   __classcell__)r$   s   @r%   r	   r	      s    
 r&   r	   c                       e Zd ZdZy)RulesModelBasezQ
    A subclass of Django's ModelBase with the RulesModelBaseMixin mixed in.
    N)r'   r(   r)   r*    r&   r%   r-   r-   +   s    r&   r-   c                   0    e Zd ZdZed        Zed        Zy)RulesModelMixina  
    A mixin for Django's Model that adds hooks for stepping into the process of
    permission registration, which are called by the metaclass implementation in
    RulesModelBaseMixin.

    Use this mixin in a custom subclass of Model in order to change its behavior.
    c                 f    | j                   j                  d|d| j                   j                  S )zConverts permission type ("add") to permission name ("app.add_modelname")

        :param perm_type: "add", "change", etc., or custom value
        :type  perm_type: str
        :returns str:
        r   _)r   	app_label
model_name)r   r"   s     r%   r   zRulesModelMixin.get_perm:   s%     !YY00)SYY=Q=QRRr&   c                      y)a;  May alter a permissions dict before it's processed further.

        Use this, for instance, to alter the supplied permissions or insert default
        values into the given dict.

        :param perms:
            Shallow-copied value of the rules_permissions model Meta option
        :type  perms: dict
        Nr.   )r   r    s     r%   r   z,RulesModelMixin.preprocess_rules_permissionsD   s    r&   N)r'   r(   r)   r*   classmethodr   r   r.   r&   r%   r0   r0   1   s1     S S 	 	r&   r0   c                   $    e Zd ZdZ G d d      Zy)
RulesModela  
    An abstract model with RulesModelMixin mixed in, using RulesModelBase as metaclass.

    Use this as base for your models directly if you don't need to customize the
    behavior of RulesModelMixin and thus don't want to create a custom base class.
    c                       e Zd ZdZy)RulesModel.MetaTN)r'   r(   r)   abstractr.   r&   r%   r   r:   Y   s    r&   r   N)r'   r(   r)   r*   r   r.   r&   r%   r8   r8   Q   s     r&   r8   )	metaclassN)django.core.exceptionsr   django.db.modelsr   django.db.models.baser   permissionsr   r	   r-   r0   r8   r.   r&   r%   <module>rA      sF    7 " + "   F()  @	%> 	r&   