diff --git a/rhodecode/controllers/admin/settings.py b/rhodecode/controllers/admin/settings.py --- a/rhodecode/controllers/admin/settings.py +++ b/rhodecode/controllers/admin/settings.py @@ -225,6 +225,34 @@ class SettingsController(BaseController) prefix_error=False, encoding="UTF-8") + + if setting_id == 'hooks': + ui_key = request.POST.get('new_hook_ui_key') + ui_value = request.POST.get('new_hook_ui_value') + try: + + if ui_value and ui_key: + RhodeCodeUi.create_or_update_hook(ui_key, ui_value) + h.flash(_('Added new hook'), + category='success') + + # check for edits + update = False + _d = request.POST.dict_of_lists() + for k, v in zip(_d.get('hook_ui_key',[]), _d.get('hook_ui_value_new',[])): + RhodeCodeUi.create_or_update_hook(k, v) + update = True + + if update: + h.flash(_('Updated hooks'), category='success') + + except: + log.error(traceback.format_exc()) + h.flash(_('error occurred during hook creation'), + category='error') + + return redirect(url('admin_edit_setting', setting_id='hooks')) + return redirect(url('admin_settings')) @HasPermissionAllDecorator('hg.admin') @@ -236,7 +264,11 @@ class SettingsController(BaseController) # h.form(url('admin_setting', setting_id=ID), # method='delete') # url('admin_setting', setting_id=ID) - + if setting_id == 'hooks': + hook_id = request.POST.get('hook_id') + RhodeCodeUi.delete(hook_id) + + @HasPermissionAllDecorator('hg.admin') def show(self, setting_id, format='html'): """ @@ -250,6 +282,16 @@ class SettingsController(BaseController) edit an existing item""" # url('admin_edit_setting', setting_id=ID) + c.hooks = RhodeCodeUi.get_builtin_hooks() + c.custom_hooks = RhodeCodeUi.get_custom_hooks() + + return htmlfill.render( + render('admin/settings/hooks.html'), + defaults={}, + encoding="UTF-8", + force_defaults=False + ) + @NotAnonymous() def my_account(self): """ diff --git a/rhodecode/lib/db_manage.py b/rhodecode/lib/db_manage.py --- a/rhodecode/lib/db_manage.py +++ b/rhodecode/lib/db_manage.py @@ -261,7 +261,7 @@ class DbManage(object): """ #HOOKS - hooks1_key = 'changegroup.update' + hooks1_key = RhodeCodeUi.HOOK_UPDATE hooks1_ = self.sa.query(RhodeCodeUi)\ .filter(RhodeCodeUi.ui_key == hooks1_key).scalar() @@ -271,7 +271,7 @@ class DbManage(object): hooks1.ui_value = 'hg update >&2' hooks1.ui_active = False - hooks2_key = 'changegroup.repo_size' + hooks2_key = RhodeCodeUi.HOOK_REPO_SIZE hooks2_ = self.sa.query(RhodeCodeUi)\ .filter(RhodeCodeUi.ui_key == hooks2_key).scalar() @@ -282,12 +282,12 @@ class DbManage(object): hooks3 = RhodeCodeUi() hooks3.ui_section = 'hooks' - hooks3.ui_key = 'pretxnchangegroup.push_logger' + hooks3.ui_key = RhodeCodeUi.HOOK_PUSH hooks3.ui_value = 'python:rhodecode.lib.hooks.log_push_action' hooks4 = RhodeCodeUi() hooks4.ui_section = 'hooks' - hooks4.ui_key = 'preoutgoing.pull_logger' + hooks4.ui_key = RhodeCodeUi.HOOK_PULL hooks4.ui_value = 'python:rhodecode.lib.hooks.log_pull_action' #For mercurial 1.7 set backward comapatibility with format diff --git a/rhodecode/model/db.py b/rhodecode/model/db.py --- a/rhodecode/model/db.py +++ b/rhodecode/model/db.py @@ -125,7 +125,13 @@ class BaseModel(object): @classmethod def get(cls, id_): return Session.query(cls).get(id_) - + + @classmethod + def delete(cls, id_): + obj = Session.query(cls).get(id_) + Session.delete(obj) + Session.commit() + class RhodeCodeSettings(Base, BaseModel): __tablename__ = 'rhodecode_settings' @@ -181,7 +187,13 @@ class RhodeCodeSettings(Base, BaseModel) class RhodeCodeUi(Base, BaseModel): __tablename__ = 'rhodecode_ui' - __table_args__ = {'extend_existing':True} + __table_args__ = (UniqueConstraint('ui_key'), {'extend_existing':True}) + + HOOK_UPDATE = 'changegroup.update' + HOOK_REPO_SIZE = 'changegroup.repo_size' + HOOK_PUSH = 'pretxnchangegroup.push_logger' + HOOK_PULL = 'preoutgoing.pull_logger' + ui_id = Column("ui_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) ui_section = Column("ui_section", String(length=255, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) ui_key = Column("ui_key", String(length=255, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) @@ -194,6 +206,35 @@ class RhodeCodeUi(Base, BaseModel): return Session.query(cls).filter(cls.ui_key == key) + @classmethod + def get_builtin_hooks(cls): + q = cls.query() + q = q.filter(cls.ui_key.in_([cls.HOOK_UPDATE, + cls.HOOK_REPO_SIZE, + cls.HOOK_PUSH, cls.HOOK_PULL])) + return q.all() + + @classmethod + def get_custom_hooks(cls): + q = cls.query() + q = q.filter(~cls.ui_key.in_([cls.HOOK_UPDATE, + cls.HOOK_REPO_SIZE, + cls.HOOK_PUSH, cls.HOOK_PULL])) + q = q.filter(cls.ui_section == 'hooks') + return q.all() + + @classmethod + def create_or_update_hook(cls, key, val): + new_ui = cls.get_by_key(key).scalar() or cls() + new_ui.ui_section = 'hooks' + new_ui.ui_active = True + new_ui.ui_key = key + new_ui.ui_value = val + + Session.add(new_ui) + Session.commit() + + class User(Base, BaseModel): __tablename__ = 'users' __table_args__ = (UniqueConstraint('username'), UniqueConstraint('email'), {'extend_existing':True}) diff --git a/rhodecode/templates/admin/settings/hooks.html b/rhodecode/templates/admin/settings/hooks.html new file mode 100644 --- /dev/null +++ b/rhodecode/templates/admin/settings/hooks.html @@ -0,0 +1,96 @@ +## -*- coding: utf-8 -*- +<%inherit file="/base/base.html"/> + +<%def name="title()"> + ${_('Settings administration')} - ${c.rhodecode_name} + + +<%def name="breadcrumbs_links()"> + ${h.link_to(_('Admin'),h.url('admin_home'))} » ${_('Settings')} + + +<%def name="page_nav()"> + ${self.menu('admin')} + + +<%def name="main()"> +
+ +
+ ${self.breadcrumbs()} +
+ + +

${_('Built in hooks - read only')}

+
+
+ % for hook in c.hooks: +
+
+ +
+
+ ${h.text(hook.ui_key,hook.ui_value,size=60,readonly="readonly")} +
+
+ % endfor +
+
+ +

${_('Custom hooks')}

+ ${h.form(url('admin_setting', setting_id='hooks'),method='put')} +
+
+ + % for hook in c.custom_hooks: +
+
+ +
+
+ ${h.hidden('hook_ui_key',hook.ui_key)} + ${h.hidden('hook_ui_value',hook.ui_value)} + ${h.text('hook_ui_value_new',hook.ui_value,size=60)} + + ${_('remove')} + +
+
+ % endfor + +
+
+
+ ${h.text('new_hook_ui_key',size=30)} +
+
+
+ ${h.text('new_hook_ui_value',size=60)} +
+
+
+ ${h.submit('save','Save',class_="ui-button")} +
+
+
+ ${h.end_form()} +
+ + + diff --git a/rhodecode/templates/admin/settings/settings.html b/rhodecode/templates/admin/settings/settings.html --- a/rhodecode/templates/admin/settings/settings.html +++ b/rhodecode/templates/admin/settings/settings.html @@ -138,6 +138,9 @@
+
+ ${h.link_to('advanced setup',url('admin_edit_setting',setting_id='hooks'))} +
${h.checkbox('hooks_changegroup_update','True')}