How to add custom control to django admin site? - django

Hey, I want to add custom buttom to the django admin site, and the users must be administrators, I do not expect you tell me how to do in each step, but please brief write the right approach, if you also can provide some reading URLs, that's gonna be awesome.

http://docs.djangoproject.com/en/dev/intro/tutorial02/ - "Customize the admin form" shows how to modify the admin section of an app.
http://docs.djangoproject.com/en/dev/topics/auth/ - "get_group_permissions()" will allow you to get the group permissions of a user. "has_perm()" returns true for a single permission.
http://docs.djangoproject.com/en/dev/howto/custom-management-commands/ - how to customize django management
http://docs.djangoproject.com/en/dev/ref/contrib/admin/ - "ModelAdmin" can be used to specify a template for the admin site
Using these, you can put together a custom template with any custom controls and only show them if the user has a specific permission.

You can copy from django /django/contrib/admin/templates/admin/base.html (or base_site.html) to your project /templates/admin/base.html then customize base.html
This part {% block footer %}<div id="footer"></div>{% endblock %}
Also this peace of template could help {% if user.is_active and user.is_staff %}

Related

How to change admin color in Django based on whether person is an Superuser or not?

I would like to implement a function in Django admin, that if a person logged is superuser then the color in admin is different. How can I approach it?
I don't want 'normal' users to have access to admin, but I want to have 2 levels of access - superuser that can change everything (and add more personnel etc) and normal staff level, who can't add other employees and is limited in what he can do.
How can I approach the issue?
Can I simply add flag somewhere which states that if logged user is superuser then use different/additional css or something?
You can use the is_superuser check on the User object to determine if the user is a superuser or not a superuser.
Below is a sample code that you can use in your HTML
{% if request.user.is_superuser %}
<!---Add color code for admin--->
{% else %}
<!---Add color code for other users---->
{% endif %}

How to embed the user registration template within the frontpage with Flask-security?

Flask-security provides a template for registering new users.
I want to include a form in the frontpage of my site for user registrations. Can I somehow embed flask-security template + endpoint/function in my home endpoint? Or do I have to create a new template and function from scratch?
Thanks!
You can add {% include "security/register_user.html" %} to your homepage template. As long as you set the endpoint for registering users, it will work.

adding code for a Django admin field

I have a tinymce textarea in my django admin and I need users to be able to upload images (via AJAX) that will be linked in this textarea.
That would be accomplished by adding an "Image Upload" button (it's already working) in the top of the "Content" textarea.
What's the recommended way of doing that?
I can think on 2 solutions:
extending change_form and replacing {% for fieldset in adminform %} for the actual fields... and when it's the content field, I add this value
dynamically adding this button with javascript (find out where the content field is and add a <div> before it)
A better solution, if possible, would be to override just this specific field in the admin templates. Is that possible? Or are there better solutions?
PS: this field is not part of the DB (it just uploads one or more images, saves it to the storage and returns a link that will be included on the tinymce).
maybe you could use this
formfield_overrides from the djangodocs
if not, create a subclass of modelform and overide the form of admin with ModelAdmin.form.

Django admin checkbox with multiple select

I have a Django app. Pretty basic one at that.
In the model I have a class for items and a class for groups. The groups has a many to many for the items:
items = models.ManyToManyField(item, verbose_name="list of items", max_length=100000, blank=True)
When I add this to the admin section I would like to have a checkbox with multiple select. Is this possible. All of the solutions I have looked at don't sow it in the context of an admin page too. The Django admin page is all I need it to work on as I am not making any custom, public facing pages.
What is the easiest and most simple solution to replace the multiple select box with a multiple checkbox.
PS. I am relatively in-experienced with Django so I need to see what I need to import in the model and admin.
Thanks
If you know how to do it for a standard modelform, you know how to do it in an admin page too, as they are based on normal forms.
Just define the form as normal, then tell the admin to use it for your model:
class MyModelAdmin(admin.ModelAdmin):
form = MyFormWithTheMultipleSelect
way based on overriding admin templates
/myproject/templates/admin/myapp/mymodel/change_form.html
{% extends "admin/change_form.html" %}
{{ block.super }}
<script type='text/javascript' src='/media/js/jquery.js'></script>
<script>
$(document).ready(function(){
myselect = $("#id_M2M_FIELD_NAME");
// here you manipulating with your multiple select,
// and convert it to checkboxes, or something else.
})
</script>
{% endblock %}

Dynamic User Menu in Django

Is there a way to have a user menu that changes according to the permissions assigned to the user group a user belongs to? I am thinking of something that checks for these permissions at the view level, and also removes menu options a user does not have permission to.
Yes it is possible to access the user object in the template and check if the user is staff like this:
{% if user.is_staff %}
<li>
Admin
</li>
{% endif %}
This would be an example where your menu where li items of links. The admin link would only be rendered for users with is_staff status. The same could be done with is_authenticated.
Django is build to have logic and presentation separated, so if you want to do some more fine grained control of the menu, I would suggest doing the logic inside of the view, and then set a variable that you can check in the template to determine which menus to show.
For the most part, django's admin already doesnt give you links to things you can't do.
Django grappelli (a django admin skin) implements some sort of bookmarking, if that is what you mean http://code.google.com/p/django-grappelli/