Exemplo n.º 1
0
def remove_user_tag_unsafe(request, user_id, tag_id):
    tag = get_object_or_404(CoreTag.objects.using("ak"), id=tag_id)
    agent = get_object_or_404(CoreUser.objects.using("ak"), id=user_id)
    affected_actions = CoreAction.objects.using("ak").filter(
        page__pagetags__tag__id=tag_id, user__id=agent.id).select_related("page")
    if request.method == "GET":

        ## check whether any OTHER tags will be removed from the user as a consequence
        affected_actions_page_ids = [action.page.id for action in affected_actions]
        affected_actions_tags = CoreTag.objects.using("ak").filter(
            pagetags__page__id__in=affected_actions_page_ids)
        orphaned_tags = set()
        for tag in affected_actions_tags:
            other_actions = CoreAction.objects.using("ak").filter(
                page__pagetags__tag__id=tag.id, user__id=agent.id).exclude(
                page__id__in=affected_actions_page_ids).exists()
            if not other_actions:
                orphaned_tags.add(tag)
        return locals()

    are_you_sure = request.POST.get("are_you_sure")
    if not are_you_sure:
        return redirect(".")
    try:
        are_you_sure = int(are_you_sure)
    except ValueError:
        return redirect(".")        
    affected_actions = list(affected_actions)
    if are_you_sure != len(affected_actions):
        return redirect(".")

    for action in affected_actions:
        rest.delete_action(action.id)

    return redirect("detail", user_id)
Exemplo n.º 2
0
def remove_user_tag_safe(request, user_id, tag_id):    
    allowed_tag = get_object_or_404(AllowedTag, ak_tag_id=tag_id)
    action = get_object_or_404(CoreAction.objects.using("ak"),
                               page__id=allowed_tag.ak_page_id,
                               user__id=user_id)
    rest.delete_action(action.id)
    if request.is_ajax():
        return HttpResponse(action.id)
    return redirect("detail", user_id)