+ last_books = request.session.get("wiki_last_books", {})
+ last_books[slug, chunk.slug] = {
+ 'time': access_time,
+ 'title': chunk.pretty_name(),
+ }
+
+ if len(last_books) > MAX_LAST_DOCS:
+ oldest_key = min(last_books, key=lambda x: last_books[x]['time'])
+ del last_books[oldest_key]
+ request.session['wiki_last_books'] = last_books
+
+ return direct_to_template(request, template_name, extra_context={
+ 'chunk': chunk,
+ 'forms': {
+ "text_save": DocumentTextSaveForm(prefix="textsave"),
+ "text_revert": DocumentTextRevertForm(prefix="textrevert"),
+ "add_tag": DocumentTagForm(prefix="addtag"),
+ },
+ 'REDMINE_URL': settings.REDMINE_URL,
+ })
+
+
+@require_GET
+def editor_readonly(request, slug, chunk=None, template_name='wiki/document_details_readonly.html'):
+ try:
+ chunk = Chunk.get(slug, chunk)
+ revision = request.GET['revision']
+ except (Chunk.MultipleObjectsReturned, Chunk.DoesNotExist, KeyError):
+ raise Http404
+
+ access_time = datetime.now()
+ last_books = request.session.get("wiki_last_books", {})
+ last_books[slug, chunk.slug] = {
+ 'time': access_time,
+ 'title': chunk.book.title,
+ }
+
+ if len(last_books) > MAX_LAST_DOCS:
+ oldest_key = min(last_books, key=lambda x: last_books[x]['time'])
+ del last_books[oldest_key]
+ request.session['wiki_last_books'] = last_books
+
+ return direct_to_template(request, template_name, extra_context={
+ 'chunk': chunk,
+ 'revision': revision,
+ 'readonly': True,
+ 'REDMINE_URL': settings.REDMINE_URL,
+ })
+
+
+def create_missing(request, slug):
+ slug = slug.replace(' ', '-')
+
+ if request.method == "POST":
+ form = DocumentCreateForm(request.POST, request.FILES)
+ if form.is_valid():
+
+ if request.user.is_authenticated():
+ creator = request.user
+ else:
+ creator = None
+ book = Book.create(creator=creator,
+ slug=form.cleaned_data['slug'],
+ title=form.cleaned_data['title'],
+ text=form.cleaned_data['text'],
+ )
+
+ return http.HttpResponseRedirect(reverse("wiki_editor", args=[book.slug]))
+ else:
+ form = DocumentCreateForm(initial={
+ "slug": slug,
+ "title": slug.replace('-', ' ').title(),
+ })
+
+ return direct_to_template(request, "wiki/document_create_missing.html", extra_context={
+ "slug": slug,
+ "form": form,
+ })
+
+
+def upload(request):
+ if request.method == "POST":
+ form = DocumentsUploadForm(request.POST, request.FILES)
+ if form.is_valid():
+ import slughifi
+
+ if request.user.is_authenticated():
+ creator = request.user
+ else:
+ creator = None
+
+ zip = form.cleaned_data['zip']
+ skipped_list = []
+ ok_list = []
+ error_list = []
+ slugs = {}
+ existing = [book.slug for book in Book.objects.all()]
+ for filename in zip.namelist():
+ if filename[-1] == '/':
+ continue
+ title = os.path.basename(filename)[:-4]
+ slug = slughifi(title)
+ if not (slug and filename.endswith('.xml')):
+ skipped_list.append(filename)
+ elif slug in slugs:
+ error_list.append((filename, slug, _('Slug already used for %s' % slugs[slug])))
+ elif slug in existing:
+ error_list.append((filename, slug, _('Slug already used in repository.')))
+ else:
+ try:
+ zip.read(filename).decode('utf-8') # test read
+ ok_list.append((filename, slug, title))
+ except UnicodeDecodeError:
+ error_list.append((filename, title, _('File should be UTF-8 encoded.')))
+ slugs[slug] = filename
+
+ if not error_list:
+ for filename, slug, title in ok_list:
+ Book.create(creator=creator,
+ slug=slug,
+ title=title,
+ text=zip.read(filename).decode('utf-8'),
+ )
+
+ return direct_to_template(request, "wiki/document_upload.html", extra_context={
+ "form": form,
+ "ok_list": ok_list,
+ "skipped_list": skipped_list,
+ "error_list": error_list,
+ })
+ else:
+ form = DocumentsUploadForm()
+
+ return direct_to_template(request, "wiki/document_upload.html", extra_context={
+ "form": form,
+ })
+
+
+@never_cache
+@decorator_from_middleware(GZipMiddleware)
+def text(request, slug, chunk=None):
+ try:
+ doc = Chunk.get(slug, chunk).doc
+ except (Chunk.MultipleObjectsReturned, Chunk.DoesNotExist):
+ raise Http404
+