Exemple #1
0
    def comment_on_item(item_id, user_id, comment, rate):

        valid = item_collection.find_one({'_id':
                                          ObjectId(item_id)}) is not None

        if not valid:
            return Tools.Result(False, Tools.errors('INF'))

        # # make sure user did not comment on the item before
        # commented_before = item_collection.find_one({'_id': ObjectId(item_id), 'Comments.UserId': user_id}) is not None

        # if commented_before:
        #     return Tools.Result(False, Tools.errors('IAE'))

        # update the comments
        item_collection.update_one({'_id': ObjectId(item_id)}, {
            '$push': {
                'Comments': {
                    'CommentId': ObjectId(),
                    'UserId': user_id,
                    'Comment': comment,
                    'Rate': rate,
                    'Seen': False,
                    'Created_at': datetime.now()
                }
            }
        })

        return Tools.Result(True, 'd')
Exemple #2
0
    def unlike_item(item_id, user_id):
        valid = item_collection.find_one({'_id':
                                          ObjectId(item_id)}) is not None

        if not valid:
            return Tools.Result(False, Tools.errors('INF'))

        # make sure user did not liked the item before
        liked_before = item_collection.find_one({
            '_id': ObjectId(item_id),
            'Likes.UserId': user_id
        }) is not None

        if not liked_before:
            return Tools.Result(False, Tools.errors('INF'))

        # update the likes
        item_collection.update_one({'_id': ObjectId(item_id)}, {
            '$pull': {
                'Likes': {
                    'UserId': user_id
                }
            },
            '$inc': {
                'LikesCount': -1
            }
        })

        return Tools.Result(True, 'd')
Exemple #3
0
    def delete_image_from_gallery(item_id, gallery_image_id):
        item = item_collection.find_one({'_id': ObjectId(item_id)}, {'_id': 1})

        if item is None:
            return Tools.Result(False, Tools.errors('INF'))

        item_collection.update_one(
            {'_id': ObjectId(item_id)},
            {'$pull': {
                'Gallery': {
                    'Id': gallery_image_id
                }
            }})

        return Tools.Result(True, 'd')
Exemple #4
0
    def unlike_image_gallery(item_id, user_id, gallery_image_id):
        valid = item_collection.find_one({'_id':
                                          ObjectId(item_id)}) is not None

        if not valid:
            return Tools.Result(False, Tools.errors('INF'))

        gallery = item_collection.find_one(
            {
                '_id': ObjectId(item_id),
                'Gallery': {
                    '$elemMatch': {
                        'Id': ObjectId(gallery_image_id)
                    }
                }
            }, {
                '_id': 0,
                'Gallery': 1
            })

        found = False
        for images in gallery['Gallery']:
            if str(images['Id']) == gallery_image_id:
                for like in images['Likes']:
                    if like['UserId'] == user_id:
                        found = True

        if not found:
            return Tools.Result(False, Tools.errors('NA'))

        # update the likes
        item_collection.update_one({'_id': ObjectId(item_id)}, {
            '$dec': {
                'Gallery.$[elem].LikesCount': -1
            },
            '$pull': {
                'Gallery.$[elem].Likes': {
                    'UserId': user_id
                }
            }
        },
                                   array_filters=[{
                                       'elem.Id':
                                       ObjectId(gallery_image_id)
                                   }])

        return Tools.Result(True, 'd')
Exemple #5
0
    def modify_item(item_id,
                    row_id=None,
                    category_name=None,
                    title=None,
                    description=None,
                    price=None,
                    menu_image_url=None,
                    item_image_url=None):

        # make sure at least on attribute is not null
        if row_id is None and category_name is None and title is None and price is None and menu_image_url is None and item_image_url is None and description is None:
            return Tools.Result(False, Tools.errors('NA'))

        if (row_id is None
                and category_name is not None) or (row_id is not None
                                                   and category_name is None):
            return Tools.Result(False, Tools.errors('NA'))

        valid = item_collection.find_one({'_id': ObjectId(item_id)},
                                         {'_id': 1}) is not None

        if not valid:
            return Tools.Result(False, Tools.errors('INF'))

        updating_values = {}
        if title is not None:
            updating_values['Title'] = title
        if row_id is not None:
            updating_values['RowId'] = str(row_id)
            updating_values['CategoryName'] = category_name
        if price is not None:
            updating_values['Price'] = price
        if menu_image_url is not None:
            updating_values['MenuImageUrl.MenuImage'] = menu_image_url
        if item_image_url is not None:
            updating_values['ItemImageUrl.ItemImage'] = item_image_url
        if description is not None:
            updating_values['Description'] = description

        item_collection.update_one({'_id': ObjectId(item_id)},
                                   {'$set': {
                                       **updating_values
                                   }})

        return Tools.Result(True, 'd')
Exemple #6
0
    def admin_saw_comment(comment_id):

        valid = item_collection.find_one(
            {'Comments.CommentId': ObjectId(comment_id)},
            {'_id': 1}) is not None

        if not valid:
            return Tools.Result(False, Tools.errors('INF'))

        item_collection.update_one(
            {'Comments.CommentId': ObjectId(comment_id)},
            {'$set': {
                'Comments.$[elem].Seen': True
            }},
            array_filters=[{
                'elem.CommentId': ObjectId(comment_id)
            }])

        return Tools.Result(True, 'd')
Exemple #7
0
    def add_image_gallery(item_id, gallery):

        gallery_objects = []
        for item in gallery:
            gallery_objects.append({
                'ImageUrl': item,
                'Likes': [],
                'LikesCount': 0,
                'Id': str(ObjectId())
            })

        item_collection.update_one(
            {'_id': ObjectId(item_id)},
            {'$push': {
                'Gallery': {
                    '$each': gallery_objects
                }
            }})

        return Tools.Result(True, 'd')