-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #55 from aj3sh/v1.0.1-templatetags-issues
v1.0.1 templatetags issues
- Loading branch information
Showing
5 changed files
with
272 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,164 @@ | ||
import datetime | ||
from typing import Optional, Union | ||
|
||
from django import template | ||
from django.utils import timezone | ||
|
||
from nepali.datetime import nepalidate as _nepalidate | ||
from nepali.datetime import nepalidatetime as _nepalidatetime | ||
from nepali.datetime import nepalihumanize as humanize | ||
from nepali.exceptions import InvalidNepaliDateTimeObjectException | ||
from nepali.utils import to_nepalidatetime | ||
|
||
_DEFAULT_DATE_FORMAT = "%B %d, %Y, %A" | ||
_datetime = Union[datetime.date, datetime.datetime, _nepalidate, _nepalidatetime] | ||
register = template.Library() | ||
|
||
|
||
@register.filter(name="nepalidate") | ||
def nepalidate(datetime_obj, format="%B %d, %Y, %A"): | ||
nepali_datetime_obj = to_nepalidatetime(datetime_obj) | ||
if nepali_datetime_obj is None: | ||
return None | ||
return nepali_datetime_obj.strftime(format) | ||
def nepalidate( | ||
datetime_obj: _datetime, format: str = _DEFAULT_DATE_FORMAT | ||
) -> Union[str, None]: | ||
""" | ||
Renders the datetime object into nepali datetime format in 'en-US' locale (English). | ||
Usage: | ||
``` | ||
{{ datetime_obj|nepalidate }} | ||
{{ datetime_obj|nepalidate:"%Y-%m-%d" }} | ||
``` | ||
:param datetime_obj: Datetime object | ||
:param format: Output format, defaults to "%B %d, %Y, %A" | ||
:returns: Nepali datetime format in 'en-US' locale | ||
""" | ||
return nepalidate_en(datetime_obj, format=format) | ||
|
||
|
||
@register.filter(name="nepalidate_en") | ||
def nepalidate_en(datetime_obj, format="%B %d, %Y, %A"): | ||
nepali_datetime_obj = to_nepalidatetime(datetime_obj) | ||
if nepali_datetime_obj is None: | ||
def nepalidate_en( | ||
datetime_obj: _datetime, format: str = _DEFAULT_DATE_FORMAT | ||
) -> Union[str, None]: | ||
""" | ||
Renders the datetime object into nepali datetime format in 'en-US' locale (English). | ||
Usage: | ||
``` | ||
{{ datetime_obj|nepalidate_en }} | ||
{{ datetime_obj|nepalidate_en:"%Y-%m-%d" }} | ||
``` | ||
:param datetime_obj: Datetime object | ||
:param format: Output format, defaults to "%B %d, %Y, %A" | ||
:returns: Nepali datetime format in 'en-US' locale | ||
""" | ||
try: | ||
nepali_datetime_obj = to_nepalidatetime(datetime_obj) | ||
return nepali_datetime_obj.strftime_en(format) | ||
except InvalidNepaliDateTimeObjectException: | ||
return None | ||
|
||
|
||
@register.filter(name="nepalidate_ne") | ||
def nepalidate_ne( | ||
datetime_obj: _datetime, format: str = _DEFAULT_DATE_FORMAT | ||
) -> Union[str, None]: | ||
""" | ||
Renders the datetime object into nepali datetime format in 'ne' locale (Nepali). | ||
Usage: | ||
``` | ||
{{ datetime_obj|nepalidate_ne }} | ||
{{ datetime_obj|nepalidate_ne:"%Y-%m-%d" }} | ||
``` | ||
:param datetime_obj: Datetime object | ||
:param format: Output format, defaults to "%B %d, %Y, %A" | ||
:returns: Nepali datetime format in 'ne' locale | ||
""" | ||
try: | ||
nepali_datetime_obj = to_nepalidatetime(datetime_obj) | ||
return nepali_datetime_obj.strftime_ne(format) | ||
except InvalidNepaliDateTimeObjectException: | ||
return None | ||
return nepali_datetime_obj.strftime_en(format) | ||
|
||
|
||
@register.filter(name="nepalihumanize") | ||
def nepalihumanize(datetime_obj, threshold=None, format=None): | ||
"""templatetag to humanize nepalidatetime""" | ||
nepali_datetime_obj = to_nepalidatetime(datetime_obj) | ||
if nepali_datetime_obj is None: | ||
def nepalihumanize( | ||
datetime_obj: _datetime, | ||
threshold: Optional[int] = None, | ||
format: Optional[str] = None, | ||
) -> Union[str, None]: | ||
""" | ||
Renders the datetime object to a human readable form for 'ne' locale (Nepali). | ||
Usage: | ||
``` | ||
{{ datetime_obj|nepalihumanize }} | ||
``` | ||
:param datetime_obj: Datetime object | ||
:param threshold: Threshold in seconds that determines when to render | ||
the datetime object in the standard datetime format, optional | ||
:param format: Output format if threshold exceeded, optional | ||
:returns: Datetime object in human readable form | ||
""" | ||
try: | ||
nepali_datetime_obj = to_nepalidatetime(datetime_obj) | ||
return humanize(nepali_datetime_obj, threshold=threshold, format=format) | ||
except InvalidNepaliDateTimeObjectException: | ||
return None | ||
return humanize(nepali_datetime_obj, threshold=threshold, format=format) | ||
|
||
|
||
@register.simple_tag | ||
def nepalinow(format="%B %d, %Y, %A"): | ||
"""templatetag to display current datetime in nepali format""" | ||
def nepalinow(format: str = _DEFAULT_DATE_FORMAT) -> str: | ||
""" | ||
Renders the current nepali datetime in 'en-US' locale (English). | ||
Usage: | ||
``` | ||
{% nepalinow %} | ||
{% nepalinow '%Y-%m-%d' %} | ||
``` | ||
:param datetime_obj: Datetime object | ||
:param format: Output format, defaults to "%B %d, %Y, %A" | ||
:returns: Current nepali datetime | ||
""" | ||
return nepalinow_en(format) | ||
|
||
|
||
@register.simple_tag | ||
def nepalinow_en(format: str = _DEFAULT_DATE_FORMAT) -> str: | ||
""" | ||
Renders the current nepali datetime in 'en-US' locale (English). | ||
Usage: | ||
``` | ||
{% nepalinow_en %} | ||
{% nepalinow_en '%Y-%m-%d' %} | ||
``` | ||
:param datetime_obj: Datetime object | ||
:param format: Output format, defaults to "%B %d, %Y, %A" | ||
:returns: Current nepali datetime | ||
""" | ||
return to_nepalidatetime(timezone.now()).strftime(format) | ||
|
||
|
||
@register.simple_tag | ||
def nepalinow_ne(format: str = _DEFAULT_DATE_FORMAT) -> str: | ||
""" | ||
Renders the current nepali datetime in 'ne' locale (Nepali). | ||
Usage: | ||
``` | ||
{% nepalinow_ne %} | ||
{% nepalinow_ne '%Y-%m-%d' %} | ||
``` | ||
:param datetime_obj: Datetime object | ||
:param format: Output format, defaults to "%B %d, %Y, %A" | ||
:returns: Current nepali datetime | ||
""" | ||
return to_nepalidatetime(timezone.now()).strftime_ne(format) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,77 @@ | ||
from typing import Any | ||
|
||
from django import template | ||
|
||
from nepali.number import nepali_to_english, convert_and_add_comma | ||
from nepali.number import ( | ||
add_comma, | ||
add_comma_english, | ||
convert_and_add_comma, | ||
english_to_nepali, | ||
) | ||
|
||
register = template.Library() | ||
|
||
|
||
@register.filter(name="nepalinumber") | ||
def nepalinumber(value): | ||
return nepali_to_english(value) | ||
def nepalinumber(value: Any) -> str: | ||
""" | ||
Converts the number into nepali number and renders it. | ||
Usage: | ||
``` | ||
{{ number|nepalinumber }} | ||
``` | ||
:param value: Number to be converted | ||
:returns: Nepali output of given number | ||
""" | ||
return english_to_nepali(value) | ||
|
||
|
||
@register.filter(name="nepalinumber_with_comma") | ||
def nepalinumber_with_comma(value): | ||
def nepalinumber_with_comma(value: Any) -> str: | ||
""" | ||
Converts the number into nepali number and renders it. | ||
Usage: | ||
``` | ||
{{ number|nepalinumber_with_comma }} | ||
``` | ||
Basically same as `{{ number|nepalinumber|nepali_comma }}` | ||
:param value: Number to be converted and commas added | ||
:returns: Nepali output of given number with commas | ||
""" | ||
return convert_and_add_comma(value) | ||
|
||
|
||
@register.filter(name="nepali_comma") | ||
def nepali_comma(value: Any) -> str: | ||
""" | ||
Renders the given value with commas added in Nepali style without converting the number. | ||
Usage: | ||
``` | ||
{{ number|nepali_comma }} | ||
``` | ||
:param value: Number to be added with commas | ||
:returns: Output of given number with commas | ||
""" | ||
return add_comma(value) | ||
|
||
|
||
@register.filter(name="english_comma") | ||
def english_comma(value: Any) -> str: | ||
""" | ||
Renders the given value with commas added in English style without converting the number. | ||
Usage: | ||
``` | ||
{{ number|english_comma }} | ||
``` | ||
:param value: Number to be added with commas | ||
:returns: Output of given number with commas | ||
""" | ||
return add_comma_english(value) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,7 +34,7 @@ | |
|
||
setuptools.setup( | ||
name="nepali", | ||
version="1.0.0", | ||
version="1.0.1", | ||
author="opensource-nepal", | ||
author_email="[email protected], [email protected]", | ||
description="nepalidatetime compatible with python's datetime feature. " | ||
|