def delete(self): """Deletes the enclosed object in the database, if it existed.""" if self.obj is None: return None old_object = self.obj.copy() DB.delete_one({'_id': self.get_id()}, self.collection_name) return old_object
def check_already_exists(self): """Checks if the user already exists in the database.""" existing_user = DB.fetch_one({'googleId': self.obj['googleId']}, self.collection_name) if existing_user is not None: raise ClientError(Response(409, 'User already exists.'))
def get_all_authorizations(self): """Get all user IDs having access to this project.""" return [ str(user['_id']) for user in DB.fetch_all({'authorizations': { 'projectId': self.obj['_id'] }}, User.collection_name) ]
def from_id(cls, _id): """Fetches the document with given ID from the collection.""" if isinstance(_id, str) and len(_id) == 24: _id = ObjectId(_id) elif not isinstance(_id, ObjectId): return cls(None) return cls(DB.fetch_one({'_id': _id}, cls.collection_name))
def GET(request): """Return all prefabs the user is authorized to access""" user = User.from_google_id(request.google_id) user.check_exists() own_prefabs = DB.fetch_all({'authorId': user.get_id()}, Prefab.collection_name) public_prefabs = DB.fetch_all({'visibility': 'public'}, Prefab.collection_name) authorizations = {"authorizations": []} authorizations["authorizations"].append(own_prefabs) authorizations["authorizations"].append(public_prefabs) return Response(200, 'Successfully fetched authorizations.', authorizations)
def from_google_id(cls, google_id): """Fetches the user with given Google ID from the collection.""" return User(DB.fetch_one({'googleId': google_id}, User.collection_name))
def from_email(cls, email): """Fetches the user with given email from the collection.""" return User(DB.fetch_one({'email': email}, User.collection_name))
def update(self): """Updates the enclosed object and updates the internal reference to the newly inserted object.""" DB.update(self.get_id(), self.obj, self.collection_name)
def insert(self): """Inserts the enclosed object and generates a UUID for it.""" self.obj['_id'] = ObjectId() DB.insert(self.obj, self.collection_name)
def get_all(cls): """Fetches all documents from the collection.""" return cls(DB.fetch_all({}, cls.collection_name))