예제 #1
0
def create_source(account_id, body, token_info):
    _validate_account_access(token_info, account_id)

    with Transaction() as t:
        source_repo = SourceRepo(t)
        source_id = str(uuid.uuid4())
        name = body["source_name"]
        source_type = body['source_type']

        if source_type == Source.SOURCE_TYPE_HUMAN:
            # TODO: Unfortunately, humans require a lot of special handling,
            #  and we started mixing Source calls used for transforming to/
            #  from the database with source calls to/from the api.
            #  Would be nice to split this out better.
            source_info = HumanInfo.from_dict(body,
                                              consent_date=date.today(),
                                              date_revoked=None)
            # the "legacy" value of the age_range enum is not valid to use when
            # creating a new source, so do not allow that.
            # NB: Not necessary to do this check when updating a source as
            # only source name and description (not age_range) may be updated.
            if source_info.age_range == "legacy":
                raise RepoException("Age range may not be set to legacy.")
        else:
            source_info = NonHumanInfo.from_dict(body)

        new_source = Source(source_id,
                            account_id,
                            source_type,
                            name,
                            source_info)
        source_repo.create_source(new_source)

        # Must pull from db to get creation_time, update_time
        s = source_repo.get_source(account_id, new_source.id)
        t.commit()

    response = jsonify(s.to_api())
    response.status_code = 201
    response.headers['Location'] = '/api/accounts/%s/sources/%s' % \
                                   (account_id, source_id)
    return response
def create_dummy_source(name, source_type, content_dict, create_dummy_1=True,
                        iss=ACCT_MOCK_ISS,
                        sub=ACCT_MOCK_SUB):
    with Transaction() as t:
        dummy_source_id = SOURCE_ID_1
        dummy_acct_id = _create_dummy_acct_from_t(t, create_dummy_1, iss, sub)
        source_repo = SourceRepo(t)
        if source_type == Source.SOURCE_TYPE_HUMAN:
            dummy_info_obj = HumanInfo.from_dict(content_dict,
                                                 DUMMY_CONSENT_DATE,
                                                 None)
        else:
            dummy_info_obj = NonHumanInfo.from_dict(content_dict)

        source_repo.create_source(Source(dummy_source_id, dummy_acct_id,
                                         source_type, name,
                                         dummy_info_obj))
        t.commit()

    return dummy_acct_id, dummy_source_id
예제 #3
0
def _row_to_source(r):
    row_to_obj = row_to_obj_funcs_by_type[r['source_type']]
    return Source(r[0], r[1], r[2], r[3], row_to_obj(r))
예제 #4
0
import unittest

import datetime

from microsetta_private_api.exceptions import RepoException

from microsetta_private_api.repo.transaction import Transaction
from microsetta_private_api.repo.source_repo import SourceRepo
from microsetta_private_api.model.source import Source, HumanInfo

ACCOUNT_ID = '607f6723-c704-4b52-bc26-556a9aec85f6'
HUMAN_SOURCE = Source(
    'ffffffff-ffff-cccc-aaaa-aaaaaaaaaaaa', ACCOUNT_ID,
    Source.SOURCE_TYPE_HUMAN, 'test person',
    HumanInfo('*****@*****.**', False, None, None, None, datetime.datetime.now(),
              None, None, '18-plus'))


class SourceRepoTests(unittest.TestCase):
    def setUp(self):
        with Transaction() as t:
            sr = SourceRepo(t)
            sr.create_source(HUMAN_SOURCE)
            t.commit()

    def tearDown(self):
        with Transaction() as t:
            sr = SourceRepo(t)
            sr.delete_source(HUMAN_SOURCE.account_id, HUMAN_SOURCE.id)
            t.commit()