def activate_policy(request): """Activate a selected policy, requires privacy.activate_policy priviledges. """ if not request.user.has_perm('privacy.activate_policy'): return HttpResponseRedirect(reverse('privacy:versions')) if request.method == "POST": try: policy = get_object_or_404(Policy, id=request.POST['active']) except ValueError: # not a valid ID raise Http404 if not policy.active: # need to change active policy, first, deactivate existing # actives, then set the new one active. And create LogEntries. qa = Policy.objects.filter(active=True) for _p in qa: LogEntry.objects.log_action(request.user.id, Policy.contenttype().id, _p.id, force_text(_p), CHANGE, change_message="deactivate") qa.update(active=False) LogEntry.objects.log_action(request.user.id, Policy.contenttype().id, policy.id, force_text(policy), CHANGE, change_message="activate") policy.active = True policy.save() return HttpResponseRedirect(reverse('privacy:versions'))
def activate_policy(request): """Activate a selected policy, requires privacy.activate_policy priviledges. """ if not request.user.has_perm('privacy.activate_policy'): return HttpResponseRedirect(reverse('privacy.views.versions')) if request.method == "POST": try: policy = get_object_or_404(Policy, id=request.POST['active']) except ValueError: # not a valid ID raise Http404 if not policy.active: # need to change active policy, first, deactivate existing # actives, then set the new one active. And create LogEntries. qa = Policy.objects.filter(active=True) for _p in qa: LogEntry.objects.log_action(request.user.id, Policy.contenttype().id, _p.id, force_unicode(_p), CHANGE, change_message="deactivate") qa.update(active=False) LogEntry.objects.log_action(request.user.id, Policy.contenttype().id, policy.id, force_unicode(policy), CHANGE, change_message="activate") policy.active = True policy.save() return HttpResponseRedirect(reverse('privacy.views.versions'))
def post_policy(request): if not (request.user.has_perm('privacy.add_policy') and request.user.has_perm('privacy.add_comment')): return HttpResponseForbidden("not sufficient permissions") p = Policy.objects.create(text=request.POST['content']) c = Comment.objects.create(text=request.POST['comment'], policy=p, who=request.user) LogEntry.objects.log_action(request.user.id, Policy.contenttype().id, p.id, force_text(p), ADDITION) LogEntry.objects.log_action(request.user.id, Comment.contenttype().id, c.id, force_text(c), ADDITION) return redirect(reverse('privacy:show', kwargs={'id': p.id}))
def post_policy(request): if not (request.user.has_perm('privacy.add_policy') and request.user.has_perm('privacy.add_comment')): return HttpResponseForbidden("not sufficient permissions") p = Policy.objects.create(text=request.POST['content']) c = Comment.objects.create(text=request.POST['comment'], policy=p, who=request.user) LogEntry.objects.log_action(request.user.id, Policy.contenttype().id, p.id, force_unicode(p), ADDITION) LogEntry.objects.log_action(request.user.id, Comment.contenttype().id, c.id, force_unicode(c), ADDITION) return redirect(reverse('privacy.views.show_policy', kwargs={'id': p.id}))
def show_policy(request, id=None): """Display the currently active policy, or, if id is given, the specified policy. """ try: if id is None: p = Policy.objects.get(active=True) else: p = Policy.objects.get(id=int(id)) except (Policy.DoesNotExist, ValueError): p = None if p is not None and p.active: logs = LogEntry.objects.filter(content_type=Policy.contenttype()) logs = logs.filter(object_id=p.id, action_flag=CHANGE, change_message='activate') logs = logs.order_by('-action_time') activation = logs.values_list('action_time', flat=True)[0] else: activation = None c = {"policy": p, "activation": activation} return render(request, 'privacy/show-policy.html', c)
def show_policy(request, id=None): """Display the currently active policy, or, if id is given, the specified policy. """ try: if id is None: p = Policy.objects.get(active=True) else: p = Policy.objects.get(id=int(id)) except: p = None if p is not None and p.active: logs = LogEntry.objects.filter(content_type=Policy.contenttype()) logs = logs.filter(object_id=p.id, action_flag=CHANGE, change_message='activate') logs = logs.order_by('-action_time') activation = logs.values_list('action_time', flat=True)[0] else: activation = None c = {"policy": p, "activation": activation} return render(request, 'privacy/show-policy.html', c)
def versions(request): """Show all policies including their active times. If the user has permissions, offers to activate a policy, comment, or create a new policy. """ policies = Policy.objects.order_by('-pk') policies = policies.annotate(noc=Count('comments')) policies = policies.prefetch_related('comments') los = LogEntry.objects.filter(content_type=Policy.contenttype()) details = {} for lo in los.order_by('action_time'): if lo.object_id not in details: detail = {} details[lo.object_id] = detail else: detail = details[lo.object_id] if lo.action_flag == ADDITION: detail['created'] = lo.action_time elif lo.action_flag == CHANGE: if lo.change_message not in ('deactivate', 'activate'): continue if 'active_time' not in detail: detail['active_time'] = [] if lo.change_message == 'activate': detail['active_time'].append([lo.action_time]) else: detail['active_time'][-1].append(lo.action_time) def do_policies(_pols, _details): for _p in _pols: _d = _p.__dict__.copy() _d.update(_details[str(_p.id)]) _d['comments'] = _p.comments.all() yield _d c = {"policies": do_policies(policies, details)} return render(request, 'privacy/versions.html', c)
def versions(request): """Show all policies including their active times. If the user has permissions, offers to activate a policy, comment, or create a new policy. """ policies = Policy.objects.order_by('-pk') policies = policies.annotate(noc=Count('comments')) policies = policies.select_related('comments') los = LogEntry.objects.filter(content_type=Policy.contenttype()) details = {} for lo in los.order_by('action_time'): if lo.object_id not in details: detail = {} details[lo.object_id] = detail else: detail = details[lo.object_id] if lo.action_flag == ADDITION: detail['created'] = lo.action_time elif lo.action_flag == CHANGE: if lo.change_message not in ('deactivate', 'activate'): continue if 'active_time' not in detail: detail['active_time'] = [] if lo.change_message == 'activate': detail['active_time'].append([lo.action_time]) else: detail['active_time'][-1].append(lo.action_time) def do_policies(_pols, _details): for _p in _pols: _d = _p.__dict__.copy() _d.update(_details[str(_p.id)]) _d['comments'] = _p.comments.all() yield _d c = {"policies": do_policies(policies, details)} return render(request, 'privacy/versions.html', c)