-def create_missing(request, slug):
- slug = slug.replace(' ', '-')
-
- if request.method == "POST":
- form = forms.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 = forms.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 = forms.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 = forms.DocumentsUploadForm()
-
- return direct_to_template(request, "wiki/document_upload.html", extra_context={
- "form": form,
- })
-
-