my-account: moved emails config into pyramid views.

This commit is contained in:
Marcin Kuzminski 2017-06-19 15:30:09 +02:00
parent ad54b9a57d
commit f442430df8
8 changed files with 171 additions and 100 deletions

View file

@ -46,6 +46,16 @@ def includeme(config):
name='my_account_auth_tokens_delete',
pattern=ADMIN_PREFIX + '/my_account/auth_tokens/delete')
config.add_route(
name='my_account_emails',
pattern=ADMIN_PREFIX + '/my_account/emails')
config.add_route(
name='my_account_emails_add',
pattern=ADMIN_PREFIX + '/my_account/emails/new')
config.add_route(
name='my_account_emails_delete',
pattern=ADMIN_PREFIX + '/my_account/emails/delete')
# channelstream test
config.add_route(
name='my_account_notifications_test_channelstream',

View file

@ -0,0 +1,93 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2010-2017 RhodeCode GmbH
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License, version 3
# (only), as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# This program is dual-licensed. If you wish to learn more about the
# RhodeCode Enterprise Edition, including its added features, Support services,
# and proprietary license terms, please see https://rhodecode.com/licenses/
import pytest
from rhodecode.apps._base import ADMIN_PREFIX
from rhodecode.model.db import User, UserEmailMap
from rhodecode.tests import (
TestController, TEST_USER_ADMIN_LOGIN, TEST_USER_REGULAR_EMAIL,
assert_session_flash)
from rhodecode.tests.fixture import Fixture
fixture = Fixture()
def route_path(name, **kwargs):
return {
'my_account_emails':
ADMIN_PREFIX + '/my_account/emails',
'my_account_emails_add':
ADMIN_PREFIX + '/my_account/emails/new',
'my_account_emails_delete':
ADMIN_PREFIX + '/my_account/emails/delete',
}[name].format(**kwargs)
class TestMyAccountEmails(TestController):
def test_my_account_my_emails(self):
self.log_user()
response = self.app.get(route_path('my_account_emails'))
response.mustcontain('No additional emails specified')
def test_my_account_my_emails_add_existing_email(self):
self.log_user()
response = self.app.get(route_path('my_account_emails'))
response.mustcontain('No additional emails specified')
response = self.app.post(route_path('my_account_emails_add'),
{'new_email': TEST_USER_REGULAR_EMAIL,
'csrf_token': self.csrf_token})
assert_session_flash(response, 'This e-mail address is already taken')
def test_my_account_my_emails_add_mising_email_in_form(self):
self.log_user()
response = self.app.get(route_path('my_account_emails'))
response.mustcontain('No additional emails specified')
response = self.app.post(route_path('my_account_emails_add'),
{'csrf_token': self.csrf_token})
assert_session_flash(response, 'Please enter an email address')
def test_my_account_my_emails_add_remove(self):
self.log_user()
response = self.app.get(route_path('my_account_emails'))
response.mustcontain('No additional emails specified')
response = self.app.post(route_path('my_account_emails_add'),
{'new_email': 'foo@barz.com',
'csrf_token': self.csrf_token})
response = self.app.get(route_path('my_account_emails'))
email_id = UserEmailMap.query().filter(
UserEmailMap.user == User.get_by_username(
TEST_USER_ADMIN_LOGIN)).filter(
UserEmailMap.email == 'foo@barz.com').one().email_id
response.mustcontain('foo@barz.com')
response.mustcontain('<input id="del_email_id" name="del_email_id" '
'type="hidden" value="%s" />' % email_id)
response = self.app.post(
route_path('my_account_emails_delete'), {
'del_email_id': email_id,
'csrf_token': self.csrf_token})
assert_session_flash(response, 'Email successfully deleted')
response = self.app.get(route_path('my_account_emails'))
response.mustcontain('No additional emails specified')

View file

@ -21,6 +21,7 @@
import logging
import datetime
import formencode
from pyramid.httpexceptions import HTTPFound
from pyramid.view import view_config
@ -32,6 +33,7 @@ from rhodecode.lib.channelstream import channelstream_request, \
ChannelstreamException
from rhodecode.lib.utils2 import safe_int, md5
from rhodecode.model.auth_token import AuthTokenModel
from rhodecode.model.db import UserEmailMap
from rhodecode.model.meta import Session
from rhodecode.model.user import UserModel
from rhodecode.model.validation_schema.schemas import user_schema
@ -161,7 +163,7 @@ class MyAccountView(BaseAppView):
@NotAnonymous()
@CSRFRequired()
@view_config(
route_name='my_account_auth_tokens_add', request_method='POST')
route_name='my_account_auth_tokens_add', request_method='POST',)
def my_account_auth_tokens_add(self):
_ = self.request.translate
c = self.load_default_context()
@ -196,6 +198,65 @@ class MyAccountView(BaseAppView):
return HTTPFound(h.route_path('my_account_auth_tokens'))
@LoginRequired()
@NotAnonymous()
@view_config(
route_name='my_account_emails', request_method='GET',
renderer='rhodecode:templates/admin/my_account/my_account.mako')
def my_account_emails(self):
_ = self.request.translate
c = self.load_default_context()
c.active = 'emails'
c.user_email_map = UserEmailMap.query()\
.filter(UserEmailMap.user == c.user).all()
return self._get_template_context(c)
@LoginRequired()
@NotAnonymous()
@CSRFRequired()
@view_config(
route_name='my_account_emails_add', request_method='POST')
def my_account_emails_add(self):
_ = self.request.translate
c = self.load_default_context()
email = self.request.POST.get('new_email')
try:
UserModel().add_extra_email(c.user.user_id, email)
Session().commit()
h.flash(_("Added new email address `%s` for user account") % email,
category='success')
except formencode.Invalid as error:
msg = error.error_dict['email']
h.flash(msg, category='error')
except Exception:
log.exception("Exception in my_account_emails")
h.flash(_('An error occurred during email saving'),
category='error')
return HTTPFound(h.route_path('my_account_emails'))
@LoginRequired()
@NotAnonymous()
@CSRFRequired()
@view_config(
route_name='my_account_emails_delete', request_method='POST')
def my_account_emails_delete(self):
_ = self.request.translate
c = self.load_default_context()
del_email_id = self.request.POST.get('del_email_id')
if del_email_id:
UserModel().delete_extra_email(
c.user.user_id, del_email_id)
Session().commit()
h.flash(_("Email successfully deleted"),
category='success')
return HTTPFound(h.route_path('my_account_emails'))
@LoginRequired()
@NotAnonymous()
@CSRFRequired()

View file

@ -497,13 +497,6 @@ def make_map(config):
m.connect('my_account_perms', '/my_account/perms',
action='my_account_perms', conditions={'method': ['GET']})
m.connect('my_account_emails', '/my_account/emails',
action='my_account_emails', conditions={'method': ['GET']})
m.connect('my_account_emails', '/my_account/emails',
action='my_account_emails_add', conditions={'method': ['POST']})
m.connect('my_account_emails', '/my_account/emails',
action='my_account_emails_delete', conditions={'method': ['DELETE']})
m.connect('my_account_notifications', '/my_account/notifications',
action='my_notifications',
conditions={'method': ['GET']})

View file

@ -200,42 +200,6 @@ class MyAccountController(BaseController):
return render('admin/my_account/my_account.mako')
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.mako')
@auth.CSRFRequired()
def my_account_emails_add(self):
email = request.POST.get('new_email')
try:
UserModel().add_extra_email(c.rhodecode_user.user_id, email)
Session().commit()
h.flash(_("Added new email address `%s` for user account") % email,
category='success')
except formencode.Invalid as error:
msg = error.error_dict['email']
h.flash(msg, category='error')
except Exception:
log.exception("Exception in my_account_emails")
h.flash(_('An error occurred during email saving'),
category='error')
return redirect(url('my_account_emails'))
@auth.CSRFRequired()
def my_account_emails_delete(self):
email_id = request.POST.get('del_email_id')
user_model = UserModel()
user_model.delete_extra_email(c.rhodecode_user.user_id, email_id)
Session().commit()
h.flash(_("Removed email address from user account"),
category='success')
return redirect(url('my_account_emails'))
def _extract_ordering(self, request):
column_index = safe_int(request.GET.get('order[0][column]'))
order_dir = request.GET.get('order[0][dir]', 'desc')

View file

@ -34,7 +34,7 @@
% if my_account_oauth_url:
<li class="${'active' if c.active=='oauth' else ''}"><a href="${my_account_oauth_url}">${_('OAuth Identities')}</a></li>
% endif
<li class="${'active' if c.active=='emails' else ''}"><a href="${h.url('my_account_emails')}">${_('Emails')}</a></li>
<li class="${'active' if c.active=='emails' else ''}"><a href="${h.route_path('my_account_emails')}">${_('Emails')}</a></li>
<li class="${'active' if c.active=='repos' else ''}"><a href="${h.url('my_account_repos')}">${_('Repositories')}</a></li>
<li class="${'active' if c.active=='watched' else ''}"><a href="${h.url('my_account_watched')}">${_('Watched')}</a></li>
<li class="${'active' if c.active=='pullrequests' else ''}"><a href="${h.url('my_account_pullrequests')}">${_('Pull Requests')}</a></li>

View file

@ -25,10 +25,10 @@
<span class="user email">${em.email}</span>
</td>
<td class="td-action">
${h.secure_form(url('my_account_emails'),method='delete')}
${h.secure_form(h.route_path('my_account_emails_delete'), method='POST')}
${h.hidden('del_email_id',em.email_id)}
<button class="btn btn-link btn-danger" type="submit" id="remove_email_%s" % em.email_id
onclick="return confirm('${_('Confirm to delete this email: %s') % em.email}');">
<button class="btn btn-link btn-danger" type="submit" id="${'remove_email_%s'.format(em.email_id)}"
onclick="return confirm('${_('Confirm to delete this email: {}').format(em.email)}');">
${_('Delete')}
</button>
${h.end_form()}
@ -48,7 +48,7 @@
</div>
<div>
${h.secure_form(url('my_account_emails'), method='post')}
${h.secure_form(h.route_path('my_account_emails_add'), method='POST')}
<div class="form">
<!-- fields -->
<div class="fields">

View file

@ -23,8 +23,7 @@ import pytest
from rhodecode.lib import helpers as h
from rhodecode.model.db import User, UserFollowing, Repository
from rhodecode.tests import (
TestController, url, TEST_USER_ADMIN_LOGIN, TEST_USER_REGULAR_EMAIL,
assert_session_flash)
TestController, url, TEST_USER_ADMIN_LOGIN, assert_session_flash)
from rhodecode.tests.fixture import Fixture
from rhodecode.tests.utils import AssertResponse
@ -90,56 +89,7 @@ class TestMyAccountController(TestController):
response.mustcontain('"name_raw": %s' % pr.pull_request_id)
response.mustcontain('TestMyAccountPR')
def test_my_account_my_emails(self):
self.log_user()
response = self.app.get(url('my_account_emails'))
response.mustcontain('No additional emails specified')
def test_my_account_my_emails_add_existing_email(self):
self.log_user()
response = self.app.get(url('my_account_emails'))
response.mustcontain('No additional emails specified')
response = self.app.post(url('my_account_emails'),
{'new_email': TEST_USER_REGULAR_EMAIL,
'csrf_token': self.csrf_token})
assert_session_flash(response, 'This e-mail address is already taken')
def test_my_account_my_emails_add_mising_email_in_form(self):
self.log_user()
response = self.app.get(url('my_account_emails'))
response.mustcontain('No additional emails specified')
response = self.app.post(url('my_account_emails'),
{'csrf_token': self.csrf_token})
assert_session_flash(response, 'Please enter an email address')
def test_my_account_my_emails_add_remove(self):
self.log_user()
response = self.app.get(url('my_account_emails'))
response.mustcontain('No additional emails specified')
response = self.app.post(url('my_account_emails'),
{'new_email': 'foo@barz.com',
'csrf_token': self.csrf_token})
response = self.app.get(url('my_account_emails'))
from rhodecode.model.db import UserEmailMap
email_id = UserEmailMap.query().filter(
UserEmailMap.user == User.get_by_username(
TEST_USER_ADMIN_LOGIN)).filter(
UserEmailMap.email == 'foo@barz.com').one().email_id
response.mustcontain('foo@barz.com')
response.mustcontain('<input id="del_email_id" name="del_email_id" '
'type="hidden" value="%s" />' % email_id)
response = self.app.post(
url('my_account_emails'), {
'del_email_id': email_id, '_method': 'delete',
'csrf_token': self.csrf_token})
assert_session_flash(response, 'Removed email address from user account')
response = self.app.get(url('my_account_emails'))
response.mustcontain('No additional emails specified')
@pytest.mark.parametrize(
"name, attrs", [