示例#1
0
 async def create(self, user: UD) -> UD:
     oauth_accounts = getattr(user, "oauth_accounts", [])
     model = await self.model(**user.dict(exclude={"oauth_accounts"})).save()
     if oauth_accounts and self.oauth_account_model:
         await self._create_oauth_models(model=model, oauth_accounts=oauth_accounts)
     user_db = await self._get_user(id=user.id)
     return cast(UD, user_db)
示例#2
0
 async def _update(self, user: models.UD, update_dict: Dict[str, Any]) -> models.UD:
     for field, value in update_dict.items():
         if field == "email" and value != user.email:
             try:
                 await self.get_by_email(value)
                 raise UserAlreadyExists()
             except UserNotExists:
                 user.email = value
                 user.is_verified = False
         elif field == "password":
             await self.validate_password(value, user)
             hashed_password = get_password_hash(value)
             user.hashed_password = hashed_password
         else:
             setattr(user, field, value)
     return await self.user_db.update(user)
示例#3
0
    async def update(self, user: UD) -> UD:
        user_dict = user.dict()

        if "oauth_accounts" in user_dict:
            if self.oauth_accounts is None:
                raise NotSetOAuthAccountTableError()

            query = self.oauth_accounts.delete().where(
                self.oauth_accounts.c.user_id == user.id)
            await self.database.execute(query)

            oauth_accounts_values = []
            oauth_accounts = user_dict.pop("oauth_accounts")
            for oauth_account in oauth_accounts:
                oauth_accounts_values.append({
                    "user_id": user.id,
                    **oauth_account
                })

            query = self.oauth_accounts.insert()
            await self.database.execute_many(query, oauth_accounts_values)

        query = self.users.update().where(
            self.users.c.id == user.id).values(user_dict)
        await self.database.execute(query)
        return user
示例#4
0
文件: graph.py 项目: IBM/arcade
    def update(self, user: UD) -> UD:
        """Updates a user node with the data in the provided pydantic model

        :param user: pydantic model containing the user's data
        """
        user = User.create_or_update(user.dict())
        return user
示例#5
0
 async def create(self, user: UD) -> UD:
     oauth_accounts = getattr(user, "oauth_accounts", [])
     model = await self.model(**user.dict(exclude={"oauth_accounts"})
                              ).save()
     if oauth_accounts and self.oauth_account_model:
         await self.create_oauth_models(model=model,
                                        oauth_accounts=oauth_accounts)
     return user
示例#6
0
 async def update(self, user: UD) -> UD:
     oauth_accounts = getattr(user, "oauth_accounts", [])
     model = await self._get_db_user(id=user.id)
     await model.update(**user.dict(exclude={"oauth_accounts"}))
     if oauth_accounts and self.oauth_account_model:
         await model.oauth_accounts.clear(keep_reversed=False)
         await self._create_oauth_models(model=model, oauth_accounts=oauth_accounts)
     user_db = await self._get_user(id=user.id)
     return cast(UD, user_db)
示例#7
0
文件: graph.py 项目: IBM/arcade
    def create(self, user: UD) -> UD:
        """Creates a user node using the data in the pydantic model.

        :param user: pydantic model containing the user's data
        """
        user_dict = user.dict()
        # The fastapi_users library requires that the database record use the
        # `id` property for the user's UUID, however neo4j internally uses the
        # `id` property as an incrementing integer.  Here we store the
        # fastapi_users UUID as the `uid` property and remove the `id` field
        # before storing in neo4j.
        user_dict['uid'] = user_dict.pop('id')
        User(**user_dict).save()
        return user
示例#8
0
    async def update(self, user: UD) -> UD:
        oauth_accounts = getattr(user, "oauth_accounts", [])
        model = await self.get_db_user(id=user.id)
        # have no idea why other backends does not check if user exists?
        # is it some pattern that we have no exception at all?
        if not model:
            raise NoMatch("User with given id does not exist!")
        await model.update(**user.dict(exclude={"oauth_accounts"}))

        if oauth_accounts and self.oauth_account_model:
            # we issued query with select_related so we have oauths if they exist
            await model.oauth_accounts.clear(keep_reversed=False)
            await self.create_oauth_models(model=model,
                                           oauth_accounts=oauth_accounts)
        return user
示例#9
0
    async def create(self, user: UD) -> UD:
        user_dict = user.dict()
        oauth_accounts = user_dict.pop("oauth_accounts", None)

        model = self.model(**user_dict)
        await model.save()

        if oauth_accounts and self.oauth_account_model:
            oauth_account_objects = []
            for oauth_account in oauth_accounts:
                oauth_account_objects.append(
                    self.oauth_account_model(user=model, **oauth_account))
            await self.oauth_account_model.bulk_create(oauth_account_objects)

        return user
示例#10
0
    async def update(self, user: UD) -> UD:
        user_dict = user.dict()
        user_dict.pop("id")  # Tortoise complains if we pass the PK again
        oauth_accounts = user_dict.pop("oauth_accounts", None)

        model = await self.model.get(id=user.id)
        for field in user_dict:
            setattr(model, field, user_dict[field])
        await model.save()

        if oauth_accounts and self.oauth_account_model:
            await model.oauth_accounts.all().delete()
            oauth_account_objects = []
            for oauth_account in oauth_accounts:
                oauth_account_objects.append(
                    self.oauth_account_model(user=model, **oauth_account))
            await self.oauth_account_model.bulk_create(oauth_account_objects)

        return user
示例#11
0
    async def create(self, user: UD) -> UD:
        user_dict = user.dict()
        oauth_accounts_values = None

        if "oauth_accounts" in user_dict:
            oauth_accounts_values = []

            oauth_accounts = user_dict.pop("oauth_accounts")
            for oauth_account in oauth_accounts:
                oauth_accounts_values.append({
                    "user_id": user.id,
                    **oauth_account
                })

        query = self.users.insert()
        await self.database.execute(query, user_dict)

        if oauth_accounts_values is not None:
            if self.oauth_accounts is None:
                raise NotSetOAuthAccountTableError()
            query = self.oauth_accounts.insert()
            await self.database.execute_many(query, oauth_accounts_values)

        return user
示例#12
0
 async def update(self, user: UD) -> UD:
     await self.collection.replace_one({"id": user.id}, user.dict())
     return user
示例#13
0
 async def create(self, user: UD) -> UD:
     await self.collection.insert_one(user.dict())
     return user
示例#14
0
 async def create(self, user: UD) -> UD:
     model = self.model(**user.dict())
     await model.save()
     return user
示例#15
0
 async def update(self, user: UD) -> UD:
     user_dict = user.dict()
     user_dict.pop("id")  # Tortoise complains if we pass the PK again
     await self.model.filter(id=user.id).update(**user_dict)
     return user
示例#16
0
 async def create(self, user: UD) -> UD:
     query = self.users.insert().values(**user.dict())
     await self.database.execute(query)
     return user
示例#17
0
 async def update(self, user: UD) -> UD:
     query = (self.users.update().where(self.users.c.id == user.id).values(
         **user.dict()))
     await self.database.execute(query)
     return user