Skip to content

Instantly share code, notes, and snippets.

@ddahan
Last active January 4, 2017 16:47
Show Gist options
  • Select an option

  • Save ddahan/8b68c275ca60abf545a3dd5d67b747e3 to your computer and use it in GitHub Desktop.

Select an option

Save ddahan/8b68c275ca60abf545a3dd5d67b747e3 to your computer and use it in GitHub Desktop.

Revisions

  1. ddahan revised this gist Jan 4, 2017. 1 changed file with 6 additions and 8 deletions.
    14 changes: 6 additions & 8 deletions file.py
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,7 @@
    def custom_localize(date_or_datetime):
    """ Take a naïve date or datetime and return the corresponding aware date or datetime with localtime """
    def get_updated_time_or_datetime(time_or_datetime):

    return pytz.timezone(get_current_timezone_name()).localize(date_or_datetime)

    >>> datetime(2016, 12, 20, 0, 0)
    datetime.datetime(2016, 12, 20, 0, 0) # Naïve datetime, pas bon !!!
    >>> custom_localize(datetime(2016, 12, 20, 0, 0))
    datetime.datetime(2016, 12, 20, 0, 0, tzinfo=<DstTzInfo 'Europe/Paris' CET+1:00:00 STD>) # Tout va bien, notre objet sera automatiquement converti en UTC s'il doit être stocké en base.
    if time_or_datetime:
    if is_naive(time_or_datetime):
    return make_aware(time_or_datetime, timezone.utc) - timedelta(hours=2)
    else:
    return time_or_datetime
  2. ddahan revised this gist Jan 4, 2017. 1 changed file with 9 additions and 1 deletion.
    10 changes: 9 additions & 1 deletion file.py
    Original file line number Diff line number Diff line change
    @@ -1 +1,9 @@
    def custom_localize(date_or_datetime): """ Take a naïve date or datetime and return the corresponding aware date or datetime with localtime """ return pytz.timezone(get_current_timezone_name()).localize(date_or_datetime) >>> datetime(2016, 12, 20, 0, 0) datetime.datetime(2016, 12, 20, 0, 0) # Naïve datetime, pas bon !!! >>> custom_localize(datetime(2016, 12, 20, 0, 0)) datetime.datetime(2016, 12, 20, 0, 0, tzinfo=<DstTzInfo 'Europe/Paris' CET+1:00:00 STD>) # Tout va bien, notre objet sera automatiquement converti en UTC s'il doit être stocké en base.
    def custom_localize(date_or_datetime):
    """ Take a naïve date or datetime and return the corresponding aware date or datetime with localtime """

    return pytz.timezone(get_current_timezone_name()).localize(date_or_datetime)

    >>> datetime(2016, 12, 20, 0, 0)
    datetime.datetime(2016, 12, 20, 0, 0) # Naïve datetime, pas bon !!!
    >>> custom_localize(datetime(2016, 12, 20, 0, 0))
    datetime.datetime(2016, 12, 20, 0, 0, tzinfo=<DstTzInfo 'Europe/Paris' CET+1:00:00 STD>) # Tout va bien, notre objet sera automatiquement converti en UTC s'il doit être stocké en base.
  3. ddahan revised this gist Jan 4, 2017. 1 changed file with 1 addition and 8 deletions.
    9 changes: 1 addition & 8 deletions file.py
    Original file line number Diff line number Diff line change
    @@ -1,8 +1 @@
    >>> now_utc = timezone.now()
    >>> now_utc
    datetime.datetime(2016, 7, 12, 13, 55, 57, 317577, tzinfo=<UTC>) # Il est 13h55 au temps universel ...
    >>> now_paris = localtime(now_utc)
    >>> now_paris
    datetime.datetime(2016, 7, 12, 15, 55, 57, 317577, tzinfo=<DstTzInfo 'Europe/Paris' CEST+2:00:00 DST>) # ... et il est 15h55 à Paris !
    >>> now_utc == now_paris
    True # Ces objets sont égaux, car il s'agit de la même date représentée de 2 manières différentes !
    def custom_localize(date_or_datetime): """ Take a naïve date or datetime and return the corresponding aware date or datetime with localtime """ return pytz.timezone(get_current_timezone_name()).localize(date_or_datetime) >>> datetime(2016, 12, 20, 0, 0) datetime.datetime(2016, 12, 20, 0, 0) # Naïve datetime, pas bon !!! >>> custom_localize(datetime(2016, 12, 20, 0, 0)) datetime.datetime(2016, 12, 20, 0, 0, tzinfo=<DstTzInfo 'Europe/Paris' CET+1:00:00 STD>) # Tout va bien, notre objet sera automatiquement converti en UTC s'il doit être stocké en base.
  4. ddahan revised this gist Jan 4, 2017. 1 changed file with 8 additions and 22 deletions.
    30 changes: 8 additions & 22 deletions file.py
    Original file line number Diff line number Diff line change
    @@ -1,22 +1,8 @@
    # views.py
    def generateRobots(request):
    ''' Génère la page de robots.txt en fonction de l'environnement (dynamique)
    '''

    # Récupère les arguments dans une liste
    donotvisit_list = settings.DISALLOW_LIST.split(sep=",")

    robots_str = "User-agent: *\n" # Présent dans chaque env
    for elt in donotvisit_list:
    robots_str += "Disallow: {0}\n".format(elt)

    return render_to_response(
    'dynamic_robots.html',
    {
    'robots_str': robots_str
    },
    RequestContext(request)
    )

    # urls.py
    url(r'^robots.txt$', views.generateRobots, name='generateRobots'),
    >>> now_utc = timezone.now()
    >>> now_utc
    datetime.datetime(2016, 7, 12, 13, 55, 57, 317577, tzinfo=<UTC>) # Il est 13h55 au temps universel ...
    >>> now_paris = localtime(now_utc)
    >>> now_paris
    datetime.datetime(2016, 7, 12, 15, 55, 57, 317577, tzinfo=<DstTzInfo 'Europe/Paris' CEST+2:00:00 DST>) # ... et il est 15h55 à Paris !
    >>> now_utc == now_paris
    True # Ces objets sont égaux, car il s'agit de la même date représentée de 2 manières différentes !
  5. ddahan created this gist Jan 4, 2017.
    22 changes: 22 additions & 0 deletions file.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    # views.py
    def generateRobots(request):
    ''' Génère la page de robots.txt en fonction de l'environnement (dynamique)
    '''

    # Récupère les arguments dans une liste
    donotvisit_list = settings.DISALLOW_LIST.split(sep=",")

    robots_str = "User-agent: *\n" # Présent dans chaque env
    for elt in donotvisit_list:
    robots_str += "Disallow: {0}\n".format(elt)

    return render_to_response(
    'dynamic_robots.html',
    {
    'robots_str': robots_str
    },
    RequestContext(request)
    )

    # urls.py
    url(r'^robots.txt$', views.generateRobots, name='generateRobots'),