def edit_file(request, space_name, idfile, root=0): """ This function will allow you to update a file inside the folder ``folder``. It edits also a :class:`URLCommunityResource <community.models.URLCommunityResource>` existing object. **Arguments:** * request: Request object * space_name: string containing the space_name to which belongs this folder * idfolder: integer number that contains the parent folder (dentro de que folder reside) **Templates:** ``fhy/fhy_file_form.html`` **Decorators:** :func:`django.contrib.auth.decorators.login_required` """ file_object = get_object_or_404(FileFHY, space__slug=space_name, id = idfile) space = get_object_or_404(Space, user=request.user, slug=space_name) root = bool(int(root)) if request.method == 'POST': form = FileFHYForm(request.POST, request.FILES, instance=file_object) try: if form.is_valid(): form.save(commit=False) # This condition ensures that there are always a file associated. # We cannot do it in def clean_file since we wouldn't be able to check # the existence of a previous file. In this case, since we are editing, # there must have been a previous file or adD_file would have raised an # alert. if request.FILES: #file_object.file.delete() handle_uploaded_file(request.FILES['file']) file_object.pub_date = date.today() file_object.save() form.save_m2m() args = [space_name] if root: template = 'fhy_show_root_list' else: args.append(file_object.content_object.id) template = 'fhy_show_folder_content_list' return HttpResponseRedirect(reverse(template, args=args)) except MultiValueDictKeyError: form.non_field_errors = 'Please, you must submit a file.' else: form = FileFHYForm(instance=file_object) return render_to_response('fhy/fhy_file_form.html', {'form': form, 'space': space, 'add': False}, context_instance= RequestContext(request))
def add_file(request, space_name, folder): """ This function will upload a file inside the folder ``folder``. **Arguments:** * request: Request object * space_name: string containing the space_name to which belongs this folder * folder: integer number that contains the parent folder (dentro de que folder reside) **Templates:** ``fhy/fhy_file_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': form = FileFHYForm(request.POST, request.FILES) try: if form.is_valid(): new_file = form.save(commit=False) # when doing form.save, it relates the form.file with new_file.file handle_uploaded_file(request.FILES['file']) new_file.space = space new_file.pub_date = date.today() content_type = ContentType.objects.select_related().get(name='folder') new_file.content_type = content_type new_file.object_id = int(folder.split('/')[0]) new_file.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 file with the same title exist. Please select another title.''' except MultiValueDictKeyError: form.non_field_errors = 'Please, you must submit a file.' else: form = FileFHYForm() return render_to_response('fhy/fhy_file_form.html', {'form': form, 'space': space, 'add': True}, context_instance= RequestContext(request))
def add_file_in_root(request, space_name, datatype): """ This view will create files in the root directory **Arguments:** * request: Request object * space_name: string containing the space_name to which belongs this file **Template:** * ``fhy/fhy_file_form.html`` * Reverse: `fhy_show_root_list` **Decorators:** :func:`django.contrib.auth.decorators.login_required` """ space = get_object_or_404(Space, user=request.user, slug=space_name) # Since this template is used for either Files and Images, we must # determine which template to apply since the beginning if int(datatype) == PDF: template = 'fhy/fhy_file_form.html' elif int(datatype) == IMAGE: template = 'fhy/fhy_image_form.html' else: raise Http404("Invalid page") if request.method == 'POST': if int(datatype) == PDF: form = FileFHYForm(request.POST, request.FILES) elif int(datatype) == IMAGE: form = ImageFHYForm(request.POST, request.FILES) try: if form.is_valid(): new_file = form.save(commit=False) handle_uploaded_file(request.FILES['file']) # if int(datatype) == PDF: # handle_uploaded_file(request.FILES['file']) # elif int(datatype) == IMAGE: # handle_uploaded_file(request.FILES['file']) new_file.space = space new_file.pub_date = date.today() #Space.objects.filter(user=request.user).get(slug=space_name) content_type = ContentType.objects.select_related().get(name='root') new_file.content_type = content_type root = Root.objects.select_related().get(space=new_file.space) new_file.object_id = root.id new_file.object_id = root.id new_file.save() form.save_m2m() args = [space_name,] return HttpResponseRedirect(reverse('fhy_show_root_list', args=args)) except IntegrityError: form.non_field_errors = '''Error, another file with the same title exist. Please select another title.''' else: if int(datatype) == PDF: form = FileFHYForm() template = 'fhy/fhy_file_form.html' elif int(datatype) == IMAGE: form = ImageFHYForm() template = 'fhy/fhy_image_form.html' else: raise Http404("Invalid page") return render_to_response(template, {'form': form, 'space': space, 'add': True}, context_instance= RequestContext(request))