Permalink
Browse files

Issue 67101: Unify line breaks upon upload.

git-svn-id: http://rietveld.googlecode.com/svn/trunk@422 e30e9e8f-1b4c-0410-881d-6f13584adfe0
  • Loading branch information...
albrecht.andi
albrecht.andi committed Jun 11, 2009
1 parent cf02317 commit 7136001af7bc6740501baa99419f9147136247b1
Showing with 27 additions and 9 deletions.
  1. +22 −5 codereview/engine.py
  2. +5 −4 codereview/views.py
@@ -133,7 +133,8 @@ def FetchBase(base, patch):
msg = 'Error fetching %s: HTTP status %s' % (url, result.status_code)
logging.warn('FetchBase: %s', msg)
raise FetchError(msg)
return models.Content(text=ToText(result.content), parent=patch)
return models.Content(text=ToText(UnifyLinebreaks(result.content)),
parent=patch)
def _MakeUrl(base, filename, rev):
@@ -803,7 +804,23 @@ def ToText(text):
Returns:
A db.Text instance.
"""
try:
return db.Text(text, encoding='utf-8')
except UnicodeDecodeError:
return db.Text(text, encoding='latin-1')
if isinstance(text, unicode):
# A TypeError is raised if text is unicode and an encoding is given.
return db.Text(text)
else:
try:
return db.Text(text, encoding='utf-8')
except UnicodeDecodeError:
return db.Text(text, encoding='latin-1')
def UnifyLinebreaks(text):
"""Helper to return a string with all line breaks converted to LF.
Args:
text: a string.
Returns:
A string with all line breaks converted to LF.
"""
return text.replace('\r\n', '\n').replace('\r', '\n')
@@ -916,7 +916,7 @@ def upload_content(request):
if patch.is_binary:
content.data = data
else:
content.text = engine.ToText(data)
content.text = engine.ToText(engine.UnifyLinebreaks(data))
content.checksum = checksum
content.put()
return HttpResponse('OK', content_type='text/plain')
@@ -946,8 +946,9 @@ def upload_patch(request):
if patchset.data:
return HttpResponse('ERROR: Can\'t upload patches to patchset with data.',
content_type='text/plain')
text = engine.ToText(engine.UnifyLinebreaks(form.get_uploaded_patch()))
patch = models.Patch(patchset=patchset,
text=engine.ToText(form.get_uploaded_patch()),
text=text,
filename=form.cleaned_data['filename'], parent=patchset)
patch.put()
if form.cleaned_data.get('content_upload'):
@@ -1054,7 +1055,7 @@ def _get_data_url(form):
return None
if data is not None:
data = db.Blob(data.read())
data = db.Blob(engine.UnifyLinebreaks(data.read()))
url = None
elif url:
try:
@@ -1065,7 +1066,7 @@ def _get_data_url(form):
if fetch_result.status_code != 200:
form.errors['url'] = ['HTTP status code %s' % fetch_result.status_code]
return None
data = db.Blob(fetch_result.content)
data = db.Blob(engine.UnifyLinebreaks(fetch_result.content))
return data, url, separate_patches

0 comments on commit 7136001

Please sign in to comment.