forked from cartwheelweb/packaginator
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'issue-51' of https://github.com/ojii/djangopackages int…
…o ojii-issue-51
- Loading branch information
Showing
9 changed files
with
168 additions
and
2 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# -*- coding: utf-8 -*- | ||
from classytags.helpers import InclusionTag | ||
from django import template | ||
from django.conf import settings | ||
|
||
|
||
register = template.Library() | ||
|
||
|
||
class FixedGA(InclusionTag): | ||
template = 'templatetags/ga.html' | ||
|
||
name = 'fixed_ga' | ||
|
||
def render_tag(self, context): | ||
URCHIN_ID = getattr(settings, "URCHIN_ID", None) | ||
if URCHIN_ID and not settings.DEBUG: | ||
return super(FixedGA, self).render_tag(context) | ||
return '' | ||
|
||
def get_context(self, context): | ||
""" | ||
If it get's here, we already checked that this setting is set! | ||
""" | ||
return {'URCHIN_ID': settings.URCHIN_ID} | ||
|
||
register.tag(FixedGA) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
# -*- coding: utf-8 -*- | ||
from django.conf import settings | ||
from django.utils.translation import get_language, activate | ||
from shutil import rmtree as _rmtree | ||
from tempfile import template, mkdtemp, _exists | ||
|
||
class NULL: | ||
pass | ||
|
||
class SettingsOverride(object): | ||
""" | ||
Overrides Django settings within a context and resets them to their inital | ||
values on exit. | ||
Example: | ||
with SettingsOverride(DEBUG=True): | ||
# do something | ||
""" | ||
|
||
def __init__(self, **overrides): | ||
self.overrides = overrides | ||
|
||
def __enter__(self): | ||
self.old = {} | ||
for key, value in self.overrides.items(): | ||
self.old[key] = getattr(settings, key, NULL) | ||
setattr(settings, key, value) | ||
|
||
def __exit__(self, type, value, traceback): | ||
for key, value in self.old.items(): | ||
if value is not NULL: | ||
setattr(settings, key, value) | ||
else: | ||
delattr(settings,key) # do not pollute the context! | ||
|
||
|
||
class LanguageOverride(object): | ||
def __init__(self, language): | ||
self.newlang = language | ||
|
||
def __enter__(self): | ||
self.oldlang = get_language() | ||
activate(self.newlang) | ||
|
||
def __exit__(self, type, value, traceback): | ||
activate(self.oldlang) | ||
|
||
|
||
class TemporaryDirectory: | ||
"""Create and return a temporary directory. This has the same | ||
behavior as mkdtemp but can be used as a context manager. For | ||
example: | ||
with TemporaryDirectory() as tmpdir: | ||
... | ||
Upon exiting the context, the directory and everthing contained | ||
in it are removed. | ||
""" | ||
|
||
def __init__(self, suffix="", prefix=template, dir=None): | ||
self.name = mkdtemp(suffix, prefix, dir) | ||
|
||
def __enter__(self): | ||
return self.name | ||
|
||
def cleanup(self): | ||
if _exists(self.name): | ||
_rmtree(self.name) | ||
|
||
def __exit__(self, exc, value, tb): | ||
self.cleanup() | ||
|
||
|
||
class ChangeModel(object): | ||
""" | ||
Changes attributes on a model while within the context. | ||
These changes *ARE* saved to the database for the context! | ||
""" | ||
def __init__(self, instance, **overrides): | ||
self.instance = instance | ||
self.overrides = overrides | ||
|
||
def __enter__(self): | ||
self.old = {} | ||
for key, value in self.overrides.items(): | ||
self.old[key] = getattr(self.instance, key, NULL) | ||
setattr(self.instance, key, value) | ||
self.instance.save() | ||
|
||
def __exit__(self, exc, value, tb): | ||
for key in self.overrides.keys(): | ||
old_value = self.old[key] | ||
if old_value is NULL: | ||
delattr(self.instance, key) | ||
else: | ||
setattr(self.instance, key, old_value) | ||
self.instance.save() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# -*- coding: utf-8 -*- | ||
from core.test_utils.context_managers import SettingsOverride | ||
from django import template | ||
from django.test.testcases import TestCase | ||
|
||
|
||
class PackaginatorTagsTests(TestCase): | ||
def test_fixed_ga(self): | ||
tpl = template.Template(""" | ||
{% load packaginator_tags %} | ||
{% fixed_ga %} | ||
""") | ||
context = template.Context() | ||
|
||
with SettingsOverride(URCHIN_ID='testid', DEBUG=False): | ||
output = tpl.render(context) | ||
self.assertTrue('var pageTracker = _gat._getTracker("testid");' in output) | ||
|
||
with SettingsOverride(URCHIN_ID='testid', DEBUG=True): | ||
output = tpl.render(context) | ||
self.assertEqual(output.strip(), "") | ||
|
||
with SettingsOverride(URCHIN_ID=None, DEBUG=True): | ||
output = tpl.render(context) | ||
self.assertEqual(output.strip(), "") | ||
|
||
with SettingsOverride(URCHIN_ID=None, DEBUG=False): | ||
output = tpl.render(context) | ||
self.assertEqual(output.strip(), "") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -145,6 +145,7 @@ | |
PROJECT_APPS = [ | ||
"about", | ||
"grid", | ||
'core', | ||
"homepage", | ||
"package", | ||
"profiles", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<script type="text/javascript"> | ||
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www."); | ||
document.write(unescape("%%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%%3E%%3C/script%%3E")); | ||
</script> | ||
<script type="text/javascript"> | ||
var pageTracker = _gat._getTracker("{{ URCHIN_ID }}"); | ||
pageTracker._trackPageview(); | ||
</script> |