admin panel recent actions - django

I was wondering if I can get django admin (logged as super user) to display in some kind of "recent actions box", changes other users (non super users) made?
Thanx,
Luka

LogEntry.objects.log_action()
http://www.djangosnippets.org/snippets/1052/
Tying in to Django Admin's Model History

it was my problem too, and it has a simple answer
Just go this address and do the following;
Address: your virtulenv/lib/site-packages/django/contrib/admin/templates/admin
And find base.html, inside of this file edit after {% load Log %} and simply add this :
{% if user.is_superuser %}
{% get _admin_log (numbers of actions you want) as admin_log %}
And for the else do the same but at the end don't forget to add for_user user 😊

Related

Make navbar active on url and any url's following from it

I am trying to make my navbar element active if the user is on the current url and any url's leading from it.
For example, I want the navbar to be active on:
http://example.com/products
AND
http://example.com/products/discounted-items
I was using this:
{% if url_name == 'products' %}active{% endif %}
and was very happy till I realised that once I progress from the 'products' page to 'products/discounted-items' it would cease being active.
Is there a clean way to do this in django?
Thank you very much.
In your case you could simply do the following:
{% if 'products' in url_name %}active{% endif %}
Be aware that this also causes /productsfoobar to be active.
To prevent that you could use products/ instead of checking it without the trailing-slash.
In case (as i seen the page) if u are using Bootstrap u can add "active" in the class field in nav

Multiple views for webpage depending on authentication status in Django

I am currently working on a developing a web application that people can use to browse through book reviews, but if they login they can post comments on the book reviews, or post their own reviews of books.
While I have implemented posting new reviews. I am do not understand how to modify the method in my views.py file that renders a book review such that if the user is logged in, in shows all previous comments, a form for the user to post a comment and a logout button, and if the user is not logged in it simply shows the book review and a log in button.
You can tell if your user is logged in or not in your views like this:
if request.user.is_authenticated():
# logged in logic
return render_to_response("loggedin_templates.html")
else:
# not logged in logic
return render_to_response("not_loggedin_templates.html")
Or you can tell them in the templates like this:
{% if user.is_authenticated %}
<span>{{ user.username }}</span>
{% else %}
<li>login
{% endif %}

How to add calendar events with Django-Bootstrap-Calendar?

Django-Bootstrap-Calendar (https://github.com/sandlbn/django-bootstrap-calendar) is a Django implementation of this calendar application: http://bootstrap-calendar.azurewebsites.net/
However, I can't figure out how to add events to the calendar via Django. The "non-Django" bootstrap calendar is rather simple, you just add events to the JSON file that gets loaded.
Has anyone here added events to Django-Bootstrap-Calendar before? I emailed the author of the project, however, he never responded to me.
The closest post I found to my question is here: Getting a bootstrap-calendar component to work with Django However, the author figured out how to add entries to the non-Django calendar only.
Edit: I figured out how to add events via the Admin panel.
In models.py, you simply add:
###Admin
class CalendarEventAdmin(admin.ModelAdmin):
list_display = ["title", "url", "css_class", "start", "end"]
list_filler = ["title"]
admin.site.register(CalendarEvent,CalendarEventAdmin)
Then, in the admin panel you can add events (ultimately, I want non-admin users and anonymous visitors to be able to register events on the Calendar.)
However, this reveals a larger problem: the calendar doesn't display.
Edit 2: I have the calendar loading now by adding
{% load bootstrap_calendar %}
{% bootstrap_calendar_css %}
{% bootstrap_calendar_js language="template" %}
{% bootstrap_calendar_init language="template" %}
<!-- {% bootstrap_controls 'optional-css-classes' %} -->
{% bootstrap_calendar 'optional-css-classes' %}
to my index.html file. However, the calendar doesn't display the event(s) created with the Admin Panel.
Simply put
{% bootstrap_calendar_init language="template" %}
after
{% bootstrap_calendar 'optional-css-classes' %}

Including non-form in a Django SessionWizardView

Can a non-form web page be included in a django SessionWizardView?
For example, I want the user to FillOut Form1, Form2, Then View a web page (in same session) (click next), and then Form3? All this while maintaining the same session.
If so, how is this best accomplished? Any examples or snippets?
There's a fairly easy hack for this. Create a plain old form that has one field that is hidden from the user, has no content, and isn't required.
I do this:
class BlankForm(forms.Form):
nothing = forms.CharField(required=False, widget=HiddenInput)
Include it in your SessionWizardView call just like the other pages:
SessionWizardView.as_view([Form1, Form2, BlankForm, Form3])
In the template page you can use some logic like this to display info:
{% if wizard.steps.current == '2' %}
Whatever you want to show on the BlankForm
{% endif %}

Get user group in a template

I want to display a menu that changes according to the user group of the currently logged in user, with this logic being inside of my view, and then set a variable to check in the template to determine which menu items to show....I'd asked this question before, but my logic was being done in the template. So now I want it in my view...The menu looks as below
<ul class="sidemenu">
<li>General List </li>
<li>Sales List </li>
<li>Add a New Record </li>
<li>Edit Existing Record </li>
<li>Filter Records </li>
<li>Logout </li>
</ul>
Suppossing the user is management, they'll see everything...But assuming the user is in the group sales, they'll only see the first two and the last two items...and so on. I also want a dynamic redirect after login based on the user's group. Any ideas?
The standard Django way of checking permissions is by the individual permission flags rather than testing for the group name.
If you must check group names, being aware that Users to Groups is a many-to-many relationship, you can get the first group in the list of groups in your template with something like this:
{{ user.groups.all.0 }}
or using it like this in a conditional (untested but should work):
{% ifequal user.groups.all.0 'Sales' %}
...
{% endif %}
If you go with the preferred permission model you would do something like the following.
...
{% if perms.vehicle.can_add_vehicle %}
<li>Add a New Record </li>
{% endif %}
{% if perms.vehicle.can_change_vehicle %}
<li>Edit Existing Record </li>
{% endif %}
...
These are the permissions automatically created for you by syncdb assuming your app is called vehicle and the model is called Vehicle.
If the user is a superuser they automatically have all permissions.
If the user is in a Sales group they won't have those vehicle permissions (unless you've added those to the group of course).
If the user is in a Management group they can have those permissions, but you need to add them to the group in the Django admin site.
For your other question, redirect on login based on user group: Users to Groups is a many-to-many relationship so it's not really a good idea to use it like a one-to-many.
user.groups.all.0.name == "groupname"
Create a user_tags.py in your app/templatetags follow above:
# -*- coding:utf-8 -*-
from __future__ import unicode_literals
# Stdlib imports
# Core Django imports
from django import template
# Third-party app imports
# Realative imports of the 'app-name' package
register = template.Library()
#register.filter('has_group')
def has_group(user, group_name):
"""
Verifica se este usuário pertence a um grupo
"""
groups = user.groups.all().values_list('name', flat=True)
return True if group_name in groups else False
And finally in template use it:
{% if request.user|has_group:"Administradores"%}
<div> Admins can see everything </div>
{% endif %}
If you are working with Custom User Model (best practice with Django), you can create a method:
CustomUser(AbstractUser):
# Your user stuff
def is_manager(self):
return self.groups.filter(name='Management').exists()
Then inside your template you just call it this way:
{% if user.is_manager %}
{# Do your thing #}
{% endif %}
That method will be also useful validating permission in other parts of your code (views, etc.)