Mercurial > kallithea
comparison rhodecode/controllers/summary.py @ 3749:b950b884ab87 beta
auth decorators are not used anymore on __before__
- this will allow to create a whitelist of API enabled controller functions
- moved around functions with _ to beginning of the files to make it cleaner
author | Marcin Kuzminski <marcin@python-works.com> |
---|---|
date | Mon, 15 Apr 2013 00:46:34 +0200 |
parents | be78bf3b1a1f |
children | f485eb78b519 |
comparison
equal
deleted
inserted
replaced
3748:9d743ca9cede | 3749:b950b884ab87 |
---|---|
63 key=lambda y:y[0][1] + y[1][1])] | 63 key=lambda y:y[0][1] + y[1][1])] |
64 | 64 |
65 | 65 |
66 class SummaryController(BaseRepoController): | 66 class SummaryController(BaseRepoController): |
67 | 67 |
68 @LoginRequired() | |
69 @HasRepoPermissionAnyDecorator('repository.read', 'repository.write', | |
70 'repository.admin') | |
71 def __before__(self): | 68 def __before__(self): |
72 super(SummaryController, self).__before__() | 69 super(SummaryController, self).__before__() |
73 | 70 |
74 def index(self, repo_name): | 71 def _get_download_links(self, repo): |
75 c.dbrepo = dbrepo = c.rhodecode_db_repo | 72 |
76 | 73 download_l = [] |
77 def url_generator(**kw): | 74 |
78 return url('shortlog_home', repo_name=repo_name, size=10, **kw) | 75 branches_group = ([], _("Branches")) |
79 | 76 tags_group = ([], _("Tags")) |
80 c.repo_changesets = RepoPage(c.rhodecode_repo, page=1, | 77 |
81 items_per_page=10, url=url_generator) | 78 for name, chs in c.rhodecode_repo.branches.items(): |
82 page_revisions = [x.raw_id for x in list(c.repo_changesets)] | 79 #chs = chs.split(':')[-1] |
83 c.statuses = c.rhodecode_db_repo.statuses(page_revisions) | 80 branches_group[0].append((chs, name),) |
84 | 81 download_l.append(branches_group) |
85 if self.rhodecode_user.username == 'default': | 82 |
86 # for default(anonymous) user we don't need to pass credentials | 83 for name, chs in c.rhodecode_repo.tags.items(): |
87 username = '' | 84 #chs = chs.split(':')[-1] |
88 password = '' | 85 tags_group[0].append((chs, name),) |
89 else: | 86 download_l.append(tags_group) |
90 username = str(self.rhodecode_user.username) | 87 |
91 password = '@' | 88 return download_l |
92 | 89 |
93 parsed_url = urlparse(url.current(qualified=True)) | |
94 | |
95 default_clone_uri = '{scheme}://{user}{pass}{netloc}{path}' | |
96 | |
97 uri_tmpl = config.get('clone_uri', default_clone_uri) | |
98 uri_tmpl = uri_tmpl.replace('{', '%(').replace('}', ')s') | |
99 decoded_path = safe_unicode(urllib.unquote(parsed_url.path)) | |
100 uri_dict = { | |
101 'user': urllib.quote(username), | |
102 'pass': password, | |
103 'scheme': parsed_url.scheme, | |
104 'netloc': parsed_url.netloc, | |
105 'path': urllib.quote(safe_str(decoded_path)) | |
106 } | |
107 | |
108 uri = (uri_tmpl % uri_dict) | |
109 # generate another clone url by id | |
110 uri_dict.update( | |
111 {'path': decoded_path.replace(repo_name, '_%s' % c.dbrepo.repo_id)} | |
112 ) | |
113 uri_id = uri_tmpl % uri_dict | |
114 | |
115 c.clone_repo_url = uri | |
116 c.clone_repo_url_id = uri_id | |
117 c.repo_tags = OrderedDict() | |
118 for name, hash_ in c.rhodecode_repo.tags.items()[:10]: | |
119 try: | |
120 c.repo_tags[name] = c.rhodecode_repo.get_changeset(hash_) | |
121 except ChangesetError: | |
122 c.repo_tags[name] = EmptyChangeset(hash_) | |
123 | |
124 c.repo_branches = OrderedDict() | |
125 for name, hash_ in c.rhodecode_repo.branches.items()[:10]: | |
126 try: | |
127 c.repo_branches[name] = c.rhodecode_repo.get_changeset(hash_) | |
128 except ChangesetError: | |
129 c.repo_branches[name] = EmptyChangeset(hash_) | |
130 | |
131 td = date.today() + timedelta(days=1) | |
132 td_1m = td - timedelta(days=calendar.mdays[td.month]) | |
133 td_1y = td - timedelta(days=365) | |
134 | |
135 ts_min_m = mktime(td_1m.timetuple()) | |
136 ts_min_y = mktime(td_1y.timetuple()) | |
137 ts_max_y = mktime(td.timetuple()) | |
138 | |
139 if dbrepo.enable_statistics: | |
140 c.show_stats = True | |
141 c.no_data_msg = _('No data loaded yet') | |
142 recurse_limit = 500 # don't recurse more than 500 times when parsing | |
143 run_task(get_commits_stats, c.dbrepo.repo_name, ts_min_y, | |
144 ts_max_y, recurse_limit) | |
145 else: | |
146 c.show_stats = False | |
147 c.no_data_msg = _('Statistics are disabled for this repository') | |
148 c.ts_min = ts_min_m | |
149 c.ts_max = ts_max_y | |
150 | |
151 stats = self.sa.query(Statistics)\ | |
152 .filter(Statistics.repository == dbrepo)\ | |
153 .scalar() | |
154 | |
155 c.stats_percentage = 0 | |
156 | |
157 if stats and stats.languages: | |
158 c.no_data = False is dbrepo.enable_statistics | |
159 lang_stats_d = json.loads(stats.languages) | |
160 c.commit_data = stats.commit_activity | |
161 c.overview_data = stats.commit_activity_combined | |
162 | |
163 lang_stats = ((x, {"count": y, | |
164 "desc": LANGUAGES_EXTENSIONS_MAP.get(x)}) | |
165 for x, y in lang_stats_d.items()) | |
166 | |
167 c.trending_languages = json.dumps( | |
168 sorted(lang_stats, reverse=True, key=lambda k: k[1])[:10] | |
169 ) | |
170 last_rev = stats.stat_on_revision + 1 | |
171 c.repo_last_rev = c.rhodecode_repo.count()\ | |
172 if c.rhodecode_repo.revisions else 0 | |
173 if last_rev == 0 or c.repo_last_rev == 0: | |
174 pass | |
175 else: | |
176 c.stats_percentage = '%.2f' % ((float((last_rev)) / | |
177 c.repo_last_rev) * 100) | |
178 else: | |
179 c.commit_data = json.dumps({}) | |
180 c.overview_data = json.dumps([[ts_min_y, 0], [ts_max_y, 10]]) | |
181 c.trending_languages = json.dumps({}) | |
182 c.no_data = True | |
183 | |
184 c.enable_downloads = dbrepo.enable_downloads | |
185 if c.enable_downloads: | |
186 c.download_options = self._get_download_links(c.rhodecode_repo) | |
187 | |
188 c.readme_data, c.readme_file = \ | |
189 self.__get_readme_data(c.rhodecode_db_repo) | |
190 return render('summary/summary.html') | |
191 | |
192 @NotAnonymous() | |
193 @jsonify | |
194 def repo_size(self, repo_name): | |
195 if request.is_xhr: | |
196 return c.rhodecode_db_repo._repo_size() | |
197 else: | |
198 raise HTTPBadRequest() | |
199 | 90 |
200 def __get_readme_data(self, db_repo): | 91 def __get_readme_data(self, db_repo): |
201 repo_name = db_repo.repo_name | 92 repo_name = db_repo.repo_name |
202 | 93 |
203 @cache_region('long_term') | 94 @cache_region('long_term') |
238 if inv is not None: | 129 if inv is not None: |
239 region_invalidate(_get_readme_from_cache, None, key) | 130 region_invalidate(_get_readme_from_cache, None, key) |
240 CacheInvalidation.set_valid(inv.cache_key) | 131 CacheInvalidation.set_valid(inv.cache_key) |
241 return _get_readme_from_cache(key) | 132 return _get_readme_from_cache(key) |
242 | 133 |
243 def _get_download_links(self, repo): | 134 @LoginRequired() |
244 | 135 @HasRepoPermissionAnyDecorator('repository.read', 'repository.write', |
245 download_l = [] | 136 'repository.admin') |
246 | 137 def index(self, repo_name): |
247 branches_group = ([], _("Branches")) | 138 c.dbrepo = dbrepo = c.rhodecode_db_repo |
248 tags_group = ([], _("Tags")) | 139 |
249 | 140 def url_generator(**kw): |
250 for name, chs in c.rhodecode_repo.branches.items(): | 141 return url('shortlog_home', repo_name=repo_name, size=10, **kw) |
251 #chs = chs.split(':')[-1] | 142 |
252 branches_group[0].append((chs, name),) | 143 c.repo_changesets = RepoPage(c.rhodecode_repo, page=1, |
253 download_l.append(branches_group) | 144 items_per_page=10, url=url_generator) |
254 | 145 page_revisions = [x.raw_id for x in list(c.repo_changesets)] |
255 for name, chs in c.rhodecode_repo.tags.items(): | 146 c.statuses = c.rhodecode_db_repo.statuses(page_revisions) |
256 #chs = chs.split(':')[-1] | 147 |
257 tags_group[0].append((chs, name),) | 148 if self.rhodecode_user.username == 'default': |
258 download_l.append(tags_group) | 149 # for default(anonymous) user we don't need to pass credentials |
259 | 150 username = '' |
260 return download_l | 151 password = '' |
152 else: | |
153 username = str(self.rhodecode_user.username) | |
154 password = '@' | |
155 | |
156 parsed_url = urlparse(url.current(qualified=True)) | |
157 | |
158 default_clone_uri = '{scheme}://{user}{pass}{netloc}{path}' | |
159 | |
160 uri_tmpl = config.get('clone_uri', default_clone_uri) | |
161 uri_tmpl = uri_tmpl.replace('{', '%(').replace('}', ')s') | |
162 decoded_path = safe_unicode(urllib.unquote(parsed_url.path)) | |
163 uri_dict = { | |
164 'user': urllib.quote(username), | |
165 'pass': password, | |
166 'scheme': parsed_url.scheme, | |
167 'netloc': parsed_url.netloc, | |
168 'path': urllib.quote(safe_str(decoded_path)) | |
169 } | |
170 | |
171 uri = (uri_tmpl % uri_dict) | |
172 # generate another clone url by id | |
173 uri_dict.update( | |
174 {'path': decoded_path.replace(repo_name, '_%s' % c.dbrepo.repo_id)} | |
175 ) | |
176 uri_id = uri_tmpl % uri_dict | |
177 | |
178 c.clone_repo_url = uri | |
179 c.clone_repo_url_id = uri_id | |
180 c.repo_tags = OrderedDict() | |
181 for name, hash_ in c.rhodecode_repo.tags.items()[:10]: | |
182 try: | |
183 c.repo_tags[name] = c.rhodecode_repo.get_changeset(hash_) | |
184 except ChangesetError: | |
185 c.repo_tags[name] = EmptyChangeset(hash_) | |
186 | |
187 c.repo_branches = OrderedDict() | |
188 for name, hash_ in c.rhodecode_repo.branches.items()[:10]: | |
189 try: | |
190 c.repo_branches[name] = c.rhodecode_repo.get_changeset(hash_) | |
191 except ChangesetError: | |
192 c.repo_branches[name] = EmptyChangeset(hash_) | |
193 | |
194 td = date.today() + timedelta(days=1) | |
195 td_1m = td - timedelta(days=calendar.mdays[td.month]) | |
196 td_1y = td - timedelta(days=365) | |
197 | |
198 ts_min_m = mktime(td_1m.timetuple()) | |
199 ts_min_y = mktime(td_1y.timetuple()) | |
200 ts_max_y = mktime(td.timetuple()) | |
201 | |
202 if dbrepo.enable_statistics: | |
203 c.show_stats = True | |
204 c.no_data_msg = _('No data loaded yet') | |
205 recurse_limit = 500 # don't recurse more than 500 times when parsing | |
206 run_task(get_commits_stats, c.dbrepo.repo_name, ts_min_y, | |
207 ts_max_y, recurse_limit) | |
208 else: | |
209 c.show_stats = False | |
210 c.no_data_msg = _('Statistics are disabled for this repository') | |
211 c.ts_min = ts_min_m | |
212 c.ts_max = ts_max_y | |
213 | |
214 stats = self.sa.query(Statistics)\ | |
215 .filter(Statistics.repository == dbrepo)\ | |
216 .scalar() | |
217 | |
218 c.stats_percentage = 0 | |
219 | |
220 if stats and stats.languages: | |
221 c.no_data = False is dbrepo.enable_statistics | |
222 lang_stats_d = json.loads(stats.languages) | |
223 c.commit_data = stats.commit_activity | |
224 c.overview_data = stats.commit_activity_combined | |
225 | |
226 lang_stats = ((x, {"count": y, | |
227 "desc": LANGUAGES_EXTENSIONS_MAP.get(x)}) | |
228 for x, y in lang_stats_d.items()) | |
229 | |
230 c.trending_languages = json.dumps( | |
231 sorted(lang_stats, reverse=True, key=lambda k: k[1])[:10] | |
232 ) | |
233 last_rev = stats.stat_on_revision + 1 | |
234 c.repo_last_rev = c.rhodecode_repo.count()\ | |
235 if c.rhodecode_repo.revisions else 0 | |
236 if last_rev == 0 or c.repo_last_rev == 0: | |
237 pass | |
238 else: | |
239 c.stats_percentage = '%.2f' % ((float((last_rev)) / | |
240 c.repo_last_rev) * 100) | |
241 else: | |
242 c.commit_data = json.dumps({}) | |
243 c.overview_data = json.dumps([[ts_min_y, 0], [ts_max_y, 10]]) | |
244 c.trending_languages = json.dumps({}) | |
245 c.no_data = True | |
246 | |
247 c.enable_downloads = dbrepo.enable_downloads | |
248 if c.enable_downloads: | |
249 c.download_options = self._get_download_links(c.rhodecode_repo) | |
250 | |
251 c.readme_data, c.readme_file = \ | |
252 self.__get_readme_data(c.rhodecode_db_repo) | |
253 return render('summary/summary.html') | |
254 | |
255 @LoginRequired() | |
256 @NotAnonymous() | |
257 @HasRepoPermissionAnyDecorator('repository.read', 'repository.write', | |
258 'repository.admin') | |
259 @jsonify | |
260 def repo_size(self, repo_name): | |
261 if request.is_xhr: | |
262 return c.rhodecode_db_repo._repo_size() | |
263 else: | |
264 raise HTTPBadRequest() |