Opened 10 years ago

Closed 9 years ago

Last modified 6 years ago

#919 closed enhancement (wontfix)

[patch] Implement `random` template tag

Reported by: Tom Tobin <korpios@…> Owned by: adrian
Component: Template system Version:
Severity: normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

This patch implements a random template tag, doing for tag blocks what the random filter does for list items.

A random block begins with random and ends with endrandom; items are separated with or tags. Each possible tag block has an equal possibility of being selected, just as with the random filter.

I'm not sure or is an optimal separator tag for the random choices, but it was the best I've been able to come up with; suggestions for a superior tag name would be greatly appreciated.

I'm looking to additionally implement a "weighted" random option, which will allow one to specify unequal possibilities for random selection -- e.g., of two choices, the first is selected four times more often than the second. This will require a helper class, but I'm not certain which module it belongs in (perhaps django.core.extensions?).

Attachments (5)

random_tag.patch (2.1 KB) - added by Tom Tobin <korpios@…> 10 years ago.
Implements random template tag
random_tag_weighted.patch (4.1 KB) - added by Tom Tobin <korpios@…> 10 years ago.
Implements random template tag, with optional weighting.
random_tag_weighted_newstyle.patch (3.9 KB) - added by Tom Tobin <korpios@…> 10 years ago.
Implements random template tag, with optional weighting, updated for new-style tag registration.
random_tag_weighted_newstyle_fixed.patch (3.9 KB) - added by Tom Tobin <korpios@…> 10 years ago.
Implements random template tag, fixed for bisect / randint bug.
random_tag_current.diff (4.0 KB) - added by Tom Tobin <korpios@…> 10 years ago.
Implements random template tag; current version (will overwrite with future updates).

Download all attachments as: .zip

Change History (13)

Changed 10 years ago by Tom Tobin <korpios@…>

Implements random template tag

comment:1 Changed 10 years ago by Tom Tobin <korpios@…>

Silly me; I realized that I don't need a helper class to implement the weighted random option, as the logic could just go in RandomNode -- although such a helper class might make it easier to implement such weighted random selection in multiple places (e.g., in the random filter as well). Either way, I'm still curious as to where such a thing would go! :)

Changed 10 years ago by Tom Tobin <korpios@…>

Implements random template tag, with optional weighting.

comment:2 Changed 10 years ago by Tom Tobin <korpios@…>

Added a modified patch that implements optional weighted selection.

Changed 10 years ago by Tom Tobin <korpios@…>

Implements random template tag, with optional weighting, updated for new-style tag registration.

comment:3 Changed 10 years ago by Tom Tobin <korpios@…>

Added an updated patch to take into account new-style tag registration introduced in [1443].

Changed 10 years ago by Tom Tobin <korpios@…>

Implements random template tag, fixed for bisect / randint bug.

comment:4 Changed 10 years ago by Tom Tobin <korpios@…>

In further testing, I realized that the weighted distribution was off; it turns out I had assumed that random.randint was exclusive of its upper bound, whereas it is inclusive of both bounds. Furthermore, the proper bisect function is bisect_right, not bisect_left.

Changed 10 years ago by Tom Tobin <korpios@…>

Implements random template tag; current version (will overwrite with future updates).

comment:5 Changed 10 years ago by Tom Tobin <korpios@…>

Added a check for the optional weights to ensure they are positive integers.

All further patch updates will be overwrites of random_tag_current.diff; I didn't realize the clutter I was creating. :-)

comment:6 Changed 9 years ago by jacob

  • Resolution set to wontfix
  • Status changed from new to closed

This is out of the scope of the built-in tag set (but still kinda cool).

comment:7 Changed 8 years ago by korpios

  • Reporter changed from Tom Tobin <korpios@…> to Tom Tobin <korpios@…>

comment:8 Changed 6 years ago by slig

Hi,

Just for the record: With Django 1.1, the code only worked here after I removed the line 681: parser.delete_first_token()

Thanks for the code!

Note: See TracTickets for help on using tickets.
Back to Top