

_Vn[                 @   s  d  d l  m Z d  d l Z d  d l Z d  d l Z d  d l Z d  d l Z d  d l Z d  d l Z d  d l	 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 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, Gd d   d e-  Z. Gd d   d e/  Z0 Gd d   d e/  Z1 d Z2 d S)    )unicode_literalsN)import_module)get_version)apps)
migrationsmodels)MigrationLoader)	Operation)COMPILED_REGEX_TYPERegexObject)datetime_safesix)upath)
force_text)
LazyObjectPromise)get_func_args)
module_dir)nowutc)get_docs_versionc               @   s.   e  Z d  Z d Z d d   Z d d   Z d S)SettingsReferencez
    Special subclass of string which actually references a current settings
    value. It's treated as the value in memory, but serializes out to a
    settings.NAME attribute reference.
    c             C   s   t  j |  |  S)N)str__new__)selfvaluesetting_name r   =/tmp/pip-build-ghmbqnp_/Django/django/db/migrations/writer.pyr   $   s    zSettingsReference.__new__c             C   s   | |  _  d  S)N)r   )r   r   r   r   r   r   __init__'   s    zSettingsReference.__init__N)__name__
__module____qualname____doc__r   r   r   r   r   r   r      s   r   c               @   s[   e  Z d  Z d d d  Z d d   Z d d   Z d d	   Z d
 d   Z d d   Z d S)OperationWriter   c             C   s   | |  _  g  |  _ | |  _ d  S)N)	operationbuffindentation)r   r&   r(   r   r   r   r   ,   s    		zOperationWriter.__init__c       
         sl     f d d   } t       j j   \ } } } t  j j  } t t | d    j j k rz  j d |  n:   j	 d  j j j
   j d  j j j
 | f   j   x7 t |  D]) \ } } | } | | }	 | |	 |  q Wt |  } x> | | d   D], }	 |	 | k r| |	 } | |	 |  qqW j    j d   j     f S)Nc                s  |   j  j k r6t | t t t f  r6t | t  r_ j d |    j   x | j   D] \ } } t	 j
 |  \ } } t	 j
 |  \ } } | j   } t |  d k r j d | | d f  x% | d d  D] }	  j |	  q W j d | d  n  j d | | f    j |    j |  qa W j    j d  q j d |    j   x | D] }
 t	 j
 |
  \ } } | j   } t |  d k rx% | d  d  D] }	  j |	  qW j d | d  n  j d |    j |  qW j    j d	  n t	 j
 |  \ } } | j   } t |  d k r j d
 |  | d f  x% | d d  D] }	  j |	  qW j d | d  n  j d |  | f    j |  d  S)Nz%s={   z%s: %sr   z%s,z%s: %s,z},z%s=[z],z%s=%sz%s=%s,r*   r*   r*   r*   r*   )r&   Zserialization_expand_args
isinstancelisttupledictfeedindentitemsMigrationWriter	serialize
splitlineslenupdateunindent)Z	_arg_nameZ
_arg_valuekeyr   Z
key_stringZkey_imports
arg_stringarg_importsargsargitem)importsr   r   r   _write3   sR    



z)OperationWriter.serialize.<locals>._writezmigrations.%s(z	import %sz%s.%s(z),)setr&   deconstructr   r   getattrr   	__class__r/   addr!   r0   	enumerater5   r7   render)
r   r?   namer;   kwargsZoperation_argsir<   Z	arg_valueZarg_namer   )r>   r   r   r3   1   s*    0	 



zOperationWriter.serializec             C   s   |  j  d 7_  d  S)Nr)   )r(   )r   r   r   r   r0      s    zOperationWriter.indentc             C   s   |  j  d 8_  d  S)Nr)   )r(   )r   r   r   r   r7      s    zOperationWriter.unindentc             C   s#   |  j  j d |  j d |  d  S)N    )r'   appendr(   )r   liner   r   r   r/      s    zOperationWriter.feedc             C   s   d j  |  j  S)N
)joinr'   )r   r   r   r   rF      s    zOperationWriter.renderN)	r    r!   r"   r   r3   r0   r7   r/   rF   r   r   r   r   r$   +   s   Qr$   c               @   s   e  Z d  Z d Z d d   Z d d   Z e d d    Z e d d	    Z	 e d
 d    Z
 e d d    Z e d d    Z e d d    Z e d d    Z d S)r2   zk
    Takes a Migration instance and is able to produce the contents
    of the migration file from it.
    c             C   s   | |  _  d |  _ d  S)NF)	migrationneeds_manual_porting)r   rP   r   r   r   r      s    	zMigrationWriter.__init__c             C   s  i d d 6d d 6} t    } g  } xF |  j j D]8 } t |  j   \ } } | j |  | j |  q0 W| r d j |  d n d | d <g  } x |  j j D]r } | d d k r | j d | d	  | j	 d
  q t
 d d   | D  } | j d |  j |  d  q W| r1d j |  d n d | d <t    }	 x_ t |  D]Q }
 t j d |
  rQ|	 j	 |
 j d  d	 j    | j |
  d |  _ qQqQWd | k r| j d  | j	 d  n | j	 d  t | d d d   } | rd j |  d n d | d <|	 rA| d d d j t |	   7<n  |  j j rqd |  j |  j j  d | d <n  | j d t   d t   j d   |  j j rd | d <n  t | j d   S)!z8
        Returns a string of the file contents.
         Zreplaces_strZinitial_strrN   
operationsr   Z__setting__z5        migrations.swappable_dependency(settings.%s),r)   z from django.conf import settingsc             s   s   |  ] } t  |  Vq d  S)N)r   ).0sr   r   r   	<genexpr>   s    z,MigrationWriter.as_string.<locals>.<genexpr>z        %s,dependenciesz^import (.*)\.\d+[^\s]*$importTzfrom django.db import modelsz(from django.db import migrations, modelsz from django.db import migrationsr8   c             S   s   |  j    d S)Nr)   )split)rI   r   r   r   <lambda>   s    z+MigrationWriter.as_string.<locals>.<lambda>r>   z

# Functions from the following migrations need manual copying.
# Move them and any dependencies into this file, then update the
# RunPython operations to refer to the local versions:
# %sz
# z
    replaces = %s
version	timestampz%Y-%m-%d %H:%Mz
    initial = True
utf8)r@   rP   rS   r$   r3   r6   rL   rO   rW   rD   r-   r,   rematchrY   stripremoverQ   discardsortedZreplacesr   r   strftimeinitialMIGRATION_TEMPLATEencode)r   r1   r>   rS   r&   Zoperation_stringZoperation_importsrW   
dependencyZmigration_importsrM   Zsorted_importsr   r   r   	as_string   sT    
	#"#	 #$		zMigrationWriter.as_stringc             C   sk   |  j  d k	 r0 |  j  t k r0 |  j t  }  n  t |   j d d  } t |  t j  rg d | } n  | S)z
        Returns a serialized version of a datetime object that is valid,
        executable python code. It converts timezone-aware values to utc with
        an 'executable' utc representation of tzinfo.
        Nz<UTC>r   zdatetime.%s)tzinfor   
astimezonereprreplacer+   r   datetime)r   
value_reprr   r   r   serialize_datetime   s    z"MigrationWriter.serialize_datetimec          #   C   s  t  j |  j j  } | d  k r: t d |  j j   n  y t |  } Wn t k
 r^ Yn* Xy t t |   SWn t k
 r Yn Xt	 j
 |  j j  } | j d  \ } } } | j | k r t j j | j |  S| j d  g  } } x | ru| j d | j d   y t d j |   }	 Wn t k
 rBw Yq Xy t t |	   }
 Wn t k
 rpw Yq XPq Wt d |   t j j |
 |  } t j j |  st j |  n  xH | D]@ } t j j |
 |  }
 t t j j |
 d  d   Wd  QXqW| S)	NztDjango can't create migrations for app '%s' because migrations have been disabled via the MIGRATION_MODULES setting..r   r)   zCould not locate an appropriate location to create migrations package %s. Make sure the toplevel package exists and can be imported.z__init__.pywr*   )r   migrations_modulerP   Z	app_label
ValueErrorr   ImportErrorr   r   r   Zget_app_config
rpartitionrG   ospathrO   rY   insertpopisdirmakedirsopen)r   Zmigrations_package_namers   Z
app_configZmaybe_app_name_Zmigrations_package_basenameZexisting_dirsZmissing_dirsZbase_modulebase_dirZ	final_dirZmissing_dirr   r   r   basedir   sP    	

zMigrationWriter.basedirc             C   s   d |  j  j S)Nz%s.py)rP   rG   )r   r   r   r   filename-  s    zMigrationWriter.filenamec             C   s   t  j j |  j |  j  S)N)rw   rx   rO   r   r   )r   r   r   r   rx   1  s    zMigrationWriter.pathc             C   s   |  j  |  \ } } g  } x= | D]5 } |  j |  \ } }	 | j |  | j |	  q" WxY t | j    D]E \ }
 } |  j |  \ } }	 | j |	  | j d |
 | f  qn Wd | d j |  f | f S)Nz%s=%sz%s(%s)z, )_serialize_pathr3   rL   r6   rc   r1   rO   )clsrx   r;   rH   rG   r>   stringsr<   r9   r:   kwr   r   r   serialize_deconstructed5  s    z'MigrationWriter.serialize_deconstructedc             C   sW   | j  d d  \ } } | d k r: d h } d | } n d | h } | } | | f S)Nrq   r)   zdjango.db.modelszfrom django.db import modelsz	models.%sz	import %s)rsplit)r   rx   modulerG   r>   r   r   r   r   C  s    	zMigrationWriter._serialize_pathc       '      C   s	  t  | t  r t |  } n& t  | t  rD | j   d d } n  t  | t t t t f  r+t   } g  } x= | D]5 } |  j	 |  \ } } | j
 |  | j |  qu Wt  | t  r d } nN t  | t  r d } n6 t  | t  rt |  d k rd n d } n d } | d j |  | f St  | t  rt   } g  } xw t | j    D]c \ } }	 |  j	 |  \ }
 } |  j	 |	  \ } } | j
 |  | j
 |  | j |
 | f  q\Wd	 d j d
 d   | D  | f St  | t j  r@|  j |  } d g } | j d k	 r0| j d  n  | t |  f St  | t j  rt |  } t  | t j  r}d | } n  | d h f St  | t j  rt |  } t  | t j  rd | } n  | d h f St  | t j  rt |  d h f St  | t  rd | j d h f St  | t  rrt j |  sIt j |  r_d j |  t   f St |  t   f St  | t  j! t" t# d  f  rt |  t   f St  | t  j$  rt |  } t  j% rd | } n  | t   f St  | t  j&  r/t |  } t  j% r"| d d  } n  | t   f St  | t' j(  rTt |  d h f St  | t) j*  r| j+   \ } } } } |  j, | | |  St  | t#  rLt) j- d g  f g } x3 | D]+ \ } } } | | k r| t |  f SqWt. | d  r	| j/ } | t  j0 j1 j2 k r(| j2 t   f Sd | | j2 f d | h f Sq	nTt  | t) j3 j4  r| j+   \ } } } } } | r|  j5 |  \ } } d | | f S|  j, | | |  Snt  | t6  rt7 | d d j	   \ } } | j8 d  | f St  | t9 j:  rd h } |  j	 | j;  \ } } |  j	 | j<  \ } } |  j	 | j=  \ } }  | j
 |  | j
 |  | j
 |   d | | | f | f St. | d  r|  j, | j+     St  | t> j? t> j@ f  rPtA | d d  r3t  | jB t#  r3| jB }! |! j/ } d  | |! j2 | j2 f d | h f S| j2 d! k rQtC d"   n  | j/ d k rstC d# |   n  tA | d$ d  rtA | d d  rd% | jD k rd | j/ | jD f d | j/ h f Sn  | j/ }" tE |"  } t. | | j2  stC d& | j2 |" tF   f   n  |" d' k r2| j2 t   f Sd |" | j2 f d |" h f St  | tG jH  rt   } g  } x= | D]5 } |  j	 |  \ } } | j
 |  | j |  qxWt |  d k rd n d } | d j |  | f St  | tI tJ f  r	d( h } |  j	 | jK  \ }# }$ |  j	 | jL  \ }% }& | j
 |$  | j
 |&  |# g } | jL rp	| j |%  n  d) d j |  | f StC d* | tF   f   d S)+z
        Serializes the value to a string that's parsable by Python, along
        with any needed imports to make that string work.
        More advanced than repr() as it can encode things
        like datetime.datetime.now.
        r)   r   z	set([%s])zfrozenset([%s])z(%s)z(%s,)z[%s]z, z{%s}c             s   s%   |  ] \ } } d  | | f Vq d S)z%s: %sNr   )rT   kvr   r   r   rV   ~  s    z,MigrationWriter.serialize.<locals>.<genexpr>zimport datetimeNz%from django.utils.timezone import utczdatetime.%szsettings.%sz from django.conf import settingszfloat("{}")bzfrom decimal import Decimalzmodels.Modelr!   z%s.%sz	import %sz%s.as_manager()r(   ,zimport functoolsz functools.partial(%s, *%s, **%s)rA   __self__z%s.%s.%sz<lambda>z!Cannot serialize function: lambdaz'Cannot serialize function %r: No moduler"   <ab  Could not find function %s in %s.
Please note that due to Python 2 limitations, you cannot serialize unbound method functions (e.g. a method declared and used in the same class body). Please move the function into the main module body to use migrations.
For more information, see https://docs.djangoproject.com/en/%s/topics/migrations/#serializing-values__builtin__z	import rezre.compile(%s)zCannot serialize: %r
There are some values Django cannot serialize into migration files.
For more, see https://docs.djangoproject.com/en/%s/topics/migrations/#migration-serializing)Mr+   r   r   r   
__reduce__	frozensetr,   r@   r-   r3   r6   rL   r5   rO   r.   rc   r1   rn   rp   rj   daterl   r   time	timedeltar   r   floatmathisnanisinfformatr   integer_typesbooltypebinary_typePY2	text_typedecimalZDecimalr   ZFieldrA   r   ZModelhasattrr!   movesbuiltinsr    managerZBaseManagerr   r	   r$   rstrip	functoolspartialfuncr;   keywordstypesFunctionTypeBuiltinFunctionTyperB   r   rt   r"   r   r   collectionsIterabler
   r   patternflags)'r   r   r>   r   r=   Zitem_stringZitem_importsr   r   r   Zk_stringZ	k_importsZv_stringZ	v_importsro   	attr_namerx   r;   rH   Zspecial_casesZcasestringr   Z
as_managerZmanager_pathZqs_pathrG   Zfunc_stringZfunc_importsZargs_stringZargs_importsZkeywords_stringZkeywords_importsklassmodule_nameZregex_patternZpattern_importsZregex_flagsZflag_importsr   r   r   r3   N  s"   			!	$	"			$	$		$$'					zMigrationWriter.serializeN)r    r!   r"   r#   r   ri   staticmethodrp   propertyr   r   rx   classmethodr   r   r3   r   r   r   r   r2      s   I=r2   a  # -*- coding: utf-8 -*-
# Generated by Django %(version)s on %(timestamp)s
from __future__ import unicode_literals

%(imports)s

class Migration(migrations.Migration):
%(replaces_str)s%(initial_str)s
    dependencies = [
%(dependencies)s    ]

    operations = [
%(operations)s    ]
)3
__future__r   r   rn   r   r   r   rw   r^   r   	importlibr   Zdjangor   Zdjango.appsr   Z	django.dbr   r   Zdjango.db.migrations.loaderr   Z$django.db.migrations.operations.baser	   Zdjango.db.migrations.utilsr
   r   Zdjango.utilsr   r   Zdjango.utils._osr   Zdjango.utils.encodingr   Zdjango.utils.functionalr   r   Zdjango.utils.inspectr   Zdjango.utils.module_loadingr   Zdjango.utils.timezoner   r   Zdjango.utils.versionr   r   r   objectr$   r2   rf   r   r   r   r   <module>   s8   d 