I am working on some apps that use row level permissions. For many permission tests I intend to make custom tags so I can locate the permission test used by the template and by the view in a common module.
However, frequently, before I decide what the actual end-tag is going to be I need a way to expression a more powerful 'if' structure in the template language.
Frequently the existing if and if_has_perm tags are just not powerful enough to express these things.
This may make the template language unacceptably more like a programming language but it has helped me quickly modify complex permission statements without having to next and repeat four or five clauses.
As you can see this is intended to work with django off of the row level permissions branch.
Here is an example of a template that is using the 'fancy_if' statement:
`
{% fancy_if or ( and not discussion.locked not discussion.closed "asforums.post_discussion" discussion ) "asforums.moderate_forum" discussion.forum %}
<li><a href="./create_post/?in_reply_to={{ post.id }}">{% icon "comment_add" "Reply to post" %}</a></li>
{% end_fancy_if %}
{% fancy_if or "asforums.moderate_forum" discussion.forum ( and not discussion.closed ( or eq post.author user eq discussion.author user ) ) %}
{% fancy_if or "asforums.moderate_forum" discussion.forum eq post.author user %}
<li><a href="{{ post.get_absolute_url }}update/">{% icon "comment_edit" "Edit Post" %}</a></li>
{% end_fancy_if %}
<li><a href="{{ post.get_absolute_url }}delete/">{% icon "comment_delete" "Delete Post" %}</a></li>
{% end_fancy_if %}
`
This script automates the process of installing Lighttpd, Flup, Django and a Django app (with init script) on a TextDrive shared server.
Usage instructions: http://textusers.com/wiki/Installing_Django
# Changelog
* April 5, 2007
* Added (www.\)? conditional in Lighty conf
* Updated to Django 0.96
**I won't be able to debug this until tonight... so if you see this message there may still be bugs in this**
This Middleware replaces the behavior of the APPEND_SLASH in the CommonMiddleware. Please set APPEND_SLASH = False if you are going to use this Middleware.
Add the key defined by APPENDSLASH to the view_kwargs and a True or False to determine the behaivor of the appended slashes. For instance I set my DEFAULTBEHAVIOR to the default of True, then to override that behaivor I add 'AppendSlash':False to the URLs that I wish to have no slash.
**Example**
`
urlpatterns = patterns('some_site.some_app.views',
(r'^test/no_append$','test_no_append',{'AppendSlash':False}),
(r'^test/append/$','test_append'),
)
`
In this type of model you are allowed to define a model with a generic type.
For instance, a location can be an address, GPS coordinates, an intersection and many others types. Using a many to many field, models can have multiple locations without worrying about the type of location referencing. New locations types can be added without changing the references in other models.
This code is also used in Django's built in ContentTypes app.
[Snippet #2](http://www.djangosnippets.org/snippets/2/) demonstrated some cool tricks possible with manager methods. This example shows how to assign and use a custom manager method.
In this snippet the `belongs_to_user` method returns an Account queryset containing only those accounts associated with the specified user. The method is useful because it hides the implementation of User in the Account model.
Line 17 associates the custom manager with the Account model.
I was faced with the fact that I wanted to post 2 paragraph-long summaries on one of my sites, and this is what I did (you could of course cut it down earlier, but I'd say this belongs to what is called "template logic")
Use like so:
{% load myExtraModule %}
{{ blogpost.content|paragraphs:"2" }}
The lines filter works the exact same way, and you might want to improve on these a bit, I don't maintain them as I don't use them anymore.
This module is not aimed to replace the newforms, but I would like manually write html code,
and just need a pure validate module, so I write this, and many things may be similar with
newforms. So if you like me would only need a pure validator module, you can use it.
And it has some different features from newforms:
1. Support validator_list parameter, so you could use it just like the old manipuator class
2. Supply easy method, such as `validate_and_save()`, so you can pass a request object, and
get a tuple result `(flag, obj_or_error)`, if the `flag` is `True`, then the next value
is an object; and if the `flag` is `False`, then the next value is error message.
3. Each field has a `validate_and_get` method, and it'll validate first and then return the
result, maybe an object or error message. Just like above.
4. SplitDateTimeField is somewhat different from the newforms. For example::
c = SplitDateTimeField('date', 'time')
print c.validate_and_get({'date':'2006/11/30', 'time':'12:13'})
So the first parameter is DateField's field_name, and the second parameter is TimeField's
field_name.
5. Add yyyy/mm/dd date format support
6. Support default value of a field. You can add a default value for a field, if this field
is not required, and the value is *empty*, Validator will return the default value.
This module is new, so many things could be changed.
This will help to secure the sensitive secrets, token, api keys, etc from logger.
As we know there is security issue when we include the sensitive information to the logger in case logger got leaked/hacked.
Before:
```
INFO ('192.168.1.1', 33321) - "WebSocket /ssh?token=abcdefg&width=20&heigh20"
```
After:
```
INFO ('192.168.1.1', 33321) - "WebSocket /ssh?token=********&width=20&heigh20"
```
If you have multiple items in a list and want them to be displayed as human readable list of items, this will add the proper punctuation to generate the text. You'll need to provide a conjugation to the end of the list like "or" or "and"; it defaults to "or".
Intended use:
`{% for item in items %}{{item}}{% list_punctuation forloop "and" %}{% endfor %}`
* If items was `['a']`; the template would return `a`.
* If items was `['a', 'b']`; the template would return `a and b`.
* If items was `['a', 'b', 'c']`; the template would return `a, b, and c`.
add JSONRequestMiddleware to your enabled middleware in Django settings. Now, in your view functions, you can call request.json() to get a parsed json body! json is consumed lazily, and cached.
Creates a model serializer class on the fly, just taking the model (class) as its argument.
My use case: When importing data from spreadsheets, the DRF serializers are an easy way to create model instances from a dictionary. This function saves me from creating a custom serializer each time I add a new importer.
Using `__all__` is dangerous ;-)
Don't forget to replace "self.image" by your image field name from your model ex ( self.cover )
replace Product by your model name
works pretty well :)
Sometimes a plain-text help-text isn't sufficient, and it's handy to be able to add links to pages, documentation or external websites.
This javascript snippet can be added to your page, in combination with adding a class to your help text in your template. This assumes you're using jQuery on your website.
Field template snippet:
```
{% if field.help_text %}<p class="help-text">{{ field.help_text }}</p>{% endif %}
```
On document ready, this will convert the markdown-style links into anchor tags, allowing you to have richer help text for your users
A simple jQuery javascript that collapses all stacked inline rows for better handling of large inline fieldsets.
It also adds "Show"/"Hide"-buttons for showing/hiding each row, which could be customized and styled using css.
**Usage (see below for example):**
Include the javascript on your admin page, together with jQuery, and it'll automatically affect all stacked inlines.
**Works with**
Django 3.1.4 (Might work with other versions with or without adjustments, but not tested)
**Use example:**
*admin.py:*
class DateInline(admin.StackedInline):
model = Date
extra = 10
class EventAdmin(admin.ModelAdmin):
inlines = [DateInline]
class Media:
js = ['js/collapsed-stacked-inlines.js']
admin.site.register(Event, EventAdmin)
You're looking at the most-bookmarked snippets on the site; if you'd like to help useful snippets show up here, sign up for an account and you'll get your own bookmarks list.