Exemplo n.º 1
0
    def get_all_items():
        items_object = item_collection.find({})

        items = []
        for item in items_object:
            items.append(item)

        if len(items) == 0:
            return Tools.Result(False, Tools.errors('INF'))

        items = Item._get_gallery_image_urls_for_items(items)

        for item in items:
            menu_image_id = item['MenuImageUrl']['MenuImageId']
            item.pop('MenuImageUrl')
            item[
                'MenuImageUrl'] = 'https://cafe-art-backend.liara.run/item/menu/image/{}'.format(
                    menu_image_id)

            item_image_id = item['ItemImageUrl']['ItemImageId']
            item.pop('ItemImageUrl')
            item[
                'ItemImageUrl'] = 'https://cafe-art-backend.liara.run/item/item/image/{}'.format(
                    item_image_id)

        return Tools.Result(True, Tools.dumps(items))
Exemplo n.º 2
0
    def get_item(item_id):
        item_object = item_collection.find_one({'_id': ObjectId(item_id)}, {
            'RowId': 1,
            'CategoryName': 1,
            'Title': 1,
            'Description': 1,
            'MenuImageUrl': 1,
            'ItemImageUrl': 1,
            'Likes': 1,
            'Price': 1
        })

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

        menu_image_id = item_object['MenuImageUrl']['MenuImageId']
        item_object.pop('MenuImageUrl')
        item_object[
            'MenuImageUrl'] = 'https://cafe-art-backend.liara.run/item/menu/image/{}'.format(
                menu_image_id)

        item_image_id = item_object['ItemImageUrl']['ItemImageId']
        item_object.pop('ItemImageUrl')
        item_object[
            'ItemImageUrl'] = 'https://cafe-art-backend.liara.run/item/item/image/{}'.format(
                item_image_id)

        gallery_images_urls = Item._get_gallery_image_urls(item_id)

        item_object['GalleryUrls'] = gallery_images_urls

        return Tools.Result(True, Tools.dumps(item_object))
Exemplo n.º 3
0
    def login(username, password):
        # make sure admin with specified username exists
        admin_object = admin_collection.find_one({'UserName': username}, {
            'Password': 1,
            'Key': 1
        })

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

        hash_key = str(admin_object['Key'])[2:-1]

        encrypted_password = str(admin_object['Password'])[2:-1].encode()

        cipher_suite = Fernet(hash_key)

        decrypted_password = str(
            cipher_suite.decrypt(encrypted_password))[2:-1]

        if decrypted_password != password:
            return Tools.Result(False, 'NA')

        token = Auth.add_token(admin_object['_id'])

        if token is False:
            return Tools.Result(False, Tools.errors("FTGT"))

        response = {'Id': admin_object['_id'], 'Token': token}

        return Tools.Result(True, Tools.dumps(response))
Exemplo n.º 4
0
    def get_gallery_images(item_id):
        item = item_collection.find_one({'_id': ObjectId(item_id)},
                                        {'Gallery': 1})

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

        return Tools.Result(True, Tools.dumps(item['Gallery']))
Exemplo n.º 5
0
    def get_info(admin_id):
        # make sure admin exists
        info = admin_collection.find_one({'_id': ObjectId(admin_id)})

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

        return Tools.Result(True, Tools.dumps(info))
Exemplo n.º 6
0
    def get_all_comments():
        item_object = item_collection.find({}, {'Comments': 1})

        comments = []
        for item in item_object:
            comments.append(item['Comments'])

        return Tools.Result(True, Tools.dumps(comments))
Exemplo n.º 7
0
    def get_notifications():

        notification_objects = notification_collection.find({})

        notifications = []
        for notification in notification_objects:
            notifications.append(notification)

        return Tools.Result(True, Tools.dumps(notifications))
Exemplo n.º 8
0
    def get_favorite_items(user_id):

        items_object = item_collection.find({'Likes.UserId': user_id})

        favorite_items = []
        for item in items_object:
            favorite_items.append(item)

        return Tools.Result(True, Tools.dumps(favorite_items))
Exemplo n.º 9
0
    def get_image_urls():
        images = cafe_collection.find_one({}, {'_id': 0})

        image_urls = []
        for image in images['Images']:
            image_urls.append(
                'https://cafe-art-backend.liara.run/cafe/image/{}'.format(
                    image['ImageId']))

        return Tools.Result(True, Tools.dumps(image_urls))
Exemplo n.º 10
0
    def get_comments_on_item(item_id):
        item_object = item_collection.find_one({'_id': ObjectId(item_id)},
                                               {'Comments': 1})

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

        comments = item_object['Comments']

        return Tools.Result(True, Tools.dumps(comments))
Exemplo n.º 11
0
    def add_item(row_id, category_name, title, description, price,
                 menu_image_url, item_image_url):

        result = item_collection.insert_one(
            Item(str(row_id), category_name, title, description, price,
                 menu_image_url, item_image_url).__dict__)

        return Tools.Result(
            True, Tools.dumps({
                '_id': result.inserted_id,
                'name': title
            }))
Exemplo n.º 12
0
    def get_events():
        event_object = event_collection.find({})

        events = []
        for event in event_object:
            event_image_id = event['ImageUrl']['ImageId']
            event[
                'ImageUrl'] = 'https://cafe-art-backend.liara.run/event/image/{}'.format(
                    event_image_id)
            events.append(event)

        return Tools.Result(True, Tools.dumps(events))
Exemplo n.º 13
0
    def login_as_guest(uuid):
        guest_id = ObjectId()
        token = Auth.add_token(str(guest_id))

        if not token:
            return Tools.Result(False, Tools.errors("FTGT"))

        user_collection.insert_one({'_id': guest_id, 'Uuid': uuid})

        response = {'Id': str(guest_id), 'Token': token}

        return Tools.Result(True, Tools.dumps(response))
Exemplo n.º 14
0
    def get_profile_info(user_id):
        user_object = user_collection.find_one({'_id': ObjectId(user_id)})

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

        favorite_items = Item._get_favorite_items(user_id)

        response = {
            'ProfileInfo': user_object,
            'FavoriteItems': favorite_items
        }

        return Tools.Result(True, Tools.dumps(response))
Exemplo n.º 15
0
    def get_items(row_id, user_id):

        if str(row_id) != "-1":
            items_object = item_collection.find({'RowId': row_id}, {
                'Comments': 0,
                'CategoryName': 0
            })
        else:
            items_object = item_collection.find({}, {
                'Comments': 0,
                'CategoryName': 0
            })

        items = []
        for item in items_object:
            items.append(item)

        if len(items) == 0:
            return Tools.Result(False, Tools.errors('INF'))

        items = Item._get_gallery_image_urls_for_items(items)

        for item in items:
            menu_image_id = item['MenuImageUrl']['MenuImageId']
            item.pop('MenuImageUrl')
            item[
                'MenuImageUrl'] = 'https://cafe-art-backend.liara.run/item/menu/image/{}'.format(
                    menu_image_id)

            item_image_id = item['ItemImageUrl']['ItemImageId']
            item.pop('ItemImageUrl')
            item[
                'ItemImageUrl'] = 'https://cafe-art-backend.liara.run/item/item/image/{}'.format(
                    item_image_id)

            if len(item['Likes']) == 0:
                item['IsLiked'] = False

            for user_id_ in item['Likes']:
                item['IsLiked'] = True if user_id_[
                    'UserId'] == user_id else False

        return Tools.Result(True, Tools.dumps(items))
Exemplo n.º 16
0
    def get_top_items():
        items = item_collection.find({}, {
            'Comments': 1,
            'Title': 1,
            'ItemImageUrl': 1
        })

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

        items_arr = []
        for item in items:
            items_arr.append(item)

        items_rate_arr = []
        for item in items_arr:
            sum_ = 0
            total = len(item['Comments'])
            if total == 0:
                continue
            for comment in item['Comments']:
                sum_ += int(comment['Rate'])

            items_rate_arr.append({
                'Title':
                item['Title'],
                'AverageRate':
                sum_ / total,
                'Image':
                'https://cafe-art-backend.liara.run/item/item/image/{}'.format(
                    item['ItemImageUrl']['ItemImageId']),
                'Total':
                total
            })

        # sort items based on average rate
        sorted_items = sorted(items_rate_arr,
                              key=itemgetter('AverageRate'),
                              reverse=True)

        upper_bound = min(5, len(items_rate_arr))

        return Tools.Result(True, Tools.dumps(sorted_items[:upper_bound]))
Exemplo n.º 17
0
    def get_rate_distribution(item_id):
        item_object = item_collection.find_one({'_id': ObjectId(item_id)},
                                               {'Comments': 1})

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

        comments = item_object['Comments']

        total = len(comments)
        rates = {'0': 0, '1': 0, '2': 0, '3': 0, '4': 0, '5': 0, '6': 0}

        for comment in comments:
            rates[str(comment['Rate'])] += 1

        return Tools.Result(True, Tools.dumps({
            'Total': total,
            'Rates': rates
        }))
Exemplo n.º 18
0
    def get_all_unseen_comments():
        items = item_collection.find({}, {'Comments': 1, 'Title': 1})

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

        items_arr = []
        for item in items:
            items_arr.append(item)

        unseen_comments = []
        for item in items_arr:
            if len(item['Comments']) == 0:
                continue
            for comment in item['Comments']:
                if not comment['Seen']:
                    comment['Title'] = item['Title']
                    unseen_comments.append(comment)

        return Tools.Result(True, Tools.dumps(unseen_comments))
Exemplo n.º 19
0
    def get_categories():
        categories_object = category_collection.find({}).sort('RowId', 1)

        categories = []
        for category in categories_object:
            
            # pop images
            category['IconUrl'].pop('IconImage')
            icon_id = category['IconUrl'].pop('IconId')
            category.pop('IconUrl')
            category['IconUrl'] = 'https://cafe-art-backend.liara.run/category/icon/{}'.format(icon_id)

            category['ImageUrl'].pop('ImageUrl')
            image_id = category['ImageUrl'].pop('ImageId')
            category.pop('ImageUrl')
            category['ImageUrl'] = 'https://cafe-art-backend.liara.run/category/image/{}'.format(image_id)
            
            categories.append(category)

        return Tools.Result(True, Tools.dumps(categories))
Exemplo n.º 20
0
    def comments_seen():
        items = item_collection.find({}, {'Comments': 1})

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

        items_arr = []
        for item in items:
            items_arr.append(item)

        total = 0
        seen = 0
        for item in items_arr:
            total += len(item['Comments'])
            if total == 0:
                continue
            for comment in item['Comments']:
                seen += 1 if comment['Seen'] is True else 0

        response = {'Seen': seen, 'Total': total}

        return Tools.Result(True, Tools.dumps(response))
Exemplo n.º 21
0
    def login_verification(phone_number, verification_code):
        # validate phone number and activation code
        if (re.match(User.Constants.phone_regex, phone_number) is None
                or re.match(User.Constants.activation_code_regex,
                            str(verification_code)) is None):
            return Tools.Result(False, Tools.errors('NA'))

        # check whether user is registered and is assigned the specified activation code
        # also make sure code is not used before
        user_object = user_collection.find_one(
            {
                'PhoneNumber': phone_number,
                'Code.Code': int(verification_code),
                'Code.Is_Used': False
            }, {'_id': 1})

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

        # update user status to confirmed
        user_collection.update_one({'PhoneNumber': phone_number}, {
            '$set': {
                'Update_at': datetime.now(),
                'Status': 'Confirm',
                'Code.Is_Used': True,
                'IsActive': True
            }
        })

        token = Auth.add_token(user_id=str(user_object["_id"]))

        if not token:
            return Tools.Result(False, Tools.errors("FTGT"))

        response = {'Id': user_object["_id"], 'Token': token}

        return Tools.Result(True, Tools.dumps(response))
Exemplo n.º 22
0
    def get_average_rate(row_id):
        items = item_collection.find({'RowId': row_id}, {
            'Comments': 1,
            'Title': 1
        })

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

        items_arr = []
        for item in items:
            items_arr.append(item)

        items_rate_arr = {}
        for item in items_arr:
            sum_ = 0
            total = len(item['Comments'])
            if total == 0:
                continue
            for comment in item['Comments']:
                sum_ += int(comment['Rate'])
            items_rate_arr[item['Title']] = sum_ / total

        return Tools.Result(True, Tools.dumps(items_rate_arr))
Exemplo n.º 23
0
    def get_events_sorted():
        event_object = event_collection.find({})

        events = []
        for event in event_object:
            events.append(event)

        passed_events = []
        upcoming_events = []
        for event in events:

            event_image_id = event['ImageUrl']['ImageId']
            event[
                'ImageUrl'] = 'https://cafe-art-backend.liara.run/event/image/{}'.format(
                    event_image_id)

            event_date = event['Date']
            splitted = event_date.split('/')
            year = int(splitted[0])
            month = int(splitted[1])
            day = int(splitted[2])
            cr_date = jdatetime.date(year, month, day).togregorian()
            if cr_date < datetime.now().date():
                passed_events.append({'sdate': cr_date, 'Event': event})
            else:
                upcoming_events.append({'sdate': cr_date, 'Event': event})

        passed_events = sorted(passed_events, key=lambda i: i['sdate'])
        upcoming_events = sorted(upcoming_events, key=lambda i: i['sdate'])

        response = {
            'PassedEvents': passed_events,
            'UpcomingEvents': upcoming_events
        }

        return Tools.Result(True, Tools.dumps(response))
Exemplo n.º 24
0
    def get_images():
        images = cafe_collection.find_one({}, {'_id': 0})

        return Tools.Result(True, Tools.dumps(images))