Snippet List
Mixin to support pagination when randomizing querysets.
Requirements: Postgres, Django Sessions
Note: This shouldn't be used on large complex datasets. It utilizes the relatively slow method of '?' randomized sorting. Use with caution.
Todo: MySQL support, Support for larger datasets
- django
- session
- pagination
- random
- postgres
- mixin
- postgresql
- cbv
- seeded
This is a snippet for a simple CAPTCHA. A random image from a list of images is shown, and the form checks if the correct solution was given.
Normally I would use django-simple-captcha or maybe reCAPTCHA, but in this case I wanted to have a number of fixed images, nothing dynamically generated.
I wanted to include the contact form in multiple pages, most of which are `direct_to_template` generic views.
However, passing the random image to the `extra_context` of `direct_to_template` didn't work, because the value was only initialized once on startup.
Therefore I pass the list of possible choices to `extra_context`, and use the template filter `|random` to select one image. The form's clean method will check if the correct solution was given when `form.is_valid()` is called in the view. If not, the view will display a new captcha.
Of course there are other, more elegant solutions like a custom template tag or overriding the generic view, but this works fine for me. Using a fixed number of images will probably not provide good protection for sites that are of much interest to spammers, but for smaller sites it should be sufficient.
You can see the CAPTCHA in action at [http://www.lackieren-in-polen.de/](http://www.lackieren-in-polen.de/)
To put obfuscated primary keys in any class, simply inherit from this one. For example:
class Offer(ObfuscatedPKModel)
You can match for these bigint primary keys in your urls.py like this:
'^offer/(?P<offer_pk>[0-9\-]+)$'
- models
- model
- random
- abstract
- primary-key
- obfuscation
- obfuscated
- ID
Manager Mixin to implement get_random() in your models.
You can override get_objects to tune the queriset
To use, define your class:
class MyManager(models.Manager, RandomObjectManager):
DEFAULT_NUMBER = 5 # I can change that
def get_objects(self):
return self.filter(active=True) # Only active models plz
class MyModel(models.Model):
active = models.BooleanField()
objects = MyManager()
Now you can do:
MyModel.objects.get_random()
These are template tags meant to support the construction of text in a random or seeded random (reproducible) way. Two tags are provided: `seed_randomization` and `any`.
Only seed the randomization if you wish to have the options generated the same way each time. Only necessary once per request, if done early enough in the rendering process.
Example without seeding:
<p>
{% any %}
One day
Once upon a time
In a galaxy far, far away
{% endany %}
a young foolish {% any %}programmer|lawyer|Jedi{% endany %}
{% any %}
set out
began his quest
ran screaming
{% endany %}
to pay his stupid tax.
</p>
# Possible outcomes:
<p>In a galaxy far, far away a young foolish lawyer set out to pay his stupid tax.</p>
<p>One day a young foolish programmer ran screaming to pay his stupid tax.</p>
Be sure to read the documentation in the code.
Generate model data with this django management command!
Data is generated based off of the model field types. And will also correctly generate foreign key's to other randomly generated records for join tables. And generate images with random colors and random words in the image - for image fields.
You can supply quite a few parameters that control how the data is generated. And you can control it per field, per model. Or you can supply your own callable function which you can return your own random data.
**SEE THE DOCS / EXAMPLE IN THE CODE SNIPPET FOR AVAILABLE OPTIONS, AND HOW TO CONTROL GENERATED DATA PARAMETERS**
You can generate data that looks like real content, without having to write fixtures and such. Just generate it!
It can generate data for these types of fields:
EmailField
SlugField
BooleanField
DateField
DateTimeField
TimeField
IntegerField
DecimalField
TextField
CharField
IPAddressField
URLField
SmallIntegerField
PositiveSmallIntegerField
PositiveIntegerField
ImageField
There are also a few callables included that you can use to generate this kind of data:
zip, extended zip, hashkey and uuid
It's also worth noting that I keep this project up to date on my own git repository. There are a few fonts you'll need if you want to generate imaages, included in my git repo.
http://gitweb.codeendeavor.com/?p=dilla.git;a=summary
- model
- random
- data
- management
- command
- lipsum
Adds a templatetag that works like an if block, but . The one and only argument is a float that reflects the percentage chance. It defaults to .2, %20.
{% sometimes %}
<img src='spy_behind_sniper.jpg'/>
{% else %}
<img src='sniper.jpg'/>
{% endsometimes %}
-- or --
{% sometimes .001 %}
You win!
{% else %}
Sorry, not a winner. Play again!
{% endsometimes %}
-- or --
{% sometimes .5 %}
This shows up half the time.
{% endsometimes %}
12 snippets posted so far.