def annotation_import(request, slug, section): """ Saves the file directly from the request object. Disclaimer: This is code is just an example, and should not be used on a real website. It does not validate file uploaded: it could be used to execute an arbitrary script on the server. """ context = {} name = dewikify(slug) page = Page.objects.get(name=name) if request.method == 'POST': form = AnnotationImportForm(request.POST, request.FILES) if form.is_valid(): f = request.FILES['file'] data = "" for chunk in f.chunks(): data += chunk srt = unicode(audacity_to_srt(data).decode('utf-8')) # Preserves the old header, because audacity only keeps timed section. # TODO: decide wether it should be handled here or in sectionalize_replace section = int(section) header = sectionalize(page.content)[section]['header'] + "\n\n" context = {'content': header + srt, 'section': section, 'page': page} return render_to_response("aawiki/annotation_import_confirm.html", context, context_instance=RequestContext(request)) else: form = AnnotationImportForm() context['form'] = form return render_to_response("aawiki/annotation_import.html", context, context_instance=RequestContext(request))
def annotation_export(request, slug, section, _format="audacity", force_endtime=False): context = {} name = dewikify(slug) page = Page.objects.get(name=name) section = sectionalize(page.content)[int(section)] # TODO: Regex should not be defined more once within active archives TIME_RE = r'(\d\d:)?(\d\d):(\d\d)([,.]\d{1,3})?' TIMECODE_RE = r'(?P<start>%(TIME_RE)s)[ \t]*-->([ \t]*(?P<end>%(TIME_RE)s))?' % locals() OTHER_RE = r'.+' TIMECODE_HEADER_RE = r'^%(TIMECODE_RE)s(%(OTHER_RE)s)?$' % locals() pattern = re.compile(TIMECODE_HEADER_RE, re.I | re.M | re.X) stack = [] for t in spliterator(pattern, section['header'] + section['body']): m = pattern.match(t['header']).groupdict() if force_endtime: if len(stack) and stack[-1]['end'] == '': stack[-1]['end'] = timecode_tosecs(m['start']) end = timecode_tosecs(m['end']) or '' else: end = timecode_tosecs(m['end']) or timecode_tosecs(m['start']) stack.append({ 'start': timecode_tosecs(m['start']), 'end': end, 'body': t['body'].strip('\n'), }) context = {'sections': stack} return render_to_response("aawiki/annotation_export.audacity", context, context_instance=RequestContext(request), mimetype="text/plain;charset=utf-8")
def page_edit(request, slug): """ Displays the edit form for :model:`aawiki.Page` **methods** ``GET`` Either the edit form, OR provides Markdown source via AJAX call ``POST`` Receives/commits edits on POST (either via form or AJAX) **parameters** ``section`` Optional. Limits the scope of edition to the given section. **template** :template:`aawiki/page_edit.html` """ context = {} name = dewikify(slug) section = int(request.REQUEST.get('section', 0)) is_ajax = request.META.get('HTTP_X_REQUESTED_WITH') == 'XMLHttpRequest' try: page = Page.objects.get(name=name) except Page.DoesNotExist: page = None # Gets the edit form if request.method == "GET": if page: # Gets the whole content or just a section if section: sections = sectionalize(page.content) sectiondict = sections[section] context['content'] = sectiondict['header'] + sectiondict['body'] context['section'] = section else: context['content'] = page.content # Returns plain content in case of ajax editing if is_ajax: return HttpResponse(context['content']) else: context['page'] = page # So templates nows about what page we are editing context['form'] = PageEditForm(initial={"content": context['content']}) else: context['content'] = '' context['name'] = name # So templates nows about what page we are editing rendered = render_to_string("aawiki/partials/initial_page_content.md", context) context['form'] = PageEditForm(initial={"content": rendered, "message": '/* Created a new page "%s" */' % name, }) return render_to_response("aawiki/page_edit.html", context, \ context_instance=RequestContext(request)) elif request.method == "POST": content = request.POST.get('content', '') content = convert_line_endings(content, 0) # Normalizes EOL content = content.strip() + "\n\n" # Normalize whitespace around the markdown is_cancelled = request.POST.get('cancel', None) if is_cancelled: url = reverse('aa-page-detail', kwargs={'slug': slug}) return redirect(url) form = PageEditForm(request.POST) if form.is_valid(): # Processes the content of the form # Retrieves and cleans the form values content = form.cleaned_data["content"] content = convert_line_endings(content, 0) # Normalizes EOL content = content.strip() + "\n\n" # Normalize whitespace around the markdown message = form.cleaned_data["message"] or "<no messages>" is_minor = form.cleaned_data["is_minor"] if request.user.is_authenticated(): author = "%s <%s@%s>" % (request.user.username, request.user.username, request.META['REMOTE_ADDR']) else: author = "Anonymous <anonymous@%s>" % request.META['REMOTE_ADDR'] if page: old_content = page.content if section: # section edit keep_header = bool(request.REQUEST.get('keep_header')) #print(keep_header) if section == -1: page.content = page.content.rstrip() + "\n\n" + content else: page.content = sectionalize_replace(page.content, section, content) if page.content != old_content: page.commit(message=message, author=author, is_minor=is_minor) else: if content == "delete": page.delete() else: page.content = content if page.content != old_content: page.commit(message=message, author=author, is_minor=is_minor) else: if content == "delete": pass else: page = Page(content=content, name=name) page.commit(message=message, author=author, is_minor=is_minor) if is_ajax: # FIXME: apply typogrify filters here too! md = get_markdown() rendered = md.convert(content) return HttpResponse(rendered) else: # Returns the invalid form for correction # TODO: factorize this chunk context['page'] = page # So templates nows about what page we are editing context['name'] = name # So templates nows about what page we are editing context['form'] = form return render_to_response("aawiki/page_edit.html", context, \ context_instance=RequestContext(request)) url = reverse('aa-page-detail', kwargs={'slug': slug}) return redirect(url)