a
    Cg                     @   s   d dl mZ G dd dZG dd deZG dd dZG dd	 d	eeZeejZG d
d deeZ	G dd deje	dZ
G dd deejjZG dd de	ejjZG dd de
ejedZdS )    )formsc                   @   s   e Zd ZdZdddZdS )Optionsa  
    An object that serves as a container for configuration options. When a class is defined using
    OptionCollectingMetaclass as its metaclass, any attributes defined on an inner `class Meta`
    will be copied to an Options instance which will then be accessible as the class attribute
    `_meta`.

    The base Options class has no functionality of its own, but exists so that specific
    configuration options can be defined as mixins and collectively merged in to either Options or
    another base class with the same interface such as django.forms.models.ModelFormOptions,
    to arrive at a final class that recognises the desired set of options.
    Nc                 C   s   d S N selfoptionsr   r   V/var/www/lab.imftr.de/x/nb_venv/lib/python3.9/site-packages/permissionedforms/forms.py__init__   s    zOptions.__init__)N)__name__
__module____qualname____doc__r
   r   r   r   r	   r      s   r   c                       s$   e Zd ZdZdZ fddZ  ZS )OptionCollectingMetaclassa%  
    Metaclass that handles inner `class Meta` definitions. When a class using
    OptionCollectingMetaclass defines an inner Meta class and an `options_class` attribute
    specifying an Options class, an Options object will be created from it and set as the class
    attribute `_meta`.
    Nc                    s0   t  | |||}| jr,| t|dd |_|S )NZMeta)super__new__options_classgetattr_meta)Zmcsnamebasesattrs	new_class	__class__r   r	   r      s    z!OptionCollectingMetaclass.__new__)r   r   r   r   r   r   __classcell__r   r   r   r	   r      s   r   c                       s"   e Zd ZdZd fdd	Z  ZS )PermissionedFormOptionsMixinz9Handles the field_permissions option for PermissionedFormNc                    s   t  | t|dd | _d S )Nfield_permissions)r   r
   r   r   r   r   r   r	   r
   &   s    z%PermissionedFormOptionsMixin.__init__)Nr   r   r   r   r
   r   r   r   r   r	   r   $   s   r   c                   @   s   e Zd ZdZdS )PermissionedFormOptionsz"Options class for PermissionedFormNr   r   r   r   r   r   r   r	   r   +   s   r   c                   @   s   e Zd ZdZeZdS )PermissionedFormMetaclassz
    Extends the django.forms.Form metaclass with support for an inner `class Meta` that accepts
    a `field_permissions` configuration option
    N)r   r   r   r   r   r   r   r   r   r	   r!   1   s   r!   c                       s&   e Zd ZdZdd fdd
Z  ZS )PermissionedFormax  
    An extension to `django.forms.Form` to accept an optional `for_user` keyword argument
    indicating the user the form will be presented to.
    
    Any fields named in the `field_permissions` dict in Meta will apply a permission test on the
    named permission using `User.has_perm`; if the user lacks that permission, the field will be
    omitted from the form.
    N)for_userc                   sJ   t  j|i | |rF| jjp i }| D ]\}}||s*| j|= q*d S r   )r   r
   r   r   itemsZhas_permfields)r   r#   argskwargsZfield_perms
field_namepermr   r   r	   r
   B   s    
zPermissionedForm.__init__r   r   r   r   r	   r"   9   s   r"   )	metaclassc                   @   s   e Zd ZdZdS )PermissionedModelFormOptionszp
    Options class for PermissionedModelForm; extends ModelForm's options to accept
    `field_permissions`
    Nr    r   r   r   r	   r+   K   s   r+   c                   @   s   e Zd ZdZeZdS )PermissionedModelFormMetaclassa  
    Metaclass for PermissionedModelForm; extends the ModelForm metaclass to use
    PermissionedModelFormOptions in place of ModelFormOptions and thus accept the
    `field_permissions` option.

    Note that because ModelForm does not participate in the OptionCollectingMetaclass logic, this
    has the slightly hacky effect of letting ModelFormMetaclass construct a ModelFormOptions object
    for the lifetime of ModelFormMetaclass.__new__, which we promptly throw out and recreate as a
    PermissionedModelFormOptions object.
    N)r   r   r   r   r+   r   r   r   r   r	   r,   R   s   
r,   c                   @   s   e Zd ZdZdS )PermissionedModelFormzQA ModelForm that implements the `for_user` keyword argument from PermissionedFormNr    r   r   r   r	   r-   `   s   r-   N)Zdjangor   r   typer   r   r   ZFormZFormMetaclassr!   r"   modelsZModelFormOptionsr+   ZModelFormMetaclassr,   Z	ModelFormr-   r   r   r   r	   <module>   s   
