Example #1
0
def load_animal(a):
    animal = Animal(
        name=a["name"],
        year_of_birth=a["year_of_birth"],
        gender=a["gender"],
        date_of_entry=parse_date(a["date_of_entry"]),
        species=a["species"],
        breed=a["breed"],
    )
    for f in a["fostering"]:
        p = f["parent"]
        parent = FosterParent(
            name=p["name"],
            address=p["address"],
            phone_number=p["phone"],
            max_animals=None
        )
        foster = Foster(
            parent=parent,
            animal=animal,
            start_date=parse_date(f["start"]),
            end_date=parse_date(f.get("end")),
        )
        animal.fosters.append(foster)
        parent.fosters.append(foster)
    for e in a["exams"]:
        exam = Exam(
            vet=e["vet"],
            date=parse_date(e["date"]),
            report=e["report"],
        )
        animal.exams.append(exam)
    adopted = a.get("adopted")
    if adopted is not None:
        adoption = Adoption(
            date=parse_date(adopted["date"]),
            adopter_name=adopted["name"],
            adopter_address=adopted["address"],
        )
        animal.adoption = adoption
    return animal
Example #2
0
def load(id, *, db):
    shelter = Shelter(id=id)

    foster_parents = {}

    shelter_parents = db.execute(query_select_shelter_parents,
                                 (shelter.id, )).fetchall()
    for (pid, parent_id, max_animals) in shelter_parents:
        (name, address, phone_number) = db.execute(query_select_foster_parent,
                                                   (parent_id, )).fetchone()
        parent = FosterParent(
            id=pid,
            name=name,
            address=address,
            phone_number=phone_number,
            max_animals=max_animals,
        )
        foster_parents[parent.id] = parent
        shelter.foster_parents.append(parent)

    shelter_animals = db.execute(query_select_shelter_animals,
                                 (shelter.id, )).fetchall()
    for (aid, animal_id, date_of_entry) in shelter_animals:
        (name, year_of_birth, gender, species,
         breed) = db.execute(query_select_animal, (animal_id, )).fetchone()
        animal = Animal(
            id=aid,
            name=name,
            year_of_birth=year_of_birth,
            gender=gender,
            date_of_entry=datetime.strptime(date_of_entry, "%Y-%m-%d").date(),
            species=species,
            breed=breed,
        )
        exams = db.execute(query_select_exams, (aid, )).fetchall()
        for (exam_id, vet, date, report) in exams:
            exam = Exam(
                id=exam_id,
                vet=vet,
                date=datetime.strptime(date, "%Y-%m-%d").date(),
                report=report,
            )
            animal.exams.append(exam)

        fosters = db.execute(query_select_fosters, (aid, )).fetchall()

        for (foster_id, parent_id, start_date, end_date) in fosters:
            parent = foster_parents[parent_id]
            foster = Foster(
                id=foster_id,
                parent=parent,
                animal=animal,
                start_date=datetime.strptime(start_date, "%Y-%m-%d").date(),
                end_date=None if end_date == None else datetime.strptime(
                    end_date, "%Y-%m-%d").date(),
            )
            animal.fosters.append(foster)
            parent.fosters.append(foster)

        adoption = db.execute(query_select_adoption, (aid, )).fetchone()
        if adoption is not None:
            (adopter_name, adopter_address, date) = adoption
            animal.adoption = Adoption(
                id=aid,
                date=datetime.strptime(date, "%Y-%m-%d").date(),
                adopter_name=adopter_name,
                adopter_address=adopter_address,
            )

        shelter.animals.append(animal)

    return shelter
Example #3
0
def make_exam(*, vet="kek", date=date.today(), report="good"):
    return Exam(
        vet=vet,
        date=date,
        report=report,
    )