comparison rhodecode/controllers/files.py @ 1136:93b980ebee55

changes for release 1.1.5
author Marcin Kuzminski <marcin@python-works.com>
date Thu, 17 Mar 2011 01:13:48 +0100
parents 136af52f374b
children a3b2b4b4e440
comparison
equal deleted inserted replaced
1095:3cdacd152b24 1136:93b980ebee55
5 5
6 Files controller for RhodeCode 6 Files controller for RhodeCode
7 7
8 :created_on: Apr 21, 2010 8 :created_on: Apr 21, 2010
9 :author: marcink 9 :author: marcink
10 :copyright: (C) 2009-2010 Marcin Kuzminski <marcin@python-works.com> 10 :copyright: (C) 2009-2011 Marcin Kuzminski <marcin@python-works.com>
11 :license: GPLv3, see COPYING for more details. 11 :license: GPLv3, see COPYING for more details.
12 """ 12 """
13 # This program is free software; you can redistribute it and/or 13 # This program is free software; you can redistribute it and/or
14 # modify it under the terms of the GNU General Public License 14 # modify it under the terms of the GNU General Public License
15 # as published by the Free Software Foundation; version 2 15 # as published by the Free Software Foundation; version 2
53 'repository.admin') 53 'repository.admin')
54 def __before__(self): 54 def __before__(self):
55 super(FilesController, self).__before__() 55 super(FilesController, self).__before__()
56 c.cut_off_limit = self.cut_off_limit 56 c.cut_off_limit = self.cut_off_limit
57 57
58 def __get_cs_or_redirect(self, rev, repo_name):
59 """
60 Safe way to get changeset if error occur it redirects to tip with
61 proper message
62
63 :param rev: revision to fetch
64 :param repo_name: repo name to redirect after
65 """
66
67 _repo = ScmModel().get_repo(c.repo_name)
68 try:
69 return _repo.get_changeset(rev)
70 except EmptyRepositoryError, e:
71 h.flash(_('There are no files yet'), category='warning')
72 redirect(h.url('summary_home', repo_name=repo_name))
73
74 except RepositoryError, e:
75 h.flash(str(e), category='warning')
76 redirect(h.url('files_home', repo_name=repo_name, revision='tip'))
77
58 def index(self, repo_name, revision, f_path): 78 def index(self, repo_name, revision, f_path):
59 hg_model = ScmModel() 79 cs = self.__get_cs_or_redirect(revision, repo_name)
60 c.repo = hg_model.get_repo(c.repo_name) 80 c.repo = ScmModel().get_repo(c.repo_name)
81
61 revision = request.POST.get('at_rev', None) or revision 82 revision = request.POST.get('at_rev', None) or revision
62 83
63 def get_next_rev(cur): 84 def get_next_rev(cur):
64 max_rev = len(c.repo.revisions) - 1 85 max_rev = len(c.repo.revisions) - 1
65 r = cur + 1 86 r = cur + 1
70 def get_prev_rev(cur): 91 def get_prev_rev(cur):
71 r = cur - 1 92 r = cur - 1
72 return r 93 return r
73 94
74 c.f_path = f_path 95 c.f_path = f_path
75 96 c.changeset = cs
76 97 cur_rev = c.changeset.revision
77 try: 98 prev_rev = c.repo.get_changeset(get_prev_rev(cur_rev)).raw_id
78 c.changeset = c.repo.get_changeset(revision) 99 next_rev = c.repo.get_changeset(get_next_rev(cur_rev)).raw_id
79 cur_rev = c.changeset.revision 100
80 prev_rev = c.repo.get_changeset(get_prev_rev(cur_rev)).raw_id 101 c.url_prev = url('files_home', repo_name=c.repo_name,
81 next_rev = c.repo.get_changeset(get_next_rev(cur_rev)).raw_id 102 revision=prev_rev, f_path=f_path)
82 103 c.url_next = url('files_home', repo_name=c.repo_name,
83 c.url_prev = url('files_home', repo_name=c.repo_name, 104 revision=next_rev, f_path=f_path)
84 revision=prev_rev, f_path=f_path) 105
85 c.url_next = url('files_home', repo_name=c.repo_name, 106 try:
86 revision=next_rev, f_path=f_path) 107 c.files_list = c.changeset.get_node(f_path)
87 108 c.file_history = self._get_history(c.repo, c.files_list, f_path)
88 try: 109 except RepositoryError, e:
89 c.files_list = c.changeset.get_node(f_path) 110 h.flash(str(e), category='warning')
90 c.file_history = self._get_history(c.repo, c.files_list, f_path) 111 redirect(h.url('files_home', repo_name=repo_name,
91 except RepositoryError, e: 112 revision=revision))
92 h.flash(str(e), category='warning')
93 redirect(h.url('files_home', repo_name=repo_name, revision=revision))
94
95 except EmptyRepositoryError, e:
96 h.flash(_('There are no files yet'), category='warning')
97 redirect(h.url('summary_home', repo_name=repo_name))
98
99 except RepositoryError, e:
100 h.flash(str(e), category='warning')
101 redirect(h.url('files_home', repo_name=repo_name, revision='tip'))
102
103 113
104 114
105 return render('files/files.html') 115 return render('files/files.html')
106 116
107 def rawfile(self, repo_name, revision, f_path): 117 def rawfile(self, repo_name, revision, f_path):
108 hg_model = ScmModel() 118 cs = self.__get_cs_or_redirect(revision, repo_name)
109 c.repo = hg_model.get_repo(c.repo_name) 119 try:
110 file_node = c.repo.get_changeset(revision).get_node(f_path) 120 file_node = cs.get_node(f_path)
121 except RepositoryError, e:
122 h.flash(str(e), category='warning')
123 redirect(h.url('files_home', repo_name=repo_name,
124 revision=cs.raw_id))
125
126 fname = f_path.split('/')[-1].encode('utf8', 'replace')
127
128 response.content_disposition = 'attachment; filename=%s' % fname
111 response.content_type = file_node.mimetype 129 response.content_type = file_node.mimetype
112 response.content_disposition = 'attachment; filename=%s' \
113 % f_path.split('/')[-1]
114 return file_node.content 130 return file_node.content
115 131
116 def raw(self, repo_name, revision, f_path): 132 def raw(self, repo_name, revision, f_path):
117 hg_model = ScmModel() 133 cs = self.__get_cs_or_redirect(revision, repo_name)
118 c.repo = hg_model.get_repo(c.repo_name) 134 try:
119 file_node = c.repo.get_changeset(revision).get_node(f_path) 135 file_node = cs.get_node(f_path)
136 except RepositoryError, e:
137 h.flash(str(e), category='warning')
138 redirect(h.url('files_home', repo_name=repo_name,
139 revision=cs.raw_id))
140
120 response.content_type = 'text/plain' 141 response.content_type = 'text/plain'
121
122 return file_node.content 142 return file_node.content
123 143
124 def annotate(self, repo_name, revision, f_path): 144 def annotate(self, repo_name, revision, f_path):
125 hg_model = ScmModel() 145 cs = self.__get_cs_or_redirect(revision, repo_name)
126 c.repo = hg_model.get_repo(c.repo_name) 146 try:
127 147 c.file = cs.get_node(f_path)
128 try: 148 except RepositoryError, e:
129 c.cs = c.repo.get_changeset(revision) 149 h.flash(str(e), category='warning')
130 c.file = c.cs.get_node(f_path) 150 redirect(h.url('files_home', repo_name=repo_name, revision=cs.raw_id))
131 except RepositoryError, e: 151
132 h.flash(str(e), category='warning') 152 c.file_history = self._get_history(ScmModel().get_repo(c.repo_name), c.file, f_path)
133 redirect(h.url('files_home', repo_name=repo_name, revision=revision)) 153 c.cs = cs
134
135 c.file_history = self._get_history(c.repo, c.file, f_path)
136
137 c.f_path = f_path 154 c.f_path = f_path
138 155
139 return render('files/files_annotate.html') 156 return render('files/files_annotate.html')
140 157
141 def archivefile(self, repo_name, revision, fileformat): 158 def archivefile(self, repo_name, revision, fileformat):
199 if c.action == 'download': 216 if c.action == 'download':
200 diff_name = '%s_vs_%s.diff' % (diff1, diff2) 217 diff_name = '%s_vs_%s.diff' % (diff1, diff2)
201 response.content_type = 'text/plain' 218 response.content_type = 'text/plain'
202 response.content_disposition = 'attachment; filename=%s' \ 219 response.content_disposition = 'attachment; filename=%s' \
203 % diff_name 220 % diff_name
221 if node1.is_binary or node2.is_binary:
222 return _('binary file changed')
204 return diff.raw_diff() 223 return diff.raw_diff()
205 224
206 elif c.action == 'raw': 225 elif c.action == 'raw':
207 response.content_type = 'text/plain' 226 response.content_type = 'text/plain'
227 if node1.is_binary or node2.is_binary:
228 return _('binary file changed')
208 return diff.raw_diff() 229 return diff.raw_diff()
209 230
210 elif c.action == 'diff': 231 elif c.action == 'diff':
211 if node1.size > self.cut_off_limit or node2.size > self.cut_off_limit: 232 if node1.size > self.cut_off_limit or node2.size > self.cut_off_limit:
212 c.cur_diff = _('Diff is to big to display') 233 c.cur_diff = _('Diff is to big to display')
234 elif node1.is_binary or node2.is_binary:
235 c.cur_diff = _('Binary file')
213 else: 236 else:
214 c.cur_diff = diff.as_html() 237 c.cur_diff = diff.as_html()
215 else: 238 else:
216 #default option 239 #default option
217 if node1.size > self.cut_off_limit or node2.size > self.cut_off_limit: 240 if node1.size > self.cut_off_limit or node2.size > self.cut_off_limit:
218 c.cur_diff = _('Diff is to big to display') 241 c.cur_diff = _('Diff is to big to display')
242 elif node1.is_binary or node2.is_binary:
243 c.cur_diff = _('Binary file')
219 else: 244 else:
220 c.cur_diff = diff.as_html() 245 c.cur_diff = diff.as_html()
221 246
222 if not c.cur_diff: c.no_changes = True 247 if not c.cur_diff:
248 c.no_changes = True
223 return render('files/file_diff.html') 249 return render('files/file_diff.html')
224 250
225 def _get_history(self, repo, node, f_path): 251 def _get_history(self, repo, node, f_path):
226 from vcs.nodes import NodeKind 252 from vcs.nodes import NodeKind
227 if not node.kind is NodeKind.FILE: 253 if not node.kind is NodeKind.FILE:
248 #chs = chs.split(':')[-1] 274 #chs = chs.split(':')[-1]
249 tags_group[0].append((chs, name),) 275 tags_group[0].append((chs, name),)
250 hist_l.append(tags_group) 276 hist_l.append(tags_group)
251 277
252 return hist_l 278 return hist_l
253
254 # [
255 # ([("u1", "User1"), ("u2", "User2")], "Users"),
256 # ([("g1", "Group1"), ("g2", "Group2")], "Groups")
257 # ]
258