Exemplo n.º 1
0
    def get_full_user_info(user_id):
        """Return user information for the given user_id."""
        header_list = {'Content-Type': 'application/json'}
        user_info_request = requests.get(url='{0}/userinfo/by_id/{1}'.format(
            get_config().get('metadata', 'endpoint_url'), user_id),
                                         headers=header_list)
        if user_info_request.status_code != 200:
            return None

        user_info = user_info_request.json()

        # get available projects for this user
        proj_list = QueryBase._get_user_lookups(
            '{0}/projectinfo/by_user_id/{1}'.format(
                get_config().get('metadata', 'endpoint_url'), user_id),
            header_list)
        # get available instruments for this user
        inst_list = QueryBase._get_user_lookups(
            url='{0}/instrumentinfo/by_user_id/{1}'.format(
                get_config().get('metadata', 'endpoint_url'), user_id),
            header_list=header_list)
        user_info['project_list'] = proj_list
        user_info['instrument_list'] = inst_list

        return user_info
Exemplo n.º 2
0
    def _get_projects_for_keywords(self, user_id, search_terms=None):
        """Return a list with all the projects involving this user."""
        is_admin = self._is_admin(user_id) if user_id is not None else False
        if not search_terms and user_id:
            # no terms, just get any projects for this user
            md_url = '{0}/projectinfo/by_user_id/{1}'.format(
                get_config().get('metadata', 'endpoint_url'), user_id)
        else:
            md_url = '{0}/projectinfo/search/{1}'.format(
                get_config().get('metadata', 'endpoint_url'), search_terms)

        results = []

        response = requests.get(url=md_url)
        if int(response.status_code / 100) == 2:
            if is_admin:
                results = response.json()
            else:
                available_projects = self._get_available_projects(user_id)
                results = [
                    x for x in response.json()
                    if x.get('id') in available_projects
                ]

        return results
Exemplo n.º 3
0
    def _get_projects_for_user(user_id=None):
        """Return a list with all the projects involving this user."""
        md_url = '{0}/projectinfo/by_user_id/{1}'.format(
            get_config().get('metadata', 'endpoint_url'), user_id)
        response = requests.get(url=md_url)

        return response.json()
Exemplo n.º 4
0
    def _get_transaction_list_summary(
            time_basis, object_list, object_type, start_date, end_date, user_id
    ):
        header_list = {'Content-Type': 'application/json'}
        url = '{0}/summaryinfo/by_date/'.format(
            get_config().get('metadata', 'endpoint_url')
        )
        url += '{0}/{1}/{2}/{3}'.format(
            time_basis, object_type, start_date, end_date
        )
        req = requests.post(
            url=url,
            json=object_list,
            headers=header_list
        )
        user_info = QueryBase._merge_two_dicts(
            QueryBase.base_user_info,
            QueryBase.get_full_user_info(user_id),
        )

        req_json = req.json()

        types_to_check = ['instrument', 'project']

        for entry_type in types_to_check:
            req_json['summary_totals']['upload_stats'][entry_type] = TransactionSummary._cleanup_object_stats(
                object_listing=req_json['summary_totals']['upload_stats'][entry_type],
                object_type=entry_type,
                user_info=user_info
            )

        return req_json
Exemplo n.º 5
0
    def _get_transactions_for_keywords(kwargs, option=None):
        """Return a list with all the projects involving this user."""
        md_url = '{0}/transactioninfo/search/{1}'.format(
            get_config().get('metadata', 'endpoint_url'), option)
        response = requests.get(url=md_url, params=kwargs)

        return response.json()
Exemplo n.º 6
0
 def test_meta_connect(self):
     """Test the try meta connect method."""
     httpretty.register_uri(httpretty.GET,
                            get_config().get('metadata', 'status_url'),
                            body=dumps([]),
                            content_type='application/json')
     Root.try_meta_connect(0)
     self.assertEqual(httpretty.last_request().method, 'GET')
Exemplo n.º 7
0
 def _get_instruments_for_project(project_id):
     """Return a list with all the instruments belonging to this project."""
     md_url = u'{0}/projectinfo/by_project_id/{1}'.format(
         get_config().get('metadata', 'endpoint_url'), project_id
     )
     query = requests.get(url=md_url)
     if query.status_code == 200:
         return query.json()
     raise HTTPError('404 Not Found')
Exemplo n.º 8
0
 def _get_instruments_for_keywords(self, user_id, search_terms=''):
     """Return a list with all the instruments having this term."""
     # is_admin = self._is_admin(user_id)
     inst_list_url = '{0}/instrumentinfo/search/{1}'.format(
         get_config().get('metadata', 'endpoint_url'), search_terms)
     inst_query = requests.get(url=inst_list_url)
     if inst_query.status_code == 200:
         inst_response = loads(inst_query.text)
         return self._clean_up_instrument_list(inst_response, user_id)
     raise HTTPError(inst_query.status_code)
Exemplo n.º 9
0
class QueryBase(AdminPolicy):
    """This pulls the common bits of instrument and project query into a single class."""

    md_url = get_config().get('metadata', 'endpoint_url')
    all_instruments_url = '{0}/instruments'.format(md_url)
    all_projects_url = '{0}/projects'.format(md_url)
    all_transactions_url = '{0}/transactions'.format(md_url)

    def _get_available_projects(self, user_id):
        return self._projects_for_user(user_id)
Exemplo n.º 10
0
 def _get_users_for_keywords(kwargs, search_terms=None, option=None):
     """Return a list with all the projects involving this user."""
     md_url = '{0}/userinfo/search/{1}'.format(
         get_config().get('metadata', 'endpoint_url'), search_terms)
     if option is not None:
         md_url += '/{0}'.format(option)
     response = requests.get(url=md_url, params=kwargs)
     retval = response.json()
     if response.status_code == 200:
         return retval
     raise HTTPError(response.status_code)
class TestUploader(TestCase):
    """Test the uploader policy with httpretty."""

    sample_user_id = 23
    admin_user_id = 45
    admin_group_id = 127
    user_group_json = [
        {
            'group': admin_group_id,
            'person': admin_user_id
        }
    ]
    admin_group_json = [
        {
            '_id': admin_group_id
        }
    ]
    group_url = '{0}/groups'.format(
        get_config().get('metadata', 'endpoint_url')
    )
    user_group_url = '{0}/user_group'.format(
        get_config().get('metadata', 'endpoint_url')
    )

    @httpretty.activate
    def test_failed_admin_id(self):
        """check failed admin id fallback works."""
        httpretty.register_uri(httpretty.GET, self.group_url,
                               body=dumps([]),
                               content_type='application/json')
        httpretty.register_uri(httpretty.GET, self.user_group_url,
                               body=dumps([]),
                               content_type='application/json')
        upolicy = UploaderPolicy()
        # pylint: disable=no-member
        # pylint: disable=protected-access
        ret = upolicy._is_admin(10)
        # pylint: enable=protected-access
        # pylint: enable=no-member
        self.assertFalse(ret)
Exemplo n.º 12
0
    def _clean_up_instrument_list(self, inst_response, user_id):
        """Clear out entries that done belong to this user."""
        is_admin = self._is_admin(user_id)
        output_list = []
        if not is_admin:
            inst_for_user_url = '{0}/instrumentinfo/by_user_id/{1}'.format(
                get_config().get('metadata', 'endpoint_url'), user_id)
            query = requests.get(url=inst_for_user_url)
            response = loads(query.text)
            output_list = InstrumentKeywordSearch._squash_output_list(
                response, inst_response)
        else:
            output_list = inst_response

        return output_list
Exemplo n.º 13
0
 def try_meta_connect(cls, attempts=0):
     """Try to connect to the metadata service see if its there."""
     try:
         ret = requests.get(get_config().get('metadata', 'status_url'))
         if ret.status_code != 200:
             raise Exception('try_meta_connect: {0}\n'.format(
                 ret.status_code))
     # pylint: disable=broad-except
     except Exception:
         # pylint: enable=broad-except
         if attempts < METADATA_CONNECT_ATTEMPTS:
             sleep(METADATA_WAIT)
             attempts += 1
             cls.try_meta_connect(attempts)
         else:
             raise Exception
Exemplo n.º 14
0
 def test_meta_connect_failure(self):
     """Test the try meta connect method but fail."""
     httpretty.register_uri(httpretty.GET,
                            get_config().get('metadata', 'status_url'),
                            body='',
                            status=500,
                            content_type='application/json')
     hit_exception = False
     try:
         Root.try_meta_connect(39)
     # pylint: disable=broad-except
     except Exception:
         hit_exception = True
     # pylint: enable=broad-except
     self.assertTrue(hit_exception)
     self.assertEqual(httpretty.last_request().method, 'GET')
Exemplo n.º 15
0
    def _get_transaction_list_details(transaction_list, user_id):
        header_list = {'Content-Type': 'application/json'}
        req = requests.post(url='{0}/summaryinfo/transaction_details'.format(
            get_config().get('metadata', 'endpoint_url')),
                            json=transaction_list,
                            headers=header_list)

        user_info = QueryBase._merge_two_dicts(
            QueryBase.base_user_info, QueryBase.get_full_user_info(user_id))

        results = {}
        req_json = req.json()
        for transaction_id in req_json.keys():
            info = req_json[transaction_id]
            if user_info['emsl_employee'] or (
                    info['project_id'] in user_info['project_list']
                    and info['instrument_id'] in user_info['instrument_list']):
                results[transaction_id] = info
        return results
Exemplo n.º 16
0
 def _get_file_list(transaction_id=None):
     """Return files for the specified transaction entry."""
     filelist_url = '{0}/transactioninfo/files/{1}'.format(
         get_config().get('metadata', 'endpoint_url'), transaction_id)
     return requests.get(url=filelist_url).json()
Exemplo n.º 17
0
 def _get_projects_details(project_id=None):
     """Return a details about this project."""
     lookup_url = u'{0}/projectinfo/by_project_id/{1}'.format(
         get_config().get('metadata', 'endpoint_url'), project_id)
     return requests.get(url=lookup_url).json()
Exemplo n.º 18
0
 def _get_transaction_details(transaction_id=None):
     """Return details for the specified transaction entry."""
     md_url = '{0}/transactioninfo/by_id/{1}'.format(
         get_config().get('metadata', 'endpoint_url'), transaction_id)
     return requests.get(url=md_url).json()
Exemplo n.º 19
0
 def _get_user_info(user_id):
     """Return detailed info about a given user."""
     lookup_url = '{0}/userinfo/by_id/{1}'.format(
         get_config().get('metadata', 'endpoint_url'), user_id
     )
     return requests.get(url=lookup_url).json()