3

\A                 @   s   d dl Z d dlmZ d dlmZ d dlmZ d dlmZ e	eddZ
ye
jdd	\ZZW n ek
rt   ed
Y nX dd Zdd ZG dd dZG dd dZdS )    N)settings)User)ImproperlyConfigured)	get_modelAUTH_USER_MODELz	auth.User.   z:AUTH_USER_MODEL must be of the form 'app_label.model_name'c              C   s   yt tt} W n" tk
r0   tdtj Y nX tdd tj	j
D }tdd | j	j
D }|j|}t|dk| j	_|| j	_| S )a  
    Return the User model. Doesn't require the app cache to be fully
    initialised.

    This used to live in compat to support both Django 1.4's fixed User model
    and custom user models introduced thereafter.
    Support for Django 1.4 has since been dropped in Oscar, but our
    get_user_model remains because code relies on us annotating the _meta class
    with the additional fields, and other code might rely on it as well.
    z@AUTH_USER_MODEL refers to model '%s' that has not been installedc             S   s   g | ]
}|j qS  )name).0fr	   r	   ,/var/www/html/oscar2019/oscar/core/compat.py
<listcomp>+   s    z"get_user_model.<locals>.<listcomp>c             S   s   g | ]
}|j qS r	   )r
   )r   r   r	   r	   r   r   ,   s    r   )r   AUTH_USER_APP_LABELAUTH_USER_MODEL_NAMELookupErrorr   r   r   setr   _metafields
differencelenZhas_additional_fieldsZadditional_fields)modelZcore_fieldsmodel_fields
new_fieldsr	   r	   r   get_user_model   s    
r   c                s*   t  jj}dd |D   fdd| D S )a  
    Starting with Django 1.6, the User model can be overridden  and it is no
    longer safe to assume the User model has certain fields. This helper
    function assists in writing portable forms Meta.fields definitions
    when those contain fields on the User model

    Usage:
    class UserForm(forms.Form):
        ...
        class Meta:
            # won't break if first_name is not defined on User model
            fields = existing_user_fields(['first_name', 'last_name'])
    c             S   s   g | ]
}|j qS r	   )r
   )r   fieldr	   r	   r   r   C   s    z(existing_user_fields.<locals>.<listcomp>c                s   g | ]}| kr|qS r	   r	   )r   r   )user_field_namesr	   r   r   D   s    )r   r   r   )r   Zuser_fieldsr	   )r   r   existing_user_fields4   s    
r   c               @   s@   e Zd ZejdfddZdd Zdd Zdd	 ZeZ	d
d Z
dS )UnicodeCSVReaderzutf-8c             K   s   || _ || _|| _|| _d S )N)filenamedialectencodingkw)selfr   r    r!   r"   r	   r	   r   __init__X   s    zUnicodeCSVReader.__init__c             C   s8   t | jd| jdd| _tj| jfd| ji| j| _| S )Nrt )r!   newliner    )openr   r!   r   csvreaderr    r"   )r#   r	   r	   r   	__enter___   s    zUnicodeCSVReader.__enter__c             C   s   | j j  d S )N)r   close)r#   typevalue	tracebackr	   r	   r   __exit__e   s    zUnicodeCSVReader.__exit__c             C   s
   t | jS )N)nextr*   )r#   r	   r	   r   r1   h   s    zUnicodeCSVReader.nextc             C   s   | S )Nr	   )r#   r	   r	   r   __iter__m   s    zUnicodeCSVReader.__iter__N)__name__
__module____qualname__r)   excelr$   r+   r0   r1   __next__r2   r	   r	   r	   r   r   W   s   r   c               @   sL   e Zd ZdZddejdfddZdd Zdd	 Zd
d Z	dd Z
dd ZdS )UnicodeCSVWritera.  
    Python 2 3 compatible CSV writer. Supports two modes:
    * Writing to an open file or file-like object:
      writer = UnicodeCSVWriter(open_file=your_file)
      ...
      your_file.close()
    * Writing to a new file:
      with UnicodeCSVWriter(filename=filename) as writer:
          ...
    Nzutf-8c             K   sZ   ||  kod kn  r t d|| _|| _|| _|| _|| _d | _| jrV| j| j d S )Nz5You need to specify either a filename or an open file)r   r   r   r    r!   r"   writeradd_bom)r#   r   	open_filer    r!   r"   r	   r	   r   r$   |   s    zUnicodeCSVWriter.__init__c             C   s4   | j d k	stt| j d| jdd| _| j| j | S )Nwtr&   )r!   r'   )r   AssertionErrorr(   r!   r   r:   )r#   r	   r	   r   r+      s    zUnicodeCSVWriter.__enter__c             C   s&   | j d k	st| j d k	r"| jj  d S )N)r   r=   r   r,   )r#   r-   r.   r/   r	   r	   r   r0      s    
zUnicodeCSVWriter.__exit__c             C   s&   | j dkr"ttddr"| jjd d S )Nzutf-8ZOSCAR_CSV_INCLUDE_BOMFu   ﻿)r!   getattrr   r   write)r#   r   r	   r	   r   r:      s    
zUnicodeCSVWriter.add_bomc             C   s<   | j d kr(tj | jfd| ji| j| _ | j jt| d S )Nr    )r9   r)   r   r    r"   writerowlist)r#   rowr	   r	   r   r@      s    
zUnicodeCSVWriter.writerowc             C   s   x|D ]}| j | qW d S )N)r@   )r#   rowsrB   r	   r	   r   	writerows   s    
zUnicodeCSVWriter.writerows)r3   r4   r5   __doc__r)   r6   r$   r+   r0   r:   r@   rD   r	   r	   r	   r   r8   q   s   
r8   )r)   django.confr   django.contrib.auth.modelsr   django.core.exceptionsr   oscar.core.loadingr   r>   r   rsplitr   r   
ValueErrorr   r   r   r8   r	   r	   r	   r   <module>   s   !#