api.py
97 lines
| 2.8 KiB
| text/x-python
|
PythonLexer
r1500 | import traceback | ||
import logging | |||
r1445 | from rhodecode.controllers.api import JSONRPCController, JSONRPCError | ||
from rhodecode.lib.auth import HasPermissionAllDecorator | |||
from rhodecode.model.scm import ScmModel | |||
r1508 | from rhodecode.model.db import User, UsersGroup, Repository | ||
r1500 | |||
log = logging.getLogger(__name__) | |||
r1445 | |||
class ApiController(JSONRPCController): | |||
""" | |||
API Controller | |||
Each method needs to have USER as argument this is then based on given | |||
API_KEY propagated as instance of user object | |||
Preferably this should be first argument also | |||
Each function should also **raise** JSONRPCError for any | |||
errors that happens | |||
""" | |||
@HasPermissionAllDecorator('hg.admin') | |||
r1500 | def pull(self, apiuser, repo): | ||
r1445 | """ | ||
Dispatch pull action on given repo | |||
r1500 | :param user: | ||
:param repo: | |||
r1445 | """ | ||
r1508 | if Repository.is_valid(repo) is False: | ||
raise JSONRPCError('Unknown repo "%s"' % repo) | |||
r1445 | try: | ||
ScmModel().pull_changes(repo, self.rhodecode_user.username) | |||
return 'Pulled from %s' % repo | |||
except Exception: | |||
raise JSONRPCError('Unable to pull changes from "%s"' % repo) | |||
r1500 | @HasPermissionAllDecorator('hg.admin') | ||
def create_user(self, apiuser, username, password, active, admin, name, | |||
lastname, email): | |||
""" | |||
Creates new user | |||
:param apiuser: | |||
:param username: | |||
:param password: | |||
:param active: | |||
:param admin: | |||
:param name: | |||
:param lastname: | |||
:param email: | |||
""" | |||
form_data = dict(username=username, | |||
password=password, | |||
active=active, | |||
admin=admin, | |||
name=name, | |||
lastname=lastname, | |||
email=email) | |||
try: | |||
u = User.create(form_data) | |||
return {'id':u.user_id, | |||
'msg':'created new user %s' % name} | |||
except Exception: | |||
log.error(traceback.format_exc()) | |||
raise JSONRPCError('failed to create user %s' % name) | |||
r1445 | |||
r1500 | @HasPermissionAllDecorator('hg.admin') | ||
def create_users_group(self, apiuser, name, active): | |||
""" | |||
Creates an new usergroup | |||
:param name: | |||
:param active: | |||
""" | |||
form_data = {'users_group_name':name, | |||
'users_group_active':active} | |||
try: | |||
ug = UsersGroup.create(form_data) | |||
return {'id':ug.users_group_id, | |||
'msg':'created new users group %s' % name} | |||
except Exception: | |||
log.error(traceback.format_exc()) | |||
raise JSONRPCError('failed to create group %s' % name) | |||