1. Eric Larson
  2. Dragoman

Source

Dragoman / dragoman / control / web.py

import simplejson
import cherrypy

from eggmonster import env
from dragoman.control import PageMixin, MethodHandler, ContentTypes
from dragoman.lib.storage import CatalogList, Catalog, LanguageList
from dragoman.lib.languages import languages as available_languages

from pprint import pformat, pprint


def page(f):
    def wrapper(obj, *args, **kw):
        cl = CatalogList()
        page = {
            'catalogs': cl.catalogs,
            'langs': [],
            'title': 'Translations by Dragoman',
        }
        return f(obj, page, *args, **kw)
    return wrapper


class HomePage(PageMixin):
    @page
    def index(self, page):
        return self.render('index.tmpl', page)


class HelpPage(PageMixin):
    @page
    def index(self, page):
        page.update({
            'catalogs': {},
            'doc_body': '',
        })
        return self.render('help.tmpl', page)


class EditLanguage(PageMixin, MethodHandler):
    def POST(self, abbrev, name, *are, **kw):
        langs = LanguageList()
        langs.add(abbrev, name)
        referrer = cherrypy.request.headers.get('Referer')
        raise cherrypy.HTTPRedirect(referrer)

    def DELETE(self, abbrev):
        langs = LanguageList()
        langs.delete(abbrev)
        referrer = cherrypy.request.headers.get('Referer')
        raise cherrypy.HTTPRedirect(referrer)


class EditCatalogs(PageMixin, MethodHandler):
    @page
    def GET(self, page):
        return self.render('catalogs_edit.tmpl', page)

    def POST(self, catalog=None, **kw):
        if catalog:
            cl = CatalogList()
            cl.add(catalog)
        raise cherrypy.HTTPRedirect('/catalog')
    
    def DELETE(self, catalog=None, **kw):
        if catalog:
            c = CatalogList()
            c.delete(catalog)
        raise cherrypy.HTTPRedirect('/catalog')


class EditCatalog(PageMixin, MethodHandler):

    @page    
    def GET(self, page, catalog=None):
        page.update({
            'available_languages': available_languages,
            'catalog': catalog,
            'langs': page['catalogs'][catalog],            
        })
        return self.render('catalog_edit.tmpl', page)


    def POST(self, catalog, lang=None):
        if not lang or lang == '':
            raise cherrypy.HTTPError(400)
        c = Catalog(catalog, lang)
        c.initialize()
        raise cherrypy.HTTPRedirect('/catalog/%s' % catalog)

    def DELETE(self, catalog, lang=None):
        if not lang or lang == '':
            raise cherrypy.HTTPError(400)
        c = Catalog(catalog, lang)
        c.delete()
        raise cherrypy.HTTPRedirect('/catalog/%s' % catalog)


class EditPhrases(PageMixin, MethodHandler):

    @page    
    def GET(self, page, catalog, lang):
        c = Catalog(catalog, lang)
        page.update({
            'langs': page['catalogs'][catalog],
            'phrases' : c.phrases,
            'catalog' : catalog,
            'lang': lang,
        })
        return self.render('phrases_edit.tmpl', page)

    def POST(self, catalog, lang, **kw):
        c = Catalog(catalog, lang)
        for name, translation in kw.items():
            if name.startswith('id_'):
                obj_id = name[3:]
                if translation:
                    c.update(obj_id, {'translation': translation})
        raise cherrypy.HTTPRedirect(cherrypy.request.path_info)

    def PUT(self, catalog, lang, msgid, msgstr, **kw):
        c = Catalog(catalog, lang)
        extra = dict([(k, v) for k, v in kw.items() if not k.startswith('_')])
        c.add(msgid, msgstr, extra)
        raise cherrypy.HTTPRedirect(cherrypy.request.path_info)


def parse_args(args):
    if args:
        return filter(lambda x: x, args.split('/'))
    return args


def reqargs(*args, **kw):
    required_args = args
    def dec(f):
        def wrapper(obj, args=None):
            result = parse_args(args)
            if len(result) != len(required_args):
                raise cherrypy.HTTPError(404)
            return f(obj, **dict(zip(required_args, result)))
        return wrapper
    return dec
                    

class AjaxController(PageMixin, ContentTypes):

    def test(self, *args, **kw):
        return self.txt('hello world')

    @reqargs('catalog', 'lang')
    def empty_phrases(self, catalog, lang):
        catalog = Catalog(catalog, lang)
        docs = catalog.find({'translation': ''})
        docs = [(doc['phrase'], doc) for doc in docs]
        docs.sort()
        return self.json([doc for k, doc in docs])
    
    @reqargs('catalog', 'lang')
    def all_phrases(self, catalog, lang):
        catalog = Catalog(catalog, lang)
        return self.json(list(catalog.phrases))

    def get_catalog_tree(self, args=None):
        catalog_list = CatalogList()
        return self.json(catalog_list.catalogs)
        

if __name__ == '__main__':
    home = HomePage()
    print home.index()