本文整理汇总了Python中forms.DocumentForm.save方法的典型用法代码示例。如果您正苦于以下问题:Python DocumentForm.save方法的具体用法?Python DocumentForm.save怎么用?Python DocumentForm.save使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类forms.DocumentForm
的用法示例。
在下文中一共展示了DocumentForm.save方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: add_document
# 需要导入模块: from forms import DocumentForm [as 别名]
# 或者: from forms.DocumentForm import save [as 别名]
def add_document(request):
""" Add a document """
label_options = {'labels': {'item_type': 'Document type'}}
dform = DocumentForm(user=request.user)
# rform = ReferenceForm()
tag_formset = get_tag_formset(**label_options)
if request.method == 'POST':
# rform = ReferenceForm(request.POST)
dform = DocumentForm(request.POST, request.FILES, user=request.user)
tag_formset = get_tag_formset(dform.instance, data=request.POST, **label_options)
#this avoids ignoring the evaluation of the form to show the errors
# rf_is_valid = rform.is_valid()
rf_is_valid = True
df_is_valid = dform.is_valid()
if rf_is_valid and df_is_valid and tag_formset.is_valid():
# rform.save()
# dform.instance.reference = rform.instance
dform.save()
# file = dform.cleaned_data['file']
# dform.instance.seet_file(file=file, filename=file.name)
assign_perm('documents.access_document', request.user, dform.instance)
tag_formset.save()
return HttpResponseRedirect(reverse('documents.views.list_documents'))
return render_to_response('add_document.html', {
'dform': dform,
'rform': None,
'formset': tag_formset,
}, context_instance=RequestContext(request))
示例2: document_add_typed
# 需要导入模块: from forms import DocumentForm [as 别名]
# 或者: from forms.DocumentForm import save [as 别名]
def document_add_typed(request, folder_id=None, response_format='html'):
"Document add to preselected folder"
folder = None
if folder_id:
folder = get_object_or_404(Folder, pk=folder_id)
if not request.user.profile.has_permission(folder, mode='x'):
folder = None
document = Document()
if request.POST:
if 'cancel' not in request.POST:
form = DocumentForm(
request.user.profile, folder_id, request.POST, instance=document)
if form.is_valid():
document = form.save()
document.set_user_from_request(request)
return HttpResponseRedirect(reverse('documents_document_view', args=[document.id]))
else:
return HttpResponseRedirect(reverse('document_index'))
else:
form = DocumentForm(request.user.profile, folder_id)
context = _get_default_context(request)
context.update({'form': form,
'folder': folder})
return render_to_response('documents/document_add_typed', context,
context_instance=RequestContext(request),
response_format=response_format)
示例3: document_edit
# 需要导入模块: from forms import DocumentForm [as 别名]
# 或者: from forms.DocumentForm import save [as 别名]
def document_edit(request, document_id, response_format='html'):
"Document edit page"
document = get_object_or_404(Document, pk=document_id)
if not request.user.profile.has_permission(document, mode='w'):
return user_denied(request, message="You don't have access to this Document")
if request.POST:
if 'cancel' not in request.POST:
form = DocumentForm(
request.user.profile, None, request.POST, instance=document)
if form.is_valid():
document = form.save()
return HttpResponseRedirect(reverse('documents_document_view', args=[document.id]))
else:
return HttpResponseRedirect(reverse('documents_document_view', args=[document.id]))
else:
form = DocumentForm(
request.user.profile, None, instance=document)
context = _get_default_context(request)
context.update({'form': form,
'document': document})
return render_to_response('documents/document_edit', context,
context_instance=RequestContext(request),
response_format=response_format)
示例4: create_doc
# 需要导入模块: from forms import DocumentForm [as 别名]
# 或者: from forms.DocumentForm import save [as 别名]
def create_doc(request):
if request.method == 'POST':
doc = Document()
form = DocumentForm(request.POST, request.FILES, instance=doc)
if (form.is_valid()):
form.save()
return redirect(reverse('documents:index'))
else:
form = DocumentForm(request.POST, request.FILES, instance=doc)
else:
form = DocumentForm()
context = {
'form':form,
'form_url':reverse('documents:create_doc'),
}
return render(request, 'documents/edit_doc.html', context)
示例5: edit_document
# 需要导入模块: from forms import DocumentForm [as 别名]
# 或者: from forms.DocumentForm import save [as 别名]
def edit_document(request, id=None):
"""Edit a document"""
ctx = copy.copy(default_ctx)
if id is None:
doc = None
cur_revision = 0
else:
doc = get_object_or_404(Document, id=id)
if request.user != doc.owner and not doc.contributors.filter(id=request.user.id).exists():
raise Http404
cur_revision = doc.revision
if request.method == 'POST':
# giving doc to the form may change its contents, so copy them now
if doc is not None:
cur_content = doc.content
form = DocumentForm(request.POST, instance=doc)
if form.is_valid():
if cur_revision == form.cleaned_data['revision']:
new_doc = form.save(commit=False)
# update revision
new_doc.revision = cur_revision + 1
if doc is None:
# owner is not set when creating a new document
new_doc.owner = request.user
new_doc.save()
return redirect('show_document', new_doc.id)
else:
# document was changed during editing
ctx['diff_warning'] = True
ctx['cur_content'] = cur_content
ctx['diff'] = ''.join(difflib.ndiff(doc.content.splitlines(1), cur_content.splitlines(1)))
form = DocumentForm(instance=doc, initial={'revision': cur_revision })
ctx['doc'] = doc
ctx['form'] = form
return render_to_response('edit_document.html', ctx, RequestContext(request))
示例6: upload_document_with_type
# 需要导入模块: from forms import DocumentForm [as 别名]
# 或者: from forms.DocumentForm import save [as 别名]
def upload_document_with_type(request, document_type_id, multiple=True):
check_permissions(request.user, "documents", [PERMISSION_DOCUMENT_CREATE])
document_type = get_object_or_404(DocumentType, pk=document_type_id)
local_form = DocumentForm(prefix="local", initial={"document_type": document_type})
if USE_STAGING_DIRECTORY:
staging_form = StagingDocumentForm(prefix="staging", initial={"document_type": document_type})
if request.method == "POST":
if "local-submit" in request.POST.keys():
local_form = DocumentForm(
request.POST, request.FILES, prefix="local", initial={"document_type": document_type}
)
if local_form.is_valid():
try:
if (not UNCOMPRESS_COMPRESSED_LOCAL_FILES) or (
UNCOMPRESS_COMPRESSED_LOCAL_FILES
and not _handle_zip_file(request, request.FILES["local-file"], document_type)
):
instance = local_form.save()
_handle_save_document(request, instance, local_form)
messages.success(request, _(u"Document uploaded successfully."))
except Exception, e:
messages.error(request, e)
if multiple:
return HttpResponseRedirect(request.get_full_path())
else:
return HttpResponseRedirect(reverse("document_list"))
elif "staging-submit" in request.POST.keys() and USE_STAGING_DIRECTORY:
staging_form = StagingDocumentForm(
request.POST, request.FILES, prefix="staging", initial={"document_type": document_type}
)
if staging_form.is_valid():
try:
staging_file = StagingFile.get(staging_form.cleaned_data["staging_file_id"])
if (not UNCOMPRESS_COMPRESSED_STAGING_FILES) or (
UNCOMPRESS_COMPRESSED_STAGING_FILES
and not _handle_zip_file(request, staging_file.upload(), document_type)
):
document = Document(file=staging_file.upload(), document_type=document_type)
document.save()
_handle_save_document(request, document, staging_form)
messages.success(
request, _(u"Staging file: %s, uploaded successfully.") % staging_file.filename
)
if DELETE_STAGING_FILE_AFTER_UPLOAD:
staging_file.delete()
messages.success(request, _(u"Staging file: %s, deleted successfully.") % staging_file.filename)
except Exception, e:
messages.error(request, e)
if multiple:
return HttpResponseRedirect(request.META["HTTP_REFERER"])
else:
return HttpResponseRedirect(reverse("document_list"))
示例7: edit_doc
# 需要导入模块: from forms import DocumentForm [as 别名]
# 或者: from forms.DocumentForm import save [as 别名]
def edit_doc(request, doc_id=None):
try:
doc = Document.objects.get(id=doc_id)
except Document.DoesNotExist:
return redirect('documents:create_event')
if request.method == 'POST':
form = DocumentForm(request.POST, request.FILES, instance=doc)
if (form.is_valid()):
form.save()
return redirect(reverse('documents:index'))
else:
form = DocumentForm(request.POST, request.FILES, instance=event)
else:
form = DocumentForm(instance=doc)
context = {
'form':form,
'form_url': reverse('documents:edit_doc', args=[doc_id])
}
return render(request, 'documents/edit_doc.html', context)
示例8: main
# 需要导入模块: from forms import DocumentForm [as 别名]
# 或者: from forms.DocumentForm import save [as 别名]
def main(request):
# Handle file upload
if request.method == 'POST':
test_form = DocumentForm(request.POST, request.FILES)
if test_form.is_valid():
test_form.save()
messages.success(request, 'The test was uploaded. Thank you!')
return HttpResponseRedirect(reverse('Library.views.main'))
textbook_form = AddTextbookForm(request.POST)
if textbook_form.is_valid():
textbook_form.save()
messages.success(request, 'The record was uploaded. Thank you!')
return HttpResponseRedirect(reverse('Library.views.main'))
else:
test_form = DocumentForm() # A empty, unbound form
textbook_form = AddTextbookForm() # A empty, unbound form
tests = Test.objects.all()
textbooks = Textbook.objects.all()
# Render list page with the documents and the form
return render_to_response(
'library.html',
{
'tests': tests,
'test_form': test_form,
'textbooks': textbooks,
'textbook_form': textbook_form
},
context_instance=RequestContext(request)
)
示例9: add_document
# 需要导入模块: from forms import DocumentForm [as 别名]
# 或者: from forms.DocumentForm import save [as 别名]
def add_document(request):
""" Add a document """
dform = DocumentForm(user=request.user)
rform = ReferenceForm()
if request.method == 'POST':
rform = ReferenceForm(request.POST)
dform = DocumentForm(request.POST, request.FILES, user=request.user)
#this avoids ignoring the evaluation of the form to show the errors
rf_is_valid = rform.is_valid()
df_is_valid = dform.is_valid()
if rf_is_valid and df_is_valid:
rform.save()
dform.instance.reference = rform.instance
dform.save()
file = dform.cleaned_data['file']
dform.instance.set_file(file=file, filename=file.name)
assign_perm('documents.access_document', request.user, dform.instance)
return HttpResponseRedirect(reverse('documents.views.list_documents'))
return render_to_response('add_document.html', {
'dform': dform,
'rform': rform,
}, context_instance=RequestContext(request))
示例10: create_document
# 需要导入模块: from forms import DocumentForm [as 别名]
# 或者: from forms.DocumentForm import save [as 别名]
def create_document(request):
"""View for creating new documents"""
if request.method == 'POST':
form = DocumentForm(request.POST)
if form.is_valid():
d = form.save(commit=False)
d.owner = request.user
d.save()
return redirect('show_document', d.id)
else:
form = DocumentForm()
ctx = copy.copy(default_ctx)
ctx['form'] = form
return render_to_response('create_document.html', ctx, RequestContext(request))
示例11: draft_new
# 需要导入模块: from forms import DocumentForm [as 别名]
# 或者: from forms.DocumentForm import save [as 别名]
def draft_new(request, template_name='doccomment/doc_editor.html'):
if request.method == 'POST':
form = DocumentForm(request.POST)
if form.is_valid():
doc = form.save(commit=False)
doc.author = request.user
doc.save()
if request.POST.get('save-and-continue', None):
url = reverse('doccomment_draft_edit', kwargs={'id':doc.id})
else:
url = reverse('doccomment_draft_list')
# TODO: use django-messages to display "saved" message
return HttpResponseRedirect(url)
else:
form = DocumentForm()
return render_to_response(template_name, {
'form' : form,
}, context_instance=RequestContext(request))
示例12: draft_edit
# 需要导入模块: from forms import DocumentForm [as 别名]
# 或者: from forms.DocumentForm import save [as 别名]
def draft_edit(request, id, template_name='doccomment/doc_editor.html'):
doc = get_object_or_404(Document, pk=id)
if doc.author != request.user and not Permission.user_is_editor(request.user):
return HttpResponseForbidden('You can only edit documents you created')
if request.method == 'POST':
form = DocumentForm(request.POST, instance=doc)
if form.is_valid():
doc = form.save(commit=False)
doc.has_modification = True
doc.save()
if request.POST.get('save-and-continue', None):
url = reverse('doccomment_draft_edit', kwargs={'id':doc.id})
else:
url = reverse('doccomment_draft_list')
# TODO: use django-messages to display "saved" message
return HttpResponseRedirect(url)
else:
form = DocumentForm(instance=doc)
return render_to_response(template_name, {
'form' : form,
'document' : doc,
}, context_instance=RequestContext(request))
示例13: document_add
# 需要导入模块: from forms import DocumentForm [as 别名]
# 或者: from forms.DocumentForm import save [as 别名]
def document_add(request, response_format='html'):
"new document form"
if request.POST:
if 'cancel' not in request.POST:
document = Document()
form = DocumentForm(
request.user.profile, None, request.POST, instance=document)
if form.is_valid():
document = form.save()
document.set_user_from_request(request)
return HttpResponseRedirect(reverse('documents_document_view', args=[document.id]))
else:
return HttpResponseRedirect(reverse('document_index'))
else:
form = DocumentForm(request.user.profile, None)
context = _get_default_context(request)
context.update({'form': form})
return render_to_response('documents/document_add', context,
context_instance=RequestContext(request),
response_format=response_format)