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)
def index(request): if request.method == 'POST': p_id = request.POST.get('id', False) if p_id: name = request.POST.get('name', "") try: user = Policy.objects.get(name=name) except ObjectDoesNotExist: user = False if user.name == name: return HttpResponseRedirect(p_id) else: return render(request, 'index.html', {'msg': 'A user with that ID was not found. Please try again.', 'form': form.PolicyForm()}) else: name = request.POST.get('id_name', False) view = request.POST.get('viewers', False) displen = request.POST.get('viewlen', False) delete = request.POST.get('deleted', False) datause = request.POST.get('datause', False) tracking = request.POST.get('track', False) gps = request.POST.get('gps', False) if name and view and displen and delete and datause and tracking and gps: policy_str = "(" if view == "ADS": policy_str += "(ADS or PVB or ME) and " elif view == "PVB": policy_str += "(PVB or ME) and " else: policy_str += "(ME) and " if displen == "720": policy_str += "(720 or 168 or 24 or 1) and " elif displen == "168": policy_str += "(168 or 24 or 1) and " elif displen == "24": policy_str += "(24 or 1) and " else: policy_str += "(1) and " if datause == "TARGET": policy_str += "(TARGET or STATS or PARTNER or SITE) and " elif datause == "PARTNER": policy_str += "(STATS or PARTNER or SITE) and " elif datause == "STATS": policy_str += "(STATS or SITE) and " else: policy_str += "(SITE) and " if delete == "KEEP": policy_str += "(DELETED OR KEEP) and " else: policy_str += "(KEEP) and " if tracking == "TRACK": policy_str += "(TRACK or NOTRACK) and " else: policy_str += "(NOTRACK) and " if gps == "LOCATE": policy_str += "(LOCATE or NOLOCATE)" else: policy_str += "(NOLOCATE)" policy_str += ")" print "-------------------------------------------------------" print "The user policy is:" print policy_str p = Policy(name=name, policy=policy_str) p.save() new_user = Policy.objects.get(name=name) p_id = new_user.id return HttpResponseRedirect(str(p_id)) else: return render(request, 'index.html', {'msg': 'There was a mistake in your form entries. Please try again.', 'form': form.PolicyForm()}) return render(request, 'index.html', {'form': form.PolicyForm()})
def index(request): if request.method == 'POST': p_id = request.POST.get('id', False) if p_id: name = request.POST.get('name', "") try: user = Policy.objects.get(name=name) except ObjectDoesNotExist: user = False if user.name == name: return HttpResponseRedirect(p_id) else: return render( request, 'index.html', { 'msg': 'A user with that ID was not found. Please try again.', 'form': form.PolicyForm() }) else: name = request.POST.get('id_name', False) view = request.POST.get('viewers', False) displen = request.POST.get('viewlen', False) delete = request.POST.get('deleted', False) datause = request.POST.get('datause', False) tracking = request.POST.get('track', False) gps = request.POST.get('gps', False) if name and view and displen and delete and datause and tracking and gps: policy_str = "(" if view == "ADS": policy_str += "(ADS or PVB or ME) and " elif view == "PVB": policy_str += "(PVB or ME) and " else: policy_str += "(ME) and " if displen == "720": policy_str += "(720 or 168 or 24 or 1) and " elif displen == "168": policy_str += "(168 or 24 or 1) and " elif displen == "24": policy_str += "(24 or 1) and " else: policy_str += "(1) and " if datause == "TARGET": policy_str += "(TARGET or STATS or PARTNER or SITE) and " elif datause == "PARTNER": policy_str += "(STATS or PARTNER or SITE) and " elif datause == "STATS": policy_str += "(STATS or SITE) and " else: policy_str += "(SITE) and " if delete == "KEEP": policy_str += "(DELETED OR KEEP) and " else: policy_str += "(KEEP) and " if tracking == "TRACK": policy_str += "(TRACK or NOTRACK) and " else: policy_str += "(NOTRACK) and " if gps == "LOCATE": policy_str += "(LOCATE or NOLOCATE)" else: policy_str += "(NOLOCATE)" policy_str += ")" print "-------------------------------------------------------" print "The user policy is:" print policy_str p = Policy(name=name, policy=policy_str) p.save() new_user = Policy.objects.get(name=name) p_id = new_user.id return HttpResponseRedirect(str(p_id)) else: return render( request, 'index.html', { 'msg': 'There was a mistake in your form entries. Please try again.', 'form': form.PolicyForm() }) return render(request, 'index.html', {'form': form.PolicyForm()})