Beispiel #1
0
 async def remove(  # type: ignore
     self, item: "Model", keep_reversed: bool = True
 ) -> None:
     if item not in self:
         raise NoMatch(
             f"Object {self._owner.get_name()} has no "
             f"{item.get_name()} with given primary key!"
         )
     super().remove(item)
     relation_name = self.related_field_name
     relation = item._orm._get(relation_name)
     if relation is None:  # pragma nocover
         raise ValueError(
             f"{self._owner.get_name()} does not have relation {relation_name}"
         )
     relation.remove(self._owner)
     self.relation.remove(item)
     if self.type_ == ormar.RelationType.MULTIPLE:
         await self.queryset_proxy.delete_through_instance(item)
     else:
         if keep_reversed:
             setattr(item, relation_name, None)
             await item.update()
         else:
             await item.delete()
Beispiel #2
0
 async def load(self: T) -> T:
     expr = self.Meta.table.select().where(self.pk_column == self.pk)
     row = await self.Meta.database.fetch_one(expr)
     if not row:  # pragma nocover
         raise NoMatch(
             "Instance was deleted from database and cannot be refreshed")
     kwargs = dict(row)
     kwargs = self.translate_aliases_to_columns(kwargs)
     self.from_dict(kwargs)
     self.set_save_status(True)
     return self
Beispiel #3
0
    async def remove(  # type: ignore
        self, item: "T", keep_reversed: bool = True
    ) -> None:
        """
        Removes the related from relation with parent.

        Through models are automatically deleted for m2m relations.

        For reverse FK relations keep_reversed flag marks if the reversed models
        should be kept or deleted from the database too (False means that models
        will be deleted, and not only removed from relation).

        :param item: child to remove from relation
        :type item: Model
        :param keep_reversed: flag if the reversed model should be kept or deleted too
        :type keep_reversed: bool
        """
        if item not in self:
            raise NoMatch(
                f"Object {self._owner.get_name()} has no "
                f"{item.get_name()} with given primary key!"
            )
        await self._owner.signals.pre_relation_remove.send(
            sender=self._owner.__class__,
            instance=self._owner,
            child=item,
            relation_name=self.field_name,
        )
        super().remove(item)
        relation_name = self.related_field_name
        relation = item._orm._get(relation_name)
        if relation is None:  # pragma nocover
            raise ValueError(
                f"{self._owner.get_name()} does not have relation {relation_name}"
            )
        relation.remove(self._owner)
        self.relation.remove(item)
        if self.type_ == ormar.RelationType.MULTIPLE:
            await self.queryset_proxy.delete_through_instance(item)
        else:
            if keep_reversed:
                setattr(item, relation_name, None)
                await item.update()
            else:
                await item.delete()
        await self._owner.signals.post_relation_remove.send(
            sender=self._owner.__class__,
            instance=self._owner,
            child=item,
            relation_name=self.field_name,
        )
Beispiel #4
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
Beispiel #5
0
 async def remove(self, item: "Model") -> None:  # type: ignore
     if item not in self:
         raise NoMatch(
             f"Object {self._owner.get_name()} has no "
             f"{item.get_name()} with given primary key!"
         )
     super().remove(item)
     rel_name = item.resolve_relation_name(item, self._owner)
     relation = item._orm._get(rel_name)
     if relation is None:  # pragma nocover
         raise ValueError(
             f"{self._owner.get_name()} does not have relation {rel_name}"
         )
     relation.remove(self._owner)
     if self.relation._type == ormar.RelationType.MULTIPLE:
         await self.queryset_proxy.delete_through_instance(item)
Beispiel #6
0
    async def load(self: T) -> T:
        """
        Allow to refresh existing Models fields from database.
        Be careful as the related models can be overwritten by pk_only models in load.
        Does NOT refresh the related models fields if they were loaded before.

        :raises NoMatch: If given pk is not found in database.

        :return: reloaded Model
        :rtype: Model
        """
        expr = self.Meta.table.select().where(self.pk_column == self.pk)
        row = await self.Meta.database.fetch_one(expr)
        if not row:  # pragma nocover
            raise NoMatch("Instance was deleted from database and cannot be refreshed")
        kwargs = dict(row)
        kwargs = self.translate_aliases_to_columns(kwargs)
        self.update_from_dict(kwargs)
        self.set_save_status(True)
        return self