You have pagination for some blog. At the bottom of the page you have space for seven page links. This module tries to always fill out those slots. It handles the corner cases for first page and last page etc.
The logic for this is complex and sadly the Django pagination module does not deal with this. Also, it's a hell to implement this in Django templates. Believe me.
See pydoc for detailed documentation. Running tests was done with `doctest`.
Sample usage for using decorator
`@json_response(ajax_required=True, login_required=True)`
`def subscribe(request):`
` return {"status":"success"}`
Converts a function returning dict into json response. Does is_ajax check and user authenticated check if set in flags. When function returns HttpResponse does nothing.
This template tag was inspired by http://djangosnippets.org/snippets/592/, but with improvements in the syntax it is used with to be more function-like, and avoiding the problem of conditional recursion as noted in http://djangosnippets.org/comments/cr/15/592/#c2472.
The syntax for using it can be seen in the docstring of the defrecurse() function. Additionally, a magic "level" variable is used to indicate the level of recursion, starting with 0 for the outermost level.
This should theoretically allow for nested recursion, but the inner {% recurse %} call cannot call the outer {% defrecurse %} block.
I thought it would be useful to have a `get_addr()` method available on request objects, similar to the `get_host()` provided by Django. This middleware will add a `get_addr()` method to requests which uses the `X-Forwarded-For` header (useful if you're behind a proxy) if it's present and you have the `USE_X_FORWARDED_FOR` header set to `True` (default is `False`) and otherwise will use the `REMOTE_ADDR` environment variable. Note that if you are *not* behind a proxy and have `USE_X_FORWARDED_FOR` set to `True`, then clients can spoof their IP by simply setting the `X-Forwarded-For header`.
The code is Django 1.4 version of code based on the [Django 1.3 snippet](http://djangosnippets.org/snippets/2593/) that speeds up Django's admin pages with postgres back-end for big tables (> few hundred thousands of records).
Based on [onecreativenerd](http://djangosnippets.org/users/onecreativenerd/) code.
Sometimes it's a real pain to use the @login_required decorator all over the views of a complicated site. This middleware requires login on every page by default and supports a list of regular expression to figure out the exceptions. This way you don't have to worry about forgetting to decorate a view.
This snippet requires LOGIN_URL to be set in settings.py, and optionally allows you fill out LOGIN_EXEMPT_URLS, a tuple of regular expressions (similar to urls.py) that lists your exceptions.
Example:
LOGIN_EXEMPT_URLS = (
r'^about\.html$',
r'^legal/', # allow the entire /legal/* subsection
)
This code allows you to register a model to Django that is only used for unit testing.
It will not exist in the regular Django workflow. After the tests executed, the Django settings are restored.
Usage:
1. Change `tests.py` into a `tests` package.
2. Place a `models.py` in the `tests` package.
3. Use the following code below to enable it.
Example:
class MyTest(CustomSettingsTestCase):
new_settings = dict(
INSTALLED_APPS=(
'django.contrib.contenttypes',
'django.contrib.auth',
'app_to_test',
'app_to_test.tests',
)
)
Based on http://djangosnippets.org/snippets/1011/ as Django 1.4 version
Simple tag to check which page we are on, based on resolve: useful to add an 'active' css class in menu items that needs to be aware when they are selected.
Typical usage is like:
`
<ul>
<li class="{% active request "myapp:myview1" %}">My View 1</li>
<li class="{% active request "myapp:myview2" %}">My View 2</li>
</ul>
`
This method will return an inline formset class that validates values across the given field are unique among all forms. For instance:
ApprovedUserFormSet = inlineformset_factory(Request, ApprovedUser, formset=unique_field_formset('email'), form=ApprovedUserForm)
Will make sure all ApprovedUser objects created for the Request have unique "email" fields.
Validate Ukraine telephone numbers in popular formats:
+380 XX XXX-XX-XX
0XX-XXX-XX-XX
(0XX) XXX-XX-XX
This snippet fixes the errors found in
http://djangosnippets.org/snippets/2579/
Validates and cleans UK telephone numbers. Number length is checked, and numbers are cleaned into a common format. For example, "+44 (0)1234 567890" will be stored as "01234 567890".
Can reject premium numbers (0912 312 3123) or service numbers (1471, 118 118) with UKPhoneNumberField(reject=('premium', 'service'))
Can reject multiple number types so you can tune the form input to accept only landline or only mobile, or whatever combination you want.
Corrects the errors found in http://djangosnippets.org/snippets/1207/ and adds extra functionality and detail to the code found at http://djangosnippets.org/snippets/2809/
In particular, this version rejects individual invalid area codes and caters for area codes with mixed-length numbering in fine-grained detail.
**Uses info from:** [here](http://www.aa-asterisk.org.uk/index.php/Regular_Expressions_for_Validating_and_Formatting_UK_Telephone_Numbers)
Validates and cleans UK telephone numbers. Number length is checked, and numbers are cleaned into a common format. For example, "+44 (0)1234 567890" will be stored as "01234 567890"
Can reject premium numbers (0912 312 3123) or service numbers (1471, 118 118) with UKPhoneNumberField(reject=('premium', 'service'))
Corrects the errors found in http://djangosnippets.org/snippets/1207/
You're looking at the top-rated snippets currently on the site; if you'd like to contribute, sign up for an account and you'll be able to rate any snippet you see.