~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to loggerhead/controllers/__init__.py

  • Committer: John Arbash Meinel
  • Date: 2011-03-15 10:24:40 UTC
  • mfrom: (432.1.3 loggerhead)
  • Revision ID: john@arbash-meinel.com-20110315102440-n4zr988738z125cd
Merge fix for bug #569358. Redirect requests so people can URL hack.

If you edit /files/directory to become /files/directory/filename, redirect it to
the /view/directory/filename, and conversly if you go to /view/directory redirect
it to /files/directory. This way people can edit the URL in a logical way
and still get the view they are expecting.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#
 
2
# Copyright (C) 2008  Canonical Ltd.
2
3
# Copyright (C) 2006  Robey Pointer <robey@lag.net>
3
4
# Copyright (C) 2006  Goffredo Baroncelli <kreijack@inwind.it>
4
5
#
16
17
# along with this program; if not, write to the Free Software
17
18
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
19
 
 
20
import bzrlib.errors
19
21
import time
20
22
 
21
 
from paste.request import path_info_pop
 
23
from paste.httpexceptions import HTTPNotFound
 
24
from paste.request import path_info_pop, parse_querystring
22
25
 
23
26
from loggerhead import util
24
27
from loggerhead.templatefunctions import templatefunctions
25
28
from loggerhead.zptsupport import load_template
26
29
 
 
30
 
 
31
class BufferingWriter(object):
 
32
 
 
33
    def __init__(self, writefunc, buf_limit):
 
34
        self.bytes = 0
 
35
        self.buf = []
 
36
        self.buflen = 0
 
37
        self.writefunc = writefunc
 
38
        self.buf_limit = buf_limit
 
39
 
 
40
    def flush(self):
 
41
        self.writefunc(''.join(self.buf))
 
42
        self.buf = []
 
43
        self.buflen = 0
 
44
 
 
45
    def write(self, data):
 
46
        self.buf.append(data)
 
47
        self.buflen += len(data)
 
48
        self.bytes += len(data)
 
49
        if self.buflen > self.buf_limit:
 
50
            self.flush()
 
51
 
 
52
 
27
53
class TemplatedBranchView(object):
28
54
 
29
55
    template_path = None
30
56
 
31
 
    def __init__(self, branch):
 
57
    def __init__(self, branch, history_callable):
32
58
        self._branch = branch
 
59
        self._history_callable = history_callable
 
60
        self.__history = None
33
61
        self.log = branch.log
34
62
 
35
 
    def default(self, request, response):
36
 
        z = time.time()
37
 
        h = self._branch.history
38
 
        kw = request.GET
39
 
        util.set_context(kw)
40
 
 
41
 
        h._branch.lock_read()
42
 
        try:
43
 
            args = []
44
 
            while 1:
45
 
                arg = path_info_pop(request.environ)
46
 
                if arg is None:
47
 
                    break
48
 
                args.append(arg)
49
 
 
50
 
            vals = {
51
 
                'branch': self._branch,
52
 
                'util': util,
53
 
                'history': h,
54
 
                'url': self._branch.context_url,
55
 
            }
56
 
            vals.update(templatefunctions)
57
 
            del response.headers['Content-Type']
58
 
            vals.update(self.get_values(h, args, kw, response))
59
 
 
60
 
            self.log.info('/%s: %r secs' % (
61
 
                self.__class__.__name__, time.time() - z,))
62
 
            if 'Content-Type' not in response.headers:
63
 
                response.headers['Content-Type'] = 'text/html'
64
 
            template = load_template(self.template_path)
65
 
            template.expand_into(response, **vals)
66
 
        finally:
67
 
            h._branch.unlock()
68
 
 
 
63
    @property
 
64
    def _history(self):
 
65
        if self.__history is not None:
 
66
            return self.__history
 
67
        self.__history = self._history_callable()
 
68
        return self.__history
 
69
 
 
70
    def __call__(self, environ, start_response):
 
71
        z = time.time()
 
72
        kwargs = dict(parse_querystring(environ))
 
73
        util.set_context(kwargs)
 
74
        args = []
 
75
        while True:
 
76
            arg = path_info_pop(environ)
 
77
            if arg is None:
 
78
                break
 
79
            args.append(arg)
 
80
 
 
81
        path = None
 
82
        if len(args) > 1:
 
83
            path = unicode('/'.join(args[1:]), 'utf-8')
 
84
        self.args = args
 
85
 
 
86
        vals = {
 
87
            'static_url': self._branch.static_url,
 
88
            'branch': self._branch,
 
89
            'util': util,
 
90
            'url': self._branch.context_url,
 
91
        }
 
92
        vals.update(templatefunctions)
 
93
        headers = {}
 
94
 
 
95
        vals.update(self.get_values(path, kwargs, headers))
 
96
 
 
97
        self.log.info('Getting information for %s: %r secs' % (
 
98
            self.__class__.__name__, time.time() - z))
 
99
        if 'Content-Type' not in headers:
 
100
            headers['Content-Type'] = 'text/html'
 
101
        writer = start_response("200 OK", headers.items())
 
102
        if environ.get('REQUEST_METHOD') == 'HEAD':
 
103
            # No content for a HEAD request
 
104
            return []
 
105
        template = load_template(self.template_path)
 
106
        z = time.time()
 
107
        w = BufferingWriter(writer, 8192)
 
108
        template.expand_into(w, **vals)
 
109
        w.flush()
 
110
        self.log.info(
 
111
            'Rendering %s: %r secs, %s bytes' % (
 
112
                self.__class__.__name__, time.time() - z, w.bytes))
 
113
        return []
 
114
 
 
115
    def get_revid(self):
 
116
        h = self._history
 
117
        if h is None:
 
118
            return None
 
119
        if len(self.args) > 0 and self.args != ['']:
 
120
            try:
 
121
                revid = h.fix_revid(self.args[0])
 
122
            except bzrlib.errors.NoSuchRevision:
 
123
                raise HTTPNotFound;
 
124
        else:
 
125
            revid = h.last_revid
 
126
        return revid