
v^                 @   s   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	 d Z
 d d d	  Z d
 d   Z Gd d   d e j  Z d S)    N)color_style)
migrationstransaction)Q)IntegrityErrorz
    A problem arose migrating proxy model permissions for {old} to {new}.

      Permission(s) for {new} already existed.
      Codenames Q: {query}

    Ensure to audit ALL permissions for {old} and {new}.
Fc                s  t    } |  j d d  } |  j d d  } | j j } x|  j   D]} | j     j sa qF   f d d     j D } t d |  }	 x0   j	 D]% \ }
 } |	 t d |
 d	 |  B}	 q W| j
 j |  } | j | d
 d } | j | d
 d } | r| n | } | r| n | } yI t j d |  0 | j
 j |  j |	 d | j d |  Wd QRXWqF t k
 rd j | j | j  } d j | j | j  } t j j | j t j d | d | d |	    YqF XqF Wd S)zk
    Update the content_type of proxy model permissions to use the ContentType
    of the proxy model.
    auth
PermissioncontenttypesContentTypec                s#   g  |  ] } d  |   j  f  q S)z%s_%s)Z
model_name).0action)opts ^/tmp/pip-build-8lau8j11/django/django/contrib/auth/migrations/0011_update_proxy_permissions.py
<listcomp>    s   	z2update_proxy_model_permissions.<locals>.<listcomp>Zcodename__incodenamenameZfor_concrete_modelTFusingcontent_typeNz{}_{}oldnewquery)r   Z	get_model
connectionaliasZ
get_modelsZ_metaproxyZdefault_permissionsr   permissionsZobjectsZ
db_managerZget_for_modelr   Zatomicr   filterupdater   formatZ	app_labelmodelsysstdoutwriteWARNING)appsschema_editorreversestyler   r
   r   ZModelZ#proxy_default_permissions_codenamesZpermissions_queryr   r   Zcontent_type_managerZconcrete_content_typeZproxy_content_typeZold_content_typeZnew_content_typer   r   r   )r   r   update_proxy_model_permissions   s8    			r(   c             C   s   t  |  | d d d S)zn
    Update the content_type of proxy model permissions to use the ContentType
    of the concrete model.
    r&   TN)r(   )r$   r%   r   r   r   revert_proxy_model_permissions7   s    r)   c               @   s1   e  Z d  Z d d g Z e j e e  g Z d S)	Migrationr    0010_alter_group_name_max_lengthr	   0002_remove_content_type_nameN)zauthr+   )zcontenttypesr,   )	__name__
__module____qualname__Zdependenciesr   Z	RunPythonr(   r)   
operationsr   r   r   r   r*   ?   s   	r*   )r    Zdjango.core.management.colorr   Z	django.dbr   r   Zdjango.db.modelsr   Zdjango.db.utilsr   r#   r(   r)   r*   r   r   r   r   <module>   s   	%