Ejemplo n.º 1
0
    async def del_users_wish(self, uid, pid):
        product = await Product.objects.get(_id=pid)
        if product is None:
            return False

        profile = await Profile.objects.get(_id=uid)
        if profile is None:
            return False

        # TODO развернуть кластер mongodb
        # async with await db.client.start_session() as s:
        #     async with s.start_transaction():
        s = None
        collection = Profile.Meta.collection_name
        db = get_mongo_conn()

        selector, update = del_users_wish_query(uid, pid)
        res = await db[collection].update_one(selector, update, session=s)
        if res is None:
            # await s.abort_transaction()

            return False

        # TODO FIX
        selector, update = del_users_intention_query_wo_sponsor(uid, pid)
        res = await db[collection].update_one(selector, update, session=s)
        if res is None:
            # await s.abort_transaction()

            return False

        return True
Ejemplo n.º 2
0
    async def count(self) -> int:
        collection: str = self.model_cls.Meta.collection_name
        db = get_mongo_conn()

        n: int = await db[collection].count_documents({})

        return n
Ejemplo n.º 3
0
    async def delete(self):
        collection = self.Meta.collection_name
        _id = self.__dict__['_id']

        db = get_mongo_conn()

        await db[collection].delete_one({'_id': ObjectId(_id)})
Ejemplo n.º 4
0
    async def delete(self, **kwargs) -> DeleteResult:
        for key, value in kwargs.items():
            self.model_cls.__dict__[key].validate(value)

        collection = self.model_cls.Meta.collection_name
        db = get_mongo_conn()

        await db[collection].delete_many(kwargs)
Ejemplo n.º 5
0
    async def __aiter__(self):
        db = get_mongo_conn()

        self.data = await db[self.collection].find(self.selector).skip(
            self.qs_offset).to_list(length=self.qs_limit)
        self.length = len(self.data)
        self.i = 0

        return self
Ejemplo n.º 6
0
    async def create_product(self, product):
        document = product.to_dict()
        db = get_mongo_conn()

        collection = Product.Meta.collection_name

        res = await db[collection].insert_one(document)

        return str(res.inserted_id)
Ejemplo n.º 7
0
    async def del_profile(self, uid):
        db = get_mongo_conn()

        collection = Profile.Meta.collection_name

        res = await db[collection].delete_one({'_id': ObjectId(uid)})
        if res is None:
            return False
        else:
            return True
Ejemplo n.º 8
0
    async def check_product(self, pid):
        db = get_mongo_conn()

        collection = Product.Meta.collection_name

        res = await db[collection].find_one({'_id': ObjectId(pid)})
        if res is None:
            return False
        else:
            return True
Ejemplo n.º 9
0
    async def add_users_intention(self, uid, pid, dest_id):
        res = await self.check_user(uid)
        if res is None:

            return False

        res = await self.check_user(dest_id)
        if res is None:
            return False

        res = await self.check_wish(dest_id, pid)
        if res is None:
            return False

        # TODO развернуть кластер mongodb
        # async with await db.client.start_session() as s:
        #     async with s.start_transaction():
        s = None
        collection = Profile.Meta.collection_name

        selector, update = add_users_intention_query(uid, pid, dest_id)
        db = get_mongo_conn()

        res = await db[collection].update_one(selector, update, session=s)
        if res is None:
            # await s.abort_transaction()

            return False

        selector, update = reserve_users_wish_query(uid, pid, dest_id)
        db = get_mongo_conn()

        res = await db[collection].update_one(selector, update, session=s)
        if res is None:
            # await s.abort_transaction()

            return False

        return True
Ejemplo n.º 10
0
    async def check_wish(self, uid, pid):
        db = get_mongo_conn()

        collection = Profile.Meta.collection_name

        res = await db[collection].find_one({
            '_id': ObjectId(uid),
            'wishes': {
                'p_id': ObjectId(pid)
            }
        })
        if res is None:
            return False
        else:
            return True
Ejemplo n.º 11
0
    async def save(self):
        collection = self.Meta.collection_name

        if self.changed is None:
            document = self.__dict__.copy()

            document.pop('_id', document)
            document.pop('changed', document)

            db = get_mongo_conn()
            res = await db[collection].insert_one(document)
            self._id = (str(res.inserted_id))

        elif self.changed == True:
            document = self.__dict__.copy()

            _id = document.pop('_id', document)
            document.pop('changed', document)

            db = get_mongo_conn()

            await db[collection].update_one({'_id': ObjectId(_id)},
                                            {'$set': document})
            self.changed = False
Ejemplo n.º 12
0
    async def check_intention(self, uid, pid, dest_id):
        db = get_mongo_conn()

        collection = Profile.Meta.collection_name

        res = await db[collection].find_one({
            '_id': ObjectId(uid),
            'intentions': {
                'product_id': pid,
                'dest_id': dest_id
            }
        })
        if res is None:
            return False
        else:
            return True
Ejemplo n.º 13
0
    async def add_users_wish(self, uid, pid):
        product = await Product.objects.get(_id=pid)
        if product is None:
            return False

        profile = await Profile.objects.get(_id=uid)
        if profile is None:
            return False

        collection = Profile.Meta.collection_name

        selector, update = add_users_wish_query(uid, pid)
        db = get_mongo_conn()

        res = await db[collection].update_one(selector, update)
        if res is None:
            return False

        return True
Ejemplo n.º 14
0
    async def get(self, **kwargs) -> dict:
        collection = self.model_cls.Meta.collection_name
        selector = dict()

        for key, value in kwargs.items():
            self.model_cls.__dict__[key].validate(value)

            if key == '_id':
                try:
                    selector[key] = ObjectId(value)
                except bson.errors.InvalidId:
                    raise

            else:
                selector[key] = value

        db = get_mongo_conn()
        data = await db[collection].find_one(selector)
        if data is not None:
            data['_id'] = str(data['_id'])

        return data