annotate rhodecode/controllers/admin/gists.py @ 4030:647308db13ff

Added codemirror syntax mode in gists. - autopropose file extension when syntax mode is choosen
author Marcin Kuzminski <marcin@python-works.com>
date Fri, 21 Jun 2013 02:42:32 +0200
parents 73f7149f2cc0
children ffd45b185016
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
4030
647308db13ff Added codemirror syntax mode in gists.
Marcin Kuzminski <marcin@python-works.com>
parents: 3867
diff changeset
97 filename = form_result['filename'] or 'gistfile1.txt'
3840
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
98 nodes = {
4030
647308db13ff Added codemirror syntax mode in gists.
Marcin Kuzminski <marcin@python-works.com>
parents: 3867
diff changeset
99 filename: {
3840
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
100 'content': form_result['content'],
4030
647308db13ff Added codemirror syntax mode in gists.
Marcin Kuzminski <marcin@python-works.com>
parents: 3867
diff changeset
101 'lexer': form_result['mimetype'] # None is autodetect
3840
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 }
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
104 _public = form_result['public']
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
105 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
106 gist = GistModel().create(
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
107 description=form_result['description'],
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
108 owner=c.rhodecode_user,
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
109 gist_mapping=nodes,
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
110 gist_type=gist_type,
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
111 lifetime=form_result['lifetime']
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
112 )
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
113 Session().commit()
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
114 new_gist_id = gist.gist_access_id
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
115 except formencode.Invalid, errors:
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
116 defaults = errors.value
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
117
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
118 return formencode.htmlfill.render(
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
119 render('admin/gists/new.html'),
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
120 defaults=defaults,
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
121 errors=errors.error_dict or {},
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
122 prefix_error=False,
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
123 encoding="UTF-8"
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
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
126 except Exception, e:
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
127 log.error(traceback.format_exc())
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
128 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
129 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
130 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
131
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
132 @LoginRequired()
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
133 @NotAnonymous()
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
134 def new(self, format='html'):
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
135 """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
136 # url('new_gist')
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
137 self.__load_defaults()
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
138 return render('admin/gists/new.html')
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
139
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
140 @LoginRequired()
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
141 @NotAnonymous()
3866
1fdec7e3aeb2 refactored url.resource to full definition of routes
Marcin Kuzminski <marcin@python-works.com>
parents: 3847
diff changeset
142 def update(self, gist_id):
1fdec7e3aeb2 refactored url.resource to full definition of routes
Marcin Kuzminski <marcin@python-works.com>
parents: 3847
diff changeset
143 """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
144 # 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
145 # <input type="hidden" name="_method" value="PUT" />
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
146 # Or using helpers:
3866
1fdec7e3aeb2 refactored url.resource to full definition of routes
Marcin Kuzminski <marcin@python-works.com>
parents: 3847
diff changeset
147 # h.form(url('gist', gist_id=ID),
3840
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
148 # method='put')
3866
1fdec7e3aeb2 refactored url.resource to full definition of routes
Marcin Kuzminski <marcin@python-works.com>
parents: 3847
diff changeset
149 # url('gist', gist_id=ID)
3840
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
150
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
151 @LoginRequired()
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
152 @NotAnonymous()
3866
1fdec7e3aeb2 refactored url.resource to full definition of routes
Marcin Kuzminski <marcin@python-works.com>
parents: 3847
diff changeset
153 def delete(self, gist_id):
1fdec7e3aeb2 refactored url.resource to full definition of routes
Marcin Kuzminski <marcin@python-works.com>
parents: 3847
diff changeset
154 """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
155 # 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
156 # <input type="hidden" name="_method" value="DELETE" />
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
157 # Or using helpers:
3866
1fdec7e3aeb2 refactored url.resource to full definition of routes
Marcin Kuzminski <marcin@python-works.com>
parents: 3847
diff changeset
158 # h.form(url('gist', gist_id=ID),
3840
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
159 # method='delete')
3866
1fdec7e3aeb2 refactored url.resource to full definition of routes
Marcin Kuzminski <marcin@python-works.com>
parents: 3847
diff changeset
160 # url('gist', gist_id=ID)
1fdec7e3aeb2 refactored url.resource to full definition of routes
Marcin Kuzminski <marcin@python-works.com>
parents: 3847
diff changeset
161 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
162 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
163 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
164 GistModel().delete(gist)
ad4a680113b7 Gist: implemented delete of gists by owner, or super admin
Marcin Kuzminski <marcin@python-works.com>
parents: 3840
diff changeset
165 Session().commit()
ad4a680113b7 Gist: implemented delete of gists by owner, or super admin
Marcin Kuzminski <marcin@python-works.com>
parents: 3840
diff changeset
166 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
167 else:
ad4a680113b7 Gist: implemented delete of gists by owner, or super admin
Marcin Kuzminski <marcin@python-works.com>
parents: 3840
diff changeset
168 raise HTTPForbidden()
ad4a680113b7 Gist: implemented delete of gists by owner, or super admin
Marcin Kuzminski <marcin@python-works.com>
parents: 3840
diff changeset
169
ad4a680113b7 Gist: implemented delete of gists by owner, or super admin
Marcin Kuzminski <marcin@python-works.com>
parents: 3840
diff changeset
170 return redirect(url('gists'))
3840
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
171
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
172 @LoginRequired()
3867
73f7149f2cc0 Added show as raw into gist
Marcin Kuzminski <marcin@python-works.com>
parents: 3866
diff changeset
173 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
174 """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
175 # url('gist', gist_id=ID)
3840
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
176 c.gist = Gist.get_or_404(gist_id)
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
177
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
178 #check if this gist is not expired
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
179 if c.gist.gist_expires != -1:
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
180 if time.time() > c.gist.gist_expires:
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
181 log.error('Gist expired at %s' %
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
182 (time_to_datetime(c.gist.gist_expires)))
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
183 raise HTTPNotFound()
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
184 try:
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
185 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
186 except VCSError:
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
187 log.error(traceback.format_exc())
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
188 raise HTTPNotFound()
3867
73f7149f2cc0 Added show as raw into gist
Marcin Kuzminski <marcin@python-works.com>
parents: 3866
diff changeset
189 if format == 'raw':
73f7149f2cc0 Added show as raw into gist
Marcin Kuzminski <marcin@python-works.com>
parents: 3866
diff changeset
190 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
191 response.content_type = 'text/plain'
73f7149f2cc0 Added show as raw into gist
Marcin Kuzminski <marcin@python-works.com>
parents: 3866
diff changeset
192 return content
3840
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
193 return render('admin/gists/show.html')
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
194
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
195 @LoginRequired()
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
196 @NotAnonymous()
3866
1fdec7e3aeb2 refactored url.resource to full definition of routes
Marcin Kuzminski <marcin@python-works.com>
parents: 3847
diff changeset
197 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
198 """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
199 # url('edit_gist', gist_id=ID)