__doc__ = "Module documentation."
from piston.handler import BaseHandler, AnonymousBaseHandler
+from django.http import HttpResponse
import re
from datetime import date
import librarian
import librarian.html
+import difflib
from librarian import dcparser, parser
from wlrepo import *
import api.forms as forms
import api.response as response
from api.utils import validate_form, hglibrary, natural_order
-from api.models import PartCache
+from api.models import PartCache, PullRequest
#
import settings
def is_prq(username):
return username.startswith('$prq-')
+def prq_for_user(username):
+ try:
+ return PullRequest.objects.get(id=int(username[5:]))
+ except:
+ return None
+
def check_user(request, user):
log.info("user: %r, perm: %r" % (request.user, request.user.get_all_permissions()) )
#pull request
log.info("DOCID %s", docid)
doc = lib.document_create(docid)
# document created, but no content yet
-
try:
doc = doc.quickwrite('xml', data.encode('utf-8'),
'$AUTO$ XML data uploaded.', user=request.user.username)
return result
+
+class DiffHandler(BaseHandler):
+ allowed_methods = ('GET',)
+
+ @hglibrary
+ def read(self, request, source_revision, target_revision, lib):
+ '''Return diff between source_revision and target_revision)'''
+ source_document = lib.document_for_rev(source_revision)
+ target_document = lib.document_for_rev(target_revision)
+ print source_document,
+ print target_document
+ diff = difflib.unified_diff(
+ source_document.data('xml').splitlines(True),
+ target_document.data('xml').splitlines(True),
+ 'source',
+ 'target')
+
+ return ''.join(list(diff))
+
+
#
# Document Meta Data
#
# the user doesn't have this document checked out
# or some other weird error occured
# try to do the checkout
- if is_prq(user) or (user == request.user.username):
- try:
+ try:
+ if user == request.user.username:
mdoc = lib.document(docid)
doc = mdoc.take(user)
-
- if is_prq(user):
- # source revision, should probably change
- # but there are no changes yet, so...
- pass
-
- except RevisionNotFound, e:
+ elif is_prq(user):
+ prq = prq_for_user(user)
+ # commiter's document
+ prq_doc = lib.document_for_rev(prq.source_revision)
+ doc = prq_doc.take(user)
+ else:
return response.EntityNotFound().django_response({
'reason': 'document-not-found',
'message': e.message,
- 'docid': docid
+ 'docid': docid,
+ 'user': user,
})
- else:
+ except RevisionNotFound, e:
return response.EntityNotFound().django_response({
'reason': 'document-not-found',
'message': e.message,
'docid': docid,
- 'user': user,
+ 'user': user
})
return {
if form.cleaned_data['type'] == 'update':
# update is always performed from the file branch
# to the user branch
- changed, clean = base_doc.update(request.user.username)
-
- # update user document
- if changed:
- user_doc_new = user_doc.latest()
- else:
- user_doc_new = user_doc
+ user_doc_new = base_doc.update(request.user.username)
# shared document is the same
doc_new = doc
"revision": user_doc_new.revision,
'timestamp': user_doc_new.revision.timestamp,
- "parent_revision": user_doc_new.revision,
- "parent_timestamp": user_doc_new.revision.timestamp,
+ "parent_revision": user_doc.revision,
+ "parent_timestamp": user_doc.revision.timestamp,
"shared_revision": doc_new.revision,
"shared_timestamp": doc_new.revision.timestamp,