def update_created_document(collection: str, document_id: str): # pragma: no cover now = datetime.now() update_document(collection, document_id, { CREATED_KEY: now, MODIFIED_KEY: now })
def update_last_observation( individual_id: str, phase: str, observation_date: datetime ) -> bool: individual = get_individual(individual_id) old_observation_date = individual.get( "last_observation_date", datetime.min.replace(tzinfo=timezone.utc) ) if observation_date > old_observation_date: data = {"last_observation_date": observation_date} if individual.get("type") == "individual": data["last_phenophase"] = phase update_document("individuals", individual_id, data) log.info( "updated last observation for %s (%s -> %s)", individual_id, old_observation_date, observation_date, ) return True else: log.info( "no update for last observation for %s (%s > %s)", individual_id, old_observation_date, observation_date, ) return False
def clear_resend(doc_id: str) -> None: f.update_document( INVITE_COLLECTION, doc_id, { "resend": f.DELETE_FIELD, }, )
def test_update_document(collection, doc_id, doc, doc2): assert doc != doc2 f.write_document(collection, doc_id, doc) f.update_document(collection, doc_id, doc2) result = f.get_document(collection, doc_id) assert result != doc assert result != doc2 doc.update(doc2) assert result == doc
def follow_user(follower_id: str, followee_id: str) -> bool: user = get_user(follower_id) if not user: raise ValueError("User not found %s" % follower_id) if followee_id not in user.get("following_users", []): update_document("users", follower_id, {"following_users": ArrayUnion([followee_id])}) return True else: return False
def update_documents(doc_id: str, to_mail: str) -> None: f.update_document( INVITE_COLLECTION, doc_id, { "sent": f.SERVER_TIMESTAMP, "numsent": f.Increment(1), "resend": f.DELETE_FIELD, }, ) f.write_document(LOOKUP_COLLECTION, to_mail, {"invites": f.ArrayUnion([doc_id])}, merge=True)
def change_nickname(user_id: str, nickname: str) -> None: for invite_id in get_invite_ids(user_id): log.info( "Change users nickname for %s to %s on invite %s", user_id, nickname, invite_id, ) f.update_document( INVITE_COLLECTION, invite_id, { "register_nick": nickname, }, )
def register_user_invite(invite_id: str, user_id: str, nickname: str) -> None: """ Register an user on a specific invite. """ log.info("Register user %s (%s) on invite %s", user_id, nickname, invite_id) f.update_document( INVITE_COLLECTION, invite_id, { "register_user": user_id, "register_nick": nickname, "register_date": f.SERVER_TIMESTAMP, }, ) inviter_id = f.get_document(INVITE_COLLECTION, invite_id)["user"] d.follow_user(inviter_id, user_id)
def test_update_document__non_existing(collection, doc_id, doc, doc2): assert doc != doc2 try: f.update_document(collection, doc_id, doc2) except google.api_core.exceptions.NotFound: pass # expected
def update_observation(observation_id: str, data: dict) -> None: update_document("observations", observation_id, data)
def update_individual(individual_id: str, data: dict) -> None: update_document("individuals", individual_id, data)
def update_phenoyear(year: int) -> None: update_document("definitions", "config_dynamic", {"phenoyear": year})
def process_update_nickname(user_id: str, nickname_old: str, nickname_new: str): write_document("nicknames", nickname_new, {"user": user_id}) update_document("public_users", user_id, {"nickname": nickname_new}) delete_document("nicknames", nickname_old)
def update_modified_document(collection: str, document_id: str): # pragma: no cover update_document(collection, document_id, {MODIFIED_KEY: datetime.now()})