Snippet List
Despite warning coming from django developers, I'm still using admin classes to quickly get into reverse engineering databases.
One feature is missing: searching into fields thanks to a regex.
One dirty solution I found is to overwrite get_search_results. But most of the code comes from django itself.
If anyone has a better idea ;)
**Usage:**
1. works since get_search_results is part of ModelAdmin (1.5 if I remember well)
2. Inherit your Admin class from RegexModelAdmin
3. enclose by / the field you want to regex with:
`search_fields = ['/field/', ]`
Simple middleware to complement the built in redirect middleware app. Add this after the contrib.redirect middleware - this will be fired if a 404 is triggered and the contrib.redirect fails to find a suitable redirect.
Useful if you want to add the redirects into the DB - and/or don't have access to the .htaccess script or whatever HTTP server based redirect machinery your site runs off.
You simply add in regex 'old_path' and 'new_path' entries into the same redirect app, but this will try to do a regex find and replace - i.e.
>>> r = new Redirect()
>>> r.old_path = '/my/test/path/([a-z]+)/with/regex/'
>>> r.new_path = '/my/result/path/$1/with/regex/'
>>> r.save()
this will convert:
/my/test/path/abcdef/with/regex/
into:
/my/result/path/abcdef/with/regex/'
also works with query strings:
/index.php?section=products
>>> old_path = '/index.php/\?section=([a-z]+)'
#need to add in the forward slash if ADD_SLASHES = True in your settings, and escape the question mark.
>>> new_path = '/section/$1/'
converts the url to '/section/products/'
Simple password validation for user registration - requires that password be 7 or more characters and contain both letters and numbers. Original validation with regex approach developed by kurtis. Optimized no-regex version based on code from watchedman ran as fast or significantly faster on all systems on which we tested it.
- registration
- model
- regex
- user
- validation
- form
- password
This function takes a pattern with groups and replaces them with the given args and/or kwargs. Example:
IMPORTANT: this code is NOT to use replacing Django's reverse function. The example below is just to illustrate how it works.
For a given pattern '/docs/(\d+)/rev/(\w+)/', args=(123,'abc') and kwargs={}, returns '/docs/123/rev/abc/'.
For '/docs/(?P<id>\d+)/rev/(?P<rev>\w+)/', args=() and kwargs={'rev':'abc', 'id':123}, returns '/docs/123/rev/abc/' as well.
When both args and kwargs are given, raises a ValueError.
I wanted a way to allow flexible phone number validation while making sure the saved data was uniform.
ex.
With:
RegexFormatField(r'^\(?(?P<area>\d{3})\)?[-\s.]?(?P<local>\d{3})[-\s.]?(?P<subscriber>\d{4})$',
format='%(area)s %(local)s-%(subscriber)s')
input:
(444) 444-4444
444 444-4444
444-444-4444
444.444.4444
4444444444
output:
444 444-4444
This template filter, "jumptime" will find any timecodes in a chunk of text and convert them to links that can be used to jump a video player to that point. E.g., If there is the string "3:05", it will be converted into a link that can be used to jump to that point. This is similar to what youtube does.
For information on how to implement, see Django's [custom template tag information](http://docs.djangoproject.com/en/dev/howto/custom-template-tags/#howto-custom-template-tags).
You'd use this with some javascript like this:
`jQuery(document).ready(function(){
jQuery('a.jumpToTime').bind('click',function(){
player.sendEvent('PLAY');
player.sendEvent('SEEK', jQuery(this).attr('value'));
});
});`
Outputs the contents of the block if the second argument matches (or not, depending on the tag) the regular expression represented by the first argument.
Usage:
{% ifregex "^/comments/" request.path %}
...
{% endifregex %}
{% ifnotregex "^/comments/" request.path %}
...
{% else %}
...
{% endifnotregex %}
- template
- templatetag
- regex
- ifregex
Format Number Based on Regular Expression
**Examples**
>*{{.1234|regex_comma_number:'%.4f'}}
>*'0.1234'
>*{{100|regex_comma_number:'%i'}}
>*'100'
>*{{ 234.5678|regex_comma_number:'%.4f'}}
>*'234.5678'
>*{{234.5678|regex_comma_number:'$%.4f'}}
>*'$234.5678'
>*{{1000|regex_comma_number:'%i'}}
>*'1,000'
>*{{1234.5678|regex_comma_number:'%.4f'}}
>*'1,234.5678'
>*{{1234.5678|regex_comma_number:'$%.4f'}}
>*'$1,234.5678'
>*{{1000000|regex_comma_number:'%i'}}
>*'1,000,000'
>*{{1234567.5678|regex_comma_number:'%.4f'}}
>*'1,234,567.5678'
>*{{1234567.5678|regex_comma_number:'$%.4f'}}
>*'$1,234,567.5678'
>*{{-100|regex_comma_number:'%i'}}
>*'-100'
>*{{-234.5678|regex_comma_number:'%.4f'}}
>*-234.5678'
>*{{-234.5678|regex_comma_number:'$%.4f'}}
>*'$-234.5678'
>*{{-1000|regex_comma_number:'%i'}}
>*'-1,000'
>*{{-1234.5678|regex_comma_number:'%.4f'}}
>*'-1,234.5678'
>*{{-1234.5678|regex_comma_number:'$%.4f'}}
>*'$-1,234.5678'
>*{{-1000000|regex_comma_number:'%i'}}
>*'-1,000,000'
>*{{-1234567.5678|regex_comma_number:'%.4f'}}
>*'-1,234,567.5678'
>*{{-1234567.5678|regex_comma_number:'$%.4f'}}
>*'$-1,234,567.5678'`
- templatetag
- regex
- format
- comma
- number
This will perform a regular expression search/replace on a string in your template.
`{% load replace %}`
`{{ mystring|replace:"/l(u+)pin/m\1gen" }}`
If:
`mystring = 'lupin, luuuuuupin, and luuuuuuuuuuuuupin are lè pwn'` then it will return:
`mugen, muuuuuugen, and muuuuuuuuuuuuugen are lè pwn`
The argument is in the following format:
[delim char]regexp search[delim char]regexp replace
- regex
- regular-expressions
- templates
- filters
13 snippets posted so far.