+@ajax_login_required
+@with_repo
+def file_dc(request, path, repo):
+ errors = None
+
+ if request.method == 'POST':
+ form = forms.DublinCoreForm(request.POST)
+
+ if form.is_valid():
+
+ def save_action():
+ file_contents = repo._get_file(path)
+
+ # wczytaj dokument z repozytorium
+ document = parser.WLDocument.from_string(file_contents)
+ document.book_info.update(form.cleaned_data)
+
+ # zapisz
+ repo._write_file(path, document.serialize().encode('utf-8'))
+ repo._commit( \
+ message=(form.cleaned_data['commit_message'] or 'Lokalny zapis platformy.'), \
+ user=request.user.username )
+
+ try:
+ repo.in_branch(save_action, file_branch(path, request.user) )
+ except UnicodeEncodeError, e:
+ errors = ['Bład wewnętrzny: nie można zakodować pliku do utf-8']
+ except (ParseError, ValidationError), e:
+ errors = [e.message]
+
+ if errors is None:
+ errors = ["Pole '%s': %s\n" % (field[0], field[1].as_text()) for field in form.errors.iteritems()]
+
+ return HttpResponse( json.dumps({'result': errors and 'error' or 'ok', 'errors': errors}) );
+
+ # this is unused currently, but may come in handy
+ content = []
+
+ try:
+ fulltext = repo.get_file(path, file_branch(path, request.user))
+ bookinfo = dcparser.BookInfo.from_string(fulltext)
+ content = bookinfo.to_dict()
+ except (ParseError, ValidationError), e:
+ errors = [e.message]
+
+ return HttpResponse( json.dumps({'result': errors and 'error' or 'ok',
+ 'errors': errors, 'content': content }) )
+
+# Display the main editor view
+
+@login_required
+@with_repo
+def display_editor(request, path, repo):
+
+ # this is the only entry point where we create an autobranch for the user
+ # if it doesn't exists. All other views SHOULD fail.
+ def ensure_branch_exists():
+ parent = repo.get_branch_tip('default')
+ repo._create_branch(file_branch(path, request.user), parent)
+
+ try:
+ repo.with_wlock(ensure_branch_exists)
+
+ return direct_to_template(request, 'explorer/editor.html', extra_context={
+ 'fileid': path,
+ 'panel_list': ['lewy', 'prawy'],
+ 'availble_panels': models.EditorPanel.objects.all(),
+ 'scriptlets': toolbar_models.Scriptlet.objects.all()
+ })
+ except KeyError:
+ return direct_to_template(request, 'explorer/nofile.html', \
+ extra_context = { 'fileid': path })
+
+# ===============
+# = Panel views =
+# ===============
+class panel_view(object):
+
+ def __new__(cls, request, name, path, **kwargs):
+ #try:
+ panel = models.EditorPanel.objects.get(id=name)
+ method = getattr(cls, name + '_panel', None)
+ if not panel or method is None:
+ raise HttpResponseNotFound
+
+ extra_context = method(request, path, panel, **kwargs)
+
+ if not isinstance(extra_context, dict):
+ return extra_context
+
+ extra_context.update({
+ 'toolbar_groups': panel.toolbar_groups.all(),
+ 'toolbar_extra_group': panel.toolbar_extra,
+ 'fileid': path
+ })
+
+ return direct_to_template(request, 'explorer/panels/'+name+'.html',\
+ extra_context=extra_context)
+
+ @staticmethod
+ @ajax_login_required
+ @with_repo
+ def xmleditor_panel(request, path, panel, repo):
+ return {'text': repo.get_file(path, file_branch(path, request.user))}
+
+ @staticmethod
+ @ajax_login_required
+ def gallery_panel(request, path, panel):
+ return {'form': forms.ImageFoldersForm() }
+
+ @staticmethod
+ @ajax_login_required
+ @with_repo
+ def htmleditor_panel(request, path, panel, repo):
+ user_branch = file_branch(path, request.user)
+ try:
+ return {'html': html.transform(repo.get_file(path, user_branch), is_file=False)}
+ except (ParseError, ValidationError), e:
+ return direct_to_template(request, 'explorer/panels/parse_error.html', extra_context={
+ 'fileid': path, 'exception_type': type(e).__name__, 'exception': e,
+ 'panel_name': panel.display_name})
+
+ @staticmethod
+ @ajax_login_required
+ @with_repo
+ def dceditor_panel(request, path, panel, repo):
+ user_branch = file_branch(path, request.user)
+ try:
+ doc_text = repo.get_file(path, user_branch)
+ document = parser.WLDocument.from_string(doc_text)
+ form = forms.DublinCoreForm(info=document.book_info)
+ return {'form': form}
+ except (ParseError, ValidationError), e:
+ return direct_to_template(request, 'explorer/panels/parse_error.html', extra_context={
+ 'fileid': path, 'exception_type': type(e).__name__, 'exception': e,
+ 'panel_name': panel.display_name})
+
+
+@login_required
+@with_repo
+def print_html(request, path, repo):
+ user_branch = file_branch(path, request.user)
+ return HttpResponse(
+ html.transform(repo.get_file(path, user_branch), is_file=False),
+ mimetype="text/html")
+
+@login_required
+@with_repo
+def print_xml(request, path, repo):
+ user_branch = file_branch(path, request.user)
+ return HttpResponse( repo.get_file(path, user_branch), mimetype="text/plain; charset=utf-8")
+
+# =================
+# = Utility views =
+# =================
+@ajax_login_required
+def folder_images(request, folder):
+ return direct_to_template(request, 'explorer/folder_images.html', extra_context={
+ 'images': models.get_images_from_folder(folder),