diff --git a/kallithea/controllers/admin/my_account.py b/kallithea/controllers/admin/my_account.py --- a/kallithea/controllers/admin/my_account.py +++ b/kallithea/controllers/admin/my_account.py @@ -35,16 +35,14 @@ from tg import tmpl_context as c from tg.i18n import ugettext as _ from webob.exc import HTTPFound -from kallithea.config.routing import url -from kallithea.lib import auth_modules -from kallithea.lib import helpers as h +from kallithea.controllers import base +from kallithea.lib import auth_modules, webutils from kallithea.lib.auth import AuthUser, LoginRequired -from kallithea.lib.base import BaseController, IfSshEnabled, render from kallithea.lib.utils2 import generate_api_key, safe_int +from kallithea.lib.webutils import url +from kallithea.model import db, meta from kallithea.model.api_key import ApiKeyModel -from kallithea.model.db import Repository, User, UserEmailMap, UserFollowing from kallithea.model.forms import PasswordChangeForm, UserForm -from kallithea.model.meta import Session from kallithea.model.repo import RepoModel from kallithea.model.ssh_key import SshKeyModel, SshKeyModelException from kallithea.model.user import UserModel @@ -53,35 +51,30 @@ from kallithea.model.user import UserMod log = logging.getLogger(__name__) -class MyAccountController(BaseController): - """REST Controller styled on the Atom Publishing Protocol""" - # To properly map this controller, ensure your config/routing.py - # file has a resource setup: - # map.resource('setting', 'settings', controller='admin/settings', - # path_prefix='/admin', name_prefix='admin_') +class MyAccountController(base.BaseController): @LoginRequired() def _before(self, *args, **kwargs): super(MyAccountController, self)._before(*args, **kwargs) def __load_data(self): - c.user = User.get(request.authuser.user_id) + c.user = db.User.get(request.authuser.user_id) if c.user.is_default_user: - h.flash(_("You can't edit this user since it's" + webutils.flash(_("You can't edit this user since it's" " crucial for entire application"), category='warning') raise HTTPFound(location=url('users')) def _load_my_repos_data(self, watched=False): if watched: admin = False - repos_list = Session().query(Repository) \ - .join(UserFollowing) \ - .filter(UserFollowing.user_id == + repos_list = meta.Session().query(db.Repository) \ + .join(db.UserFollowing) \ + .filter(db.UserFollowing.user_id == request.authuser.user_id).all() else: admin = True - repos_list = Session().query(Repository) \ - .filter(Repository.owner_id == + repos_list = meta.Session().query(db.Repository) \ + .filter(db.Repository.owner_id == request.authuser.user_id).all() return RepoModel().get_repos_as_dict(repos_list, admin=admin) @@ -91,7 +84,7 @@ class MyAccountController(BaseController self.__load_data() c.perm_user = AuthUser(user_id=request.authuser.user_id) managed_fields = auth_modules.get_managed_fields(c.user) - def_user_perms = AuthUser(dbuser=User.get_default_user()).permissions['global'] + def_user_perms = AuthUser(dbuser=db.User.get_default_user()).global_permissions if 'hg.register.none' in def_user_perms: managed_fields.extend(['username', 'firstname', 'lastname', 'email']) @@ -116,14 +109,14 @@ class MyAccountController(BaseController UserModel().update(request.authuser.user_id, form_result, skip_attrs=skip_attrs) - h.flash(_('Your account was updated successfully'), + webutils.flash(_('Your account was updated successfully'), category='success') - Session().commit() + meta.Session().commit() update = True except formencode.Invalid as errors: return htmlfill.render( - render('admin/my_account/my_account.html'), + base.render('admin/my_account/my_account.html'), defaults=errors.value, errors=errors.error_dict or {}, prefix_error=False, @@ -131,12 +124,12 @@ class MyAccountController(BaseController force_defaults=False) except Exception: log.error(traceback.format_exc()) - h.flash(_('Error occurred during update of user %s') + webutils.flash(_('Error occurred during update of user %s') % form_result.get('username'), category='error') if update: raise HTTPFound(location='my_account') return htmlfill.render( - render('admin/my_account/my_account.html'), + base.render('admin/my_account/my_account.html'), defaults=defaults, encoding="UTF-8", force_defaults=False) @@ -153,11 +146,11 @@ class MyAccountController(BaseController try: form_result = _form.to_python(request.POST) UserModel().update(request.authuser.user_id, form_result) - Session().commit() - h.flash(_("Successfully updated password"), category='success') + meta.Session().commit() + webutils.flash(_("Successfully updated password"), category='success') except formencode.Invalid as errors: return htmlfill.render( - render('admin/my_account/my_account.html'), + base.render('admin/my_account/my_account.html'), defaults=errors.value, errors=errors.error_dict or {}, prefix_error=False, @@ -165,9 +158,9 @@ class MyAccountController(BaseController force_defaults=False) except Exception: log.error(traceback.format_exc()) - h.flash(_('Error occurred during update of user password'), + webutils.flash(_('Error occurred during update of user password'), category='error') - return render('admin/my_account/my_account.html') + return base.render('admin/my_account/my_account.html') def my_account_repos(self): c.active = 'repos' @@ -175,7 +168,7 @@ class MyAccountController(BaseController # data used to render the grid c.data = self._load_my_repos_data() - return render('admin/my_account/my_account.html') + return base.render('admin/my_account/my_account.html') def my_account_watched(self): c.active = 'watched' @@ -183,36 +176,36 @@ class MyAccountController(BaseController # data used to render the grid c.data = self._load_my_repos_data(watched=True) - return render('admin/my_account/my_account.html') + return base.render('admin/my_account/my_account.html') def my_account_perms(self): c.active = 'perms' self.__load_data() c.perm_user = AuthUser(user_id=request.authuser.user_id) - return render('admin/my_account/my_account.html') + return base.render('admin/my_account/my_account.html') def my_account_emails(self): c.active = 'emails' self.__load_data() - c.user_email_map = UserEmailMap.query() \ - .filter(UserEmailMap.user == c.user).all() - return render('admin/my_account/my_account.html') + c.user_email_map = db.UserEmailMap.query() \ + .filter(db.UserEmailMap.user == c.user).all() + return base.render('admin/my_account/my_account.html') def my_account_emails_add(self): email = request.POST.get('new_email') try: UserModel().add_extra_email(request.authuser.user_id, email) - Session().commit() - h.flash(_("Added email %s to user") % email, category='success') + meta.Session().commit() + webutils.flash(_("Added email %s to user") % email, category='success') except formencode.Invalid as error: msg = error.error_dict['email'] - h.flash(msg, category='error') + webutils.flash(msg, category='error') except Exception: log.error(traceback.format_exc()) - h.flash(_('An error occurred during email saving'), + webutils.flash(_('An error occurred during email saving'), category='error') raise HTTPFound(location=url('my_account_emails')) @@ -220,8 +213,8 @@ class MyAccountController(BaseController email_id = request.POST.get('del_email_id') user_model = UserModel() user_model.delete_extra_email(request.authuser.user_id, email_id) - Session().commit() - h.flash(_("Removed email from user"), category='success') + meta.Session().commit() + webutils.flash(_("Removed email from user"), category='success') raise HTTPFound(location=url('my_account_emails')) def my_account_api_keys(self): @@ -238,59 +231,59 @@ class MyAccountController(BaseController c.lifetime_options = [(c.lifetime_values, _("Lifetime"))] c.user_api_keys = ApiKeyModel().get_api_keys(request.authuser.user_id, show_expired=show_expired) - return render('admin/my_account/my_account.html') + return base.render('admin/my_account/my_account.html') def my_account_api_keys_add(self): lifetime = safe_int(request.POST.get('lifetime'), -1) description = request.POST.get('description') ApiKeyModel().create(request.authuser.user_id, description, lifetime) - Session().commit() - h.flash(_("API key successfully created"), category='success') + meta.Session().commit() + webutils.flash(_("API key successfully created"), category='success') raise HTTPFound(location=url('my_account_api_keys')) def my_account_api_keys_delete(self): api_key = request.POST.get('del_api_key') if request.POST.get('del_api_key_builtin'): - user = User.get(request.authuser.user_id) + user = db.User.get(request.authuser.user_id) user.api_key = generate_api_key() - Session().commit() - h.flash(_("API key successfully reset"), category='success') + meta.Session().commit() + webutils.flash(_("API key successfully reset"), category='success') elif api_key: ApiKeyModel().delete(api_key, request.authuser.user_id) - Session().commit() - h.flash(_("API key successfully deleted"), category='success') + meta.Session().commit() + webutils.flash(_("API key successfully deleted"), category='success') raise HTTPFound(location=url('my_account_api_keys')) - @IfSshEnabled + @base.IfSshEnabled def my_account_ssh_keys(self): c.active = 'ssh_keys' self.__load_data() c.user_ssh_keys = SshKeyModel().get_ssh_keys(request.authuser.user_id) - return render('admin/my_account/my_account.html') + return base.render('admin/my_account/my_account.html') - @IfSshEnabled + @base.IfSshEnabled def my_account_ssh_keys_add(self): description = request.POST.get('description') public_key = request.POST.get('public_key') try: new_ssh_key = SshKeyModel().create(request.authuser.user_id, description, public_key) - Session().commit() + meta.Session().commit() SshKeyModel().write_authorized_keys() - h.flash(_("SSH key %s successfully added") % new_ssh_key.fingerprint, category='success') + webutils.flash(_("SSH key %s successfully added") % new_ssh_key.fingerprint, category='success') except SshKeyModelException as e: - h.flash(e.args[0], category='error') + webutils.flash(e.args[0], category='error') raise HTTPFound(location=url('my_account_ssh_keys')) - @IfSshEnabled + @base.IfSshEnabled def my_account_ssh_keys_delete(self): fingerprint = request.POST.get('del_public_key_fingerprint') try: SshKeyModel().delete(fingerprint, request.authuser.user_id) - Session().commit() + meta.Session().commit() SshKeyModel().write_authorized_keys() - h.flash(_("SSH key successfully deleted"), category='success') + webutils.flash(_("SSH key successfully deleted"), category='success') except SshKeyModelException as e: - h.flash(e.args[0], category='error') + webutils.flash(e.args[0], category='error') raise HTTPFound(location=url('my_account_ssh_keys'))