updating AUTHORS file
[ganeti_webmgr.git] / object_log / views.py
blob1d1ff406f267774ab93dcd430b18901f81d138b8
1 from django.contrib.auth.models import User, Group
2 from django.contrib.contenttypes.models import ContentType
3 from django.contrib.auth.decorators import login_required
4 from django.http import HttpResponseForbidden
5 from django.template.context import RequestContext
6 from django.db.models.query_utils import Q
7 from django.shortcuts import render_to_response, get_object_or_404
9 from object_log.models import LogItem
12 def list_for_object(request, obj):
13 """
14 Lists all actions that involve a given object. This will check
15 LogItem.object1, LogItem.object2, and LogItem.object3.
17 This view does not
18 include any permission checks as it is intend
20 @param request: http request
21 @param obj: object to retrieve log items for
22 """
23 content_type = ContentType.objects.get_for_model(obj)
25 q = Q(object_type1=content_type, object_id1=obj.pk) \
26 | Q(object_type2=content_type, object_id2=obj.pk) \
27 | Q(object_type3=content_type, object_id3=obj.pk) \
29 log = LogItem.objects.filter(q).distinct()
31 return render_to_response('object_log/log.html',
32 {'log':log,
33 'context':{'user':request.user}
35 context_instance=RequestContext(request))
38 @login_required
39 def list_for_user(request, pk):
40 """
41 Provided view for listing actions performed on a user.
43 This may only be used by superusers
44 """
45 if not request.user.is_superuser:
46 return HttpResponseForbidden('You are not authorized to view this page')
48 user = get_object_or_404(User, pk=pk)
49 return list_for_object(request, user)
52 @login_required
53 def list_for_group(request, pk):
54 """
55 Provided view for listing actions performed on a group.
57 This may only be used by superusers
58 """
59 if not request.user.is_superuser:
60 return HttpResponseForbidden('You are not authorized to view this page')
62 group = get_object_or_404(Group, pk=pk)
63 return list_for_object(request, group)
66 @login_required
67 def list_user_actions(request, pk):
68 """
69 List all actions a user has performed. This view can only be used by
70 superusers.
72 @param request: HttpRequest
73 @param pk: Primary Key of User to get log for.
74 """
75 if not request.user.is_superuser:
76 return HttpResponseForbidden('You are not authorized to view this page')
78 user = get_object_or_404(User, pk=pk)
79 log_items = LogItem.objects.filter(user=user)
81 return render_to_response('object_log/log.html',
82 {'log':log_items, 'context':{'user':request.user}},
83 context_instance=RequestContext(request))