/loggerhead/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/loggerhead/trunk

« back to all changes in this revision

Viewing changes to loggerhead/controllers/inventory_ui.py

  • Committer: Jelmer Vernooij
  • Date: 2018-09-08 15:16:32 UTC
  • mto: (491.6.1 breezy)
  • mto: This revision was merged to the branch mainline in revision 494.
  • Revision ID: jelmer@jelmer.uk-20180908151632-hsc80tbdusz1nz31
s/bazaar.conf/breezy.conf.

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
#
16
16
# You should have received a copy of the GNU General Public License
17
17
# along with this program; if not, write to the Free Software
18
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
18
# Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335  USA
19
19
#
20
20
 
21
21
import logging
22
22
import posixpath
23
23
import urllib
24
24
 
25
 
from paste.httpexceptions import HTTPNotFound
 
25
from paste.httpexceptions import HTTPNotFound, HTTPMovedPermanently
26
26
 
27
 
from bzrlib import errors
28
 
from bzrlib.revision import is_null as is_null_rev
 
27
from breezy import errors
 
28
from breezy.revision import is_null as is_null_rev
29
29
 
30
30
from loggerhead import util
31
31
from loggerhead.controllers import TemplatedBranchView
32
32
 
33
33
 
34
 
log = logging.getLogger("loggerhead.controllers")
35
 
 
36
34
 
37
35
def dirname(path):
38
36
    if path is not None:
39
37
        path = path.rstrip('/')
40
 
        path = urllib.quote(posixpath.dirname(path))
 
38
        path = urllib.quote(posixpath.dirname(path).encode('utf-8'))
41
39
    return path
42
40
 
43
41
 
44
42
class InventoryUI(TemplatedBranchView):
45
43
 
46
44
    template_path = 'loggerhead.templates.inventory'
 
45
    supports_json = True
47
46
 
48
 
    def get_filelist(self, inv, path, sort_type):
 
47
    def get_filelist(self, tree, path, sort_type, revno_url):
49
48
        """
50
49
        return the list of all files (and their attributes) within a given
51
50
        path subtree.
52
51
 
53
 
        @param inv: The inventory.
54
 
        @param path: The path of a directory within the inventory.
 
52
        @param tree: The tree
 
53
        @param path: The path of a directory within the tree.
55
54
        @param sort_type: How to sort the results... XXX.
56
55
        """
57
 
        file_id = inv.path2id(path)
58
 
        dir_ie = inv[file_id]
59
56
        file_list = []
60
57
 
 
58
        if tree.kind(path) != 'directory':
 
59
            raise HTTPMovedPermanently(self._branch.context_url(['/view', revno_url, path]))
 
60
 
61
61
        revid_set = set()
62
62
 
63
 
        for filename, entry in dir_ie.children.iteritems():
 
63
        child_entries = list(tree.iter_child_entries(path))
 
64
 
 
65
        for entry in child_entries:
64
66
            revid_set.add(entry.revision)
65
67
 
66
68
        change_dict = {}
67
69
        for change in self._history.get_changes(list(revid_set)):
68
70
            change_dict[change.revid] = change
69
71
 
70
 
        for filename, entry in dir_ie.children.iteritems():
71
 
            pathname = filename
 
72
        for entry in child_entries:
 
73
            pathname = entry.name
72
74
            if entry.kind == 'directory':
73
75
                pathname += '/'
74
76
            if path == '':
77
79
                absolutepath = path + '/' + pathname
78
80
            revid = entry.revision
79
81
 
 
82
            # TODO: For the JSON rendering, this inlines the "change" aka
 
83
            # revision information attached to each file. Consider either
 
84
            # pulling this out as a separate changes dict, or possibly just
 
85
            # including the revision id and having a separate request to get
 
86
            # back the revision info.
80
87
            file = util.Container(
81
 
                filename=filename, executable=entry.executable,
 
88
                filename=entry.name, executable=entry.executable,
82
89
                kind=entry.kind, absolutepath=absolutepath,
83
90
                file_id=entry.file_id, size=entry.text_size, revid=revid,
84
91
                change=change_dict[revid])
109
116
        start_revid = kwargs.get('start_revid', None)
110
117
        sort_type = kwargs.get('sort', 'filename')
111
118
 
112
 
        # no navbar for revisions
113
 
        navigation = util.Container()
114
 
 
115
119
        if path is not None:
116
120
            path = path.rstrip('/')
117
121
            file_id = rev_tree.path2id(path)
132
136
        else:
133
137
            updir = dirname(path)
134
138
 
135
 
        # Directory Breadcrumbs
136
 
        directory_breadcrumbs = util.directory_breadcrumbs(
137
 
                self._branch.friendly_name,
138
 
                self._branch.is_root,
139
 
                'files')
140
 
 
141
139
        if not is_null_rev(revid):
142
 
 
143
140
            change = history.get_changes([ revid ])[0]
144
141
            # If we're looking at the tip, use head: in the URL instead
145
142
            if revid == branch.last_revision():
147
144
            else:
148
145
                revno_url = history.get_revno(revid)
149
146
            history.add_branch_nicks(change)
 
147
            filelist = self.get_filelist(rev_tree, path, sort_type, revno_url)
150
148
 
151
 
            # Create breadcrumb trail for the path within the branch
152
 
            branch_breadcrumbs = util.branch_breadcrumbs(path, rev_tree, 'files')
153
 
            filelist = self.get_filelist(rev_tree.inventory, path, sort_type)
154
149
        else:
155
150
            start_revid = None
156
151
            change = None
157
152
            path = "/"
158
153
            updir = None
159
154
            revno_url = 'head:'
160
 
            branch_breadcrumbs = []
161
155
            filelist = []
162
156
 
163
157
        return {
164
 
            'branch': self._branch,
165
 
            'util': util,
166
158
            'revid': revid,
167
159
            'revno_url': revno_url,
168
160
            'change': change,
169
161
            'path': path,
170
162
            'updir': updir,
171
163
            'filelist': filelist,
172
 
            'navigation': navigation,
173
 
            'url': self._branch.context_url,
174
164
            'start_revid': start_revid,
 
165
        }
 
166
 
 
167
    def add_template_values(self, values):
 
168
        super(InventoryUI, self).add_template_values(values)
 
169
        # Directory Breadcrumbs
 
170
        directory_breadcrumbs = util.directory_breadcrumbs(
 
171
                self._branch.friendly_name,
 
172
                self._branch.is_root,
 
173
                'files')
 
174
 
 
175
        path = values['path']
 
176
        revid = values['revid']
 
177
        # no navbar for revisions
 
178
        navigation = util.Container()
 
179
 
 
180
        if is_null_rev(revid):
 
181
            branch_breadcrumbs = []
 
182
        else:
 
183
            # Create breadcrumb trail for the path within the branch
 
184
            branch = self._history._branch
 
185
            rev_tree = branch.repository.revision_tree(revid)
 
186
            branch_breadcrumbs = util.branch_breadcrumbs(path, rev_tree, 'files')
 
187
        values.update({
175
188
            'fileview_active': True,
176
189
            'directory_breadcrumbs': directory_breadcrumbs,
177
190
            'branch_breadcrumbs': branch_breadcrumbs,
178
 
        }
 
191
            'navigation': navigation,
 
192
        })