Dynamic User Menu in Django - 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/

Related

Trying to have link shown if user owns post or if user is staff

I'm trying to hide an Edit and Delete button from posts made unless it's the user who made it, or the user is staff/superuser. I got it to work in my views.py to allow staff users to edit, but I can't get it to show in the html.
{% if request.user == room.host and room.host.is_staff %}
This is what I have currently but it's not showing me the links as a staff user.

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 %}

Django 1.8 Auth/Registration Redux - How can I make #login_required and .is_authenticated() checks pass only if the user is Active?

I'm using Django Registration Redux for user activation. From what I understand, the 'Active' field in the User model tracks whether or not the user has activated their account via the Django Registration Redux activation email, but doesn't do much else. Is this correct?
If so, how can I continue to use the built-in Django auth features to check if the user is logged in AND they have activated their account?
Thanks!
The active flag can be set outside of Django Registration Redux. It can be toggled by administrators in the Django admin (e.g. change from active to inactive).
For example in Django's authentication docs I can see that:
Only active users can reset their password
The built in AuthenticationForm only allows active users to log in.
There may be other places that use the active flag as well.
The login_required decorator does not check the active flag. See this question for a way to check that the user is logged in and active.
If you have a custom user model, you could override the is_authenticated() method to check the is_active flag. Otherwise, you can do both checks in your view or template
{% if user.is_active and user.is_authenticated %}

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 %}

How to add custom control to django admin site?

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 %}