naturalday-filter added.
This commit is contained in:
parent
60d931b0b9
commit
abc421a8a7
@ -30,6 +30,7 @@ All additional filters/tags will be registered automatically.
|
|||||||
- Humanize
|
- Humanize
|
||||||
- **[intcomma](https://docs.djangoproject.com/en/dev/ref/contrib/humanize/#intcomma)** (put decimal marks into the number)
|
- **[intcomma](https://docs.djangoproject.com/en/dev/ref/contrib/humanize/#intcomma)** (put decimal marks into the number)
|
||||||
- **[ordinal](https://docs.djangoproject.com/en/dev/ref/contrib/humanize/#ordinal)** (convert integer to its ordinal as string)
|
- **[ordinal](https://docs.djangoproject.com/en/dev/ref/contrib/humanize/#ordinal)** (convert integer to its ordinal as string)
|
||||||
|
- **[naturalday](https://docs.djangoproject.com/en/dev/ref/contrib/humanize/#naturalday)** (converts `time.Time`-object into today/yesterday/tomorrow if possible; otherwise it will use `naturaltime`)
|
||||||
- **[timesince](https://docs.djangoproject.com/en/dev/ref/templates/builtins/#timesince)/[timeuntil](https://docs.djangoproject.com/en/1.6/ref/templates/builtins/#timeuntil)/[naturaltime](https://docs.djangoproject.com/en/dev/ref/contrib/humanize/#naturaltime)** (human-readable time [duration] indicator)
|
- **[timesince](https://docs.djangoproject.com/en/dev/ref/templates/builtins/#timesince)/[timeuntil](https://docs.djangoproject.com/en/1.6/ref/templates/builtins/#timeuntil)/[naturaltime](https://docs.djangoproject.com/en/dev/ref/contrib/humanize/#naturaltime)** (human-readable time [duration] indicator)
|
||||||
|
|
||||||
### Tags
|
### Tags
|
||||||
|
39
filters.go
39
filters.go
@ -7,8 +7,8 @@ import (
|
|||||||
"github.com/flosch/pongo2"
|
"github.com/flosch/pongo2"
|
||||||
|
|
||||||
"github.com/extemporalgenome/slug"
|
"github.com/extemporalgenome/slug"
|
||||||
"github.com/russross/blackfriday"
|
|
||||||
"github.com/flosch/go-humanize"
|
"github.com/flosch/go-humanize"
|
||||||
|
"github.com/russross/blackfriday"
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@ -18,6 +18,7 @@ func init() {
|
|||||||
pongo2.RegisterFilter("timeuntil", filterTimeuntilTimesince)
|
pongo2.RegisterFilter("timeuntil", filterTimeuntilTimesince)
|
||||||
pongo2.RegisterFilter("timesince", filterTimeuntilTimesince)
|
pongo2.RegisterFilter("timesince", filterTimeuntilTimesince)
|
||||||
pongo2.RegisterFilter("naturaltime", filterTimeuntilTimesince)
|
pongo2.RegisterFilter("naturaltime", filterTimeuntilTimesince)
|
||||||
|
pongo2.RegisterFilter("naturalday", filterNaturalday)
|
||||||
pongo2.RegisterFilter("intcomma", filterIntcomma)
|
pongo2.RegisterFilter("intcomma", filterIntcomma)
|
||||||
pongo2.RegisterFilter("ordinal", filterOrdinal)
|
pongo2.RegisterFilter("ordinal", filterOrdinal)
|
||||||
}
|
}
|
||||||
@ -37,13 +38,13 @@ func filterFilesizeformat(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value,
|
|||||||
func filterTimeuntilTimesince(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, error) {
|
func filterTimeuntilTimesince(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, error) {
|
||||||
basetime, is_time := in.Interface().(time.Time)
|
basetime, is_time := in.Interface().(time.Time)
|
||||||
if !is_time {
|
if !is_time {
|
||||||
return nil, errors.New("timeuntil-value is not a time.Time-instance.")
|
return nil, errors.New("time-value is not a time.Time-instance.")
|
||||||
}
|
}
|
||||||
var paramtime time.Time
|
var paramtime time.Time
|
||||||
if !param.IsNil() {
|
if !param.IsNil() {
|
||||||
paramtime, is_time = param.Interface().(time.Time)
|
paramtime, is_time = param.Interface().(time.Time)
|
||||||
if !is_time {
|
if !is_time {
|
||||||
return nil, errors.New("timeuntil-parameter is not a time.Time-instance.")
|
return nil, errors.New("time-parameter is not a time.Time-instance.")
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
paramtime = time.Now()
|
paramtime = time.Now()
|
||||||
@ -59,3 +60,35 @@ func filterIntcomma(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, error
|
|||||||
func filterOrdinal(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, error) {
|
func filterOrdinal(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, error) {
|
||||||
return pongo2.AsValue(humanize.Ordinal(in.Integer())), nil
|
return pongo2.AsValue(humanize.Ordinal(in.Integer())), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func filterNaturalday(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, error) {
|
||||||
|
basetime, is_time := in.Interface().(time.Time)
|
||||||
|
if !is_time {
|
||||||
|
return nil, errors.New("naturalday-value is not a time.Time-instance.")
|
||||||
|
}
|
||||||
|
|
||||||
|
var reference_time time.Time
|
||||||
|
if !param.IsNil() {
|
||||||
|
reference_time, is_time = param.Interface().(time.Time)
|
||||||
|
if !is_time {
|
||||||
|
return nil, errors.New("naturalday-parameter is not a time.Time-instance.")
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
reference_time = time.Now()
|
||||||
|
}
|
||||||
|
|
||||||
|
d := reference_time.Sub(basetime) / time.Hour
|
||||||
|
|
||||||
|
switch {
|
||||||
|
case d >= 0 && d < 24:
|
||||||
|
// Today
|
||||||
|
return pongo2.AsValue("today"), nil
|
||||||
|
case d >= 24:
|
||||||
|
return pongo2.AsValue("yesterday"), nil
|
||||||
|
case d < 0 && d >= -24:
|
||||||
|
return pongo2.AsValue("tomorrow"), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Default behaviour
|
||||||
|
return pongo2.ApplyFilter("naturaltime", in, param)
|
||||||
|
}
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
package pongo2addons
|
package pongo2addons
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"time"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
. "gopkg.in/check.v1"
|
. "gopkg.in/check.v1"
|
||||||
|
|
||||||
@ -30,25 +30,39 @@ func (s *TestSuite1) TestFilters(c *C) {
|
|||||||
|
|
||||||
// Timesince/timeuntil
|
// Timesince/timeuntil
|
||||||
base_date := time.Date(2014, time.February, 1, 8, 30, 00, 00, time.UTC)
|
base_date := time.Date(2014, time.February, 1, 8, 30, 00, 00, time.UTC)
|
||||||
future_date := base_date.Add(24 * 7 * 4 * time.Hour + 2 * time.Hour)
|
future_date := base_date.Add(24*7*4*time.Hour + 2*time.Hour)
|
||||||
c.Assert(pongo2.RenderTemplateString("{{ future_date|timeuntil:base_date }}",
|
c.Assert(pongo2.RenderTemplateString("{{ future_date|timeuntil:base_date }}",
|
||||||
pongo2.Context{"base_date": base_date, "future_date": future_date}), Equals, "4 weeks from now")
|
pongo2.Context{"base_date": base_date, "future_date": future_date}), Equals, "4 weeks from now")
|
||||||
|
|
||||||
base_date = time.Date(2014, time.February, 1, 8, 30, 00, 00, time.UTC)
|
base_date = time.Date(2014, time.February, 1, 8, 30, 00, 00, time.UTC)
|
||||||
future_date = base_date.Add(2 * time.Hour)
|
future_date = base_date.Add(2 * time.Hour)
|
||||||
c.Assert(pongo2.RenderTemplateString("{{ future_date|timeuntil:base_date }}",
|
c.Assert(pongo2.RenderTemplateString("{{ future_date|timeuntil:base_date }}",
|
||||||
pongo2.Context{"base_date": base_date, "future_date": future_date}), Equals, "2 hours from now")
|
pongo2.Context{"base_date": base_date, "future_date": future_date}), Equals, "2 hours from now")
|
||||||
|
|
||||||
base_date = time.Date(2014, time.February, 1, 8, 30, 00, 00, time.UTC)
|
base_date = time.Date(2014, time.February, 1, 8, 30, 00, 00, time.UTC)
|
||||||
future_date = base_date.Add(2 * time.Hour)
|
future_date = base_date.Add(2 * time.Hour)
|
||||||
c.Assert(pongo2.RenderTemplateString("{{ base_date|timesince:future_date }}",
|
c.Assert(pongo2.RenderTemplateString("{{ base_date|timesince:future_date }}",
|
||||||
pongo2.Context{"base_date": base_date, "future_date": future_date}), Equals, "2 hours ago")
|
pongo2.Context{"base_date": base_date, "future_date": future_date}), Equals, "2 hours ago")
|
||||||
|
|
||||||
// Natural time
|
// Natural time
|
||||||
base_date = time.Date(2014, time.February, 1, 8, 30, 00, 00, time.UTC)
|
base_date = time.Date(2014, time.February, 1, 8, 30, 00, 00, time.UTC)
|
||||||
future_date = base_date.Add(4 * time.Second)
|
future_date = base_date.Add(4 * time.Second)
|
||||||
c.Assert(pongo2.RenderTemplateString("{{ base_date|naturaltime:future_date }}",
|
c.Assert(pongo2.RenderTemplateString("{{ base_date|naturaltime:future_date }}",
|
||||||
pongo2.Context{"base_date": base_date, "future_date": future_date}), Equals, "4 seconds ago")
|
pongo2.Context{"base_date": base_date, "future_date": future_date}), Equals, "4 seconds ago")
|
||||||
|
|
||||||
|
// Naturalday
|
||||||
|
today := time.Date(2014, time.February, 1, 8, 30, 00, 00, time.UTC)
|
||||||
|
yesterday := today.Add(-24 * time.Hour)
|
||||||
|
tomorrow := today.Add(24 * time.Hour)
|
||||||
|
today_plus_3 := today.Add(3 * 24 * time.Hour)
|
||||||
|
c.Assert(pongo2.RenderTemplateString("{{ date|naturalday:today }}",
|
||||||
|
pongo2.Context{"date": today, "today": today}), Equals, "today")
|
||||||
|
c.Assert(pongo2.RenderTemplateString("{{ date|naturalday:today }}",
|
||||||
|
pongo2.Context{"date": yesterday, "today": today}), Equals, "yesterday")
|
||||||
|
c.Assert(pongo2.RenderTemplateString("{{ date|naturalday:today }}",
|
||||||
|
pongo2.Context{"date": tomorrow, "today": today}), Equals, "tomorrow")
|
||||||
|
c.Assert(pongo2.RenderTemplateString("{{ date|naturalday:today }}",
|
||||||
|
pongo2.Context{"date": today_plus_3, "today": today}), Equals, "3 days from now")
|
||||||
|
|
||||||
// Intcomma
|
// Intcomma
|
||||||
c.Assert(pongo2.RenderTemplateString("{{ 123456789|intcomma }}", nil), Equals, "123,456,789")
|
c.Assert(pongo2.RenderTemplateString("{{ 123456789|intcomma }}", nil), Equals, "123,456,789")
|
||||||
|
Loading…
Reference in New Issue
Block a user