def edit(request, model, object_id): """Edit or create a page.""" form_class = utils.get_model_wiki_form(model) version = None if request.method == 'POST': try: page = model.objects.get(pk=object_id) except model.DoesNotExist: page = model(pk=object_id) form = form_class(request.POST, instance=page) if form.is_valid(): with revision: # Save the author, use our metadata model if user is anonymous if not request.user.is_anonymous(): revision.user = request.user else: ip_address = request.META.get('HTTP_X_FORWARDED_FOR', request.META.get('REMOTE_ADDR')) if ip_address: revision.add_meta(VersionMeta, ip_address=ip_address) # Save a comment for the revision if form.cleaned_data.get('wikify_comment'): revision.comment = form.cleaned_data['wikify_comment'] form.save() # Successfully saved the page, now return to the 'read' view return HttpResponseRedirect(request.path) else: if request.GET.get('version_id'): # User is editing the page based on an older version try: version_id = int(request.GET.get('version_id')) version = (models.Version.objects.get_for_object_reference( model, object_id) .get(id=version_id)) page = version.object_version.object except (ValueError, models.Version.DoesNotExist): raise Http404('Version not found') form = form_class(instance=page) else: try: page = model.objects.get(pk=object_id) form = form_class(instance=page) except model.DoesNotExist: form = form_class() return render_to_response('wikify/edit.html', {'form': form, 'object_id': object_id, 'version': version}, context_instance=RequestContext(request))
def revert_wiki_page(request, group, page_name, current_app='plus_groups', **kwargs): try: obj = WikiPage.objects.plus_get(request.user, name=page_name, in_agent=group.get_ref()) except WikiPage.DoesNotExist: raise Http404 ver_id = int(request.GET['ver_id']) ver = Version.objects.get(id=ver_id) ver.revert() revision.user = request.user revision.add_meta(VersionDelta, delta="change") revision.comment = 'Reverted to version by %s written on %s at %s' %(ver.revision.user.display_name, date(ver.revision.date_created), time(ver.revision.date_created)) return HttpResponseRedirect(reverse(current_app + ':view_WikiPage', args=[group.id, obj.name]))
def create_wiki_page(request, group, page_name, template_name="plus_wiki/create_wiki.html", current_app='plus_groups', **kwargs): """creates OR saves WikiPages """ form = EditWikiForm(request.POST) try: obj = WikiPage.objects.plus_get(request.user, name=page_name, in_agent=group.get_ref()) except: raise Http404 if form.is_valid(): title = form.cleaned_data['title'] content = form.cleaned_data['content'] license = form.cleaned_data['license'] if request.POST.get('preview', None): return render_to_response(template_name, {'page':TemplateSecureWrapper(obj), 'data':form.data, 'preview_content': content, 'form_action':reverse(current_app + ":create_WikiPage", args=[obj.in_agent.obj.id, obj.name])}, context_instance=RequestContext(request, current_app=current_app)) else: if obj.stub: # we should change the "created_by" on the genericreference/permissions system to "owner" obj.created_by = request.user revision.comment = 'Original' else: revision.comment = form.cleaned_data['what_changed'] revision.user = request.user diff = htmldiffer(AttrDict({'title':title, 'content':content, 'license': license}), obj) diff = json.dumps(diff) #XXX this diff needs to only include things in the proximity of an insertion or deletion revision.add_meta(VersionDelta, delta=diff) obj.title = title obj.name_from_title() obj.content = content obj.license = license obj.stub = False obj.save() return HttpResponseRedirect(reverse(current_app + ':view_WikiPage', args=[group.id, obj.name])) return render_to_response(template_name, {'page':TemplateSecureWrapper(obj), 'revision':revision, 'data':form.data, 'errors': form.errors, 'form_action':reverse(current_app + ":create_WikiPage", args=[obj.in_agent.obj.id, obj.name])}, context_instance=RequestContext(request, current_app=current_app))
def _answer_save(request, language_code, project_slug, project_translation, question, value): project = project_translation.master question = get_object_or_404(Question, id=question) if value: #It looks like there are unforeseen consequences to a nani changeset:https://github.com/KristianOellegaard/django-hvad/commit/aba974f79bdaf9fff46c01b1d4b3e941032d02dd #It tries to create duplicate entries in the db #answer, created = Answer.objects.language(language_code).get_or_create(project=project, # question=question) #Let's work around it: base_answer, created = Answer.objects.untranslated().get_or_create(project=project, question=question) if created or language_code not in base_answer.get_available_languages(): answer = base_answer.translate(language_code) else: answer = Answer.objects.language(language_code).get(pk=base_answer.pk) #End workaround # Remove html tags value = strip_tags(value) answer.content = value answer.save() # Generate a diff diffs = make_diffs_for_object(answer.translations.get(language_code=language_code), 'content', answer.content) if request.user.is_anonymous(): revision.user = User.objects.get(id=settings.ANONYMOUS_USER_ID) else: revision.user = request.user # Create an action answer_action = action_create(actor=request.user, verb='edit_pjquestion', action_object=answer, target=project) revision.add_meta(VersionActivity, action=answer_action, diffs=diffs) response_dict = dict(text=linebreaksbr(urlize(value)), # Make line breaks and link urls redirect=project_slug is None, redirect_url=project.get_absolute_url()) return HttpResponse(simplejson.dumps(response_dict), 'application/json') else: return HttpResponseNotFound()
def revert_wiki_page(request, group, page_name, current_app='plus_groups', **kwargs): try: obj = WikiPage.objects.plus_get(request.user, name=page_name, in_agent=group.get_ref()) except WikiPage.DoesNotExist: raise Http404 ver_id = int(request.GET['ver_id']) ver = Version.objects.get(id=ver_id) ver.revert() revision.user = request.user revision.add_meta(VersionDelta, delta="change") revision.comment = 'Reverted to version by %s written on %s at %s' % ( ver.revision.user.display_name, date( ver.revision.date_created), time(ver.revision.date_created)) return HttpResponseRedirect( reverse(current_app + ':view_WikiPage', args=[group.id, obj.name]))
def _textfield_save(request, language_code, project_slug, project_translation, section, value): # Resolve the fieldname fieldname = TEXTFIELD_MAPPINGS[section] FieldForm = modelform_factory(I4pProjectTranslation, fields=(fieldname,)) # prevent html tags from being saved value = strip_tags(value) form = FieldForm({fieldname: value}, instance=project_translation) if form.is_valid(): response_dict = {} project_translation = form.save() if project_translation._meta.get_field(fieldname).choices: text = getattr(project_translation, "get_%s_display" % fieldname)() if fieldname == "completion_progress": response_dict["completion_progress"] = getattr(project_translation,fieldname) else: # Generate line breaks and link urls if found text = linebreaksbr(urlize(value)) if request.user.is_anonymous(): revision.user = User.objects.get(id=settings.ANONYMOUS_USER_ID) else: revision.user = request.user project = project_translation.master pj_translation_action = action_create(actor=request.user, verb='edit_pjfield', action_object=project_translation, target=project) revision.add_meta(VersionActivity, action=pj_translation_action) response_dict.update({'text': text or '', 'redirect': project_slug is None, 'redirect_url': project_translation.master.get_absolute_url()}) return HttpResponse(simplejson.dumps(response_dict), 'application/json') else: return HttpResponseNotFound()
def create_wiki_page(request, group, page_name, template_name="plus_wiki/create_wiki.html", current_app='plus_groups', **kwargs): """creates OR saves WikiPages """ try: obj = WikiPage.objects.plus_get(request.user, name=page_name, in_agent=group.get_ref()) except: raise Http404 post_kwargs = request.POST.copy() post_kwargs['obj'] = obj form = EditWikiForm(post_kwargs) if "delete_submit" in post_kwargs : if post_kwargs.has_key('delete_check') : obj.delete() return HttpResponseRedirect(reverse(current_app + ':group', args=[group.id])) if "move_resource_submit" in post_kwargs : form = MoveResourceForm(post_kwargs) form.user = request.user # essential, user is checked inside form validation if form.is_valid() : new_parent_group = form.cleaned_data['new_parent_group'] obj.move_to_new_group(new_parent_group) revision.user = request.user return HttpResponseRedirect(reverse(current_app + ':group', args=[form.cleaned_data['new_parent_group'].id])) if form.is_valid(): title = form.cleaned_data['title'] content = form.cleaned_data['content'] license = form.cleaned_data['license'] copyright_holder = form.cleaned_data['copyright_holder'] name = form.cleaned_data['name'] if request.POST.get('preview', None): return render_to_response(template_name, {'page':TemplateSecureWrapper(obj), 'data':form.data, 'preview_content': content, 'form_action':reverse(current_app + ":create_WikiPage", args=[obj.in_agent.obj.id, obj.name])}, context_instance=RequestContext(request, current_app=current_app)) else: if obj.stub: # we should change the "created_by" on the genericreference/permissions system to "owner" obj.created_by = request.user revision.comment = 'Original' else: revision.comment = form.cleaned_data['what_changed'] revision.user = request.user diff = htmldiffer(AttrDict({'title':title, 'content':content, 'license': license}), obj) diff = json.dumps(diff) #XXX this diff needs to only include things in the proximity of an insertion or deletion revision.add_meta(VersionDelta, delta=diff) obj.title = title obj.set_name(name) obj.content = content obj.license = license obj.copyright_holder = copyright_holder obj.stub = False obj.save() from apps.plus_feed.models import FeedItem FeedItem.post_WIKI_PAGE(request.user, obj) return HttpResponseRedirect(reverse(current_app + ':view_WikiPage', args=[group.id, obj.name])) return render_to_response(template_name, {'page':TemplateSecureWrapper(obj), 'revision':revision, 'data':form.data, 'errors': form.errors, 'form_action':reverse(current_app + ":create_WikiPage", args=[obj.in_agent.obj.id, obj.name])}, context_instance=RequestContext(request, current_app=current_app))
def create_wiki_page(request, group, page_name, template_name="plus_wiki/create_wiki.html", current_app='plus_groups', **kwargs): """creates OR saves WikiPages """ try: obj = WikiPage.objects.plus_get(request.user, name=page_name, in_agent=group.get_ref()) except: raise Http404 post_kwargs = request.POST.copy() post_kwargs['obj'] = obj form = EditWikiForm(post_kwargs) if "delete_submit" in post_kwargs: if post_kwargs.has_key('delete_check'): obj.delete() return HttpResponseRedirect( reverse(current_app + ':group', args=[group.id])) if "move_resource_submit" in post_kwargs: form = MoveResourceForm(post_kwargs) form.user = request.user # essential, user is checked inside form validation if form.is_valid(): new_parent_group = form.cleaned_data['new_parent_group'] obj.move_to_new_group(new_parent_group) revision.user = request.user return HttpResponseRedirect( reverse(current_app + ':group', args=[form.cleaned_data['new_parent_group'].id])) if form.is_valid(): title = form.cleaned_data['title'] content = form.cleaned_data['content'] license = form.cleaned_data['license'] copyright_holder = form.cleaned_data['copyright_holder'] name = form.cleaned_data['name'] if request.POST.get('preview', None): return render_to_response(template_name, { 'page': TemplateSecureWrapper(obj), 'data': form.data, 'preview_content': content, 'form_action': reverse(current_app + ":create_WikiPage", args=[obj.in_agent.obj.id, obj.name]) }, context_instance=RequestContext( request, current_app=current_app)) else: if obj.stub: # we should change the "created_by" on the genericreference/permissions system to "owner" obj.created_by = request.user revision.comment = 'Original' else: revision.comment = form.cleaned_data['what_changed'] revision.user = request.user diff = htmldiffer( AttrDict({ 'title': title, 'content': content, 'license': license }), obj) diff = json.dumps(diff) #XXX this diff needs to only include things in the proximity of an insertion or deletion revision.add_meta(VersionDelta, delta=diff) obj.title = title obj.set_name(name) obj.content = content obj.license = license obj.copyright_holder = copyright_holder obj.stub = False obj.save() from apps.plus_feed.models import FeedItem FeedItem.post_WIKI_PAGE(request.user, obj) return HttpResponseRedirect( reverse(current_app + ':view_WikiPage', args=[group.id, obj.name])) return render_to_response(template_name, { 'page': TemplateSecureWrapper(obj), 'revision': revision, 'data': form.data, 'errors': form.errors, 'form_action': reverse(current_app + ":create_WikiPage", args=[obj.in_agent.obj.id, obj.name]) }, context_instance=RequestContext( request, current_app=current_app))