Mercurial > kallithea
annotate rhodecode/controllers/admin/gists.py @ 3953:fc63a1d93803 beta
Switched order of actions when doing repo rescan.
doing invalidation after repository scan is not optimal.
In case of big repository we first create an instance then invalidate
cache that we don't actually need. Reversing this order prevents that
author | Marcin Kuzminski <marcin@python-works.com> |
---|---|
date | Wed, 05 Jun 2013 21:20:21 +0200 |
parents | 73f7149f2cc0 |
children | 647308db13ff |
rev | line source |
---|---|
3840
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
1 # -*- coding: utf-8 -*- |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
2 """ |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
3 rhodecode.controllers.admin.gist |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
4 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
5 |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
6 gist controller for RhodeCode |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
7 |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
8 :created_on: May 9, 2013 |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
9 :author: marcink |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
10 :copyright: (C) 2010-2013 Marcin Kuzminski <marcin@python-works.com> |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
11 :license: GPLv3, see COPYING for more details. |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
12 """ |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
13 # This program is free software: you can redistribute it and/or modify |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
14 # it under the terms of the GNU General Public License as published by |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
15 # the Free Software Foundation, either version 3 of the License, or |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
16 # (at your option) any later version. |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
17 # |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
18 # This program is distributed in the hope that it will be useful, |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
21 # GNU General Public License for more details. |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
22 # |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
23 # You should have received a copy of the GNU General Public License |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
24 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
25 import time |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
26 import logging |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
27 import traceback |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
28 import formencode |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
29 from formencode import htmlfill |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
30 |
3867
73f7149f2cc0
Added show as raw into gist
Marcin Kuzminski <marcin@python-works.com>
parents:
3866
diff
changeset
|
31 from pylons import request, response, tmpl_context as c, url |
3840
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
32 from pylons.controllers.util import abort, redirect |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
33 from pylons.i18n.translation import _ |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
34 |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
35 from rhodecode.model.forms import GistForm |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
36 from rhodecode.model.gist import GistModel |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
37 from rhodecode.model.meta import Session |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
38 from rhodecode.model.db import Gist |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
39 from rhodecode.lib import helpers as h |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
40 from rhodecode.lib.base import BaseController, render |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
41 from rhodecode.lib.auth import LoginRequired, NotAnonymous |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
42 from rhodecode.lib.utils2 import safe_str, safe_int, time_to_datetime |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
43 from rhodecode.lib.helpers import Page |
3843
ad4a680113b7
Gist: implemented delete of gists by owner, or super admin
Marcin Kuzminski <marcin@python-works.com>
parents:
3840
diff
changeset
|
44 from webob.exc import HTTPNotFound, HTTPForbidden |
3840
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
45 from sqlalchemy.sql.expression import or_ |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
46 from rhodecode.lib.vcs.exceptions import VCSError |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
47 |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
48 log = logging.getLogger(__name__) |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
49 |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
50 |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
51 class GistsController(BaseController): |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
52 """REST Controller styled on the Atom Publishing Protocol""" |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
53 |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
54 def __load_defaults(self): |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
55 c.lifetime_values = [ |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
56 (str(-1), _('forever')), |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
57 (str(5), _('5 minutes')), |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
58 (str(60), _('1 hour')), |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
59 (str(60 * 24), _('1 day')), |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
60 (str(60 * 24 * 30), _('1 month')), |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
61 ] |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
62 c.lifetime_options = [(c.lifetime_values, _("Lifetime"))] |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
63 |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
64 @LoginRequired() |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
65 def index(self, format='html'): |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
66 """GET /admin/gists: All items in the collection""" |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
67 # url('gists') |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
68 c.show_private = request.GET.get('private') and c.rhodecode_user.username != 'default' |
3847
bec04f371579
Gist: added shortcut for my public gists
Marcin Kuzminski <marcin@python-works.com>
parents:
3843
diff
changeset
|
69 c.show_public = request.GET.get('public') and c.rhodecode_user.username != 'default' |
bec04f371579
Gist: added shortcut for my public gists
Marcin Kuzminski <marcin@python-works.com>
parents:
3843
diff
changeset
|
70 |
3840
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
71 gists = Gist().query()\ |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
72 .filter(or_(Gist.gist_expires == -1, Gist.gist_expires >= time.time()))\ |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
73 .order_by(Gist.created_on.desc()) |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
74 if c.show_private: |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
75 c.gists = gists.filter(Gist.gist_type == Gist.GIST_PRIVATE)\ |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
76 .filter(Gist.gist_owner == c.rhodecode_user.user_id) |
3847
bec04f371579
Gist: added shortcut for my public gists
Marcin Kuzminski <marcin@python-works.com>
parents:
3843
diff
changeset
|
77 elif c.show_public: |
bec04f371579
Gist: added shortcut for my public gists
Marcin Kuzminski <marcin@python-works.com>
parents:
3843
diff
changeset
|
78 c.gists = gists.filter(Gist.gist_type == Gist.GIST_PUBLIC)\ |
bec04f371579
Gist: added shortcut for my public gists
Marcin Kuzminski <marcin@python-works.com>
parents:
3843
diff
changeset
|
79 .filter(Gist.gist_owner == c.rhodecode_user.user_id) |
bec04f371579
Gist: added shortcut for my public gists
Marcin Kuzminski <marcin@python-works.com>
parents:
3843
diff
changeset
|
80 |
3840
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
81 else: |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
82 c.gists = gists.filter(Gist.gist_type == Gist.GIST_PUBLIC) |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
83 p = safe_int(request.GET.get('page', 1), 1) |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
84 c.gists_pager = Page(c.gists, page=p, items_per_page=10) |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
85 return render('admin/gists/index.html') |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
86 |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
87 @LoginRequired() |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
88 @NotAnonymous() |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
89 def create(self): |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
90 """POST /admin/gists: Create a new item""" |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
91 # url('gists') |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
92 self.__load_defaults() |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
93 gist_form = GistForm([x[0] for x in c.lifetime_values])() |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
94 try: |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
95 form_result = gist_form.to_python(dict(request.POST)) |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
96 #TODO: multiple files support, from the form |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
97 nodes = { |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
98 form_result['filename'] or 'gistfile1.txt': { |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
99 'content': form_result['content'], |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
100 'lexer': None # autodetect |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
101 } |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
102 } |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
103 _public = form_result['public'] |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
104 gist_type = Gist.GIST_PUBLIC if _public else Gist.GIST_PRIVATE |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
105 gist = GistModel().create( |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
106 description=form_result['description'], |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
107 owner=c.rhodecode_user, |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
108 gist_mapping=nodes, |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
109 gist_type=gist_type, |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
110 lifetime=form_result['lifetime'] |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
111 ) |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
112 Session().commit() |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
113 new_gist_id = gist.gist_access_id |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
114 except formencode.Invalid, errors: |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
115 defaults = errors.value |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
116 |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
117 return formencode.htmlfill.render( |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
118 render('admin/gists/new.html'), |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
119 defaults=defaults, |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
120 errors=errors.error_dict or {}, |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
121 prefix_error=False, |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
122 encoding="UTF-8" |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
123 ) |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
124 |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
125 except Exception, e: |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
126 log.error(traceback.format_exc()) |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
127 h.flash(_('Error occurred during gist creation'), category='error') |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
128 return redirect(url('new_gist')) |
3866
1fdec7e3aeb2
refactored url.resource to full definition of routes
Marcin Kuzminski <marcin@python-works.com>
parents:
3847
diff
changeset
|
129 return redirect(url('gist', gist_id=new_gist_id)) |
3840
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
130 |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
131 @LoginRequired() |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
132 @NotAnonymous() |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
133 def new(self, format='html'): |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
134 """GET /admin/gists/new: Form to create a new item""" |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
135 # url('new_gist') |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
136 self.__load_defaults() |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
137 return render('admin/gists/new.html') |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
138 |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
139 @LoginRequired() |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
140 @NotAnonymous() |
3866
1fdec7e3aeb2
refactored url.resource to full definition of routes
Marcin Kuzminski <marcin@python-works.com>
parents:
3847
diff
changeset
|
141 def update(self, gist_id): |
1fdec7e3aeb2
refactored url.resource to full definition of routes
Marcin Kuzminski <marcin@python-works.com>
parents:
3847
diff
changeset
|
142 """PUT /admin/gists/gist_id: Update an existing item""" |
3840
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
143 # Forms posted to this method should contain a hidden field: |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
144 # <input type="hidden" name="_method" value="PUT" /> |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
145 # Or using helpers: |
3866
1fdec7e3aeb2
refactored url.resource to full definition of routes
Marcin Kuzminski <marcin@python-works.com>
parents:
3847
diff
changeset
|
146 # h.form(url('gist', gist_id=ID), |
3840
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
147 # method='put') |
3866
1fdec7e3aeb2
refactored url.resource to full definition of routes
Marcin Kuzminski <marcin@python-works.com>
parents:
3847
diff
changeset
|
148 # url('gist', gist_id=ID) |
3840
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
149 |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
150 @LoginRequired() |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
151 @NotAnonymous() |
3866
1fdec7e3aeb2
refactored url.resource to full definition of routes
Marcin Kuzminski <marcin@python-works.com>
parents:
3847
diff
changeset
|
152 def delete(self, gist_id): |
1fdec7e3aeb2
refactored url.resource to full definition of routes
Marcin Kuzminski <marcin@python-works.com>
parents:
3847
diff
changeset
|
153 """DELETE /admin/gists/gist_id: Delete an existing item""" |
3840
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
154 # Forms posted to this method should contain a hidden field: |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
155 # <input type="hidden" name="_method" value="DELETE" /> |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
156 # Or using helpers: |
3866
1fdec7e3aeb2
refactored url.resource to full definition of routes
Marcin Kuzminski <marcin@python-works.com>
parents:
3847
diff
changeset
|
157 # h.form(url('gist', gist_id=ID), |
3840
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
158 # method='delete') |
3866
1fdec7e3aeb2
refactored url.resource to full definition of routes
Marcin Kuzminski <marcin@python-works.com>
parents:
3847
diff
changeset
|
159 # url('gist', gist_id=ID) |
1fdec7e3aeb2
refactored url.resource to full definition of routes
Marcin Kuzminski <marcin@python-works.com>
parents:
3847
diff
changeset
|
160 gist = GistModel().get_gist(gist_id) |
3843
ad4a680113b7
Gist: implemented delete of gists by owner, or super admin
Marcin Kuzminski <marcin@python-works.com>
parents:
3840
diff
changeset
|
161 owner = gist.gist_owner == c.rhodecode_user.user_id |
ad4a680113b7
Gist: implemented delete of gists by owner, or super admin
Marcin Kuzminski <marcin@python-works.com>
parents:
3840
diff
changeset
|
162 if h.HasPermissionAny('hg.admin')() or owner: |
ad4a680113b7
Gist: implemented delete of gists by owner, or super admin
Marcin Kuzminski <marcin@python-works.com>
parents:
3840
diff
changeset
|
163 GistModel().delete(gist) |
ad4a680113b7
Gist: implemented delete of gists by owner, or super admin
Marcin Kuzminski <marcin@python-works.com>
parents:
3840
diff
changeset
|
164 Session().commit() |
ad4a680113b7
Gist: implemented delete of gists by owner, or super admin
Marcin Kuzminski <marcin@python-works.com>
parents:
3840
diff
changeset
|
165 h.flash(_('Deleted gist %s') % gist.gist_access_id, category='success') |
ad4a680113b7
Gist: implemented delete of gists by owner, or super admin
Marcin Kuzminski <marcin@python-works.com>
parents:
3840
diff
changeset
|
166 else: |
ad4a680113b7
Gist: implemented delete of gists by owner, or super admin
Marcin Kuzminski <marcin@python-works.com>
parents:
3840
diff
changeset
|
167 raise HTTPForbidden() |
ad4a680113b7
Gist: implemented delete of gists by owner, or super admin
Marcin Kuzminski <marcin@python-works.com>
parents:
3840
diff
changeset
|
168 |
ad4a680113b7
Gist: implemented delete of gists by owner, or super admin
Marcin Kuzminski <marcin@python-works.com>
parents:
3840
diff
changeset
|
169 return redirect(url('gists')) |
3840
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
170 |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
171 @LoginRequired() |
3867
73f7149f2cc0
Added show as raw into gist
Marcin Kuzminski <marcin@python-works.com>
parents:
3866
diff
changeset
|
172 def show(self, gist_id, format='html', revision='tip', f_path=None): |
3866
1fdec7e3aeb2
refactored url.resource to full definition of routes
Marcin Kuzminski <marcin@python-works.com>
parents:
3847
diff
changeset
|
173 """GET /admin/gists/gist_id: Show a specific item""" |
1fdec7e3aeb2
refactored url.resource to full definition of routes
Marcin Kuzminski <marcin@python-works.com>
parents:
3847
diff
changeset
|
174 # url('gist', gist_id=ID) |
3840
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
175 c.gist = Gist.get_or_404(gist_id) |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
176 |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
177 #check if this gist is not expired |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
178 if c.gist.gist_expires != -1: |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
179 if time.time() > c.gist.gist_expires: |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
180 log.error('Gist expired at %s' % |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
181 (time_to_datetime(c.gist.gist_expires))) |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
182 raise HTTPNotFound() |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
183 try: |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
184 c.file_changeset, c.files = GistModel().get_gist_files(gist_id) |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
185 except VCSError: |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
186 log.error(traceback.format_exc()) |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
187 raise HTTPNotFound() |
3867
73f7149f2cc0
Added show as raw into gist
Marcin Kuzminski <marcin@python-works.com>
parents:
3866
diff
changeset
|
188 if format == 'raw': |
73f7149f2cc0
Added show as raw into gist
Marcin Kuzminski <marcin@python-works.com>
parents:
3866
diff
changeset
|
189 content = '\n\n'.join([f.content for f in c.files if (f_path is None or f.path == f_path)]) |
73f7149f2cc0
Added show as raw into gist
Marcin Kuzminski <marcin@python-works.com>
parents:
3866
diff
changeset
|
190 response.content_type = 'text/plain' |
73f7149f2cc0
Added show as raw into gist
Marcin Kuzminski <marcin@python-works.com>
parents:
3866
diff
changeset
|
191 return content |
3840
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
192 return render('admin/gists/show.html') |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
193 |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
194 @LoginRequired() |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
195 @NotAnonymous() |
3866
1fdec7e3aeb2
refactored url.resource to full definition of routes
Marcin Kuzminski <marcin@python-works.com>
parents:
3847
diff
changeset
|
196 def edit(self, gist_id, format='html'): |
1fdec7e3aeb2
refactored url.resource to full definition of routes
Marcin Kuzminski <marcin@python-works.com>
parents:
3847
diff
changeset
|
197 """GET /admin/gists/gist_id/edit: Form to edit an existing item""" |
1fdec7e3aeb2
refactored url.resource to full definition of routes
Marcin Kuzminski <marcin@python-works.com>
parents:
3847
diff
changeset
|
198 # url('edit_gist', gist_id=ID) |