def offers(doc, categories): queryset = Product.objects.filter(is_visible=True, is_retail=True, quantity__gt=0, price__gt=0) doc.write('<offers>') for product in queryset: try: category_id = product.categories.filter(parent__isnull=False, id__in=categories).values_list('id', flat=True)[0] except IndexError: try: category_id = product.categories.filter(parent__isnull=True, id__in=categories).values_list('id', flat=True)[0] except IndexError: continue text = product.description.strip() if text: text = strip_tags(strip_spaces_between_tags(product.description)).strip() text = typograph(WHITESPACE_RE.sub(' ', text)) truncator = Truncator(text) text = truncator.chars(512) doc.write('<offer>') doc.write('<url>http://tomat-podarky.ru{}</url>'.format(product.get_absolute_url())) doc.write('<price>{}</price>'.format(product.price)) doc.write('<currencyId>RUR</currencyId>') doc.write('<categoryId>{}</categoryId>'.format(category_id)) doc.write('<delivery>true</delivery>') doc.write('<name>') doc.write(_(typograph(product.title))) doc.write('</name>') if text: doc.write('<description>') doc.write(_(text)) doc.write('</description>') doc.write('</offer>\n') doc.write('</offers>')
def _prepare_obj(instance): response = { 'value': instance.id, 'title': typograph(instance.title), 'url': instance.get_absolute_url(), } photo = instance.get_main_photo() if photo: try: thumb = get_thumbnailer(photo.image).get_thumbnail({ 'size': (60, 60), 'crop': True, }) response['image'] = thumb.url except InvalidImageFormatError: pass return response