示例#1
0
def delete_source(acct_id, source_id):
    with Transaction() as t:
        source_repo = SourceRepo(t)
        if not source_repo.delete_source(acct_id, source_id):
            return jsonify(error=404, text="No source found"), 404
        # TODO: 422?
        return '', 204
示例#2
0
def read_sample_association(acct_id, source_id, sample_id):
    with Transaction() as t:
        sample_repo = SampleRepo(t)
        sample = sample_repo.get_sample(sample_id)
        if sample is None:
            return jsonify(error=404, text="Sample not found"), 404

        return jsonify(sample)
示例#3
0
def read_answered_surveys(acct_id, source_id):
    # TODO: source_id is participant name until we make the proper schema
    #  changes.  Sorry bout that
    print(acct_id)
    print(source_id)
    with Transaction() as t:
        survey_answers_repo = SurveyAnswersRepo(t)
        return survey_answers_repo.list_answered_surveys(acct_id, source_id)
示例#4
0
def read_account(acct_id):
    # TODO:  Authentication???
    with Transaction() as t:
        acct_repo = AccountRepo(t)
        acc = acct_repo.get_account(acct_id)
        if acc is None:
            # TODO: Think this should be "code", "message" to match api?
            return jsonify(error=404, text="Account not found"), 404
        return jsonify(acc)
示例#5
0
def read_survey_template(acct_id, source_id, survey_template_id, locale_code):
    # TODO: can we get rid of source_id?  I don't have anything useful to do
    #  with it...  I guess I could check if the source is a dog before giving
    #  out a pet information survey?

    with Transaction() as t:
        survey_template_repo = SurveyTemplateRepo(t)
        survey_template = survey_template_repo.get_survey_template(
            survey_template_id)
        return vue_adapter.to_vue_schema(survey_template)
示例#6
0
def read_kit(kit_id, kit_code):
    with Transaction() as t:
        kit_repo = KitRepo(t)
        kit = kit_repo.get_kit(kit_id, kit_code)
        if kit is None:
            return jsonify(error=404, text="No such kit"), 404
        unused = []
        for s in kit.samples:
            if not s.deposited:
                unused.append(s)
        return jsonify(unused)
示例#7
0
def create_source(acct_id, source_info):
    with Transaction() as t:
        source_repo = SourceRepo(t)
        source_id = str(uuid.uuid4())
        new_source = Source.from_json(source_id, acct_id, source_info)
        source_repo.create_source(new_source)
        # Must pull from db to get creation_time, update_time
        s = source_repo.get_source(acct_id, new_source.id)
        t.commit()
        # TODO: What about 404 and 422 errors?
        return jsonify(s)
示例#8
0
def submit_answered_survey(acct_id, source_id, locale_code,
                           survey_template_id, survey_text):
    # TODO: source_id still needs to be participant name til we refactor
    # TODO: Is this supposed to return new survey id?
    # TODO: Rename survey_text to survey_model/model to match Vue's naming?
    with Transaction() as t:
        survey_answers_repo = SurveyAnswersRepo(t)
        return survey_answers_repo.submit_answered_survey(acct_id,
                                                          source_id,
                                                          locale_code,
                                                          survey_template_id,
                                                          survey_text)
示例#9
0
def update_source(acct_id, source_id):
    with Transaction() as t:
        source_repo = SourceRepo(t)
        source = source_repo.get_source(acct_id, source_id)

        # Uhhh, where do I get source_data from???
        # source.source_data = something?
        # Answer: source data is coming in in the request body

        source_repo.update_source_data(source)
        # I wonder if there's some way to get the creation_time/update_time
        # during the insert/update...
        source = source_repo.get_source(acct_id, source_id)
        t.commit()
        # TODO: 404 and 422?
        return jsonify(source)
示例#10
0
def read_survey_templates(acct_id, source_id, locale_code):
    # TODO: I don't think this query is backed by one of the existing tables
    # I think it was just hardcoded...  Which honestly seems like a fine
    # solution to me...  How much do we care that survey identifiers are
    # guessable?

    # TODO: I don't think surveys have names... only survey groups have names.
    # So what can I pass down to the user that will make any sense here?
    with Transaction() as t:
        source_repo = SourceRepo(t)
        source = source_repo.get_source(acct_id, source_id)
        if source.source_type == Source.SOURCE_TYPE_HUMAN:
            return [1, 3, 4, 5]
        elif source.source_type == Source.SOURCE_TYPE_CANINE:
            return [2]
        else:
            return []
示例#11
0
def update_account(acct_id, first_name, last_name, email, address):
    # TODO:  Authentication??
    with Transaction() as t:
        acct_repo = AccountRepo(t)
        acc = acct_repo.get_account(acct_id)
        if acc is None:
            return jsonify(error=404, text="Account not found"), 404

        # TODO: add 422 handling

        acc.first_name = first_name
        acc.last_name = last_name
        acc.email = email
        acc.address = address

        acct_repo.update_account(acc)
        t.commit()
        return jsonify(acc)
import util.vue_adapter

# TODO: Refactor me into proper unit tests!


def json_converter(o):
    if isinstance(o, datetime.datetime):
        return str(o)
    return o.__dict__


ACCT_ID = "aaaaaaaa-bbbb-cccc-dddd-eeeeffffffff"
DOGGY_ID = "dddddddd-dddd-dddd-dddd-dddddddddddd"
PLANTY_ID = "eeeeeeee-eeee-eeee-eeee-eeeeeeeeeeee"

with Transaction() as t:
    kit_repo = KitRepo(t)
    kit = kit_repo.get_kit("eba20873-b88d-33cc-e040-8a80115d392c", "#6á$E")
    print("Kit: ")
    print(json.dumps(kit, default=json_converter, indent=2))

    acct_repo = AccountRepo(t)
    acct_repo.delete_account(ACCT_ID)
    acc = Account(ACCT_ID,
                  "*****@*****.**",
                  "globus",
                  "Dan",
                  "H",
                  '{"a":5, "b":7}',
                  "USER")
    print(acct_repo.create_account(acc))
示例#13
0
def delete_answered_survey(acct_id, source_id, survey_id):
    with Transaction() as t:
        survey_answers_repo = SurveyAnswersRepo(t)
        return survey_answers_repo.delete_answered_survey(acct_id, survey_id)
示例#14
0
def read_answered_survey(acct_id, source_id, survey_id):
    # TODO: Don't need source_id, drop it or include for validation at db
    #  layer?
    with Transaction() as t:
        survey_answers_repo = SurveyAnswersRepo(t)
        return survey_answers_repo.get_answered_survey(acct_id, survey_id)
示例#15
0
def read_source(acct_id, source_id):
    with Transaction() as t:
        source_repo = SourceRepo(t)
        # TODO: What about 404?
        return source_repo.get_source(acct_id, source_id)
示例#16
0
def read_sources(acct_id, source_type):
    with Transaction() as t:
        source_repo = SourceRepo(t)
        # TODO: Also support 404? Or is that not necessary?
        return jsonify(
            source_repo.get_sources_in_account(acct_id, source_type))