##// END OF EJS Templates
Added more advanced hook management into rhodecode admin settings
marcink -
r1460:b5034881 beta
parent child Browse files
Show More
@@ -0,0 +1,96 b''
1 ## -*- coding: utf-8 -*-
2 <%inherit file="/base/base.html"/>
3
4 <%def name="title()">
5 ${_('Settings administration')} - ${c.rhodecode_name}
6 </%def>
7
8 <%def name="breadcrumbs_links()">
9 ${h.link_to(_('Admin'),h.url('admin_home'))} &raquo; ${_('Settings')}
10 </%def>
11
12 <%def name="page_nav()">
13 ${self.menu('admin')}
14 </%def>
15
16 <%def name="main()">
17 <div class="box">
18 <!-- box / title -->
19 <div class="title">
20 ${self.breadcrumbs()}
21 </div>
22 <!-- end box / title -->
23
24 <h3>${_('Built in hooks - read only')}</h3>
25 <div class="form">
26 <div class="fields">
27 % for hook in c.hooks:
28 <div class="field">
29 <div class="label label">
30 <label for="${hook.ui_key}">${hook.ui_key}</label>
31 </div>
32 <div class="input" style="margin-left:280px">
33 ${h.text(hook.ui_key,hook.ui_value,size=60,readonly="readonly")}
34 </div>
35 </div>
36 % endfor
37 </div>
38 </div>
39
40 <h3>${_('Custom hooks')}</h3>
41 ${h.form(url('admin_setting', setting_id='hooks'),method='put')}
42 <div class="form">
43 <div class="fields">
44
45 % for hook in c.custom_hooks:
46 <div class="field" id="${'id%s' % hook.ui_id }">
47 <div class="label label">
48 <label for="${hook.ui_key}">${hook.ui_key}</label>
49 </div>
50 <div class="input" style="margin-left:280px">
51 ${h.hidden('hook_ui_key',hook.ui_key)}
52 ${h.hidden('hook_ui_value',hook.ui_value)}
53 ${h.text('hook_ui_value_new',hook.ui_value,size=60)}
54 <span class="delete_icon action_button"
55 onclick="ajaxActionHook(${hook.ui_id},'${'id%s' % hook.ui_id }')">
56 ${_('remove')}
57 </span>
58 </div>
59 </div>
60 % endfor
61
62 <div class="field">
63 <div class="input" style="margin-left:-180px;position: absolute;">
64 <div class="input">
65 ${h.text('new_hook_ui_key',size=30)}
66 </div>
67 </div>
68 <div class="input" style="margin-left:280px">
69 ${h.text('new_hook_ui_value',size=60)}
70 </div>
71 </div>
72 <div class="buttons" style="margin-left:280px">
73 ${h.submit('save','Save',class_="ui-button")}
74 </div>
75 </div>
76 </div>
77 ${h.end_form()}
78 </div>
79 <script type="text/javascript">
80 function ajaxActionHook(hook_id,field_id) {
81 var sUrl = "${h.url('admin_setting', setting_id='hooks')}";
82 var callback = {
83 success: function (o) {
84 var elem = YUD.get(""+field_id);
85 elem.parentNode.removeChild(elem);
86 },
87 failure: function (o) {
88 alert("${_('Failed to remove hook')}");
89 },
90 };
91 var postData = '_method=delete&hook_id=' + hook_id;
92 var request = YAHOO.util.Connect.asyncRequest('POST', sUrl, callback, postData);
93 };
94 </script>
95
96 </%def>
@@ -225,6 +225,34 b' class SettingsController(BaseController)'
225 prefix_error=False,
225 prefix_error=False,
226 encoding="UTF-8")
226 encoding="UTF-8")
227
227
228
229 if setting_id == 'hooks':
230 ui_key = request.POST.get('new_hook_ui_key')
231 ui_value = request.POST.get('new_hook_ui_value')
232 try:
233
234 if ui_value and ui_key:
235 RhodeCodeUi.create_or_update_hook(ui_key, ui_value)
236 h.flash(_('Added new hook'),
237 category='success')
238
239 # check for edits
240 update = False
241 _d = request.POST.dict_of_lists()
242 for k, v in zip(_d.get('hook_ui_key',[]), _d.get('hook_ui_value_new',[])):
243 RhodeCodeUi.create_or_update_hook(k, v)
244 update = True
245
246 if update:
247 h.flash(_('Updated hooks'), category='success')
248
249 except:
250 log.error(traceback.format_exc())
251 h.flash(_('error occurred during hook creation'),
252 category='error')
253
254 return redirect(url('admin_edit_setting', setting_id='hooks'))
255
228 return redirect(url('admin_settings'))
256 return redirect(url('admin_settings'))
229
257
230 @HasPermissionAllDecorator('hg.admin')
258 @HasPermissionAllDecorator('hg.admin')
@@ -236,7 +264,11 b' class SettingsController(BaseController)'
236 # h.form(url('admin_setting', setting_id=ID),
264 # h.form(url('admin_setting', setting_id=ID),
237 # method='delete')
265 # method='delete')
238 # url('admin_setting', setting_id=ID)
266 # url('admin_setting', setting_id=ID)
239
267 if setting_id == 'hooks':
268 hook_id = request.POST.get('hook_id')
269 RhodeCodeUi.delete(hook_id)
270
271
240 @HasPermissionAllDecorator('hg.admin')
272 @HasPermissionAllDecorator('hg.admin')
241 def show(self, setting_id, format='html'):
273 def show(self, setting_id, format='html'):
242 """
274 """
@@ -250,6 +282,16 b' class SettingsController(BaseController)'
250 edit an existing item"""
282 edit an existing item"""
251 # url('admin_edit_setting', setting_id=ID)
283 # url('admin_edit_setting', setting_id=ID)
252
284
285 c.hooks = RhodeCodeUi.get_builtin_hooks()
286 c.custom_hooks = RhodeCodeUi.get_custom_hooks()
287
288 return htmlfill.render(
289 render('admin/settings/hooks.html'),
290 defaults={},
291 encoding="UTF-8",
292 force_defaults=False
293 )
294
253 @NotAnonymous()
295 @NotAnonymous()
254 def my_account(self):
296 def my_account(self):
255 """
297 """
@@ -261,7 +261,7 b' class DbManage(object):'
261
261
262 """
262 """
263 #HOOKS
263 #HOOKS
264 hooks1_key = 'changegroup.update'
264 hooks1_key = RhodeCodeUi.HOOK_UPDATE
265 hooks1_ = self.sa.query(RhodeCodeUi)\
265 hooks1_ = self.sa.query(RhodeCodeUi)\
266 .filter(RhodeCodeUi.ui_key == hooks1_key).scalar()
266 .filter(RhodeCodeUi.ui_key == hooks1_key).scalar()
267
267
@@ -271,7 +271,7 b' class DbManage(object):'
271 hooks1.ui_value = 'hg update >&2'
271 hooks1.ui_value = 'hg update >&2'
272 hooks1.ui_active = False
272 hooks1.ui_active = False
273
273
274 hooks2_key = 'changegroup.repo_size'
274 hooks2_key = RhodeCodeUi.HOOK_REPO_SIZE
275 hooks2_ = self.sa.query(RhodeCodeUi)\
275 hooks2_ = self.sa.query(RhodeCodeUi)\
276 .filter(RhodeCodeUi.ui_key == hooks2_key).scalar()
276 .filter(RhodeCodeUi.ui_key == hooks2_key).scalar()
277
277
@@ -282,12 +282,12 b' class DbManage(object):'
282
282
283 hooks3 = RhodeCodeUi()
283 hooks3 = RhodeCodeUi()
284 hooks3.ui_section = 'hooks'
284 hooks3.ui_section = 'hooks'
285 hooks3.ui_key = 'pretxnchangegroup.push_logger'
285 hooks3.ui_key = RhodeCodeUi.HOOK_PUSH
286 hooks3.ui_value = 'python:rhodecode.lib.hooks.log_push_action'
286 hooks3.ui_value = 'python:rhodecode.lib.hooks.log_push_action'
287
287
288 hooks4 = RhodeCodeUi()
288 hooks4 = RhodeCodeUi()
289 hooks4.ui_section = 'hooks'
289 hooks4.ui_section = 'hooks'
290 hooks4.ui_key = 'preoutgoing.pull_logger'
290 hooks4.ui_key = RhodeCodeUi.HOOK_PULL
291 hooks4.ui_value = 'python:rhodecode.lib.hooks.log_pull_action'
291 hooks4.ui_value = 'python:rhodecode.lib.hooks.log_pull_action'
292
292
293 #For mercurial 1.7 set backward comapatibility with format
293 #For mercurial 1.7 set backward comapatibility with format
@@ -125,7 +125,13 b' class BaseModel(object):'
125 @classmethod
125 @classmethod
126 def get(cls, id_):
126 def get(cls, id_):
127 return Session.query(cls).get(id_)
127 return Session.query(cls).get(id_)
128
128
129 @classmethod
130 def delete(cls, id_):
131 obj = Session.query(cls).get(id_)
132 Session.delete(obj)
133 Session.commit()
134
129
135
130 class RhodeCodeSettings(Base, BaseModel):
136 class RhodeCodeSettings(Base, BaseModel):
131 __tablename__ = 'rhodecode_settings'
137 __tablename__ = 'rhodecode_settings'
@@ -181,7 +187,13 b' class RhodeCodeSettings(Base, BaseModel)'
181
187
182 class RhodeCodeUi(Base, BaseModel):
188 class RhodeCodeUi(Base, BaseModel):
183 __tablename__ = 'rhodecode_ui'
189 __tablename__ = 'rhodecode_ui'
184 __table_args__ = {'extend_existing':True}
190 __table_args__ = (UniqueConstraint('ui_key'), {'extend_existing':True})
191
192 HOOK_UPDATE = 'changegroup.update'
193 HOOK_REPO_SIZE = 'changegroup.repo_size'
194 HOOK_PUSH = 'pretxnchangegroup.push_logger'
195 HOOK_PULL = 'preoutgoing.pull_logger'
196
185 ui_id = Column("ui_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
197 ui_id = Column("ui_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
186 ui_section = Column("ui_section", String(length=255, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
198 ui_section = Column("ui_section", String(length=255, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
187 ui_key = Column("ui_key", String(length=255, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
199 ui_key = Column("ui_key", String(length=255, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
@@ -194,6 +206,35 b' class RhodeCodeUi(Base, BaseModel):'
194 return Session.query(cls).filter(cls.ui_key == key)
206 return Session.query(cls).filter(cls.ui_key == key)
195
207
196
208
209 @classmethod
210 def get_builtin_hooks(cls):
211 q = cls.query()
212 q = q.filter(cls.ui_key.in_([cls.HOOK_UPDATE,
213 cls.HOOK_REPO_SIZE,
214 cls.HOOK_PUSH, cls.HOOK_PULL]))
215 return q.all()
216
217 @classmethod
218 def get_custom_hooks(cls):
219 q = cls.query()
220 q = q.filter(~cls.ui_key.in_([cls.HOOK_UPDATE,
221 cls.HOOK_REPO_SIZE,
222 cls.HOOK_PUSH, cls.HOOK_PULL]))
223 q = q.filter(cls.ui_section == 'hooks')
224 return q.all()
225
226 @classmethod
227 def create_or_update_hook(cls, key, val):
228 new_ui = cls.get_by_key(key).scalar() or cls()
229 new_ui.ui_section = 'hooks'
230 new_ui.ui_active = True
231 new_ui.ui_key = key
232 new_ui.ui_value = val
233
234 Session.add(new_ui)
235 Session.commit()
236
237
197 class User(Base, BaseModel):
238 class User(Base, BaseModel):
198 __tablename__ = 'users'
239 __tablename__ = 'users'
199 __table_args__ = (UniqueConstraint('username'), UniqueConstraint('email'), {'extend_existing':True})
240 __table_args__ = (UniqueConstraint('username'), UniqueConstraint('email'), {'extend_existing':True})
@@ -138,6 +138,9 b''
138 <div class="label label-checkbox">
138 <div class="label label-checkbox">
139 <label for="web_push_ssl">${_('Hooks')}:</label>
139 <label for="web_push_ssl">${_('Hooks')}:</label>
140 </div>
140 </div>
141 <div class="input">
142 ${h.link_to('advanced setup',url('admin_edit_setting',setting_id='hooks'))}
143 </div>
141 <div class="checkboxes">
144 <div class="checkboxes">
142 <div class="checkbox">
145 <div class="checkbox">
143 ${h.checkbox('hooks_changegroup_update','True')}
146 ${h.checkbox('hooks_changegroup_update','True')}
General Comments 0
You need to be logged in to leave comments. Login now