Permalink
Browse files

Add country field to snippets.

- Adds a country field to the snippet model.
- Adds a data-attribute to the snippet container div if the snippet has
  a country specified.
  • Loading branch information...
Osmose committed Mar 6, 2013
1 parent fdf5157 commit 4bf6d8c81cc93d6938153f26d9bac206c5a6ba12
View
@@ -151,17 +151,13 @@ class SnippetAdmin(admin.ModelAdmin):
'name', 'body',
)
- list_filter = (
- 'pub_start',
- 'pub_end',
- 'client_match_rules',
- )
+ list_filter = ('pub_start', 'pub_end', 'client_match_rules', 'country')
fields = (
'name', 'body',
'preview', 'disabled',
'priority', 'pub_start', 'pub_end',
- 'client_match_rules',
+ 'country', 'client_match_rules',
)
list_per_page = 250
@@ -170,7 +166,7 @@ class SnippetAdmin(admin.ModelAdmin):
'name',
'disabled',
'priority', 'pub_start', 'pub_end',
- 'modified'
+ 'country', 'modified',
)
list_links = (
@@ -12,6 +12,29 @@
from django.db.models.signals import post_save, post_delete
from django.utils.translation import ugettext_lazy as _
+from product_details import product_details
+
+
+ENGLISH_COUNTRY_CHOICES = sorted(
+ [(code, u'{0} ({1})'.format(name, code)) for code, name in
+ product_details.get_regions('en-US').items()],
+ cmp=lambda x, y: cmp(x[1], y[1])
+)
+
+
+class CountryField(models.CharField):
+ description = ('CharField with country settings specific to snippets '
+ 'defaults.')
+
+ def __init__(self, *args, **kwargs):
+ options = {
+ 'max_length': 16,
+ 'default': u'us',
+ 'choices': ENGLISH_COUNTRY_CHOICES
+ }
+ options.update(kwargs)
+ return super(CountryField, self).__init__(*args, **options)
+
CACHE_TIMEOUT = getattr(settings, 'SNIPPET_MODEL_CACHE_TIMEOUT')
@@ -310,6 +333,7 @@ def find_snippets_for_rule_ids(self, preview, include_ids, exclude_ids):
id=snippet.id,
name=snippet.name,
body=snippet.body,
+ country=snippet.country,
pub_start=snippet.pub_start,
pub_end=snippet.pub_end,
)
@@ -352,6 +376,8 @@ class Meta():
modified = models.DateTimeField( _('date last modified'),
auto_now=True, blank=False)
+ country = CountryField('Geolocation Country', blank=True, default='')
+
def snippet_update_lastmod(sender, instance, **kwargs):
"""On a change to a snippet, bump its cached lastmod timestamp"""
@@ -0,0 +1,34 @@
+from pyquery import PyQuery as pq
+
+from homesnippets.tests.utils import HomesnippetsTestCase
+
+
+class ViewSnippetsTests(HomesnippetsTestCase):
+ def test_snippet_country(self):
+ """
+ If a snippet has a country, it should have the data-country attribute
+ set on it's container. Otherwise, it should not have the attribute.
+ """
+ rules = self.setup_rules({
+ 'fields': (),
+ 'items': {
+ 'all': (),
+ }
+ })
+
+ snippets = self.setup_snippets(rules, {
+ 'fields': ('name', 'body', 'country', 'rules'),
+ 'items': {
+ 'test1': ('Test1', 'test1', 'us', (rules['all'],)),
+ 'test2': ('Test2', 'test2', '', (rules['all'],)),
+ }
+ })
+
+ response = self.browser.get('/1/Firefox/4.0/xxx/xxx/en-US/xxx/xxx/'
+ 'default/default/')
+ d = pq(response.content)
+ test1 = d('div[data-snippet-id="%s"]' % snippets['test1'].id)
+ test2 = d('div[data-snippet-id="%s"]' % snippets['test2'].id)
+
+ self.assertEqual(test1.attr('data-country'), 'us')
+ self.assertEqual(test2.attr('data-country'), None) # Doesn't exist.
View
@@ -51,8 +51,15 @@ def view_snippets(request, **kwargs):
if len(snippets) == 0:
out_txt = ''
else:
- out = ['<div data-snippet-id="%s">%s</div>' %
- (snippet['id'], snippet['body']) for snippet in snippets]
+ out = []
+ for snippet in snippets:
+ attrs = {'data-snippet-id': snippet['id']}
+ if snippet['country']:
+ attrs['data-country'] = snippet['country']
+
+ attrs_string = ' '.join(['%s="%s"' % (key, value) for key, value in
+ attrs.items()])
+ out.append('<div %s>%s</div>' % (attrs_string, snippet['body']))
out.append('<!-- content generated at %s -->' %
(strftime('%Y-%m-%dT%H:%M:%SZ', gmtime())))
@@ -0,0 +1,2 @@
+-- Add column for country-based geolocation
+ALTER TABLE `homesnippets_snippet` ADD COLUMN `country` varchar(16) NOT NULL DEFAULT '';
View
@@ -7,6 +7,7 @@ werkzeug
django-debug-toolbar
django-extensions
django-devserver
+pyquery
-e git://github.com/cmheisel/nose-xcover.git#egg=nosexcover
-e git://github.com/jbalogh/django-nose.git#egg=django_nose
View
@@ -115,6 +115,7 @@
# Uncomment the next line to enable the admin:
'django.contrib.admin',
'smuggler',
+ 'product_details',
'homesnippets',
)
2 vendor
Submodule vendor updated 1 files
+1 −1 src/django-mozilla-product-details

0 comments on commit 4bf6d8c

Please sign in to comment.