Skip to content

Commit

Permalink
django 1.7 support
Browse files Browse the repository at this point in the history
  • Loading branch information
barseghyanartur committed Oct 20, 2014
1 parent 3e187eb commit 36fd2ea
Show file tree
Hide file tree
Showing 11 changed files with 126 additions and 61 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Release history
2014-10-12

- Django 1.7 support.
- Softened `six` requirements.

0.5
-------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ implemented:

Prerequisites
======================================================
- Django 1.5.+
- Python 2.7.+, 3.3.+
- Django: 1.5, 1.6, 1.7
- Python: 2.7, 3.3

Installation
======================================================
Expand Down
1 change: 1 addition & 0 deletions install.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
python setup.py install
mkdir -p example/db/ example/static/ example/tmp/ example/media/
python example/example/manage.py collectstatic --noinput
3 changes: 3 additions & 0 deletions reinstall.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
reset
./uninstall.sh
./install.sh
6 changes: 4 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@
package_data = {'qartez': templates},
include_package_data = True,
install_requires = [
'six==1.4.1',
'radar==0.2',
'six>=1.1.0',
],
tests_require = [
'radar>=0.3',
]
)
97 changes: 65 additions & 32 deletions src/qartez/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__title__ = 'qartez.__init__'
__title__ = 'django-qartez'
__version__ = '0.6'
__build__ = 0x000006
__author__ = 'Artur Barseghyan <artur.barseghyan@gmail.com>'
Expand All @@ -15,7 +15,9 @@
from django.core.exceptions import ImproperlyConfigured

from qartez.constants import REL_ALTERNATE_HREFLANG_SITEMAP_TEMPLATE
from qartez.settings import PREPEND_LOC_URL_WITH_SITE_URL, PREPEND_IMAGE_LOC_URL_WITH_SITE_URL
from qartez.settings import (
PREPEND_LOC_URL_WITH_SITE_URL, PREPEND_IMAGE_LOC_URL_WITH_SITE_URL
)

PY2 = not PY3

Expand All @@ -28,14 +30,16 @@ class ImagesSitemap(GenericSitemap):
>>> from qartez import ImagesSitemap
>>>
>>> foo_item_images_info_dict = {
>>> 'queryset': FooItem._default_manager.exclude(image=None), # Queryset
>>> 'image_location_field': 'image', # Image location
>>> 'image_title_field': 'title', # Image title
>>> 'location_field': 'get_absolute_url' # An absolute URL of the page where image is shown
>>> 'queryset': FooItem._default_manager.exclude(image=None), # queryset
>>> 'image_location_field': 'image', # image location
>>> 'image_title_field': 'title', # image title
>>> 'location_field': 'get_absolute_url' # an absolute URL of the page
>>> # where image is shown
>>> }
>>>
>>> foo_item_images_sitemap = {
>>> 'foo_item_images': ImagesSitemap(foo_item_images_info_dict, priority=0.6),
>>> 'foo_item_images': ImagesSitemap(foo_item_images_info_dict, \
>>> priority=0.6),
>>> }
"""
def __init__(self, info_dict, priority=None, changefreq=None):
Expand All @@ -56,7 +60,9 @@ def __init__(self, info_dict, priority=None, changefreq=None):
else:
self.image_title_field = str(self.image_title_field)

self.image_geo_location_field = info_dict.get('image_geo_location_field', None)
self.image_geo_location_field = info_dict.get(
'image_geo_location_field', None
)
self.image_license_field = info_dict.get('image_license_field', None)
self.location_field = info_dict.get('location_field', None)
super(ImagesSitemap, self).__init__(info_dict, priority, changefreq)
Expand Down Expand Up @@ -149,26 +155,34 @@ def get_urls(self, page=1, site=None, protocol=None):
except Site.DoesNotExist:
pass
if site is None:
raise ImproperlyConfigured("To use sitemaps, either enable the sites framework or pass a "
"Site/RequestSite object in your view.")
raise ImproperlyConfigured(
"To use sitemaps, either enable the sites framework or "
"pass a Site/RequestSite object in your view."
)
domain = site.domain

urls = []
for item in self.paginator.page(page).object_list:
loc = self.__get('location', item, None)
if loc and PREPEND_LOC_URL_WITH_SITE_URL:
if PY2:
loc = "%s://%s%s" % (protocol, unicode(domain), unicode(loc))
loc = "{0}://{1}{2}".format(
protocol, unicode(domain), unicode(loc)
)
else:
loc = "%s://%s%s" % (protocol, str(domain), str(loc))
loc = "{0}://{1}{2}".format(protocol, str(domain), str(loc))

image_loc = self.__get('image_location', item, None)
if image_loc and PREPEND_IMAGE_LOC_URL_WITH_SITE_URL:
try:
if PY2:
image_loc = "%s://%s%s" % (protocol, unicode(domain), unicode(image_loc))
image_loc = "{0}://{1}{2}".format(
protocol, unicode(domain), unicode(image_loc)
)
else:
image_loc = "%s://%s%s" % (protocol, str(domain), str(image_loc))
image_loc = "{0}://{1}{2}".format(
protocol, str(domain), str(image_loc)
)
except Exception as e:
continue

Expand All @@ -188,7 +202,9 @@ def get_urls(self, page=1, site=None, protocol=None):
'image_caption': self.__get('image_caption', item, None),
'image_title': self.__get('image_title', item, None),
'image_license': self.__get('image_license', item, None),
'image_geo_location': self.__get('image_geo_location', item, None),
'image_geo_location': self.__get(
'image_geo_location', item, None
),
'lastmod': self.__get('lastmod', item, None),
'changefreq': changefreq,
'priority': priority
Expand All @@ -199,7 +215,8 @@ def get_urls(self, page=1, site=None, protocol=None):

class StaticSitemap(Sitemap):
"""
Sitemap for ``static`` pages. See constructor docstring for list of accepted (additional) arguments.
Sitemap for ``static`` pages. See constructor docstring for list of
accepted (additional) arguments.
:example:
>>> from qartez import StaticSitemap
Expand All @@ -209,15 +226,20 @@ class StaticSitemap(Sitemap):
>>>
>>> content_types_sitemap = StaticSitemap(priority=1.0, changefreq='daily')
>>> content_types_sitemap.add_named_pattern('blog.browse') # Homepage
>>> content_types_sitemap.add_named_pattern('blog.browse', kwargs={'content_type': 'articles'}) # Articles
>>> content_types_sitemap.add_named_pattern('blog.browse', kwargs={'content_type': 'downloads'}) # Downloads
>>> content_types_sitemap.add_named_pattern(
>>> 'blog.browse', kwargs={'content_type': 'articles'}
>>> ) # Articles
>>> content_types_sitemap.add_named_pattern(
>>> 'blog.browse', kwargs={'content_type': 'downloads'}
>>> ) # Downloads
"""
NAMED_PATTERN = 1
URL = 2

def __init__(self, *args, **kwargs):
"""
Constructor. Accepts the following optional keyword-arguments (to be only specified as keyword-arguments).
Constructor. Accepts the following optional keyword-arguments (to
be only specified as keyword-arguments).
:param float priority:
:param str changefreq:
Expand Down Expand Up @@ -252,8 +274,8 @@ def items(self):
def location(self, obj):
return obj['location']

def add_named_pattern(self, viewname, urlconf=None, args=[], kwargs=None, lastmod=None, changefreq=None, \
priority=None):
def add_named_pattern(self, viewname, urlconf=None, args=[], kwargs=None, \
lastmod=None, changefreq=None, priority=None):
"""
Ads a named pattern to the items list.
Expand Down Expand Up @@ -311,9 +333,11 @@ class RelAlternateHreflangSitemap(Sitemap):
"""
Sitemaps: rel="alternate" hreflang="x" implementation.
Read the specs the specs here http://support.google.com/webmasters/bin/answer.py?hl=en&answer=2620865
Read the specs the specs here
http://support.google.com/webmasters/bin/answer.py?hl=en&answer=2620865
IMPORTANT: When you use this class you have to override the ``alternate_hreflangs`` method in your sitemap class.
IMPORTANT: When you use this class you have to override
the ``alternate_hreflangs`` method in your sitemap class.
:example:
>>> from qartez import RelAlternateHreflangSitemap
Expand All @@ -333,24 +357,29 @@ def __get(self, name, obj, default=None):

def alternate_hreflangs(self, item):
"""
You should override the "alternate_hreflangs" method in your sitemap class.
You should override the "alternate_hreflangs" method in your sitemap
class.
"""
raise NotImplementedError(
u"""You have to override the "alternate_hreflangs" method in your sitemap class. """
u"""Refer to "qartez" app documentation for details and examples."""
"""You have to override the "alternate_hreflangs" method in """
"""your sitemap class. Refer to "qartez" app documentation for """
"""details and examples."""
)

def _render_alternate_hreflangs(self, item):
"""
Renders the tiny bit of XML responsible for rendering the alternate hreflang code.
Renders the tiny bit of XML responsible for rendering the alternate
hreflang code.
:return str:
"""
alternate_hreflangs = self.__get('alternate_hreflangs', item, [])
output = u""
output = ""
if alternate_hreflangs:
for hreflang in alternate_hreflangs:
output += REL_ALTERNATE_HREFLANG_SITEMAP_TEMPLATE % {'lang': hreflang[0], 'href': hreflang[1]}
output += REL_ALTERNATE_HREFLANG_SITEMAP_TEMPLATE.format(
**{'lang': hreflang[0], 'href': hreflang[1]}
)
return output

def get_urls(self, page=1, site=None, protocol=None):
Expand All @@ -368,13 +397,17 @@ def get_urls(self, page=1, site=None, protocol=None):
except Site.DoesNotExist:
pass
if site is None:
raise ImproperlyConfigured("To use sitemaps, either enable the sites framework or pass a "
"Site/RequestSite object in your view.")
raise ImproperlyConfigured(
"To use sitemaps, either enable the sites framework "
"or pass a Site/RequestSite object in your view."
)
domain = site.domain

urls = []
for item in self.paginator.page(page).object_list:
loc = "%s://%s%s" % (protocol, domain, self.__get('location', item))
loc = "{0}://{1}{2}".format(
protocol, domain, self.__get('location', item)
)
url_info = {
'location': loc,
'lastmod': self.__get('lastmod', item, None),
Expand Down
6 changes: 3 additions & 3 deletions src/qartez/conf.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__title__ = 'qartez'
__title__ = 'qartez.conf'
__author__ = 'Artur Barseghyan <artur.barseghyan@gmail.com>'
__all__ = ('get_setting',)

Expand All @@ -14,7 +14,7 @@ def get_setting(setting, override=None):
"""
if override is not None:
return override
if hasattr(settings, 'QARTEZ_%s' % setting):
return getattr(settings, 'QARTEZ_%s' % setting)
if hasattr(settings, 'QARTEZ_{0}'.format(setting)):
return getattr(settings, 'QARTEZ_{0}'.format(setting))
else:
return getattr(defaults, setting)
6 changes: 3 additions & 3 deletions src/qartez/constants.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
__title__ = 'qartez'
__title__ = 'qartez.constants'
__author__ = 'Artur Barseghyan <artur.barseghyan@gmail.com>'
__all__ = ('REL_ALTERNATE_HREFLANG_SITEMAP_TEMPLATE',)

# Tiny bit of XML responsible for rendering the alternate hreflang code
REL_ALTERNATE_HREFLANG_SITEMAP_TEMPLATE = """
<xhtml:link
rel="alternate"
hreflang="%(lang)s"
href="%(href)s"
hreflang="{lang}"
href="{href}"
/>
"""
17 changes: 12 additions & 5 deletions src/qartez/defaults.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
__title__ = 'qartez'
__title__ = 'qartez.defaults'
__author__ = 'Artur Barseghyan <artur.barseghyan@gmail.com>'
__all__ = ('PREPEND_LOC_URL_WITH_SITE_URL', 'PREPEND_IMAGE_LOC_URL_WITH_SITE_URL', 'CHANGEFREQ', 'DEBUG')
__all__ = (
'PREPEND_LOC_URL_WITH_SITE_URL', 'PREPEND_IMAGE_LOC_URL_WITH_SITE_URL',
'CHANGEFREQ', 'DEBUG'
)

# When set to True, current site's domain is prepended to the location URL.
PREPEND_LOC_URL_WITH_SITE_URL = True

# When set to True, current site's domain is prepended to the image location URL.
# When set to True, current site's domain is prepended to the image location
# URL.
PREPEND_IMAGE_LOC_URL_WITH_SITE_URL = True

# Valid changefreq values according to the specs http://www.sitemaps.org/protocol.html
CHANGEFREQ = ['always', 'hourly', 'daily', 'weekly', 'monthly', 'yearly', 'never']
# Valid changefreq values according to the specs
# http://www.sitemaps.org/protocol.html
CHANGEFREQ = [
'always', 'hourly', 'daily', 'weekly', 'monthly', 'yearly', 'never'
]

DEBUG = False
26 changes: 18 additions & 8 deletions src/qartez/settings.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,32 @@
"""
Override the following values in your global ``settings`` module by adding `QARTEZ_` prefix to the values.
Override the following values in your global ``settings`` module by adding
`QARTEZ_` prefix to the values.
When it comes to importing the values, import them from ``qartez.settings`` module (without `QARTEZ_` prefix).
When it comes to importing the values, import them from ``qartez.settings``
module (without `QARTEZ_` prefix).
``PREPEND_LOC_URL_WITH_SITE_URL``: When set to True, current site's domain is prepended to the location URL.
``PREPEND_LOC_URL_WITH_SITE_URL``: When set to True, current site's domain is
prepended to the location URL.
``PREPEND_IMAGE_LOC_URL_WITH_SITE_URL``: When set to True, current site's domain is prepended to the image location URL.
``PREPEND_IMAGE_LOC_URL_WITH_SITE_URL``: When set to True, current site's
domain is prepended to the image location URL.
``CHANGEFREQ``: Valid changefreq values according to the specs http://www.sitemaps.org/protocol.html
``CHANGEFREQ``: Valid changefreq values according to the specs
http://www.sitemaps.org/protocol.html
"""
__title__ = 'qartez'
__title__ = 'qartez.settings'
__author__ = 'Artur Barseghyan <artur.barseghyan@gmail.com>'
__all__ = ('PREPEND_LOC_URL_WITH_SITE_URL', 'PREPEND_IMAGE_LOC_URL_WITH_SITE_URL', 'CHANGEFREQ', 'DEBUG')
__all__ = (
'PREPEND_LOC_URL_WITH_SITE_URL', 'PREPEND_IMAGE_LOC_URL_WITH_SITE_URL',
'CHANGEFREQ', 'DEBUG',
)

from qartez.conf import get_setting

PREPEND_LOC_URL_WITH_SITE_URL = get_setting('PREPEND_LOC_URL_WITH_SITE_URL')
PREPEND_IMAGE_LOC_URL_WITH_SITE_URL = get_setting('PREPEND_IMAGE_LOC_URL_WITH_SITE_URL')
PREPEND_IMAGE_LOC_URL_WITH_SITE_URL = get_setting(
'PREPEND_IMAGE_LOC_URL_WITH_SITE_URL'
)
CHANGEFREQ = get_setting('CHANGEFREQ')

DEBUG = get_setting('DEBUG')
20 changes: 14 additions & 6 deletions src/qartez/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
from django.utils.encoding import smart_str
from django.core.paginator import EmptyPage, PageNotAnInteger

def render_images_sitemap(request, sitemaps, section=None, template_name='qartez/images_sitemap.xml'):
def render_images_sitemap(request, sitemaps, section=None, \
template_name='qartez/images_sitemap.xml'):
"""
Renders images sitemap.
Expand All @@ -20,7 +21,9 @@ def render_images_sitemap(request, sitemaps, section=None, template_name='qartez
maps, urls = [], []
if section is not None:
if section not in sitemaps:
raise Http404("No sitemap available for section: %r" % section)
raise Http404(
"No sitemap available for section: {0}".format(section)
)
maps.append(sitemaps[section])
else:
maps = sitemaps.values()
Expand All @@ -32,8 +35,13 @@ def render_images_sitemap(request, sitemaps, section=None, template_name='qartez
else:
urls.extend(site.get_urls(page))
except EmptyPage:
raise Http404("Page %s empty" % page)
raise Http404("Page {0} empty".format(page))
except PageNotAnInteger:
raise Http404("No page '%s'" % page)
xml = smart_str(loader.render_to_string(template_name, {'urlset': urls, 'request': request}))
return HttpResponse(xml, mimetype='application/xml')
raise Http404("No page {0}".format(page))
xml = smart_str(loader.render_to_string(
template_name, {'urlset': urls, 'request': request})
)
try:
return HttpResponse(xml, mimetype='application/xml')
except TypeError:
return HttpResponse(xml, content_type='application/xml')

0 comments on commit 36fd2ea

Please sign in to comment.