Ejemplo n.º 1
0
def group_remove_maps_data(request, slug):
    group = get_object_or_404(Group, slug=slug)
    if not group.user_is_role(request.user, "manager"):
        raise Http404()
    
    ctx = {}
    
    if request.method == "POST":
        map_form = GroupMapForm(request.POST)
        layer_form = GroupLayerForm(request.POST)
        
        if map_form.is_valid() and layer_form.is_valid():
            for m in map_form.cleaned_data["maps"]:
                GroupMap.objects.get(map=m).delete()
                if group.access != "private":
                    action.send(request.user, verb="removed map", action_object=m, target=group)
            map_form.cleaned_data["maps"] = []
            for l in layer_form.cleaned_data["layers"]:
                GroupLayer.objects.get(layer=l).delete()
                if group.access != "private":
                    action.send(request.user, verb="removed layer", action_object=l, target=group)
            layer_form.cleaned_data["layers"] = []
            
    # Even if we're removing data, recreate the forms so that they are missing the removed elements.
    map_ids = GroupMap.objects.filter(group=group).values_list("map", flat=True)
    layer_ids = GroupLayer.objects.filter(group=group).values_list("layer", flat=True)
    
    map_form = GroupMapForm()
    map_form.fields["maps"].queryset = Map.objects.filter(id__in=map_ids)
    
    layer_form = GroupLayerForm()
    layer_form.fields["layers"].queryset = Layer.objects.filter(id__in=layer_ids)
    
    ctx["map_form"] = map_form
    ctx["layer_form"] = layer_form
    
    ctx.update({
        "object": group,
        "members": group.member_queryset(),
        "is_member": group.user_is_member(request.user),
        "is_manager": group.user_is_role(request.user, "manager"),
    })
    
    ctx = RequestContext(request, ctx)
    return render_to_response("groups/group_remove_maps_data.html", ctx)
Ejemplo n.º 2
0
def group_add_layers(request, slug):
    group = get_object_or_404(Group, slug=slug)
    
    ctx = {}
    if request.method == "POST":
        form = GroupLayerForm(request.POST)
        
        if form.is_valid():
            ctx["layers_added"] = []
            for l in form.cleaned_data["layers"]:
                GroupLayer.objects.get_or_create(layer=l, group=group)
                if group.access != "private":
                    action.send(request.user, verb="added layer", action_object=l, target=group)
                ctx["layers_added"].append(l.title)
    else:
        form = GroupLayerForm()
    
    current_layers = GroupLayer.layers_for_group(group)
    
    form.fields["layers"].queryset = Layer.objects.filter(
            owner=request.user
            ).exclude(
            id__in=[li for li in current_layers.values_list('id', flat=True)]
    )
    
    
    ctx["form"] = form
    ctx.update({
        "object": group,
        "members": group.member_queryset(),
        "is_member": group.user_is_member(request.user),
        "is_manager": group.user_is_role(request.user, "manager"),
        "current_layers": current_layers,
    })
    ctx = RequestContext(request, ctx)
    return render_to_response("groups/group_add_layers.html", ctx)