Ejemplo n.º 1
0
def collection_edit(request, collection_id):
    collection = Collection.objects.get(id=collection_id)

    # permissions check
    if not collection.user_can_edit(request.user):
        return render_to_response('denied.html', context_instance=RequestContext(request))
        
    # standard form handling
    if request.method == 'POST':
        form = CollectionForm(request.POST, instance=collection)
        
        if form.is_valid():
            form.save()
            
            request.user.message_set.create(message='Collection updated')
            
            return HttpResponseRedirect(reverse('library_collection',
                                                kwargs={'collection_id': collection_id,
                                                        'slug': collection.slug()}
                                               ))
    else:
        form = CollectionForm(instance=collection)
        
    return render_to_response("library/collection_edit.html", 
        {'collection': collection,
         'form': form},
        context_instance=RequestContext(request))
Ejemplo n.º 2
0
def collection_create(request, parent_id=None):
    parent = None
    if parent_id:
        parent = Collection.objects.get(id=parent_id)
        if not parent.user_can_edit(request.user):
            return render_to_response('denied.html', context_instance=RequestContext(request))

    if request.method == 'POST':
        form = CollectionForm(request.POST)
        
        if form.is_valid():
            collection = form.save(commit=False)
            collection.owner = request.user
            
            if parent:
                collection.parent = parent
                
                order = 0
                children = parent.get_children()
                for c in children:
                    if c.ordering > order:
                        order = c.ordering
                order = order + 1
                collection.ordering = order
            
            collection.save()
        
            request.user.message_set.create(message='Collection created!')
            return HttpResponseRedirect(reverse('library_collection',
                                        kwargs={'collection_id': collection.id,
                                                'slug': collection.slug()}
                                       ))

    else:
        form = CollectionForm()
        
    return render_to_response("library/collection_edit.html", 
        {'form': form,
         'create': True,
         'parent': parent},
        context_instance=RequestContext(request))