Show current company instead of the latest

Change-Id: I8c2116f2906e03c539fa21450b0b02201b5fd7aa
Closes-Bug: #1495517
This commit is contained in:
Ilya Shakhat 2015-09-14 17:07:05 +03:00
parent 67214ed795
commit 28178f11c4
3 changed files with 108 additions and 3 deletions

View File

@ -16,6 +16,7 @@
import datetime
import operator
import re
import time
import six
@ -103,13 +104,22 @@ def extend_record(record):
return record
def get_current_company(user):
now = time.time()
idx = -1
for i, r in enumerate(user['companies']):
if now < r['end_date']:
idx = i
return user['companies'][idx]['company_name']
def extend_user(user):
user = user.copy()
user['id'] = user['user_id']
user['text'] = user['user_name']
if user['companies']:
company_name = user['companies'][-1]['company_name']
company_name = get_current_company(user)
user['company_link'] = make_link(
company_name, '/', {'company': company_name, 'user_id': ''})
else:

View File

@ -194,7 +194,7 @@ def get_engineers_extended(records, **kwargs):
return
user = vault.get_user_from_runtime_storage(record['id'])
record['company'] = user['companies'][-1]['company_name']
record['company'] = helpers.get_current_company(user)
record['core'] = get_core_engineer_branch(user, modules)
return record
@ -434,7 +434,7 @@ def get_users_json(record_ids, **kwargs):
if core_modules:
r['core'] = core_modules
if user['companies']:
r['company_name'] = user['companies'][-1]['company_name']
r['company_name'] = helpers.get_current_company(user)
add_flag = True
if add_flag:
result.append(r)

View File

@ -0,0 +1,95 @@
# Copyright (c) 2015 Mirantis Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import mock
import testtools
from stackalytics.dashboard import helpers
class TestHelpers(testtools.TestCase):
@mock.patch('time.time')
def test_get_current_company(self, mock_time_time):
current_timestamp = 1234567890
mock_time_time.return_value = current_timestamp
user = {
'user_id': 'smith',
'user_name': 'John Smith',
'companies': [{
'company_name': 'Current',
'end_date': current_timestamp + 1
}, {
'company_name': 'TheCompany',
'end_date': 0
}]
}
self.assertEqual('Current', helpers.get_current_company(user))
@mock.patch('stackalytics.dashboard.helpers.make_link')
def test_extend_user(self, mock_make_link):
company_link = mock.Mock()
mock_make_link.return_value = company_link
user = {
'user_id': 'smith',
'user_name': 'John Smith',
'companies': [{
'company_name': 'TheCompany',
'end_date': 0
}]
}
expected = {
'user_id': 'smith',
'user_name': 'John Smith',
'companies': [{
'company_name': 'TheCompany',
'end_date': 0
}],
'id': 'smith',
'company_link': company_link,
'text': 'John Smith',
}
observed = helpers.extend_user(user)
self.assertEqual(expected, observed)
mock_make_link.assert_called_once_with('TheCompany', '/', mock.ANY)
@mock.patch('time.time')
@mock.patch('stackalytics.dashboard.helpers.make_link')
def test_extend_user_current_company(self, mock_make_link, mock_time_time):
company_link = mock.Mock()
mock_make_link.return_value = company_link
current_timestamp = 1234567890
mock_time_time.return_value = current_timestamp
user = {
'user_id': 'smith',
'user_name': 'John Smith',
'companies': [{
'company_name': 'Current',
'end_date': current_timestamp + 1
}, {
'company_name': 'TheCompany',
'end_date': 0
}]
}
helpers.extend_user(user)
mock_make_link.assert_called_once_with('Current', '/', mock.ANY)