~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to loggerhead/controllers/__init__.py

  • Committer: Michael Hudson-Doyle
  • Date: 2012-01-19 21:37:14 UTC
  • mfrom: (461.1.2 bug-383631)
  • Revision ID: michael.hudson@linaro.org-20120119213714-3r0wrm96mewvt8pr
fix bug #383631: Breadcrumbs while annotating a file in head shows the most recent revision in the url instead of head

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
# along with this program; if not, write to the Free Software
18
18
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
19
 
 
20
import bzrlib.errors
 
21
import simplejson
20
22
import time
21
23
 
 
24
from paste.httpexceptions import HTTPNotFound, HTTPSeeOther
22
25
from paste.request import path_info_pop, parse_querystring
23
26
 
24
27
from loggerhead import util
51
54
class TemplatedBranchView(object):
52
55
 
53
56
    template_path = None
 
57
    supports_json = False
54
58
 
55
59
    def __init__(self, branch, history_callable):
56
60
        self._branch = branch
65
69
        self.__history = self._history_callable()
66
70
        return self.__history
67
71
 
68
 
    def __call__(self, environ, start_response):
69
 
        z = time.time()
 
72
    def parse_args(self, environ):
70
73
        kwargs = dict(parse_querystring(environ))
71
74
        util.set_context(kwargs)
72
75
        args = []
73
 
        while 1:
 
76
        while True:
74
77
            arg = path_info_pop(environ)
75
78
            if arg is None:
76
79
                break
80
83
        if len(args) > 1:
81
84
            path = unicode('/'.join(args[1:]), 'utf-8')
82
85
        self.args = args
 
86
        self.kwargs = kwargs
 
87
        return path
83
88
 
84
 
        vals = {
 
89
    def add_template_values(self, values):
 
90
        values.update({
85
91
            'static_url': self._branch.static_url,
86
92
            'branch': self._branch,
87
93
            'util': util,
88
94
            'url': self._branch.context_url,
89
 
        }
90
 
        vals.update(templatefunctions)
 
95
        })
 
96
        values.update(templatefunctions)
 
97
 
 
98
    def __call__(self, environ, start_response):
 
99
        z = time.time()
 
100
        if environ.get('loggerhead.as_json') and not self.supports_json:
 
101
            raise HTTPNotFound
 
102
        path = self.parse_args(environ)
91
103
        headers = {}
92
 
 
93
 
        vals.update(self.get_values(path, kwargs, headers))
94
 
 
95
 
        self.log.info('Getting information for %s: %r secs' % (
 
104
        values = self.get_values(path, self.kwargs, headers)
 
105
 
 
106
        self.log.info('Getting information for %s: %.3f secs' % (
96
107
            self.__class__.__name__, time.time() - z))
97
 
        if 'Content-Type' not in headers:
 
108
        if environ.get('loggerhead.as_json'):
 
109
            headers['Content-Type'] = 'application/json'
 
110
        elif 'Content-Type' not in headers:
98
111
            headers['Content-Type'] = 'text/html'
99
112
        writer = start_response("200 OK", headers.items())
100
 
        template = load_template(self.template_path)
 
113
        if environ.get('REQUEST_METHOD') == 'HEAD':
 
114
            # No content for a HEAD request
 
115
            return []
101
116
        z = time.time()
102
117
        w = BufferingWriter(writer, 8192)
103
 
        template.expand_into(w, **vals)
 
118
        if environ.get('loggerhead.as_json'):
 
119
            w.write(simplejson.dumps(values,
 
120
                default=util.convert_to_json_ready))
 
121
        else:
 
122
            self.add_template_values(values)
 
123
            template = load_template(self.template_path)
 
124
            template.expand_into(w, **values)
104
125
        w.flush()
105
126
        self.log.info(
106
 
            'Rendering %s: %r secs, %s bytes' % (
 
127
            'Rendering %s: %.3f secs, %s bytes' % (
107
128
                self.__class__.__name__, time.time() - z, w.bytes))
108
129
        return []
109
130
 
112
133
        if h is None:
113
134
            return None
114
135
        if len(self.args) > 0 and self.args != ['']:
115
 
            return h.fix_revid(self.args[0])
 
136
            try:
 
137
                revid = h.fix_revid(self.args[0])
 
138
            except bzrlib.errors.NoSuchRevision:
 
139
                raise HTTPNotFound;
116
140
        else:
117
 
            return h.last_revid
 
141
            revid = h.last_revid
 
142
        return revid