custom template tag with multiple arguments - python

I want to format a date in the future based on a time delta:
from django import template
from datetime import datetime, timedelta, time
register = template.Library()
#register.simple_tag
def tomorrow(format):
tommorow = datetime.now() + timedelta(days=1)
return tommorow.strftime(format)
def dayfuture(dday, format):
dayfuture = datetime.now() + timedelta(days=dday)
return dayfuture.strftime(format)
This works:
{% tomorrow "%A, %d %b, %Y" %}
But I've had no luck with dayfuture.
Also, is it possible to have multiple custom template tags in the same file. I've had no luck registering a second one.
I'm using django 1.11 pythone 3.4

This does not work because you did not register it. It is possible to have multiple template tags inside a single file.
def dayfuture(dday, format):
dayfuture = datetime.now() + timedelta(days=dday)
return dayfuture.strftime(format)
You have to put the decorator on it to register it
#register.simple_tag
def dayfuture(dday, format):
dayfuture = datetime.now() + timedelta(days=dday)
return dayfuture.strftime(format)

Related

How to change the Django date time format in a string

Given a long string (serialized Django object) how can we search all the timestamps and replace them with the correct format (dd/mm/YYYY HH:MM)
Example date (from the given example): 2022-01-11T16:49:43.897
Desired date time format: 11/01/2022 16:49
What I've tried so far:
#login_required
def my_view(request):
response = dict()
if request.method == 'GET':
files = Files.objects.all().filter(user=request.user)
for fhr in files:
fhr.updated = fhr.updated.strftime("%d/%m/%Y %H:%M:%S")
data = serialize("json", files)
return HttpResponse(data, content_type="application/json")
return HttpResponse('')
Basically I tried to edit the queryset with the correct format strftime("%d/%m/%Y %H:%M:%S") so that when I serialize the QuerySet into JSON data data = serialize("json", files) the string (data) contains the date and time in the correct format but it did not work.
So now I think that the only chance is to edit directly the string and replace those timestamps with the timestamps in the correct format.
The easiest way is to specify encoder class in serialize function. Refer to the documentation.
import typing
from datetime import datetime
from django.core.serializers.json import DjangoJSONEncoder
class DatetimeJSONEncoder(DjangoJSONEncoder):
def default(self, o: typing.Any) -> str:
result = super().default(o)
if isinstance(o, datetime):
result = o.strftime("%d/%m/%Y %H:%M:%S")
return result
data = serialize("json", files, cls=DatetimeJSONEncoder)

Adapt translation method from django to my function

I am working on a django project. I have created a function that will display the current date in french or english.
We have to install
sudo locale-gen fr_FR.utf8
and then
from datetime import datetime
import locale
def set_locale(locale_):
locale.setlocale(category=locale.LC_ALL, locale=locale_)
def date_customerprofile(language):
now_ = datetime.today()
if language == 'English':
set_locale('en_US.utf8')
date_ = now_.strftime("%A %B %d %Y")
else:
set_locale('fr_FR.utf8')
date_ = now_.strftime("%A %B %d %Y")
I know it works well, but I would like to use the django translation method. There are good informations here : https://docs.djangoproject.com/fr/1.10/topics/i18n/translation/ and in particular here : https://www.technomancy.org/python/django-i18n-manually-turn-on-a-language/.
Could anyone be able to adapt the last website method to my function?
I tried something but that doesn't work :
from django.utils import translation
def date_customerprofile(language):
now_ = datetime.today()
old_lang = 'en'
if language == 'English':
import ipdb; ipdb.set_trace()
translation.activate('en')
date_ = now_.strftime("%A, %B %d %Y")
else :
translation.activate('fr')
date_ = now_.strftime("%A, %d %B %Y")
translation.activate(old_lang)
return date_
In fact, my main purpose is to create a function which will display the current date of the form Mercredi, 29 mars 2017 when I select 'French' and the form 'Wednesday, March 29, 2017'. Could anyone have an idea how to do that with a django method?
Thanks!
P.S. Please let me know if the question is unclear.
datetime.strftime() is a method from the Python standard library and doesn't know anything about your Django language. You can use the builtin date template filter which does respect the Django language.
from datetime import datetime
from django.template.defaultfilters import date as datefilter
from django.utils import translation
def date_customerprofile(language):
now_ = datetime.today()
if language == 'English':
translation.activate('en')
return datefilter(now_, 'l, F j, Y')
else:
translation.activate('fr')
return datefilter(now_, 'l, j F Y')
Seems to give the results you want:
>>> date_customerprofile('English')
u'Thursday, March 30, 2017'
>>> date_customerprofile('French')
u'jeudi, 30 mars 2017'
Some notes:
If this is used in the context of one user who has one language, it would be better to set the language on the user's sessions rather than switch it around just within this function.
If you were really serious about localization you would put your custom date formats in format files but it may be overkill for your use case.

DateTimeField input format failing to parse str

I have the following field:
from rest_framework import serializers
class ActivitySerializer(serializers.Serializer):
startDate = serializers.DateTimeField(input_formats=['%Y-%m-%d %H:%M:%S %z'])
activity = {u'startDate': "2015-10-18 15:11:50 +0000"}
serializer = ActivitySerializer(data=activity)
serializer.is_valid() # False
serializer.errors
# {'startDate': [u'Datetime has wrong format. Use one of these formats instead: YYYY-MM-DD hh:mm:ss [+HHMM|-HHMM].']}
Any thoughts as to why the datetime string that I'm using doesn't match that format? Given that datetime string, what's the right format string to parse it?
I just created a new field, ActivityStartDateField, and implemented a custom to_internal_value. I'm surprised I couldn't get it to work with the built-in DateTimeField.
class ActivityStartDateField(serializers.Field):
def to_representation(self, obj):
return str(obj)
def to_internal_value(self, data):
dt = arrow.get(data,'YYYY-MM-DD HH:mm:ss Z').datetime
return dt

Why do my dates and times differ?

I have in my jinja2 template code for localization.
{% set currency = "SEK" %}
{% set format = "sv_SE" %}
{% set timezoneinfo = 'Europe/Stockholm' %}
{% set locale = "se" %}
But it's not working for hours and minutes if I use it like this with a filter using values from a google search API result to filter.
{{scored_document.fields.10.value|datetimeformat_list(hour=scored_document.fields.17.value|int ,minute =scored_document.fields.18.value|int, timezoneinfo=timezoneinfo, locale=locale)}}
filter
def datetimeformat_list(date, hour, minute, locale='en', timezoneinfo='Asia/Calcutta'):
tzinfo = timezone(timezoneinfo)
input = datetime(date.year, date.month, date.day, int(hour), int(minute), tzinfo=tzinfo)
time_str = format_time(input, 'H:mm', tzinfo=tzinfo, locale=locale)
return "{0}".format(time_str)
The code gives a different time than if I just do
{{ ad.modified|datetimeformat_viewad(locale='se', timezoneinfo='Europe/Stockholm') }}
with this filter
def datetimeformat_viewad(to_format, locale='en', timezoneinfo='Asia/Calcutta'):
tzinfo = timezone(timezoneinfo)
month = MONTHS[to_format.month - 1]
date_str = '{0} {1}'.format(to_format.day, _(month))
time_str = format_time(to_format, 'H:mm', tzinfo=tzinfo, locale=locale)
return "{0} {1}".format(date_str, time_str)
Why are the outputs not the same time?
It is incorrect to pass an arbitrary pytz timezone to datetime constructor directly; you should use pytz_tzinfo.localize() method instead as it is said at the very beginning of pytz docs.
There could be other issues in your code.

Reusing a Django RSS Feed for different Date Ranges

What would be a way to have date range based rss feeds in Django. For instance if I had the following type of django rss feed model.
from django.contrib.syndication.feeds import Feed
from myapp.models import *
class PopularFeed(Feed):
title = '%s : Latest SOLs' % settings.SITE_NAME
link = '/'
description = 'Latest entries to %s' % settings.SITE_NAME
def items(self):
return sol.objects.order_by('-date')
What would be a way to have PopularFeed used for All Time, Last Month, Last Week, Last 24 Hours, and vice-versa if I wanted to have LeastPopularFeed?
You need to define a class for each feed you want. For example for Last Month feed:
class LastMonthFeed(Feed):
def items(self):
ts = datetime.datetime.now() - datetime.timedelta(days=30)
return sol.object.filter(date__gte=ts).order_by('-date')
Then add these feeds to your urls.py as shown in docs: http://docs.djangoproject.com/en/1.2/ref/contrib/syndication/

Categories