Example #1
0
def add_folder(request, space_name, folder):
    """
    Create a new folder inside a previous created folder.
    
    **Arguments:**
        * request: Request object
        * space_name: string containing the space_name to which belongs this folder
        * folder: integer number that contains who is the parent folder
    
    **Templates:**
        ``fhy/fhy_form.html``
    
    **Decorators:**
        :func:`django.contrib.auth.decorators.login_required`
    """
    space = get_object_or_404(Space, user=request.user,
                                     slug=space_name)    
    
    if request.method == 'POST':
        try:
            # The id must exist, and we also use content_type in order get if there
            # exists at least one folder and not the root folder. Due to we use
            # content_types, we could also retrieve the root folder (we don't want this)
            get_object_or_404(Folder, id=folder,
                                      content_type__name='folder',
                                      )
        except Http404:
            try: 
                get_object_or_404(Folder, id=folder,
                                          content_type__name='root',)
            except Http404:
                raise Http404('No folder exists with that name')

        form = FolderForm(data = request.POST)
        
        try:
            if form.is_valid():
    
                new_folder = form.save(commit=False)
                new_folder.space = space
               
                content_type = ContentType.objects.select_related().get(name='folder')
                new_folder.content_type = content_type
                
                new_folder.object_id = int(folder.split('/')[0])
                new_folder.save()
                form.save_m2m()
                args = [space_name, folder]
                return HttpResponseRedirect(reverse('fhy_show_folder_content_list', args=args))
        except IntegrityError:
            form.non_field_errors = '''Error, another folder with the same 
                                        title exist. Please select another title.'''
    else:
        form = FolderForm()
    return render_to_response('fhy/fhy_form.html',
                              {'form': form,
                               'space': space},
                              context_instance = RequestContext(request))
Example #2
0
def add_folder_in_root(request, space_name):
    """
    This view will create folders in the root directory
    
    **Arguments:** 
        * request: Request object
        * space_name: string containing the space_name to which belongs this folder
    
    **Template:**
    ``fhy/fhy_form.html``
    
    **Decorators:**
        :func:`django.contrib.auth.decorators.login_required`
    """
    space = get_object_or_404(Space, slug=space_name,user=request.user)
    if request.method == 'POST':
        form = FolderForm(data=request.POST)
        try:
            if form.is_valid():
                new_folder = form.save(commit=False)
                new_folder.space = space
               
                content_type = ContentType.objects.select_related().get(name='root')
                new_folder.content_type = content_type
                
                root = Root.objects.select_related().get(space=new_folder.space)
                new_folder.object_id = root.id
                new_folder.save()
                form.save_m2m()
                
                return HttpResponseRedirect(reverse('fhy_show_root_list', args=[space_name]))
        except IntegrityError:
            form.non_field_errors = '''Error, another folder with the same 
                                        title exist. Please select another title.'''
    else:
        form = FolderForm()
    return render_to_response('fhy/fhy_form.html',
                              {'form': form,
                               'space': space,
                               'add': True},
                              context_instance = RequestContext(request))
Example #3
0
def edit_folder(request, space_name, folder, root=False):
    """
    View that edits an existing folder (no matter whether is a root folder or not).
    
     **Arguments:**
        * request: Request object
        * space_name: string containing the space_name to which belongs this folder
        * folder: integer number id that points to the folder we want to change
        * root: `0` or `1` indicating whether this folder belongs to the root folder
    
    **Templates:**
        * ``fhy_form.html``
        
        **Reverse:**
            * ``fhy_show_root_list``: returns to the root folder
            * ``fhy_show_folder_content_list``: returns to the folder indicated
    
    **Decorators:**
        :func:`django.contrib.auth.decorators.login_required`
    """
    folder_object = get_object_or_404(Folder, id=folder)
    root = bool(int(root))    
    
    if request.method == 'POST':
        form = FolderForm(data=request.POST, instance=folder_object)
        try:
            if form.is_valid():
                form.save()
                args = [space_name]
                if root:
                    template = 'fhy_show_root_list'
                else:
                    template = 'fhy_show_folder_content_list'
                    args.append(folder)
                return HttpResponseRedirect(reverse(template, args=args))
        except IntegrityError:
                form.non_field_errors = '''Error, another folder with the same 
                                            title exist. Please select another title.'''
    else:
        form = FolderForm(instance=folder_object)
    return render_to_response('fhy/fhy_form.html',
                              {'form': form,
                               'space': folder_object.space},
                              context_instance = RequestContext(request))