예제 #1
0
def create_plant(db: Session, plant: schemas.PlantCreate):
    db_plant = models.Plant(
        **plant.dict(),
        water_every=7,  # TODO: This will come from an API
        last_watering=datetime.datetime.now(),
        journaling_key=uuid.uuid4(),
        alive=True,
    )
    db.add(db_plant)
    db.commit()
    db.refresh(db_plant)
    db_plant.until_next_watering = _calculate_days_until_next_watering(
        db_plant.last_watering,
        db_plant.water_every,
    )
    logger.info("New plant created")
    try:
        Notifications.send(f"A new plant called {db_plant.name} has been created")
    except Exception as err:
        logger.error(f"Notification could not be sent: {str(err)}")
    try:
        Journaling.create(
            db_plant.journaling_key,
            f"A new plant called {db_plant.name} has been created",
        )
    except Exception as err:
        logger.error(f"Could not add journal entry: {str(err)}")
    return db_plant
예제 #2
0
 def test_creating_a_journal_entry(self, m_post):
     key = uuid.uuid4()
     message = "Test message"
     Journaling.create(key, message)
     m_post.assert_called_with(
         url="http://journaling-service:80/new",
         json={
             "key": str(key),
             "message": message,
         },
     )
예제 #3
0
def wash(db: Session, garment: models.Garment):
    garment.worn = 0
    garment.washing = False
    db.commit()
    db.refresh(garment)
    logger.info("Washing garment {garment.name}")
    try:
        Journaling.create(
            garment.journaling_key,
            f"Garment {garment.name} has been washed",
        )
    except Exception as err:
        logger.error(f"Could not add journal entry: {str(err)}")
    return garment
예제 #4
0
def kill_plant(db: Session, plant: models.Plant):
    plant.alive = False
    db.commit()
    db.refresh(plant)
    try:
        Notifications.send(f"The plant {plant.name} is now dead")
    except Exception as err:
        logger.error(f"Notification could not be sent: {str(err)}")
    try:
        Journaling.create(plant.journaling_key, f"A The plant {plant.name} has died")
    except Exception as err:
        logger.error(f"Could not add journal entry: {str(err)}")
    logger.info("Plant killed")
    return plant
예제 #5
0
def wear(db: Session, garment: models.Garment):
    garment.worn += 1
    garment.total_worn += 1
    garment.washing = garment.worn >= garment.wear_to_wash
    db.commit()
    db.refresh(garment)
    logger.info("Wearing garment {garment.name}")
    try:
        Journaling.create(
            garment.journaling_key,
            f"Wearing {garment.name}",
        )
    except Exception as err:
        logger.error(f"Could not add journal entry: {str(err)}")
    return garment
예제 #6
0
def post_journal_entry(
        new_entry: schemas.JournalEntryCreate,
        db: Session = Depends(get_db),
        plant_id: int = Path(None, title="The ID for the plant", ge=1),
):
    plant = _get_plant(db, plant_id)
    return Journaling.create(plant.journaling_key, new_entry.message)
예제 #7
0
def get_journal(
    db: Session = Depends(get_db),
    garment_id: int = Path(
        None, title="The ID of the garment to get the journal from", ge=1),
):
    garment = _get_garment(db, garment_id)
    return Journaling.get(garment.journaling_key)
예제 #8
0
def update_garment(
    db: Session, garment_id: int, new_garment_data: schemas.GarmentUpdate
):
    garments = db.query(models.Garment).filter(models.Garment.id == garment_id)
    garments.update(new_garment_data.dict(), synchronize_session=False)
    db.commit()
    garment = garments.first()
    logger.info("Garment updated")
    try:
        Journaling.create(
            garment.journaling_key,
            f"The garment {garment.name} has been updated",
        )
    except Exception as err:
        logger.error(f"Could not add journal entry: {str(err)}")
    return garment
예제 #9
0
def create_garment(db: Session, garment: schemas.GarmentCreate):
    db_garment = models.Garment(
        **garment.dict(),
        journaling_key=uuid.uuid4(),
        worn=0,
        total_worn=0,
        washing=False,
    )
    db.add(db_garment)
    db.commit()
    db.refresh(db_garment)
    logger.info("New garment created")
    try:
        Journaling.create(
            db_garment.journaling_key,
            f"A new garment called {db_garment.name} has been created",
        )
    except Exception as err:
        logger.error(f"Could not add journal entry: {str(err)}")
    return db_garment
예제 #10
0
 def test_retrieving_a_journal_entryset(self, m_get):
     key = uuid.uuid4()
     response_mock = Mock()
     response_mock.text = '{"data": "journaling_data"}'
     response_mock.json = lambda: {"data": "journaling_data"}
     m_get.return_value = response_mock
     response = Journaling.get(key)
     m_get.assert_called_with(
         url=f"http://journaling-service:80/{str(key)}/all",
     )
     assert response["data"] == "journaling_data"
예제 #11
0
def water_plant(db: Session, plant: models.Plant):
    plant.last_watering = datetime.datetime.now()
    db.commit()
    db.refresh(plant)
    try:
        Notifications.send(f"The plant {plant.name} has been watered")
    except Exception as err:
        logger.error(f"Notification could not be sent: {str(err)}")
    try:
        Journaling.create(
            plant.journaling_key, f"A The plant {plant.name} has been watered"
        )
    except Exception as err:
        logger.error(f"Could not add journal entry: {str(err)}")
    logger.info("Plant watered")
    plant.until_next_watering = _calculate_days_until_next_watering(
        plant.last_watering,
        plant.water_every,
    )
    return plant
예제 #12
0
def update_plant(db: Session, plant_id: int, new_plant_data: schemas.PlantUpdate):
    plants = db.query(models.Plant).filter(models.Plant.id == plant_id)
    plants.update(new_plant_data.dict(), synchronize_session=False)
    db.commit()
    plant = plants.first()
    plant.until_next_watering = _calculate_days_until_next_watering(
        plant.last_watering,
        plant.water_every,
    )
    logger.info("Plant updated")
    try:
        Notifications.send(f"The plant {plant.name} has been updated")
    except Exception as err:
        logger.error(f"Notification could not be sent: {str(err)}")
    try:
        Journaling.create(
            plant.journaling_key,
            f"The plant {plant.name} has been updated",
        )
    except Exception as err:
        logger.error(f"Could not add journal entry: {str(err)}")
    return plant