# -*- coding: utf-8 -*-
# Copyright (c) 2009 gocept gmbh & co. kg
# See also LICENSE.txt

import cjson
import zeit.cms.browser.view
import zc.resourcelibrary
import zope.component
import zope.interface


def resources(request):
    return zope.component.getAdapter(
        request, zope.interface.Interface, name='zeit.find')


class Find(zeit.cms.browser.view.Base):

    def __call__(self):
        zc.resourcelibrary.need('zeit.find')
        return super(Find, self).__call__()


class JSONView(zeit.cms.browser.view.Base):

    def __init__(self, context, request):
        super(JSONView, self).__init__(context, request)
        self.resources = resources(request)

    def __call__(self):
        self.request.response.setHeader('Content-Type', 'text/json')
        return cjson.encode(self.json())

    def json(self):
        raise NotImplementedError


class SearchForm(JSONView):

    def json(self):
        return {"template_url": self.resources['search_form.jsont']()}


class SearchResult(JSONView):

    def json(self):
        r = self.resources
        return {
            "template_url": r['search_result.jsont'](),
            "results":
                [{'uniqueId': 'foo',
                  'icon': 'http://localhost:8080/@@/zeit-content-article-interfaces-IArticle-zmi_icon.png',
                  'favorited': r['favorite.png'](),
                  'publication_status': r['published.png'](),
                  'arrow': r['arrow_right.png'](),
                  'teaser_title': 'Obama still alive',
                  'teaser_text': 'Obama is still alive and well.',
                  'preview_url': '',
                  'date': '13.02.2009',
                  'date_filter': '',
                  'week': '13/2009',
                  'week_filter': '',
                  'topics': 'Politik',
                  'topics_filter': '',
                  'author': 'Martijn Faassen',
                  'author_filter': ''},
                 {'uniqueId': 'bar',
                  'icon': 'http://localhost:8080/@@/zeit-content-article-interfaces-IArticle-zmi_icon.png',
                  'favorited': r['not_favorite.png'](),
                  'publication_status': r['unpublished.png'](),
                  'arrow': r['arrow_right.png'](),
                  'teaser_title': 'Obama journalism too extreme',
                  'teaser_text': 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et',
                  'preview_url': '',
                  'date': '19.02.2009',
                  'date_filter': '',
                  'week': '14/2009',
                  'week_filter': '',
                  'topics': 'Newspapers',
                  'topics_filter': '',
                  'author': 'Christian Zagrodnick',
                  'author_filter': ''}]
                 }

class ExtendedSearchForm(JSONView):

    def json(self):
        return {"template_url": self.resources['extended_search_form.jsont']()}

@@ -514,32 +514,56 @@
 ser.view.Base):%0A
+    template = None%0A    
 %0A    def __init_
@@ -798,26 +798,15 @@
   re
-turn cjson.encode(
+sult = 
 self
@@ -816,94 +816,148 @@
 on()
-)%0A
 %0A    
-def json(self):%0A        raise NotImplementedError%0A%0A%0Aclass SearchForm(JSONView):
+    url = self.template_url()%0A        if url is not None:%0A            result%5B'template_url'%5D = url %0A        return cjson.encode(result)
 %0A%0A  
@@ -982,33 +982,43 @@
         return %7B
-%22
+%7D%0A%0A    def 
 template_url%22: s
@@ -1013,34 +1013,173 @@
 late_url
-%22: self.resources%5B
+(self):%0A        if self.template is None:%0A            return None%0A        return self.resources%5Bself.template%5D()%0A%0Aclass SearchForm(JSONView):%0A    template = 
 'search_
@@ -1189,21 +1189,16 @@
 m.jsont'
-%5D()%7D%0A
 %0A%0Aclass 
@@ -1213,32 +1213,73 @@
 sult(JSONView):%0A
+    template = 'search_result.jsont'%0A    
 %0A    def json(se
@@ -1331,64 +1331,8 @@
 n %7B%0A
-            %22template_url%22: r%5B'search_result.jsont'%5D(),%0A
     
@@ -3156,76 +3156,23 @@
 w):%0A
-%0A
     
-def json(self):%0A        return %7B%22template_url%22: self.resources%5B
+template = 
 'ext
@@ -3199,9 +3199,6 @@
 ont'
-%5D()%7D
+%0A
 %0A

Make it easier to define the json template.