예제 #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)
예제 #2
0
def group_add_maps(request, slug):
    group = get_object_or_404(Group, slug=slug)
    
    ctx = {}
    if request.method == "POST":
        form = GroupMapForm(request.POST)
        
        if form.is_valid():
            ctx["maps_added"] = []
            for m in form.cleaned_data["maps"]:
                GroupMap.objects.get_or_create(map=m, group=group)
                if group.access != "private":
                    action.send(request.user, verb="added map", action_object=m, target=group)
                ctx["maps_added"].append(m.title)
    else:
        form = GroupMapForm()
    
    current_maps = GroupMap.maps_for_group(group)
    
    form.fields["maps"].queryset = Map.objects.filter(
            owner=request.user
            ).exclude(
            id__in=[mi for mi in current_maps.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_maps": current_maps,
    })
    ctx = RequestContext(request, ctx)
    return render_to_response("groups/group_add_maps.html", ctx)