def from_session(session_id: str) -> Optional[User]: """Return a User from the session, if it exists.""" coll = user_collection() try: return User.from_dict( next(coll.find({"multinet.session": session_id}, limit=1))) except StopIteration: return None
def get(sub: str) -> Optional[Dict]: """Return the respective user document if it exists, or None.""" coll = user_collection() try: doc = next(coll.find({"sub": sub}, limit=1)) except StopIteration: return None return doc
def save(self) -> None: """Save this user into the user collection.""" coll = user_collection() user_as_dict = self.asdict() doc = User.get(self.sub) if doc: dict_to_save = {**doc, **user_as_dict} coll.update(dict_to_save) else: coll.insert(user_as_dict)
def search(query: str) -> Cursor: """Search for users given a partial string.""" coll = user_collection() aql = system_db().aql bind_vars = {"@users": coll.name, "query": query} query = """ FOR doc in @@users FILTER CONTAINS(LOWER(doc.name), LOWER(@query)) OR CONTAINS(LOWER(doc.email), LOWER(@query)) LIMIT 50 RETURN doc """ return _run_aql_query(aql, query, bind_vars)
def delete(self) -> None: """Delete this user from the database.""" coll = user_collection() doc = User.get(self.sub) if doc: coll.delete(doc["_id"])