root/modules/trunk/GoogleMerchant/views.py @ 622

Revision 622, 2.4 KB (checked in by av, 7 months ago)

fixed google price for minimum_quantity_for_purchase; ignored products with empty urls

Line 
1from django.shortcuts import render_to_response, get_object_or_404
2from django.http import HttpResponseRedirect, Http404, HttpResponse
3from django.template import RequestContext
4from django.template.loader import render_to_string
5import djw.modules.GoogleMerchant.settings as settings
6from datetime import datetime
7from django.utils.feedgenerator import rfc3339_date
8from djw.core.ProductCatalog.models import Product
9from django.http import Http404
10# dependense
11from djw.custom.ProductCatalog_custom.models import customizedProduct
12
13# todo: move function to djhdgutils
14def _site_url():
15    from django.contrib.sites.models import Site
16    from django.conf import settings as django_settings
17    current_site = Site.objects.get(id=django_settings.SITE_ID)
18    return current_site.domain
19
20
21class GProduct(object):
22    """ wrapper for product """
23
24    def __init__(self, product, country, *args, **kwargs):
25        self._product = product
26        self._country = country
27        self._customized_product = customizedProduct.objects.get(
28            product=product)
29        super(GProduct, self).__init__(*args, **kwargs)
30
31
32    @property
33    def product(self):
34        return self._product
35
36    @property
37    def price(self):
38        if settings.PRICE_FOR_COUNTRY:
39            single_price = settings.PRICE_FOR_COUNTRY(self.product,
40                                                    self._country)
41        else:
42            single_price = self.product.price
43
44        min_quantity_price = single_price * self._customized_product\
45            .minimum_quantity_for_purchase
46
47        return min_quantity_price
48
49
50    def url_local(self):
51        return _site_url() + self.product.get_absolute_url()
52
53
54    @property
55    def id(self):
56        return '%s_%s' % (self.product.id, self._country)
57
58
59
60def products_xml(request, country):
61    """ generate google merchant xml for country ('US', 'GB' or 'DE') """
62
63    if country not in ['US', 'GB', 'DE']:
64        raise Http404('Unknown country')
65   
66    google_products = [GProduct(p, country)
67                       for p in Product.objects.all()
68                       if p.get_absolute_url() != ''
69                       ]
70
71    return render_to_response("google_products_feed.xml",
72        RequestContext(request,
73                       {'feed_link': _site_url(),
74                        'feed_updated': rfc3339_date(datetime.now()),
75                        'google_products': google_products,
76                        }))
77
Note: See TracBrowser for help on using the browser.