a
    Dge                     @   s  d dl mZ d dlmZ d dlmZ d dlmZ d dlm	Z	 d dl
mZ d dlmZ d dlm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mZ d dl m!Z! d dl"m#Z# d dl$m%Z% ddl&m'Z'm(Z( G dd de'Z)G dd de(Z*dS )    )warn)get_permission_codename)
Permission)ContentType)ImproperlyConfigured)modelform_factory)redirect)path)cached_property)capfirst)hooks)ModelAdminURLFinderregister_admin_url_finder)
ObjectList)generic)historyusage)ReferenceIndex)ModelPermissionPolicy)RemovedInWagtail70Warning   )ViewSetViewSetGroupc                       s  e Zd ZdZdZejZejZ	ej
ZejZejZejZejZejZdZdZdZdZg Zg ZdZdx fdd	Ze d	d
 Z!e"dd Z# fddZ$dd Z%dd Z&dd Z'dd Z(dd Z)dd Z*dd Z+dd Z,e dd  Z-e d!d" Z.e d#d$ Z/e d%d& Z0e d'd( Z1e d)d* Z2e d+d, Z3e d-d. Z4e d/d0 Z5e d1d2 Z6e d3d4 Z7e d5d6 Z8dyd8d9Z9e"d:d; Z:e"d<d= Z;e"d>d? Z<e"d@dA Z=e"dBdC Z>e"dDdE Z?e"dFdG Z@e"dHdI ZAe"dJdK ZBe"dLdM ZCe"dNdO ZDe"dPdQ ZEe"dRdS ZFe"dTdU ZGe"dVdW ZHe"dXdY ZIe"dZd[ ZJd\d] ZKdzd^d_ZLd`da ZMdbdc ZNddde ZOe"dfdg ZPe dhdi ZQdjdk ZRdldm ZSdndo ZTdpdq ZUdrds ZVe"dtdu ZW fdvdwZX  ZYS ){ModelViewSeta	  
    A viewset to allow listing, creating, editing and deleting model instances.

    All attributes and methods from :class:`~wagtail.admin.viewsets.base.ViewSet`
    are available.

    For more information on how to use this class, see :ref:`generic_views`.
    T    NFc                    sJ   t  jf d|i| | js(td|  | jj| _| jj| _| jj| _d S )NnamezJModelViewSet %r must define a `model` attribute or pass a `model` argument)super__init__modelr   _meta
model_opts	app_label
model_name)selfr   kwargs	__class__ [/var/www/lab.imftr.de/x/nb_venv/lib/python3.9/site-packages/wagtail/admin/viewsets/model.pyr   c   s    

zModelViewSet.__init__c                 C   s
   t | jS N)r   r   r$   r(   r(   r)   permission_policyo   s    zModelViewSet.permission_policyc                 C   s   | j S )z
        Viewset name, to use as the URL prefix and namespace.
        Defaults to the :attr:`~django.db.models.Options.model_name`.
        )r#   r+   r(   r(   r)   r   s   s    zModelViewSet.namec                    s   t  jf i | j| j| d| d| d| d| d| d| d| jd
|}| jrp| d	|d
< | jr| d|d< |S )Nindexindex_resultsr   r   addeditdelete)
r   r,   Zindex_url_nameZindex_results_url_nameZhistory_url_nameZusage_url_nameZadd_url_nameedit_url_nameZdelete_url_nameheader_iconcopyZcopy_url_nameinspectZinspect_url_name)r   get_common_view_kwargsr   r,   get_url_nameZiconcopy_view_enabledinspect_view_enabledr$   r%   Zview_kwargsr&   r(   r)   r6   {   s(    
z#ModelViewSet.get_common_view_kwargsc                 K   sJ   | j | j| j| j| j| j| j| j| j| j	| j
d|}| jrF| j|d< |S )N)template_nameresults_template_namelist_displaylist_filterlist_exportexport_headingsexport_filenamefilterset_classsearch_fieldssearch_backend_nameZpaginate_byZdefault_ordering)index_template_nameindex_results_template_namer=   r>   r?   r@   rA   rB   rC   rD   list_per_pageorderingr:   r(   r(   r)   get_index_view_kwargs   s"    
z"ModelViewSet.get_index_view_kwargsc                 K   s   | j |  | jd|S )NZpanelZ
form_classr;   )_edit_handlerget_form_classcreate_template_namer$   r%   r(   r(   r)   get_add_view_kwargs   s    z ModelViewSet.get_add_view_kwargsc                 K   s   | j | jdd| jd|S )NT)
for_updaterJ   )rK   rL   edit_template_namerN   r(   r(   r)   get_edit_view_kwargs   s    
z!ModelViewSet.get_edit_view_kwargsc                 K   s   d| j i|S )Nr;   )delete_template_namerN   r(   r(   r)   get_delete_view_kwargs   s    z#ModelViewSet.get_delete_view_kwargsc                 K   s   | j | ddd|S )Nhistory_resultsr   )r;   Zhistory_results_url_namer3   )history_template_namer7   rN   r(   r(   r)   get_history_view_kwargs   s    z$ModelViewSet.get_history_view_kwargsc                 K   s   d| j d| jjdi|S )Nr;   r   fallback)get_templatesusage_view_classr;   rN   r(   r(   r)   get_usage_view_kwargs   s    z"ModelViewSet.get_usage_view_kwargsc                 K   s   | j | j| jd|S )N)r;   fieldsZfields_exclude)inspect_template_nameinspect_view_fieldsinspect_view_fields_excluderN   r(   r(   r)   get_inspect_view_kwargs   s    z$ModelViewSet.get_inspect_view_kwargsc                 K   s   | j f i |S r*   )rO   rN   r(   r(   r)   get_copy_view_kwargs   s    z!ModelViewSet.get_copy_view_kwargsc                 C   s   | j | jfi |  S r*   construct_viewindex_view_classrI   r+   r(   r(   r)   
index_view   s
    zModelViewSet.index_viewc                 C   s    | j | jfi |  ddiS NZresults_onlyTrc   r+   r(   r(   r)   index_results_view   s    zModelViewSet.index_results_viewc                 C   s   | j | jfi |  S r*   )rd   add_view_classrO   r+   r(   r(   r)   add_view   s    zModelViewSet.add_viewc                 C   s   | j | jfi |  S r*   )rd   edit_view_classrR   r+   r(   r(   r)   	edit_view   s    zModelViewSet.edit_viewc                 C   s   | j | jfi |  S r*   )rd   delete_view_classrT   r+   r(   r(   r)   delete_view   s
    zModelViewSet.delete_viewc                    s    fdd}|S )Nc                    s(   t d jj td t d|ddS )NzQ%s's `/<pk>/` edit view URL pattern has been deprecated in favour of /edit/<pk>/.categoryr0   TZ	permanentr   r'   __name__r   r   r7   requestpkr+   r(   r)   redirect_to_edit   s    z<ModelViewSet.redirect_to_edit_view.<locals>.redirect_to_editr(   )r$   rw   r(   r+   r)   redirect_to_edit_view   s    z"ModelViewSet.redirect_to_edit_viewc                    s    fdd}|S )Nc                    s(   t d jj td t d|ddS )Nz\%s's `/<pk>/delete/` delete view URL pattern has been deprecated in favour of /delete/<pk>/.ro   r1   Trq   rr   rt   r+   r(   r)   redirect_to_delete   s    z@ModelViewSet.redirect_to_delete_view.<locals>.redirect_to_deleter(   )r$   ry   r(   r+   r)   redirect_to_delete_view   s    z$ModelViewSet.redirect_to_delete_viewc                 C   s   | j | jfi |  S r*   rd   history_view_classrW   r+   r(   r(   r)   history_view  s
    zModelViewSet.history_viewc                 C   s    | j | jfi |  ddiS rg   r{   r+   r(   r(   r)   history_results_view  s    z!ModelViewSet.history_results_viewc                 C   s   | j | jfi |  S r*   )rd   r[   r\   r+   r(   r(   r)   
usage_view  s
    zModelViewSet.usage_viewc                 C   s   | j | jfi |  S r*   )rd   inspect_view_classra   r+   r(   r(   r)   inspect_view  s
    zModelViewSet.inspect_viewc                 C   s   | j | jfi |  S r*   )rd   copy_view_classrb   r+   r(   r(   r)   	copy_view%  s    zModelViewSet.copy_viewr-   c                 C   sd   | j s|gS | j  | j d| j d| d| j  | j d| d| j  | dg}|r`|| |S )z
        Utility function that provides a list of templates to try for a given
        view, when the template isn't overridden by one of the template
        attributes on the class.
        /z.html)template_prefixr"   r#   append)r$   r   rY   Z	templatesr(   r(   r)   rZ   )  s    
zModelViewSet.get_templatesc                 C   s   | j d| jjdS )ac  
        A template to be used when rendering ``index_view``.

        Default: if :attr:`template_prefix` is specified, an ``index.html``
        template in the prefix directory and its ``{app_label}/{model_name}/``
        or ``{app_label}/`` subdirectories will be used. Otherwise, the
        ``index_view_class.template_name`` will be used.
        r-   rX   )rZ   re   r;   r+   r(   r(   r)   rE   :  s    
z ModelViewSet.index_template_namec                 C   s   | j d| jjdS )az  
        A template to be used when rendering ``index_results_view``.

        Default: if :attr:`template_prefix` is specified, a ``index_results.html``
        template in the prefix directory and its ``{app_label}/{model_name}/``
        or ``{app_label}/`` subdirectories will be used. Otherwise, the
        ``index_view_class.results_template_name`` will be used.
        r.   rX   )rZ   re   r<   r+   r(   r(   r)   rF   I  s    
z(ModelViewSet.index_results_template_namec                 C   s   | j d| jjdS )a_  
        A template to be used when rendering ``add_view``.

        Default: if :attr:`template_prefix` is specified, a ``create.html``
        template in the prefix directory and its ``{app_label}/{model_name}/``
        or ``{app_label}/`` subdirectories will be used. Otherwise, the
        ``add_view_class.template_name`` will be used.
        createrX   )rZ   ri   r;   r+   r(   r(   r)   rM   X  s    
z!ModelViewSet.create_template_namec                 C   s   | j d| jjdS )a`  
        A template to be used when rendering ``edit_view``.

        Default: if :attr:`template_prefix` is specified, an ``edit.html``
        template in the prefix directory and its ``{app_label}/{model_name}/``
        or ``{app_label}/`` subdirectories will be used. Otherwise, the
        ``edit_view_class.template_name`` will be used.
        r0   rX   )rZ   rk   r;   r+   r(   r(   r)   rQ   g  s    
zModelViewSet.edit_template_namec                 C   s   | j d| jjdS )am  
        A template to be used when rendering ``delete_view``.

        Default: if :attr:`template_prefix` is specified, a ``confirm_delete.html``
        template in the prefix directory and its ``{app_label}/{model_name}/``
        or ``{app_label}/`` subdirectories will be used. Otherwise, the
        ``delete_view_class.template_name`` will be used.
        Zconfirm_deleterX   )rZ   rm   r;   r+   r(   r(   r)   rS   v  s    
z!ModelViewSet.delete_template_namec                 C   s   | j d| jjdS )ah  
        A template to be used when rendering ``history_view``.

        Default: if :attr:`template_prefix` is specified, a ``history.html``
        template in the prefix directory and its ``{app_label}/{model_name}/``
        or ``{app_label}/`` subdirectories will be used. Otherwise, the
        ``history_view_class.template_name`` will be used.
        r   rX   )rZ   r|   r;   r+   r(   r(   r)   rV     s    
z"ModelViewSet.history_template_namec                 C   s   | j d| jjdS )ai  
        A template to be used when rendering ``inspect_view``.

        Default: if :attr:`template_prefix` is specified, an ``inspect.html``
        template in the prefix directory and its ``{app_label}/{model_name}/``
        or ``{app_label}/`` subdirectories will be used. Otherwise, the
        ``inspect_view_class.template_name`` will be used.
        r5   rX   )rZ   r   r;   r+   r(   r(   r)   r^     s    
z"ModelViewSet.inspect_template_namec                 C   s   | j jS )a@  
        A list or tuple, where each item is either:

        - The name of a field on the model;
        - The name of a callable or property on the model that accepts a single
          parameter for the model instance; or
        - An instance of the ``wagtail.admin.ui.tables.Column`` class.

        If the name refers to a database field, the ability to sort the listing
        by the database column will be offerred and the field's verbose name
        will be used as the column header.

        If the name refers to a callable or property, an ``admin_order_field``
        attribute can be defined on it to point to the database column for
        sorting. A ``short_description`` attribute can also be defined on the
        callable or property to be used as the column header.

        This list will be passed to the ``list_display`` attribute of the index
        view. If left unset, the ``list_display`` attribute of the index view
        will be used instead, which by default is defined as
        ``["__str__", wagtail.admin.ui.tables.UpdatedAtColumn()]``.
        )re   r=   r+   r(   r(   r)   r=     s    zModelViewSet.list_displayc                 C   s   | j jS )a  
        A list or tuple, where each item is the name of model fields of type
        ``BooleanField``, ``CharField``, ``DateField``, ``DateTimeField``,
        ``IntegerField`` or ``ForeignKey``.
        Alternatively, it can also be a dictionary that maps a field name to a
        list of lookup expressions.
        This will be passed as django-filter's ``FilterSet.Meta.fields``
        attribute. See
        `its documentation <https://django-filter.readthedocs.io/en/stable/guide/usage.html#generating-filters-with-meta-fields>`_
        for more details.
        If ``filterset_class`` is set, this attribute will be ignored.
        )re   r>   r+   r(   r(   r)   r>     s    zModelViewSet.list_filterc                 C   s   | j S )a  
        A subclass of ``wagtail.admin.filters.WagtailFilterSet``, which is a
        subclass of `django_filters.FilterSet <https://django-filter.readthedocs.io/en/stable/ref/filterset.html>`_.
        This will be passed to the ``filterset_class`` attribute of the index view.
        )	UNDEFINEDr+   r(   r(   r)   rB     s    zModelViewSet.filterset_classc                 C   s   | j jS )z
        The fields to use for the search in the index view.
        If set to ``None`` and :attr:`search_backend_name` is set to use a Wagtail search backend,
        the ``search_fields`` attribute of the model will be used instead.
        )re   rC   r+   r(   r(   r)   rC     s    zModelViewSet.search_fieldsc                 C   s   | j jS )z
        The name of the Wagtail search backend to use for the search in the index view.
        If set to a falsy value, the search will fall back to use Django's QuerySet API.
        )re   rD   r+   r(   r(   r)   rD     s    z ModelViewSet.search_backend_namec                 C   s   | j jS )z
        A list or tuple, where each item is the name of a field, an attribute,
        or a single-argument callable on the model to be exported.
        )re   r?   r+   r(   r(   r)   r?     s    zModelViewSet.list_exportc                 C   s   | j jS )zr
        A dictionary of export column heading overrides in the format
        ``{field_name: heading}``.
        )re   r@   r+   r(   r(   r)   r@     s    zModelViewSet.export_headingsc                 C   s
   | j jjS )z
        The base file name for the exported listing, without extensions.
        If unset, the model's :attr:`~django.db.models.Options.db_table` will be
        used instead.
        )r   r    Zdb_tabler+   r(   r(   r)   rA     s    zModelViewSet.export_filenamec                 C   s   t | jjS r*   )r   r!   Zverbose_name_pluralr+   r(   r(   r)   
menu_label   s    zModelViewSet.menu_labelc                    s4   ddl m}  fdd}t jj d|fd|iS )Nr   )MenuItemc                    s    j |j jjS r*   )r,   Zuser_has_any_permissionuserre   Zany_permission_required)_selfru   r+   r(   r)   is_shown  s    
z.ModelViewSet.menu_item_class.<locals>.is_shownr   r   )Zwagtail.admin.menur   typer   rs   )r$   r   r   r(   r+   r)   menu_item_class  s    zModelViewSet.menu_item_classc                 K   s   |j f i |S r*   )Z	formfield)r$   Zdb_fieldr%   r(   r(   r)   formfield_for_dbfield  s    z"ModelViewSet.formfield_for_dbfieldc                 C   sL   | j r| j  S |  }|  }|du r8|du r8tdt| j| j||dS )zL
        Returns the form class to use for the create / edit forms.
        NzaSubclasses of ModelViewSet must specify 'get_form_class', 'form_fields' or 'exclude_form_fields'.)Zformfield_callbackr]   exclude)rK   rL   get_form_fieldsget_exclude_form_fieldsr   r   r   r   )r$   rP   r]   r   r(   r(   r)   rL     s    
zModelViewSet.get_form_classc                 C   s   t | ddS )zc
        Returns a list or tuple of field names to be included in the create / edit forms.
        Zform_fieldsNgetattrr+   r(   r(   r)   r   /  s    zModelViewSet.get_form_fieldsc                 C   s   t | ddS )ze
        Returns a list or tuple of field names to be excluded from the create / edit forms.
        Zexclude_form_fieldsNr   r+   r(   r(   r)   r   5  s    z$ModelViewSet.get_exclude_form_fieldsc                 C   sp   t | dr| j}nRt | dr,| j}t|}n8t | jdrB| jj}n"t | jdr`| jj}t|}ndS || jS )a  
        Returns the appropriate edit handler for this ``ModelViewSet`` class.
        It can be defined either on the model itself or on the ``ModelViewSet``,
        as the ``edit_handler`` or ``panels`` properties. If none of these are
        defined, it will return ``None`` and the form will be constructed as
        a Django form using :meth:`get_form_class` (without using
        :ref:`forms_panels_overview`).
        edit_handlerpanelsN)hasattrr   r   r   r   Zbind_to_model)r$   r   r   r(   r(   r)   get_edit_handler;  s    	




zModelViewSet.get_edit_handlerc                 C   s   |   S )zj
        An edit handler that has been bound to the model class,
        to be used across views.
        )r   r+   r(   r(   r)   rK   R  s    zModelViewSet._edit_handlerc                 C   s   t dtf| j| ddS )NZ_ModelAdminURLFinderr0   )r,   r2   )r   r   r,   r7   r+   r(   r(   r)   url_finder_classZ  s    zModelViewSet.url_finder_classc                 C   s   t | j| j d S r*   )r   r   r   r+   r(   r(   r)   r   e  s    z&ModelViewSet.register_admin_url_finderc                 C   s   | j rt| j d S r*   )add_to_reference_indexr   Zregister_modelr   r+   r(   r(   r)   register_reference_indexh  s    z%ModelViewSet.register_reference_indexc                 C   s:   t j| j}tjj|d}| js6|jtd| j	d}|S )aR  
        Returns a queryset of :class:`~django.contrib.auth.models.Permission`
        objects to be registered with the :ref:`register_permissions` hook. By
        default, it returns all permissions for the model if
        :attr:`inspect_view_enabled` is set to ``True``. Otherwise, the "view"
        permission is excluded.
        )content_typeview)codename)
r   ZobjectsZget_for_modelr   r   filterr9   r   r   r!   )r$   r   Zpermissionsr(   r(   r)   get_permissions_to_registerl  s    
z(ModelViewSet.get_permissions_to_registerc                 C   s   t d| j d S )Nregister_permissions)r   registerr   r+   r(   r(   r)   r   }  s    z!ModelViewSet.register_permissionsc                 C   s   t d| jddt d| jddt d| jddt d| jd	dt d
| jddt d| jddt d| jddt d| jddg}| j	r|
t d| jdd | jr|
t d| jdd || j7 }|S )Nr   r-   )r   zresults/r.   znew/r/   zedit/<str:pk>/r0   zdelete/<str:pk>/r1   zhistory/<str:pk>/r   zhistory-results/<str:pk>/rU   zusage/<str:pk>/r   zinspect/<str:pk>/r5   zcopy/<str:pk>/r4   )r	   rf   rh   rj   rl   rn   r}   r~   r   r9   r   r   r8   r   _legacy_urlpatterns)r$   Zurlpatternsr(   r(   r)   get_urlpatterns  s*    
zModelViewSet.get_urlpatternsc                 C   s   t d| jt d| jgS )Nz	<str:pk>/z<str:pk>/delete/)r	   rx   rz   r+   r(   r(   r)   r     s    

z ModelViewSet._legacy_urlpatternsc                    s&   t    |   |   |   d S r*   )r   on_registerr   r   r   r+   r&   r(   r)   r     s    
zModelViewSet.on_register)N)r-   r   )F)Zrs   
__module____qualname____doc__r   r   Z	IndexViewre   Z
CreateViewri   ZEditViewrk   Z
DeleteViewrm   r   ZHistoryViewr|   r   Z	UsageViewr[   ZCopyViewr   ZInspectViewr   r   rG   rH   r9   r_   r`   r8   r   propertyr,   r
   r   r6   rI   rO   rR   rT   rW   r\   ra   rb   rf   rh   rj   rl   rn   rx   rz   r}   r~   r   r   r   rZ   rE   rF   rM   rQ   rS   rV   r^   r=   r>   rB   rC   rD   r?   r@   rA   r   r   r   rL   r   r   r   rK   r   r   r   r   r   r   r   r   __classcell__r(   r(   r&   r)   r      s   




































r   c                   @   s$   e Zd ZdZdd Zedd ZdS )ModelViewSetGroupz
    A container for grouping together multiple
    :class:`~wagtail.admin.viewsets.model.ModelViewSet` instances.

    All attributes and methods from
    :class:`~wagtail.admin.viewsets.base.ViewSetGroup` are available.
    c                 C   s,   | j D ] }t|dd }rt|  S qdS )Nr"   r   )Zregisterablesr   r   )r$   instancer"   r(   r(   r)   get_app_label_from_subitems  s    
z-ModelViewSetGroup.get_app_label_from_subitemsc                 C   s   |   S r*   )r   r+   r(   r(   r)   r     s    zModelViewSetGroup.menu_labelN)rs   r   r   r   r   r
   r   r(   r(   r(   r)   r     s   r   N)+warningsr   Zdjango.contrib.authr   Zdjango.contrib.auth.modelsr   Z"django.contrib.contenttypes.modelsr   Zdjango.core.exceptionsr   Zdjango.forms.modelsr   Zdjango.shortcutsr   Zdjango.urlsr	   Zdjango.utils.functionalr
   Zdjango.utils.textr   Zwagtailr   Zwagtail.admin.admin_url_finderr   r   Zwagtail.admin.panels.groupr   Zwagtail.admin.viewsr   Zwagtail.admin.views.genericr   r   Zwagtail.modelsr   Zwagtail.permissionsr   Zwagtail.utils.deprecationr   baser   r   r   r   r(   r(   r(   r)   <module>   s2        