a
    ùÜEb=  ã                   @   sZ   d dl Z d dlmZ d dlmZmZ d dlmZ d dl	m
Z
 e  e¡ZG dd„ deƒZdS )é    N)Úsettings)Ú
ForeignKeyÚSET_NULL)Úgettext_lazy)Úget_current_userc                       s6   e Zd ZdZedƒZd‡ fdd„	Z‡ fdd„Z‡  ZS )	ÚUserForeignKeyzÊModel field UserForeignKey

    Saves a reference to a user. By using the `django_userforeignkey.middleware.UserForeignKeyMiddleware`
    middleware, the field can save the reference automatically.
    ZUserFc                    sb   || | _ | _tj|d< |s |rHt|d< d|d< d|d< | dd¡|d< tt| ƒjf i |¤Ž d S )NÚtoZ	on_deleteTÚnullÚblankÚeditableF)	Ú	auto_userÚauto_user_addr   ZAUTH_USER_MODELr   ÚgetÚsuperr   Ú__init__)Úselfr   r   Úkwargs©Ú	__class__© úc/home/ja/django-apps/lartica_env/lib/python3.9/site-packages/django_userforeignkey/models/fields.pyr      s    
zUserForeignKey.__init__c                    s\   |   |¡}tƒ }|jr:| js*| jr:|r:|s:t|| j|ƒ n|jsJt d¡ t	t
| ƒ ||¡S )Nz-Cannot save reference for non-persistent user)Zvalue_from_objectr   Úpkr   r   ÚsetattrÚnameÚloggerÚinfor   r   Úpre_save)r   Zmodel_instanceÚaddZobject_userZcurrent_userr   r   r   r   "   s    

zUserForeignKey.pre_save)FF)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú_Údescriptionr   r   Ú__classcell__r   r   r   r   r      s   r   )ÚloggingZdjango.confr   Zdjango.db.modelsr   r   Zdjango.utils.translationr   r"   Zdjango_userforeignkey.requestr   Ú	getLoggerr   r   r   r   r   r   r   Ú<module>   s   
