Example #1
0
File: utils.py Project: l--f/1327
def handle_edit(request, document):
    context = RequestContext(request)
    if request.method == "POST":
        form = TextForm(request.POST)
        if form.is_valid():
            cleaned_data = form.cleaned_data

            document.text = cleaned_data["text"]
            document.type = cleaned_data["type"]
            document.author = request.user
            if document.title != cleaned_data["title"]:
                # if the user changed the title we have to delete the old version
                # because the url_title will change, too...
                document.title = cleaned_data["title"]
                new_document = Document(
                    title=document.title, text=document.text, type=document.type, author=document.author
                )
                document.delete()
                document = new_document

                # save the document and also save the user and the comment the user added
            with transaction.atomic(), reversion.create_revision():
                document.save()
                reversion.set_user(request.user)
                reversion.set_comment(cleaned_data["comment"])
            raise FormValidException
        else:
            context["errors"] = form.errors
            context["form"] = form
    else:
        form_data = {"title": document.title, "text": document.text, "type": document.type}
        context["form"] = TextForm(form_data)

    context["document"] = document
    return context
Example #2
0
File: utils.py Project: tzwenn/1327
def handle_edit(request, document):
	if request.method == 'POST':
		form = TextForm(request.POST)
		if form.is_valid():
			cleaned_data = form.cleaned_data

			document.title = cleaned_data['title']
			document.text = cleaned_data['text']
			document.author = request.user

			# save the document and also save the user and the comment the user added
			with transaction.atomic(), reversion.create_revision():
				document.save()
				reversion.set_user(request.user)
				reversion.set_comment(cleaned_data['comment'])

			# delete Autosave
			try:
				autosave = TemporaryDocumentText.objects.get(document=document)
				autosave.delete()
			except TemporaryDocumentText.DoesNotExist:
				pass

			return True, form
	else:

		# load Autosave
		try:
			autosave = TemporaryDocumentText.objects.get(document=document)
			text = autosave.text
			autosaved = True
		except TemporaryDocumentText.DoesNotExist:
			text = document.text
			autosaved = False

		if 'restore' not in request.GET:
			text = document.text
		else:
			autosaved = False

		form_data = {
			'title': document.title,
			'text': text,
		}
		form = TextForm(form_data)
		form.autosave = autosaved
		if autosaved:
			form.autosave_date = autosave.created

	return False, form
Example #3
0
File: utils.py Project: tzwenn/1327
def handle_autosave(request, document):
	if request.method == 'POST':
		form = TextForm(request.POST)
		form.is_valid()
		text_strip = request.POST['text'].strip()
		if text_strip != '':
			cleaned_data = form.cleaned_data

			if document is None:
				temporary_document_text = TemporaryDocumentText()
			elif document.text != cleaned_data['text']:
				temporary_document_text, created = TemporaryDocumentText.objects.get_or_create(document=document)
				temporary_document_text.document = document
			else:
				return

			temporary_document_text.text = cleaned_data['text']
			temporary_document_text.save()