From 9625fde4997f3d49bd321e459c971db78096ffd3 Mon Sep 17 00:00:00 2001 From: Boris Kopin Date: Fri, 21 Feb 2014 15:14:26 +0400 Subject: [PATCH] Remove webodf submodule. Move converter to core. Prettify textedit app. Task #10 --- .gitmodules | 7 +- textum/textedit/core/__init__.py | 0 .../textedit/{ => core}/converter/engine.py | 0 .../textedit/{ => core}/converter/testdoc.odt | Bin textum/textedit/{ => core}/converter/unoconv | 0 textum/textedit/core/response.py | 40 ++++++++ textum/textedit/core/serialize.py | 35 +++++++ textum/textedit/views.py | 94 ++---------------- textum/textedit/webodf | 1 - textum/website/views.py | 2 +- 10 files changed, 88 insertions(+), 91 deletions(-) create mode 100644 textum/textedit/core/__init__.py rename textum/textedit/{ => core}/converter/engine.py (100%) rename textum/textedit/{ => core}/converter/testdoc.odt (100%) rename textum/textedit/{ => core}/converter/unoconv (100%) create mode 100644 textum/textedit/core/response.py create mode 100644 textum/textedit/core/serialize.py delete mode 160000 textum/textedit/webodf diff --git a/.gitmodules b/.gitmodules index 5435749..30e1a43 100644 --- a/.gitmodules +++ b/.gitmodules @@ -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 diff --git a/textum/textedit/core/__init__.py b/textum/textedit/core/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/textum/textedit/converter/engine.py b/textum/textedit/core/converter/engine.py similarity index 100% rename from textum/textedit/converter/engine.py rename to textum/textedit/core/converter/engine.py diff --git a/textum/textedit/converter/testdoc.odt b/textum/textedit/core/converter/testdoc.odt similarity index 100% rename from textum/textedit/converter/testdoc.odt rename to textum/textedit/core/converter/testdoc.odt diff --git a/textum/textedit/converter/unoconv b/textum/textedit/core/converter/unoconv similarity index 100% rename from textum/textedit/converter/unoconv rename to textum/textedit/core/converter/unoconv diff --git a/textum/textedit/core/response.py b/textum/textedit/core/response.py new file mode 100644 index 0000000..d1d39b6 --- /dev/null +++ b/textum/textedit/core/response.py @@ -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) diff --git a/textum/textedit/core/serialize.py b/textum/textedit/core/serialize.py new file mode 100644 index 0000000..4259e6d --- /dev/null +++ b/textum/textedit/core/serialize.py @@ -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, + } diff --git a/textum/textedit/views.py b/textum/textedit/views.py index 9662a4b..e8b3e3b 100644 --- a/textum/textedit/views.py +++ b/textum/textedit/views.py @@ -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 """ @@ -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 @@ -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)) + diff --git a/textum/textedit/webodf b/textum/textedit/webodf deleted file mode 160000 index 017bfbd..0000000 --- a/textum/textedit/webodf +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 017bfbda055568629a7cbf2ceb1c942c0ebc7074 diff --git a/textum/website/views.py b/textum/website/views.py index 498f4db..424d97a 100644 --- a/textum/website/views.py +++ b/textum/website/views.py @@ -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):