Skip to content

Commit

Permalink
Merge pull request #101 from glogiotatidis/1134491
Browse files Browse the repository at this point in the history
[fix bug 1134491] Active snippets view.
  • Loading branch information
glogiotatidis committed Mar 5, 2015
2 parents d4f3b69 + 03ae9b3 commit 025339e
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 12 deletions.
41 changes: 38 additions & 3 deletions snippets/base/encoders.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import json
from datetime import datetime

from snippets.base.models import JSONSnippet
from snippets.base.models import JSONSnippet, Snippet


class SnippetEncoder(json.JSONEncoder):
class JSONSnippetEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, JSONSnippet):
data = {
Expand All @@ -16,4 +17,38 @@ def default(self, obj):
if obj.country:
data['target_geo'] = obj.country.upper()
return data
return super(SnippetEncoder, self).default(obj)
return super(JSONSnippetEncoder, self).default(obj)


class ActiveSnippetsEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, JSONSnippet):
data = {
'id': obj.id,
'type': 'JSON Snippet',
'template': 'default',
'publish_start': obj.publish_start,
'publish_end': obj.publish_end,
'on_release': obj.on_release,
'on_beta': obj.on_beta,
'on_aurora': obj.on_aurora,
'on_nightly': obj.on_nightly,
}
return data
elif isinstance(obj, Snippet):
data = {
'id': obj.id,
'type': 'Desktop Snippet',
'template': obj.template.id,
'publish_start': obj.publish_start,
'publish_end': obj.publish_end,
'on_release': obj.on_release,
'on_beta': obj.on_beta,
'on_aurora': obj.on_aurora,
'on_nightly': obj.on_nightly,
}
return data
elif isinstance(obj, datetime):
return obj.isoformat()

return super(ActiveSnippetsEncoder, self).default(obj)
57 changes: 51 additions & 6 deletions snippets/base/tests/test_encoders.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
from datetime import datetime

from mock import patch
from nose.tools import eq_

from snippets.base.encoders import SnippetEncoder
from snippets.base.tests import JSONSnippetFactory, TestCase
from snippets.base.encoders import ActiveSnippetsEncoder, JSONSnippetEncoder
from snippets.base.tests import JSONSnippetFactory, SnippetFactory, TestCase


class SnippetEncoderTests(TestCase):
class JSONSnippetEncoderTests(TestCase):
def test_encode_jsonsnippet(self):
encoder = SnippetEncoder()
encoder = JSONSnippetEncoder()
data = {'id': 99, 'text': 'test-text',
'icon': 'test-icon', 'url': 'test-url',
'country': 'us', 'weight': 100}
Expand All @@ -17,7 +19,7 @@ def test_encode_jsonsnippet(self):
eq_(result, data)

def test_encode_without_country(self):
encoder = SnippetEncoder()
encoder = JSONSnippetEncoder()
data = {'id': 99, 'text': 'test-text',
'icon': 'test-icon', 'url': 'test-url',
'weight': 100}
Expand All @@ -27,7 +29,50 @@ def test_encode_without_country(self):

@patch('snippets.base.encoders.json.JSONEncoder.default')
def test_encode_other(self, default_mock):
encoder = SnippetEncoder()
encoder = JSONSnippetEncoder()
data = {'id': 3}
encoder.default(data)
default_mock.assert_called_with(data)


class ActiveSnippetsEncoderTests(TestCase):
def test_encode_jsonsnippet(self):
encoder = ActiveSnippetsEncoder()
now = datetime.now()
data = {'id': 99, 'text': 'test-text', 'publish_start': now}
snippet = JSONSnippetFactory.build(**data)
result = encoder.default(snippet)
eq_(result, {'id': 99,
'type': 'JSON Snippet',
'template': 'default',
'publish_start': now,
'publish_end': None,
'on_release': True,
'on_beta': False,
'on_aurora': False,
'on_nightly': False,
})

def test_encode_snippet(self):
encoder = ActiveSnippetsEncoder()
now = datetime.now()
data = {'id': 99, 'publish_start': now}
snippet = SnippetFactory.create(**data)
result = encoder.default(snippet)
eq_(result, {'id': 99,
'type': 'Desktop Snippet',
'template': snippet.template.id,
'publish_start': now,
'publish_end': None,
'on_release': True,
'on_beta': False,
'on_aurora': False,
'on_nightly': False,
})

@patch('snippets.base.encoders.json.JSONEncoder.default')
def test_encode_other(self, default_mock):
encoder = ActiveSnippetsEncoder()
data = {'id': 3}
encoder.default(data)
default_mock.assert_called_with(data)
18 changes: 18 additions & 0 deletions snippets/base/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,3 +416,21 @@ def test_flag_on(self):
eq_(views.fetch_snippets(self.request, foo='bar'),
fetch_pregenerated_snippets.return_value)
fetch_pregenerated_snippets.assert_called_with(self.request, foo='bar')


class ActiveSnippetsViewTests(TestCase):
def setUp(self):
self.factory = RequestFactory()
self.request = self.factory.get('/')

def test_base(self):
snippets = SnippetFactory.create_batch(2)
jsonsnippets = JSONSnippetFactory.create_batch(2)
SnippetFactory.create(disabled=True)
JSONSnippetFactory.create(disabled=True)
response = views.ActiveSnippetsView.as_view()(self.request)
eq_(response.get('content-type'), 'application/json')
data = json.loads(response.content)
eq_(set([snippets[0].id, snippets[1].id,
jsonsnippets[0].id, jsonsnippets[1].id]),
set([x['id'] for x in data]))
1 change: 1 addition & 0 deletions snippets/base/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@
url(r'^preview/$', views.preview_snippet, name='base.preview'),
url(r'^show/(?P<snippet_id>\d+)/$', views.show_snippet, name='base.show'),
url(r'^json-snippets/', views.JSONSnippetIndexView.as_view(), name='base.index_json'),
url(r'^active-snippets.json', views.ActiveSnippetsView.as_view(), name='base.active_snippets'),
)
14 changes: 11 additions & 3 deletions snippets/base/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from django.shortcuts import get_object_or_404, render
from django.utils.cache import patch_vary_headers
from django.utils.functional import lazy
from django.views.generic import TemplateView
from django.views.generic import TemplateView, View
from django.views.decorators.cache import cache_control
from django.views.decorators.csrf import csrf_exempt

Expand All @@ -19,7 +19,7 @@
from commonware.response.decorators import xframe_allow

from snippets.base.decorators import access_control
from snippets.base.encoders import SnippetEncoder
from snippets.base.encoders import ActiveSnippetsEncoder, JSONSnippetEncoder
from snippets.base.models import Client, JSONSnippet, Snippet, SnippetBundle, SnippetTemplate
from snippets.base.util import get_object_or_none

Expand Down Expand Up @@ -142,7 +142,7 @@ def fetch_json_snippets(request, **kwargs):
.match_client(client)
.order_by('priority')
.filter_by_available())
return HttpResponse(json.dumps(matching_snippets, cls=SnippetEncoder),
return HttpResponse(json.dumps(matching_snippets, cls=JSONSnippetEncoder),
mimetype='application/json')


Expand Down Expand Up @@ -201,3 +201,11 @@ def show_snippet(request, snippet_id):
'client': PREVIEW_CLIENT,
'preview': True
})


class ActiveSnippetsView(View):
def get(self, request):
snippets = (list(Snippet.cached_objects.filter(disabled=False)) +
list(JSONSnippet.cached_objects.filter(disabled=False)))
return HttpResponse(json.dumps(snippets, cls=ActiveSnippetsEncoder),
mimetype='application/json')

0 comments on commit 025339e

Please sign in to comment.