Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Add saleor feed and feed updating command

Krzysztof Wolskib80d45005dba
diff --git a/saleor/integrations/feeds.py b/saleor/integrations/feeds.py
index 60040afe03..707256f9e3 100644
--- a/saleor/integrations/feeds.py
+++ b/saleor/integrations/feeds.py
@@ -1,5 +1,7 @@
from __future__ import unicode_literals
+from os import path
+
from django.conf import settings
from django.core.urlresolvers import reverse
from django.contrib.sites.models import Site
@@ -78,10 +80,12 @@ class GoogleProductFeedGenerator(Atom1Feed):
class GoogleProductFeed(StaticFeed):
"""
- Basic Google feed class. To adjust feed to your needs inherit from this class.
+ Basic Google feed class. To adjust feed to your needs inherit from
+ this class.
- Depending on target country and law changes required fields can change, please validate
- your feed at Google Merchant dashboard and override missing methods.
+ Depending on target country and law changes required fields can change,
+ please validate your feed at Google Merchant dashboard and override
+ missing methods.
Google feeds dashboard:
https://merchants.google.com/mc/feeds/dashboard
@@ -92,6 +96,8 @@ class GoogleProductFeed(StaticFeed):
feed_type = GoogleProductFeedGenerator
link = '/'
title = 'Google Product Feed'
+ url = ''
+ file_path = path.join(settings.INTEGRATIONS_DIR, 'google-feed.xml')
def __init__(self):
self.categories = Category.objects.none()
@@ -234,7 +240,7 @@ class GoogleProductFeed(StaticFeed):
if sale_price != price:
product_data['sale_price'] = sale_price
- tax = self.item_tax(self, item)
+ tax = self.item_tax(item)
if tax:
product_data['tax'] = tax
@@ -253,6 +259,8 @@ class SaleorFeed(GoogleProductFeed):
"""
Example of using GoogleProductFeed.
"""
+ url = 'integrations:saleor-feed'
+ file_path = path.join(settings.INTEGRATIONS_DIR, 'saleor-feed.xml')
def item_shipping(self, item):
"""Flat shipping price"""
diff --git a/saleor/integrations/management/commands/update_integrations.py b/saleor/integrations/management/commands/update_integrations.py
index 2e8aa9b61b..2f42d38e65 100644
--- a/saleor/integrations/management/commands/update_integrations.py
+++ b/saleor/integrations/management/commands/update_integrations.py
@@ -1,10 +1,43 @@
from __future__ import unicode_literals
-from django.core.management import BaseCommand
+try:
+ from StringIO import StringIO
+except ImportError:
+ from io import StringIO
+
+from django.core.files.storage import default_storage
+from django.core.management import CommandError, BaseCommand
+
+from saleor.integrations.feeds import SaleorFeed
class Command(BaseCommand):
- help = 'Updates integration feeds'
+ help = 'Updates integration feeds. '
+ feed_classes = {'saleor': SaleorFeed}
+
+ def add_arguments(self, parser):
+ parser.add_argument('feed_name', nargs='+', type=str)
+
+ @staticmethod
+ def get_feed_content(feed, url_name=''):
+ feed = feed.get_feed()
+ buffer = StringIO()
+ feed.write(buffer, 'utf-8')
+ return buffer.getvalue()
+
+ @staticmethod
+ def save_feed(file_path, content):
+ with default_storage.open(file_path, 'w') as output_file:
+ output_file.write(content)
+
+ def update_feed(self, feed):
+ content = self.get_feed_content(feed, feed.url)
+ self.save_feed(feed.file_path, content)
def handle(self, *args, **options):
- pass
+ feed_names = options['feed_name'] or self.feed_classes.keys()
+ for feed_name in feed_names:
+ feed = self.feed_classes.get(feed_name)
+ if feed is None:
+ raise CommandError('Feed "%s" does not exist' % feed_name)
+ self.update_feed(feed())
diff --git a/saleor/integrations/urls.py b/saleor/integrations/urls.py
new file mode 100644
index 0000000000..3c9a775301
--- /dev/null
+++ b/saleor/integrations/urls.py
@@ -0,0 +1,7 @@
+from django.conf.urls import url
+from . import views
+
+
+urlpatterns = [
+ url(r'saleor-feed/$', views.saleor_feed, name='saleor-feed'),
+]
diff --git a/saleor/integrations/views.py b/saleor/integrations/views.py
new file mode 100644
index 0000000000..ab5b6c22af
--- /dev/null
+++ b/saleor/integrations/views.py
@@ -0,0 +1,20 @@
+from __future__ import unicode_literals
+
+from django.core.files.storage import default_storage
+from django.http import Http404
+from django.http import HttpResponse
+
+from .feeds import SaleorFeed
+
+
+def get_integration_response(file_path, content_type):
+ if not default_storage.exists(file_path):
+ raise Http404("Integration file does not exists")
+ feed_file = default_storage.open(file_path)
+ return HttpResponse(feed_file, content_type=content_type)
+
+
+def saleor_feed(request):
+ feed = SaleorFeed()
+ return get_integration_response(feed.file_path,
+ 'application/atom+xml; charset=utf-8')
diff --git a/saleor/settings.py b/saleor/settings.py
index 23628ba96f..ef4fee9543 100644
--- a/saleor/settings.py
+++ b/saleor/settings.py
@@ -235,6 +235,8 @@ ACCOUNT_ACTIVATION_DAYS = 3
LOGIN_REDIRECT_URL = 'home'
+INTEGRATIONS_DIR = os.environ.get('INTEGRATIONS_DIR', '')
+
FACEBOOK_APP_ID = os.environ.get('FACEBOOK_APP_ID')
FACEBOOK_SECRET = os.environ.get('FACEBOOK_SECRET')
diff --git a/saleor/urls.py b/saleor/urls.py
index cd1b8cfec1..e4afc5e632 100644
--- a/saleor/urls.py
+++ b/saleor/urls.py
@@ -13,6 +13,7 @@ from .order.urls import urlpatterns as order_urls
from .product.urls import urlpatterns as product_urls
from .search.urls import urlpatterns as search_urls
from .userprofile.urls import urlpatterns as userprofile_urls
+from .integrations.urls import urlpatterns as integrations_urls
from .dashboard.urls import urlpatterns as dashboard_urls
@@ -30,6 +31,7 @@ urlpatterns = [
url(r'^profile/', include(userprofile_urls, namespace='profile')),
url(r'^search/', include(search_urls, namespace='search')),
url(r'^selectable/', include('selectable.urls')),
+ url(r'^integrations/', include(integrations_urls, namespace='integrations')),
url(r'^sitemap\.xml$', sitemap, {'sitemaps': sitemaps},
name='django.contrib.sitemaps.views.sitemap'),
url(r'', include('payments.urls'))