| 1 | from django.shortcuts import render_to_response, get_object_or_404 |
|---|
| 2 | from django.http import HttpResponseRedirect, Http404, HttpResponse |
|---|
| 3 | from django.template import RequestContext |
|---|
| 4 | from django.template.loader import render_to_string |
|---|
| 5 | import djw.modules.GoogleMerchant.settings as settings |
|---|
| 6 | from datetime import datetime |
|---|
| 7 | from django.utils.feedgenerator import rfc3339_date |
|---|
| 8 | from djw.core.ProductCatalog.models import Product |
|---|
| 9 | from django.http import Http404 |
|---|
| 10 | # dependense |
|---|
| 11 | from djw.custom.ProductCatalog_custom.models import customizedProduct |
|---|
| 12 | |
|---|
| 13 | # todo: move function to djhdgutils |
|---|
| 14 | def _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 | |
|---|
| 21 | class 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 | |
|---|
| 60 | def 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 | |
|---|