3

\                  @   s
  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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 d d
lmZmZmZ ejdejZ dd Z!d!ddZ"dd Zdd Zd"ddZ#dd Z$dd Z%dd Z&dd Z'dd  Z(dS )#    N)format_timedelta)settings)redirectresolve_url)get_language	to_locale)date)is_safe_url)import_string)slugify)get_current_timezoneis_naive
make_awarez[^\w\s-]c             C   s2   t jd| } tjd| } | jddjd} t| S )a  
    Convert a string to ASCII exactly as Django's slugify does, with the exception
    that any non-ASCII alphanumeric characters (that cannot be ASCIIfied under Unicode
    normalisation) are escaped into codes like 'u0421' instead of being deleted entirely.
    This ensures that the result of slugifying e.g. Cyrillic text will not be an empty
    string, and can thus be safely used as an identifier (albeit not a human-readable one).

    cautious_slugify was copied from Wagtail:
    <https://github.com/wagtail/wagtail/blob/8b420b9/wagtail/core/utils.py>

    Copyright (c) 2014-present Torchbox Ltd and individual contributors.
    Released under the BSD 3-clause "New" or "Revised" License
    <https://github.com/wagtail/wagtail/blob/8b420b9/LICENSE>

    Date: 2018-06-15
    NFKD asciibackslashreplace)unicodedata	normalize
SLUGIFY_REsubencodedecodedjango_slugify)value r   +/var/www/html/oscar2019/oscar/core/utils.pycautious_slugify   s    r   Fc             C   s   |rt | ddS t| S dS )z
    Oscar's default slugifier function. When unicode is allowed
    it uses Django's slugify function, otherwise it uses cautious_slugify.
    T)allow_unicodeN)r   r   )r   r   r   r   r   default_slugifier<   s    r   c             C   sx   t | } x"tjj D ]\}}| j||} qW ttj}|| tjd}x.tjD ]$}|j|d d}|jd| d}qLW |S )a  
    Slugify a string

    The OSCAR_SLUG_FUNCTION can be set with a dotted path to the slug
    function to use, defaults to 'oscar.core.utils.default_slugifier'.

    OSCAR_SLUG_MAP can be set of a dictionary of target:replacement pairs

    OSCAR_SLUG_BLACKLIST can be set to a iterable of words to remove after
    the slug is generated; though it will not reduce a slug to zero length.
    )r   -r   )	strr   OSCAR_SLUG_MAPitemsreplacer
   OSCAR_SLUG_FUNCTIONOSCAR_SLUG_ALLOW_UNICODEOSCAR_SLUG_BLACKLIST)r   kvZ	slugifierslugwordr   r   r   r   G   s    
r   c             C   s   t | dtt ptjdS )zW
    Takes an instance of timedelta and formats it as a readable translated string
       )	thresholdlocale)	format_tdr   r   r   LANGUAGE_CODE)tdr   r   r   r   e   s    r   c             C   s6   t | r t| t }tjd n| jt }t||S )au  
    Takes an instance of datetime, converts it to the current timezone and
    formats it as a string. Use this instead of
    django.core.templatefilters.date, which expects localtime.

    :param format: Common will be settings.DATETIME_FORMAT or
                   settings.DATE_FORMAT, or the resp. shorthands
                   ('DATETIME_FORMAT', 'DATE_FORMAT')
    z9oscar.core.utils.format_datetime received native datetime)r   r   r   loggingwarning
astimezonedate_filter)dtformat	localtimer   r   r   format_datetimep   s    
r9   c             C   s   t tjj| |t S )z5Timezone aware version of `datetime.datetime.combine`)r   datetimecombiner   )r   timer   r   r   datetime_combine   s    r=   c             C   s6   | j jd}|r"t|| j r"|S |r.t|S |S dS )z
    Takes the request and a default URL. Returns HTTP_REFERER if it's safe
    to use and set, and the default URL otherwise.

    The default URL can be a model with get_absolute_url defined, a urlname
    or a regular URL
    HTTP_REFERERN)METAgetr	   get_hostr   )requestdefaultZreferrerr   r   r   safe_referrer   s    rD   c             C   s   t t| |S )z
    Takes request.META and a default URL to redirect to.

    Returns a HttpResponseRedirect to HTTP_REFERER if it exists and is a safe
    URL; to the default URL otherwise.
    )r   rD   )rB   rC   r   r   r   redirect_to_referrer   s    rE   c               C   s   t jS )z
    For use as the default value for currency fields.  Use of this function
    prevents Django's core migration engine from interpreting a change to
    OSCAR_DEFAULT_CURRENCY as something it needs to generate a migration for.
    )r   OSCAR_DEFAULT_CURRENCYr   r   r   r   get_default_currency   s    rG   c             C   s   | j tjdtjS )a9  
    Explicitly round a decimal to 2 places half up, as should be used for
    money.

    >>> exponent = decimal.Decimal('0.01')
    >>> should_not_be_one = decimal.Decimal('1.005')
    >>> should_not_be_one.quantize(exponent)
    Decimal('1.00')
    >>> round_half_up(should_not_be_one)
    Decimal('1.01')
    z0.01)quantizedecimalDecimalROUND_HALF_UP)Zmoneyr   r   r   round_half_up   s    rL   )F)N))r:   rI   r2   rer   Zbabel.datesr   r/   django.confr   django.shortcutsr   r   django.utils.translationr   r   django.template.defaultfiltersr   r5   django.utils.httpr	   django.utils.module_loadingr
   django.utils.textr   r   Zdjango.utils.timezoner   r   r   compileUNICODEr   r   r   r9   r=   rD   rE   rG   rL   r   r   r   r   <module>   s0   &


	