Skip to content

Commit

Permalink
Remove webodf submodule. Move converter to core. Prettify textedit app.
Browse files Browse the repository at this point in the history
  • Loading branch information
datakop committed Feb 21, 2014
1 parent 32721eb commit 9625fde
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 91 deletions.
7 changes: 2 additions & 5 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
[submodule "textum/textedit/webodf"]
path = textum/textedit/webodf
url = https://github.com/KopBob/WebODF
[submodule "textum/textedit/converter/unoconv"]
path = textum/textedit/converter/unoconv
[submodule "textum/textedit/core/converter/unoconv"]
path = textum/textedit/core/converter/unoconv
url = https://github.com/KopBob/unoconv
Empty file.
File renamed without changes.
File renamed without changes.
40 changes: 40 additions & 0 deletions textum/textedit/core/response.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# encoding: utf-8
from django.http import HttpResponse
from django.utils import simplejson



MIMEANY = '*/*'
MIMEJSON = 'application/json'
MIMETEXT = 'text/plain'


def response_mimetype(request):
"""response_mimetype -- Return a proper response mimetype, accordingly to
what the client accepts, as available in the `HTTP_ACCEPT` header.
request -- a HttpRequest instance.
"""
can_json = MIMEJSON in request.META['HTTP_ACCEPT']
can_json |= MIMEANY in request.META['HTTP_ACCEPT']
return MIMEJSON if can_json else MIMETEXT


class JSONResponse(HttpResponse):
"""JSONResponse -- Extends HTTPResponse to handle JSON format response.
This response can be used in any view that should return a json stream of
data.
Usage:
def a_iew(request):
content = {'key': 'value'}
return JSONResponse(content, mimetype=response_mimetype(request))
"""
def __init__(self, obj='', json_opts=None, mimetype=MIMEJSON, *args, **kwargs):
json_opts = json_opts if isinstance(json_opts, dict) else {}
content = simplejson.dumps(obj, **json_opts)
super(JSONResponse, self).__init__(content, mimetype, *args, **kwargs)
35 changes: 35 additions & 0 deletions textum/textedit/core/serialize.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# encoding: utf-8
import mimetypes
import re
from django.core.urlresolvers import reverse



def order_name(name):
"""order_name -- Limit a text to 20 chars length, if necessary strips the
middle of the text and substitute it for an ellipsis.
name -- text to be limited.
"""
name = re.sub(r'^.*/', '', name)
if len(name) <= 20:
return name
return name[:10] + "..." + name[-7:]


def serialize(instance, file_attr='file'):
"""serialize -- Serialize a Picture instance into a dict.
instance -- Picture instance
file_attr -- attribute name that contains the FileField or ImageField
"""
obj = getattr(instance, file_attr)
return {
'url': obj.url,
'name': order_name(obj.name),
'type': mimetypes.guess_type(obj.path)[0] or 'application/rtf',
'thumbnailUrl': obj.url,
'size': obj.size,
}
94 changes: 10 additions & 84 deletions textum/textedit/views.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,15 @@
import json
from django.http import HttpResponse, HttpResponseRedirect
# encoding: utf-8
from django.http import HttpResponse
from django.views import generic
from django.shortcuts import render, render_to_response
from django.template import RequestContext
from django.core.urlresolvers import reverse
from django import forms
from .models import RTFFile, TImage

from django.views.generic import CreateView, DeleteView, ListView

# encoding: utf-8
from django.utils import simplejson

# encoding: utf-8
import mimetypes
import re

from django.shortcuts import render_to_response
from django.template.context import RequestContext

from .models import RTFFile, TImage
from .core.response import JSONResponse, response_mimetype
from .core.serialize import serialize



class TextEdit(generic.View):
""" TextEdit main view """
Expand All @@ -33,74 +24,6 @@ def post(self, request, *args, **kwargs):
return HttpResponse("post")


def fullView(request):
return render_to_response('textedit/fullView.html', locals(), RequestContext(request))


def order_name(name):
"""order_name -- Limit a text to 20 chars length, if necessary strips the
middle of the text and substitute it for an ellipsis.
name -- text to be limited.
"""
name = re.sub(r'^.*/', '', name)
if len(name) <= 20:
return name
return name[:10] + "..." + name[-7:]


def serialize(instance, file_attr='file'):
"""serialize -- Serialize a Picture instance into a dict.
instance -- Picture instance
file_attr -- attribute name that contains the FileField or ImageField
"""
obj = getattr(instance, file_attr)
return {
'url': obj.url,
'name': order_name(obj.name),
'type': mimetypes.guess_type(obj.path)[0] or 'application/rtf',
'thumbnailUrl': obj.url,
'size': obj.size,
}

MIMEANY = '*/*'
MIMEJSON = 'application/json'
MIMETEXT = 'text/plain'

def response_mimetype(request):
"""response_mimetype -- Return a proper response mimetype, accordingly to
what the client accepts, as available in the `HTTP_ACCEPT` header.
request -- a HttpRequest instance.
"""
can_json = MIMEJSON in request.META['HTTP_ACCEPT']
can_json |= MIMEANY in request.META['HTTP_ACCEPT']
return MIMEJSON if can_json else MIMETEXT


class JSONResponse(HttpResponse):
"""JSONResponse -- Extends HTTPResponse to handle JSON format response.
This response can be used in any view that should return a json stream of
data.
Usage:
def a_iew(request):
content = {'key': 'value'}
return JSONResponse(content, mimetype=response_mimetype(request))
"""
def __init__(self, obj='', json_opts=None, mimetype=MIMEJSON, *args, **kwargs):
json_opts = json_opts if isinstance(json_opts, dict) else {}
content = simplejson.dumps(obj, **json_opts)
super(JSONResponse, self).__init__(content, mimetype, *args, **kwargs)


class RTFCreateView(CreateView):
model = RTFFile

Expand Down Expand Up @@ -133,3 +56,6 @@ def form_invalid(self, form):
return HttpResponse(content=data, status=400, content_type='application/json')


def fullView(request):
return render_to_response('textedit/fullView.html', locals(), RequestContext(request))

1 change: 0 additions & 1 deletion textum/textedit/webodf
Submodule webodf deleted from 017bfb
2 changes: 1 addition & 1 deletion textum/website/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def post(self, request, *args, **kwargs):
def index(request):
"""Redirect to Home page"""

return HttpResponseRedirect(reverse('website:home'))
return HttpResponseRedirect(reverse('textedit:main'))


class SignIn(generic.View):
Expand Down

0 comments on commit 9625fde

Please sign in to comment.