Exemple #1
0
    def served_data(self, location):
        """Serve general index data optionally filter by region"""
        categories = list(self.root['types'].values())
        categories.sort(key=lambda x: float(x.get_data('priority')),
                        reverse=True)

        # category list
        types = list()
        all_locations = set()
        for type_ in categories:
            category_tuples = list()
            type_title_ru = type_.get_data('title_ru')
            type_primary_color = type_.get_data('primary_color')
            type_background_color = type_.get_data('background_color')
            type_.categories.sort(
                key=lambda x: float(x.get_data('priority', default=0)),
                reverse=True)
            for category in type_.categories:
                price = category.get_price(location=location)
                if price:
                    price = self.currency(price)
                    query = {'location': location} if location else None
                    url = self.request.resource_path(category, query=query)
                    title = category.get_data('keyword').split(', ')[0]
                    delta = int(
                        category.get_price_delta(self.delta_period,
                                                 location=location) * 100)
                    package_key = category.get_data('normal_package')
                    package_title = ProductPackage(package_key).get_data(
                        'synonyms')[0]
                    cat_locations = category.get_locations()
                    all_locations.update(cat_locations)
                    category_tuples.append(
                        (url, title, price, delta, package_title))
            types.append((type_.title, type_title_ru, type_primary_color,
                          type_background_color, category_tuples))
        time = format_datetime(datetime.datetime.now(),
                               format='long',
                               locale=self.request.locale_name)
        return {
            'types': types,
            'time': time,
            'current_location': location,
            'locations': list(all_locations),
            'root': True
        }
Exemple #2
0
    def serve_data(self, product_category, location):
        """Return prepared category data"""
        category = product_category.category
        category_title = category.title
        category_title_ru = category.get_data('title_ru')
        category_primary_color = category.get_data('primary_color')
        category_background_color = category.get_data('background_color')
        prod_cat_title = product_category.get_data('ru_accu_case')
        median = product_category.get_price(location=location)
        category_delta = int(
            product_category.get_price_delta(self.delta_period,
                                             location=location) * 100)
        if not prod_cat_title:
            prod_cat_title = product_category.get_data('keyword').split(
                ', ')[0]
        package_key = product_category.get_data('normal_package')
        package_title = ProductPackage(package_key).get_data('synonyms')[0]

        chart_data = list()
        datetimes = get_datetimes(self.display_days)
        for date in datetimes:
            chart_data.append([
                date.strftime('%d.%m'),
                product_category.get_price(date, location=location)
            ])
        products = list()
        locations = product_category.get_locations()
        current_path = self.request.resource_url(product_category)
        sorted_products = sorted(
            product_category.get_qualified_products(location=location),
            key=lambda pr: pr[1])
        for num, product_tuple in enumerate(sorted_products):
            try:
                product, price = product_tuple
                middle_num = int(len(sorted_products) / 2)
                is_median = (num == middle_num)
                if len(sorted_products) % 2 == 0:
                    is_median = (num == middle_num or num == middle_num - 1)

                # construct data row as tuple
                products.append(
                    (num + 1, product, self.request.resource_url(product),
                     self.currency(price),
                     int(product.get_price_delta(self.delta_period) * 100),
                     is_median))
            except TypeError:
                pass
        return {
            'price_data': json.dumps(chart_data),
            'products': products,
            'cat_title': prod_cat_title,
            'current_location': location,
            'locations': locations,
            'category_title': category_title,
            'category_title_ru': category_title_ru,
            'category_background_color': category_background_color,
            'category_primary_color': category_primary_color,
            'current_path': current_path,
            'package_title': package_title,
            'median_price': self.currency(median) if median else None,
            'category_delta': category_delta if median else None
        }
Exemple #3
0
    def serve_data(self, product):
        """Return prepared product data"""
        current_price = product.get_price()
        if current_price:
            current_price = self.currency(current_price)
            product_delta = int(
                product.get_price_delta(self.delta_period) * 100)
            last_report_url = None
        else:
            product_delta = 0
            last_price = product.get_last_reported_price()
            current_price = self.currency(last_price) if last_price else None
            last_report = self.context.get_last_report()
            last_report_url = \
                self.request.resource_url(last_report) if last_report else None
        datetimes = get_datetimes(self.display_days)
        chart_data = list()
        for date in datetimes:
            chart_data.append(
                [date.strftime('%d.%m'),
                 product.get_price(date)])
        chart_data = json.dumps(chart_data)
        package_key = product.category.get_data('normal_package')
        product_category_title = product.category.get_data('keyword').split(
            ', ')[0]
        product_category_url = self.request.resource_url(product.category)
        package_title = ProductPackage(package_key).get_data('synonyms')[0]
        type_ = product.category.category
        category_title = type_.title
        category_title_ru = type_.get_data('title_ru')
        category_primary_color = type_.get_data('primary_color')
        category_background_color = type_.get_data('background_color')
        reports = list()

        for report in sorted(
                product.get_reports(from_date_time=self.delta_period),
                reverse=True,
                key=lambda rep: rep.date_time):
            url = self.request.resource_url(report)
            date = format_datetime(report.date_time,
                                   format='short',
                                   locale=self.request.locale_name)
            merchant = report.merchant.title
            location = report.merchant.location
            price = self.currency(report.normalized_price_value)
            reports.append((url, date, merchant, location, price))

        return {
            'current_price': current_price,
            'product_delta': product_delta,
            'last_report_url': last_report_url,
            'chart_data': chart_data,
            'reports': reports,
            'product_category_title': product_category_title,
            'product_category_url': product_category_url,
            'category_title': category_title,
            'category_title_ru': category_title_ru,
            'category_background_color': category_background_color,
            'category_primary_color': category_primary_color,
            'package_title': package_title
        }