예제 #1
0
def add_composition(request):
    """
    Creates a CompositionList by selecting the 
    :class:`FriendListGroupName <friendycontrol.models.FriendListGroupName>` that
    will form this :class:`CompositionList <friendycontrol.models.CompositionList>` object.
    
    **Template:**
    ``friendycontrol/composition_form.html``
    
    **Context:**
        ``form_composition``: a select multiple box
        
        ``form_readable``: a checkbox to mark whether you give
                             or deny the readable permission
                             
        ``form_app``: the application to which you give readable access
        
        ``add``: ``True`` if you want to re-mark that is an *add* operation
    
    **Decorators:**
        :func:`django.contrib.auth.decorators.login_required` 
    """
    if request.method == 'POST':
        form_composition = CompositionListForm(request.user, data=request.POST)
        form_readable = ReadableForm(data=request.POST)
        if form_composition.is_valid() and form_readable.is_valid():
            composition = form_composition.save(commit=False)
            composition.owner = request.user
            #composition.spaces = form_app.perm_space
            composition.save()
            form_composition.save_m2m()
            
            readable = form_readable.save(commit=False)
            readable.foreign_group = composition
            readable.save()
            
            for space in form_composition.cleaned_data['spaces']:
                app = RelationModel()
                app.readable = readable
                app.content_object = space 
                app.save()
            
            return HttpResponseRedirect(reverse('friendy_show_main_compositions'))
    else:
        form_composition = CompositionListForm(request.user)
        form_readable = ReadableForm()
    return render_to_response('friendycontrol/composition_form.html',
                              {'form_composition': form_composition,
                               'form_readable': form_readable,
                               'add': True},
                               context_instance=RequestContext(request))
예제 #2
0
def edit_composition(request, id):
    """
    Edits the CompositionList by changing the elements that belongs to it.
    
    **Template:**
    ``friendycontrol/composition_form.html``
    
    **Context:**
        ``form_composition``: a select multiple box
        
        ``form_readable``: a checkbox to mark whether you give
                             or deny the readable permission
                             
        ``form_app``: the application to which you give readable access
        
        ``add``: ``True`` if you want to re-mark that is an *add* operation
    
    **Decorators:**
        :func:`django.contrib.auth.decorators.login_required` 
    """
    try:
        composition_list = get_object_or_404(CompositionList, id=id,
                                                              owner = request.user)
        readable = composition_list.readable_set.get(foreign_group=composition_list.id)
        relationmodel_list = readable.relationmodel_set.all()   
    except (Http404, Readable.DoesNotExist, RelationModel.DoesNotExist):
        raise Http404
    
    if request.method == 'POST':
        form_composition = CompositionListForm(request.user,
                                               instance=composition_list,
                                               data = request.POST)
        form_readable = ReadableForm(instance = readable, data = request.POST)
        #form_app = AppForm(request.user, data = request.POST)
        if form_composition.is_valid() and form_readable.is_valid():
            form_composition.save()
            form_readable.save()
            
            for space in form_composition.cleaned_data['spaces']:
                try:
                    # If it exists, then do nothing, since nothing has changed
                    app = get_object_or_404(RelationModel, readable=readable,
                                                           object_id = space.id)
                except Http404:
                    # If it does not exist, then create the new value
                    app = RelationModel()
                    app.readable = readable
                    app.content_object = space 
                    app.save()
            
            # We have created all the elements, but we must delete those that are no
            # longer part of this group
            for element in relationmodel_list:
                try:
                    app = relationmodel_list.get(id=element.id)
                    empty = form_composition.cleaned_data['spaces'].filter(id=app.object_id)
                    
                    if len(empty)==0:
                        app.delete()

                except Space.DoesNotExist:
                    print "Exception: %s" % (element)
                    raise Space.DoesNotExist('Hubo un problema en la app friendycontrol')            
            return HttpResponseRedirect(reverse('friendy_show_main_compositions'))
    else:
        form_composition = CompositionListForm(request.user, instance=composition_list)
        form_readable = ReadableForm(instance = readable)
        
        #initial = {'perm_space':[value[0] for value in relationmodel_list.values_list('object_id')]}
        #form_app = AppForm(request.user, initial)
    return render_to_response('friendycontrol/composition_form.html',
                              {'form_composition': form_composition,
                               'form_readable': form_readable,
                               #'form_app': form_app,
                               'add': False},
                               context_instance=RequestContext(request))