target_revision = forms.RegexField('[0-9a-f]{40}')
# any additional comments that user wants to add to the change
- comment = forms.CharField(required=False)
+ message = forms.CharField(required=False)
class DocumentUploadForm(forms.Form):
ocr_file = forms.FileField(label='Source OCR file', required=False)
ocr_data = forms.CharField(widget=forms.HiddenInput(), required=False)
bookname = forms.RegexField(regex=r'[0-9\.\w_-]+', \
- label='Publication name', help_text='Example: slowacki-beniowski')
+ label='Publication name', help_text='Example: słowacki__beniowski__pieśń_1')
generate_dc = forms.BooleanField(required=False, \
initial=True, label=u"Generate DublinCore template")
+import os.path
# -*- encoding: utf-8 -*-
__author__= "Łukasz Rekucki"
import librarian.html
from librarian import dcparser
-from wlrepo import RevisionNotFound, LibraryException, DocumentAlreadyExists
-from explorer.models import PullRequest
+from wlrepo import *
+from explorer.models import PullRequest, GalleryForDocument
# internal imports
import api.forms as forms
import api.response as response
-from api.utils import validate_form, hglibrary
+from api.utils import validate_form, hglibrary, natural_order
from api.models import PartCache
+#
+import settings
+
#
# Document List Handlers
#
# not top-level anymore
document_tree.pop(part)
parent['parts'].append(child)
+
+ # sort the right way
+
+
+ for doc in documents.itervalues():
+ doc['parts'].sort(key=natural_order(lambda d: d['name']))
- return {'documents': sorted(document_tree.values()) }
+ return {'documents': sorted(document_tree.itervalues(),
+ key=natural_order(lambda d: d['name']) ) }
@validate_form(forms.DocumentUploadForm, 'POST')
@hglibrary
try:
doc = lib.document(docid)
udoc = doc.take(request.user.username)
- except RevisionNotFound:
- return request.EnityNotFound().django_response()
+ except RevisionNotFound, e:
+ return response.EntityNotFound().django_response({
+ 'exception': type(e), 'message': e.message})
# is_shared = udoc.ancestorof(doc)
# is_uptodate = is_shared or shared.ancestorof(document)
'html_url': reverse('dochtml_view', args=[udoc.id,udoc.revision]),
'text_url': reverse('doctext_view', args=[udoc.id,udoc.revision]),
'dc_url': reverse('docdc_view', args=[udoc.id,udoc.revision]),
- #'gallery_url': reverse('docdc_view', args=[udoc.id,udoc.revision]),
+ 'gallery_url': reverse('docgallery_view', args=[udoc.id]),
+ 'merge_url': reverse('docmerge_view', args=[udoc.id]),
'user_revision': udoc.revision,
- 'public_revision': doc.revision,
+ 'user_timestamp': udoc.revision.timestamp,
+ 'public_revision': doc.revision,
+ 'public_timestamp': doc.revision.timestamp,
}
return result
#
#
class DocumentHTMLHandler(BaseHandler):
- allowed_methods = ('GET', 'PUT')
+ allowed_methods = ('GET')
@hglibrary
def read(self, request, docid, revision, lib):
else:
document = lib.document_for_rev(revision)
+ if document.id != docid:
+ return response.BadRequest().django_response({'reason': 'name-mismatch',
+ 'message': 'Provided revision refers, to document "%s", but provided "%s"' % (document.id, docid) })
+
return librarian.html.transform(document.data('xml'), is_file=False)
- except RevisionNotFound:
- return response.EntityNotFound().django_response()
+ except (EntryNotFound, RevisionNotFound), e:
+ return response.EntityNotFound().django_response({
+ 'exception': type(e), 'message': e.message})
+
+
+#
+# Image Gallery
+#
+from django.core.files.storage import FileSystemStorage
+
+class DocumentGalleryHandler(BaseHandler):
+ allowed_methods = ('GET')
+
+ def read(self, request, docid):
+ """Read meta-data about scans for gallery of this document."""
+ galleries = []
+
+ for assoc in GalleryForDocument.objects.filter(document=docid):
+ dirpath = os.path.join(settings.MEDIA_ROOT, assoc.subpath)
+
+ if not os.path.isdir(dirpath):
+ print u"[WARNING]: missing gallery %s" % dirpath
+ continue
+
+ gallery = {'name': assoc.name, 'pages': []}
+
+ for file in sorted(os.listdir(dirpath), key=natural_order()):
+ print file
+ name, ext = os.path.splitext(os.path.basename(file))
+
+ if ext.lower() not in ['.png', '.jpeg', '.jpg']:
+ print "Ignoring:", name, ext
+ continue
+
+ url = settings.MEDIA_URL + assoc.subpath + u'/' + file.decode('utf-8');
+ gallery['pages'].append(url)
+
+ galleries.append(gallery)
+
+ return galleries
#
# Document Text View
document = lib.document(docid)
else:
document = lib.document_for_rev(revision)
+
+ if document.id != docid:
+ return response.BadRequest().django_response({'reason': 'name-mismatch',
+ 'message': 'Provided revision is not valid for this document'})
# TODO: some finer-grained access control
return document.data('xml')
- except RevisionNotFound:
- return response.EntityNotFound().django_response()
+ except (EntryNotFound, RevisionNotFound), e:
+ return response.EntityNotFound().django_response({
+ 'exception': type(e), 'message': e.message})
@hglibrary
def update(self, request, docid, revision, lib):
"document": ndoc.id,
"subview": "xml",
"previous_revision": current.revision,
- "updated_revision": ndoc.revision,
+ "revision": ndoc.revision,
+ 'timestamp': ndoc.revision.timestamp,
"url": reverse("doctext_view", args=[ndoc.id, ndoc.revision])
})
except Exception, e:
doc = lib.document(docid)
else:
doc = lib.document_for_rev(revision)
+
+
+ if document.id != docid:
+ return response.BadRequest().django_response({'reason': 'name-mismatch',
+ 'message': 'Provided revision is not valid for this document'})
bookinfo = dcparser.BookInfo.from_string(doc.data('xml'))
return bookinfo.serialize()
- except RevisionNotFound:
- return response.EntityNotFound().django_response()
+ except (EntryNotFound, RevisionNotFound), e:
+ return response.EntityNotFound().django_response({
+ 'exception': type(e), 'message': e.message})
@hglibrary
def update(self, request, docid, revision, lib):
"document": ndoc.id,
"subview": "dc",
"previous_revision": current.revision,
- "updated_revision": ndoc.revision,
+ "revision": ndoc.revision,
+ 'timestamp': ndoc.revision.timestamp,
"url": reverse("docdc_view", args=[ndoc.id, ndoc.revision])
}
except Exception, e:
document=docid,
source_revision = str(udoc.revision),
status="N",
- comment = form.cleaned_data['comment'] or '$AUTO$ Document shared.'
+ comment = form.cleaned_data['message'] or '$AUTO$ Document shared.'
)
prq.save()
success, changed = udoc.update(request.user.username)
if form.cleaned_data['type'] == 'share':
- success, changed = udoc.share(form.cleaned_data['comment'])
+ success, changed = udoc.share(form.cleaned_data['message'])
if not success:
- return response.EntityConflict().django_response()
+ return response.EntityConflict().django_response({})
if not changed:
return response.SuccessNoContent().django_response()
"name": udoc.id,
"parent_user_resivion": udoc.revision,
"parent_revision": doc.revision,
- "revision": udoc.revision,
+ "revision": ndoc.revision,
+ 'timestamp': ndoc.revision.timestamp,
})
for part in created:
me.objects.create(user_id=userid, document_id=docid, part_id=part)
+
+
+
document_text_resource = Resource(dh.DocumentTextHandler, **authdata)
document_html_resource = Resource(dh.DocumentHTMLHandler, **authdata)
document_dc_resource = Resource(dh.DocumentDublinCoreHandler, **authdata)
+document_gallery = Resource(dh.DocumentGalleryHandler, **authdata)
document_merge = Resource(dh.MergeHandler, **authdata)
import api.handlers.manage_handlers as mh
'document_text_resource',
'document_html_resource',
'document_dc_resource',
+ 'document_gallery',
'document_merge',
'toolbar_buttons',
'scriptlets',
@temprepo('simple')
- def test_document_text_update(self):
+ def test_document_text_save(self):
self.assertTrue(self.client.login(username='admin', password='admin'))
TEXT = u"Ala ma kota i psa"
url(urlpath(r'documents', DOC, format=False),
document_resource, {'emitter_format': 'json'},
name="document_view"),
-
+
+ url(urlpath(r'documents', DOC, 'gallery', format=False),
+ document_gallery, {'emitter_format': 'json'},
+ name="docgallery_view"),
+
+ # XML
url(urlpath(r'documents', DOC, 'text', REVISION, format=False),
document_text_resource, {'emitter_format': 'rawxml'},
name="doctext_view"),
+ # HTML
url(urlpath(r'documents', DOC, 'html', REVISION, format=False),
document_html_resource, {'emitter_format': 'rawhtml'},
name="dochtml_view"),
+ # DC
url(urlpath(r'documents', DOC, 'dc', REVISION),
document_dc_resource,
name="docdc_view_withformat"),
document_dc_resource, {'emitter_format': 'json'},
name="docdc_view"),
+ # MERGE
url(urlpath(r'documents', DOC, 'revision', format=False),
document_merge, {'emitter_format': 'json'}, name="docmerge_view")
import api.response
-from wlrepo import MercurialLibrary
+import wlrepo
import settings
class TextEmitter(Emitter):
def hglibrary(func):
@wraps(func)
def decorated(self, *args, **kwargs):
- l = MercurialLibrary(settings.REPOSITORY_PATH)
+ l = wlrepo.open_library(settings.REPOSITORY_PATH, 'hg')
kwargs['lib'] = l
return func(self, *args, **kwargs)
- return decorated
+ return decorated
+
+
+
+import re
+NAT_EXPR = re.compile(r'(\d+)', re.LOCALE | re.UNICODE)
+def natural_order(get_key=lambda x: x):
+ def getter(key):
+ key = [int(x) if n%2 else x for (n,x) in enumerate(NAT_EXPR.split(get_key(key))) ]
+ return key
+
+ return getter
admin.site.register(explorer.models.EditorSettings)
admin.site.register(explorer.models.EditorPanel)
-admin.site.register(explorer.models.PullRequest)
\ No newline at end of file
+admin.site.register(explorer.models.PullRequest)
+admin.site.register(explorer.models.GalleryForDocument)
\ No newline at end of file
Przykład:
{
'panels': [
- {'name': 'htmleditor', 'ratio': 0.5},
+ {'name': 'htmleditor',
+ 'ratio': 0.5},
{'name': 'gallery', 'ratio': 0.5}
],
'recentFiles': [
# revision number in which the changes were merged (if any)
merged_rev = models.CharField(max_length=40, blank=True, null=True)
-
def __unicode__(self):
return unicode(self.comitter) + u':' + self.document
-
-def get_image_folders():
- return sorted(fn for fn in os.listdir(os.path.join(settings.MEDIA_ROOT, settings.IMAGE_DIR)) if not fn.startswith('.'))
-
-def get_images_from_folder(folder):
- return sorted(settings.MEDIA_URL + settings.IMAGE_DIR + u'/' + folder + u'/' + fn.decode('utf-8') for fn
- in os.listdir(os.path.join(settings.MEDIA_ROOT, settings.IMAGE_DIR, folder))
- if not fn.decode('utf-8').startswith('.'))
+# Yes, this is intentionally unnormalized !
+class GalleryForDocument(models.Model):
+ name = models.CharField(max_length=100)
+
+ # directory containing scans under MEDIA_ROOT/
+ subpath = models.CharField(max_length=255)
+ # document associated with the gallery
+ document = models.CharField(max_length=255)
def with_repo(view):
"""Open a repository for this view"""
def view_with_repo(request, *args, **kwargs):
- kwargs['repo'] = wlrepo.MercurialLibrary(settings.REPOSITORY_PATH)
+ kwargs['repo'] = wlrepo.open_library(settings.REPOSITORY_PATH, 'hg')
return view(request, *args, **kwargs)
return view_with_repo
class RevisionNotFound(LibraryException):
def __init__(self, rev):
LibraryException.__init__(self, "Revision %r not found." % rev)
- pass
+
+class EntryNotFound(LibraryException):
+ def __init__(self, rev, entry, guesses=[]):
+ LibraryException.__init__(self, \
+ u"Entry '%s' at revision %r not found. %s" % (entry, rev, \
+ (u"Posible values:\n" + u',\n'.join(guesses)) if len(guesses) else u'') )
class DocumentAlreadyExists(LibraryException):
pass
# import backends to local namespace
-from mercurial_backend.library import MercurialLibrary
\ No newline at end of file
+
+def open_library(path, proto, *args, **kwargs):
+ if proto == 'hg':
+ import wlrepo.mercurial_backend
+ return wlrepo.mercurial_backend.MercurialLibrary(path, *args, **kwargs)
+
+ raise NotImplemented()
\ No newline at end of file
@property
def document_name(self):
- return self._docname
+ return self._docname.decode('utf-8')
@property
def user_name(self):
- return self._username
+ return self._username.decode('utf-8')
def hgrev(self):
return self._changectx.node()
def hgbranch(self):
return self._changectx.branch()
+ @property
+ def timestamp(self):
+ return self._changectx.date()[0]
+
def __unicode__(self):
return u"%s" % self._changectx.hex()
+ def __str__(self):
+ return self.__unicode__().encode('utf-8')
+
def __repr__(self):
return "%s" % self._changectx.hex()
__doc__ = "Module documentation."
import wlrepo
+import mercurial.error
class MercurialDocument(wlrepo.Document):
def data(self, entry):
- path = self._revision._docname + '.' + entry
- return self._library._filectx(path, \
- self._revision.hgrev()).data()
+ path = self._library._sanitize_string(self.id + u'.' + entry)
+ try:
+ return self._library._filectx(path, \
+ self._revision.hgrev()).data().decode('utf-8')
+ except mercurial.error.LookupError, e:
+ fl = [x.decode('utf-8') for x in self._revision._changectx]
+ raise wlrepo.EntryNotFound(self._revision, path.decode('utf-8'), fl)
def quickwrite(self, entry, data, msg, user=None):
user = user or self.owner
f.close()
l._fileadd(r(entry))
- return self.invoke_and_commit(write, lambda d: (msg, self.owner))
+ return self.invoke_and_commit(write, lambda d: (msg, \
+ self._library._sanitize_string(self.owner)) )
- def invoke_and_commit(self, ops,
- commit_info):
+ def invoke_and_commit(self, ops, commit_info):
lock = self._library.lock()
try:
self._library._checkout(self._revision.hgrev())
def entry_path(entry):
- return self.id + '.' + entry
+ return self._library._sanitize_string(self.id + u'.' + entry)
ops(self._library, entry_path)
message, user = commit_info(self)
finally:
lock.release()
- def __str__(self):
+ def __unicode__(self):
return u"Document(%s:%s)" % (self.name, self.owner)
+ def __str__(self):
+ return self.__unicode__().encode('utf-8')
+
def __eq__(self, other):
return (self._revision == other._revision) and (self.name == other.name)
from mercurial import error
import wlrepo
-from wlrepo.mercurial_backend.document import MercurialDocument
from wlrepo.mercurial_backend import MercurialRevision
+from wlrepo.mercurial_backend.document import MercurialDocument
class MergeStatus(object):
def __init__(self, mstatus):
@property
def ospath(self):
- return self._ospath
+ return self._ospath.decode('utf-8')
def document_for_rev(self, revision):
if revision is None:
raise ValueError("Revision can't be None.")
if not isinstance(revision, MercurialRevision):
+ revision = self._sanitize_string(unicode(revision))
rev = self.get_revision(revision)
else:
rev = revision
return MercurialRevision(self, ctx)
- def fulldocid(self, docid, user=None):
- docid = self._sanitize_string(docid)
- user = self._sanitize_string(user)
-
- fulldocid = ''
+ def fulldocid(self, docid, user=None):
+ fulldocid = u''
if user is not None:
- fulldocid += '$user:' + user
- fulldocid += '$doc:' + docid
+ fulldocid += u'$user:' + user
+ fulldocid += u'$doc:' + docid
return fulldocid
return False
def document_create(self, docid):
- docid = self._sanitize_string(docid)
+
# check if it already exists
fullid = self.fulldocid(docid)
if self.has_revision(fullid):
- raise wlrepo.DocumentAlreadyExists("Document %s already exists!" % docid);
+ raise wlrepo.DocumentAlreadyExists(u"Document %s already exists!" % docid);
# doesn't exist
- self._create_branch(fullid)
+ self._create_branch(self._sanitize_string(fullid))
return self.document_for_rev(fullid)
#
});
+Editor.ImageGalleryModel = Editor.Model.extend({
+ _className: 'Editor.ImageGalleryModel',
+ serverURL: null,
+ state: 'empty',
+
+ init: function(serverURL) {
+ this._super();
+ this.set('state', 'empty');
+ this.serverURL = serverURL;
+ // olewać data
+ this.pages = []
+ },
+
+ load: function() {
+ if (this.get('state') == 'empty') {
+ this.set('state', 'loading');
+ $.ajax({
+ url: this.serverURL,
+ dataType: 'json',
+ success: this.loadingSucceeded.bind(this)
+ });
+ }
+ },
+
+ loadingSucceeded: function(data) {
+ if (this.get('state') != 'loading') {
+ alert('erroneous state:', this.get('state'));
+ }
+
+ this.set('pages', data.pages)
+ this.set('state', 'synced');
+ },
+
+ set: function(property, value) {
+ if (property == 'state') {
+ console.log(this.description(), ':', property, '=', value);
+ }
+ return this._super(property, value);
+ }
+});
+
+
Editor.DocumentModel = Editor.Model.extend({
_className: 'Editor.DocumentModel',
data: null, // name, text_url, latest_rev, latest_shared_rev, parts_url, dc_url, size
this.set('state', 'synced');
this.contentModels = {
'xml': new Editor.XMLModel(data.text_url),
- 'html': new Editor.HTMLModel(data.html_url)
+ 'html': new Editor.HTMLModel(data.html_url),
+ 'gallery': new Editor.ImageGalleryModel(data.gallery_url)
};
for (var key in this.contentModels) {
this.contentModels[key].addObserver(this, 'state', this.contentModelStateChanged.bind(this));
<script src="{{STATIC_URL}}js/views/split.js" type="text/javascript" charset="utf-8"></script>
<script src="{{STATIC_URL}}js/views/xml.js" type="text/javascript" charset="utf-8"></script>
<script src="{{STATIC_URL}}js/views/html.js" type="text/javascript" charset="utf-8"></script>
+ <script src="{{STATIC_URL}}js/views/gallery.js" type="text/javascript" charset="utf-8"></script>
<script src="{{STATIC_URL}}js/views/panel_container.js" type="text/javascript" charset="utf-8"></script>
<script src="{{STATIC_URL}}js/models.js" type="text/javascript" charset="utf-8"></script>
<div class="htmlview">
</div>
</script>
+
+ <script type="text/html" charset="utf-8" id="image-gallery-view-template">
+ <div class="image-gallery-view-template">
+
+ <div class="image-gallery-panel-header">
+ <p>
+ <button type="button" class="image-gallery-prev-button">Previous</button>
+ <input type="input" class="image-gallery-current-page" />
+ <button type="button" class="image-gallery-next-button">Next</button>
+ </p>
+ </div>
+
+ <div>
+ <% for (page in panels) { %>
+ <p>strona</p>
+ <% }; %>
+ </div>
+
+ </div>
+ </script>
<script type="text/html" charset="utf-8" id="button-toolbar-view-template">
<div class="buttontoolbarview">