def add_image(request, space_name, folder): space = get_object_or_404(Space, user=request.user, slug=space_name) if request.method == 'POST': form = ImageFHYForm(request.POST, request.FILES) try: print "Image valid? %s" % form.is_valid() if form.is_valid(): new_image = form.save(commit=False) handle_uploaded_file(request.FILES['file']) new_image.space = space #Space.objects.filter(user=request.user).get(slug=space_name) new_image.pub_date = date.today() content_type = ContentType.objects.select_related().get(name='folder') new_image.content_type = content_type new_image.object_id = int(folder.split('/')[0]) new_image.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 image with the same title exist. Please select another title.''' else: form = ImageFHYForm() return render_to_response('fhy/fhy_image_form.html', {'form': form, 'space': space, 'add': True}, context_instance = RequestContext(request))
def edit_image(request, space_name, idimage, 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` """ image_object = get_object_or_404(ImageFHY, space__slug=space_name, id = idimage) space = get_object_or_404(Space, user=request.user, slug=space_name) root = bool(int(root)) if request.method == 'POST': form = ImageFHYForm(request.POST, request.FILES, instance=image_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']) image_object.pub_date = date.today() image_object.save() form.save_m2m() args = [space_name] if root: template = 'fhy_show_root_list' else: args.append(image_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 = ImageFHYForm(instance=image_object) return render_to_response('fhy/fhy_image_form.html', {'form': form, 'space': space, 'add': False, 'current': image_object}, context_instance= RequestContext(request))