Exemple #1
0
def edit_space(request, space_name):

    """
    Returns a form filled with the current space data to edit. Access to
    this view is restricted only to site and space administrators. The filter
    for space administrators is given by the edit_space permission and their
    belonging to that space.
    
    :attributes: - place: current space intance.
                 - form: SpaceForm instance.
                 - form_uncommited: form instance before commiting to the DB,
                   so we can modify the data.
    :param space_name: Space URL
    :rtype: HTML Form
    :context: form, get_place
    """
    place = get_object_or_404(Space, url=space_name)

    form = SpaceForm(request.POST or None, request.FILES or None, instance=place)
    entity_forms = EntityFormSet(
        request.POST or None, request.FILES or None, queryset=Entity.objects.all().filter(space=place)
    )

    if request.method == "POST":
        if form.is_valid() and entity_forms.is_valid():
            form_uncommited = form.save(commit=False)
            form_uncommited.author = request.user

            new_space = form_uncommited.save()
            space = form_uncommited.url

            ef_uncommited = entity_forms.save(commit=False)
            for ef in ef_uncommited:
                ef.space = space
                ef.save()

            # messages.success(request, _('Space edited successfully'))
            return redirect("/spaces/" + space)

    for i in request.user.profile.spaces.all():
        if i.url == space_name or request.user.is_staff:
            return render_to_response(
                "spaces/space_edit.html",
                {"form": form, "get_place": place, "entityformset": entity_forms},
                context_instance=RequestContext(request),
            )

    return render_to_response("not_allowed.html", context_instance=RequestContext(request))
Exemple #2
0
def create_space(request):

    """
    Returns a SpaceForm form to fill with data to create a new space. There
    is an attached EntityFormset to save the entities related to the space. Only
    site administrators are allowed to create spaces.
    
    :attributes: - space_form: empty SpaceForm instance
                 - entity_forms: empty EntityFormSet
    :rtype: Space object, multiple entity objects.
    :context: form, entityformset
    """
    space_form = SpaceForm(request.POST or None, request.FILES or None)
    entity_forms = EntityFormSet(request.POST or None, request.FILES or None,
                                 queryset=Entity.objects.none())
    
    if request.user.is_staff:    
        if request.method == 'POST':
            if space_form.is_valid() and entity_forms.is_valid():
                space_form_uncommited = space_form.save(commit=False)
                space_form_uncommited.author = request.user
                
                new_space = space_form_uncommited.save()
                space = get_object_or_404(Space, name=space_form_uncommited.name)
    
                ef_uncommited = entity_forms.save(commit=False)
                for ef in ef_uncommited:
                    ef.space = space
                    ef.save()
                # We add the created spaces to the user allowed spaces
    
                request.user.profile.spaces.add(space)
                #messages.success(request, _('Space %s created successfully.') % space.name)
                return redirect('/spaces/' + space.url)
    
        return render_to_response('spaces/space_add.html',
                              {'form': space_form,
                               'entityformset': entity_forms},
                              context_instance=RequestContext(request))
    else:
        return render_to_response('not_allowed.html',
                                  context_instance=RequestContext(request))