Opened 18 years ago

Closed 18 years ago

Last modified 15 years ago

#919 closed enhancement (wontfix)

[patch] Implement `random` template tag

Reported by: Tom Tobin <korpios@…> Owned by: Adrian Holovaty
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: no UI/UX: no

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@…> 18 years ago.
Implements random template tag
random_tag_weighted.patch (4.1 KB ) - added by Tom Tobin <korpios@…> 18 years ago.
Implements random template tag, with optional weighting.
random_tag_weighted_newstyle.patch (3.9 KB ) - added by Tom Tobin <korpios@…> 18 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@…> 18 years ago.
Implements random template tag, fixed for bisect / randint bug.
random_tag_current.diff (4.0 KB ) - added by Tom Tobin <korpios@…> 18 years ago.
Implements random template tag; current version (will overwrite with future updates).

Download all attachments as: .zip

Change History (13)

by Tom Tobin <korpios@…>, 18 years ago

Attachment: random_tag.patch added

Implements random template tag

comment:1 by Tom Tobin <korpios@…>, 18 years ago

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! :)

by Tom Tobin <korpios@…>, 18 years ago

Attachment: random_tag_weighted.patch added

Implements random template tag, with optional weighting.

comment:2 by Tom Tobin <korpios@…>, 18 years ago

Added a modified patch that implements optional weighted selection.

by Tom Tobin <korpios@…>, 18 years ago

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

comment:3 by Tom Tobin <korpios@…>, 18 years ago

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

by Tom Tobin <korpios@…>, 18 years ago

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

comment:4 by Tom Tobin <korpios@…>, 18 years ago

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.

by Tom Tobin <korpios@…>, 18 years ago

Attachment: random_tag_current.diff added

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

comment:5 by Tom Tobin <korpios@…>, 18 years ago

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 by Jacob, 18 years ago

Resolution: wontfix
Status: newclosed

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

comment:7 by korpios, 17 years ago

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

comment:8 by slig, 15 years ago

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