From 4e07dfc9bb10dff42ce345feaae8d4979c290f9f Mon Sep 17 00:00:00 2001 From: Ian Byrd Date: Sat, 2 Sep 2017 02:39:48 +0300 Subject: [PATCH 1/3] Filter `random` added. Random returns a random item out of the input slice. --- filters.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/filters.go b/filters.go index 0091981..e660da1 100644 --- a/filters.go +++ b/filters.go @@ -3,6 +3,7 @@ package pongo2addons import ( "bytes" "fmt" + "math/rand" "regexp" "strings" "time" @@ -22,6 +23,7 @@ func init() { pongo2.RegisterFilter("filesizeformat", filterFilesizeformat) pongo2.RegisterFilter("truncatesentences", filterTruncatesentences) pongo2.RegisterFilter("truncatesentences_html", filterTruncatesentencesHTML) + pongo2.RegisterFilter("random", filterRandom) // Markup pongo2.RegisterFilter("markdown", filterMarkdown) @@ -218,6 +220,24 @@ func filterTruncatesentencesHTML(in *pongo2.Value, param *pongo2.Value) (*pongo2 return pongo2.AsSafeValue(newOutput.String()), nil } +func filterRandom(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) { + if !in.CanSlice() { + return nil, &pongo2.Error { + Sender: "filter:random", + OrigError: error.New("input is not sliceable"), + } + } + + if in.Len() <= 0 { + return nil, &pongo2.Error { + Sender: "filter:random", + OrigError: error.New("input slice is empty"), + } + } + + return in.Index(rand.Intn(in.Len())), nil +} + func filterTimeuntilTimesince(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) { basetime, isTime := in.Interface().(time.Time) if !isTime { From 4a11f8dd755244640217a6c36a2ea3aaed63abdd Mon Sep 17 00:00:00 2001 From: Ian Byrd Date: Sat, 2 Sep 2017 02:51:18 +0300 Subject: [PATCH 2/3] Typo fix, tests, README.md updated. --- README.md | 5 +++-- filters.go | 18 +++++++++--------- filters_test.go | 6 ++++++ 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index c975351..23ca9d8 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ [![Build Status](https://travis-ci.org/flosch/pongo2-addons.svg?branch=master)](https://travis-ci.org/flosch/pongo2-addons) [![Gratipay](http://img.shields.io/badge/gratipay-support%20pongo-brightgreen.svg)](https://gratipay.com/flosch/) -Official filter and tag add-ons for [pongo2](https://github.com/flosch/pongo2). Since this package uses +Official filter and tag add-ons for [pongo2](https://github.com/flosch/pongo2). Since this package uses 3rd-party-libraries, it's in its own package. ## How to install and use @@ -24,10 +24,11 @@ All additional filters/tags will be registered automatically. - **[filesizeformat](https://docs.djangoproject.com/en/dev/ref/templates/builtins/#filesizeformat)** (human-readable filesize; takes bytes as input) - **[slugify](https://docs.djangoproject.com/en/dev/ref/templates/builtins/#slugify)** (creates a slug for a given input) - **truncatesentences** / **truncatesentences_html** (returns the first X sentences [like truncatechars/truncatewords]; please provide X as a parameter) + - **random** (returns a random element of the input slice) - Markup - **markdown** - + - Humanize - **[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) diff --git a/filters.go b/filters.go index e660da1..c9d2ee5 100644 --- a/filters.go +++ b/filters.go @@ -222,19 +222,19 @@ func filterTruncatesentencesHTML(in *pongo2.Value, param *pongo2.Value) (*pongo2 func filterRandom(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) { if !in.CanSlice() { - return nil, &pongo2.Error { - Sender: "filter:random", - OrigError: error.New("input is not sliceable"), + return nil, &pongo2.Error{ + Sender: "filter:random", + OrigError: errors.New("input is not sliceable"), } } - + if in.Len() <= 0 { - return nil, &pongo2.Error { - Sender: "filter:random", - OrigError: error.New("input slice is empty"), - } + return nil, &pongo2.Error{ + Sender: "filter:random", + OrigError: errors.New("input slice is empty"), + } } - + return in.Index(rand.Intn(in.Len())), nil } diff --git a/filters_test.go b/filters_test.go index b2b45ca..6d146f5 100644 --- a/filters_test.go +++ b/filters_test.go @@ -89,4 +89,10 @@ func (s *TestSuite1) TestFilters(c *C) { c.Assert(getResult("{{ text|truncatesentences_html:3 }}", pongo2.Context{ "text": `
  • This is a first sentence with a 4.50 number.
  • The second one is even more fun! Isn't it?
  • Last sentence, okay.
`}), Equals, `
  • This is a first sentence with a 4.50 number.
  • The second one is even more fun! Isn't it?
`) + + // Random + c.Assert(getResult("{{ array|random }}", + pongo2.Context{"array": []int{42}}), + Equals, "42") + } From c0a092cc2ceb2a040a0f6bf4f314f244cf2f4501 Mon Sep 17 00:00:00 2001 From: Ian Byrd Date: Sat, 2 Sep 2017 02:58:18 +0300 Subject: [PATCH 3/3] Seeding with current unixtime. --- filters.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/filters.go b/filters.go index c9d2ee5..6474eff 100644 --- a/filters.go +++ b/filters.go @@ -18,6 +18,8 @@ import ( ) func init() { + rand.Seed(time.Now().UTC().UnixNano()) + // Regulars pongo2.RegisterFilter("slugify", filterSlugify) pongo2.RegisterFilter("filesizeformat", filterFilesizeformat)