def create(self, request, id=None): clone = None if id: instance = File.objects.get(id=id) clone = instance.clone() else: instance = File() form = FileForm(request.POST, request.FILES, instance=instance) if form.is_valid(): file = form.save(commit=False) file.tags = [] file.tags.add(*handle_file_tags(request.POST['tags'])) file.save() if id: clone.save() return file else: return form.errors
def create(self, request, id=None): clone = None if id: instance = File.objects.get(id=id) clone = instance.clone() else: instance = File() form = FileForm(request.POST, request.FILES, instance=instance) if form.is_valid(): file = form.save(commit=False) file.tags = [] file.tags.add(*handle_file_tags(request.POST["tags"])) file.save() if id: clone.save() return file else: return form.errors
def generic_form(request, instance, file_instance, redirect_view, additional_data, template=None): """ instance = Project, Product etc file_instance = File.objects.get(id=?) redirect_view = redirect(overview,id) """ if not template: template = "files/form.html" clone = file_instance.clone() if request.method == 'POST': form = FileForm(request.POST, request.FILES, instance=file_instance) if form.is_valid(): o = form.save(commit=False) o.parent_file = None clone.save() o.save() o.tags = [] o.tags.add(*handle_file_tags(request.POST['tags'])) instance.files.add(o) request.message_success("Successfully saved") return redirect_view else: tags = "" if file_instance.id: for tag in file_instance.tags.all(): tags += tag.name + ", " form = FileForm(instance=file_instance, initial={ 'tags': tags, 'file': None }) data = { 'title': _("File form"), 'file': file_instance, 'form': form, } for key in additional_data: data[key] = additional_data[key] return render(request, template, data)
def generic_form(request, instance, file_instance, redirect_view, additional_data, template=None): """ instance = Project, Product etc file_instance = File.objects.get(id=?) redirect_view = redirect(overview,id) """ if not template: template = "files/form.html" clone = file_instance.clone() if request.method == 'POST': form = FileForm(request.POST, request.FILES, instance=file_instance) if form.is_valid(): o = form.save(commit=False) o.parent_file = None clone.save() o.save() o.tags = [] o.tags.add(*handle_file_tags(request.POST['tags'])) instance.files.add(o) request.message_success("Successfully saved") return redirect_view else: tags = "" if file_instance.id: for tag in file_instance.tags.all(): tags += tag.name + ", " form = FileForm(instance=file_instance, initial={'tags': tags, 'file': None}) data = {'title': _("File form"), 'file': file_instance, 'form': form, } for key in additional_data: data[key] = additional_data[key] return render(request, template, data)
def file_form(request, file_id=None): if file_id: file_instance = get_object_or_404(File, id=file_id, deleted=False) else: file_instance = File() if request.method == 'POST': form = FileForm(request.POST, request.FILES, instance=file_instance) if form.is_valid(): o = form.save(commit=False) o.save() request.message_success("Successfully saved") else: form = FileForm(instance=file_instance) return render(request, "form.html", {'title': 'Fil', 'form': form, })
def file_form(request, file_id=None): if file_id: file_instance = get_object_or_404(File, id=file_id, deleted=False) else: file_instance = File() if request.method == 'POST': form = FileForm(request.POST, request.FILES, instance=file_instance) if form.is_valid(): o = form.save(commit=False) o.save() request.message_success("Successfully saved") else: form = FileForm(instance=file_instance) return render(request, "form.html", { 'title': 'Fil', 'form': form, })