def add_or_edit_generic_view(request, title, form_factory_function, save_changes_function, redirect_url=None, button_label=_('Save object'), image_field=None, template='portal/edit_object.html', extra_context={}): """ Generic view for adding/editing an object. The form_factory_function will be called to create a form. The signature of this callable is form_factory_function (is_post, new_data). The save_changes_function will be called to save the changes. The signature of this callable is save_changes_function(form, project, new_data) redirect_url is a template string that will be composed with the project url to get the url to redirect after a succesful process. button_label is the label of the submit button image_field can be specified if the content type has an image type field. """ if request.method == 'POST': #if image_field is not None and image_field in request.FILES: # read_image(request, image_field) new_data = request.POST.copy() new_files = request.FILES.copy() form = form_factory_function(True, new_data, new_files) if form.is_valid(): next_url = save_changes_function(form, new_data, new_files) if not redirect_url: redirect_url = next_url return HttpResponseRedirect(redirect_url) else: log.send_error(request, _('Form has errors')) else: form = form_factory_function(False) context = {'form': form, 'title': title, 'button_label': button_label,} context.update(extra_context) return render_to_response(template, context, context_instance = RequestContext(request))
def messagebox(context): """ Render message box """ request = context['request'] portal_messages = context['messages'] if not portal_messages: portal_messages = get_and_delete_messages(request) if not portal_messages: if 'form' in context: form = context['form'] if getattr(form, 'errors', []): if form.non_field_errors(): send_error(request, form.non_field_errors()) send_error(request, _('Form filled has errors. Please correct')) portal_messages = get_and_delete_messages(request) return { 'portal_messages': portal_messages }
def delete_object(request): content_type_id = request.POST.get('content_type_id', None) object_id = request.POST.get('object_id', None) if content_type_id is None or object_id is None: raise Http404 # usar newsitem._meta.app_label y newsitem._meta.module_name para sacar # el content type en el templatetag content_type = ContentType.objects.get(id=int(content_type_id)) if request.user.has_perm('delete_%s' % content_type.model) or \ request.user.has_perm('%s.delete_%s' % (content_type.app_label, content_type.model)): obj = content_type.get_object_for_this_type(id=object_id) log.delete(request.user.id, obj) obj.delete() else: log.send_error(request, _('You do not have permission to delete %s objects') % content_type.model) return HttpResponseRedirect('/')
def action(request, repository_name, path, base_template=FILEBROWSER_BASE_TEMPLATE, url_prefix=None): """ process rename, delete, etc. actions in files and directories """ repository = get_object_or_404(Repository, name=repository_name) if 'renameform' in request.POST: action = 'confirmrename' elif 'deleteform' in request.POST: action = 'confirmdelete' elif 'rename' in request.POST: action = 'rename' old_elems = [] else: action = 'delete' elems = [] for k, v in request.POST.items(): if (k.startswith('dir_') or k.startswith('file_') or k.startswith('elem_') or k.startswith('doc_')): if k.startswith('doc_'): try: title = Document.objects.get(slug=v).title except: title = v elems.append({'id': v, 'title': title}) else: elems.append({'id': v, 'title': v}) if action == 'rename': if k.startswith('dir_'): old_elems.append(request.POST['olddir_%s' % k[len('dir_'):]]) elif k.startswith('file_'): old_elems.append(request.POST['oldfile_%s' % k[len('file_'):]]) else: old_elems.append(request.POST['olddoc_%s' % k[len('doc_'):]]) if not elems: send_error(request, _("You didn't selected any element")) return HttpResponseRedirect(filebrowser_reverse(request, "filebrowser_dir_listing", kwargs={'repository_name': repository.name, 'path': path}, url_prefix=url_prefix)) if action in ['confirmrename', 'confirmdelete']: if action == 'confirmrename': template = 'filebrowser/rename.html' else: template = 'filebrowser/delete.html' basepath = repository.get_absolute_path(path) basepath = basepath.encode('utf-8').decode('utf-8') files = [] dirs = [] docs = [] for e in elems: file = join(basepath, e['id']) if isdir(file): dirs.append(e) elif isfile(file): files.append(e) else: docs.append(e) pathdirs, pathfiles, parents, dirname = repository.list_directory(path) return render_to_response(template, {'repository': repository, 'base_template': base_template, 'path': path, 'files': files, 'parents': parents, 'dirs': dirs, 'docs': docs, 'url_prefix': url_prefix, }, context_instance=RequestContext(request)) elif action == 'rename': repository.rename_elems(path, elems, old_elems) send_info(request, _('Elements renamed successfully')) return HttpResponseRedirect(filebrowser_reverse(request, "filebrowser_dir_listing", kwargs={'repository_name': repository.name, 'path': path}, url_prefix=url_prefix)) elif action == 'delete': repository.delete_elems(path, elems) send_info(request, _('Elements deleted successfully')) return HttpResponseRedirect(filebrowser_reverse(request, "filebrowser_dir_listing", kwargs={'repository_name': repository.name, 'path': path}, url_prefix=url_prefix)) else: return Http404