0002-add-simple-subscription-API-13284.patch
corbo/api_urls.py | ||
---|---|---|
14 | 14 |
# You should have received a copy of the GNU Affero General Public License |
15 | 15 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
16 | 16 | |
17 |
from django.conf.urls import patterns, include, url |
|
17 |
from django.conf.urls import patterns, url |
|
18 | ||
19 |
from .api_views import NewslettersView, SubscriptionsView, SubscribeView |
|
18 | 20 | |
19 |
from .api_views import NewslettersView, SubscriptionsView |
|
20 | 21 | |
21 | 22 |
urlpatterns = patterns('', |
22 | 23 |
url(r'^newsletters/', NewslettersView.as_view(), name='newsletters'), |
23 | 24 |
url(r'^subscriptions/', SubscriptionsView.as_view(), name='subscriptions'), |
25 |
url(r'^subscribe/', SubscribeView.as_view(), name='subscribe'), |
|
24 | 26 |
) |
corbo/api_views.py | ||
---|---|---|
43 | 43 | |
44 | 44 |
def get_subscriptions(self, email, uuid=None): |
45 | 45 |
subscriptions = defaultdict(dict) |
46 |
identifier = 'mailto:'+email
|
|
47 |
for s in Subscription.objects.filter(Q(identifier=identifier)|Q(uuid=uuid)):
|
|
46 |
identifier = 'mailto:' + email
|
|
47 |
for s in Subscription.objects.filter(Q(identifier=identifier) | Q(uuid=uuid)):
|
|
48 | 48 |
cat_id = s.category.pk |
49 | 49 |
subscriptions[cat_id]['id'] = str(cat_id) |
50 | 50 |
subscriptions[cat_id]['text'] = s.category.name |
... | ... | |
112 | 112 |
for subscription in self.get_subscriptions(email): |
113 | 113 |
self.update_subscriptions(subscription['id'], [], email, uuid) |
114 | 114 |
return Response({'data': True}) |
115 | ||
116 | ||
117 |
class SubscribeView(SubscriptionsView): |
|
118 | ||
119 |
def post(self, request): |
|
120 |
email = request.GET.get('email') |
|
121 |
uuid = request.GET.get('uuid') |
|
122 |
if not email: |
|
123 |
raise PermissionDenied('Email parameter required') |
|
124 |
data = json.loads(request.body) |
|
125 | ||
126 |
category_id = data['category_id'] |
|
127 |
transport = ('mailto',) |
|
128 | ||
129 |
self.update_subscriptions(category_id, transport, email, uuid) |
|
130 | ||
131 |
return Response({'data': True}) |
tests/test_api.py | ||
---|---|---|
95 | 95 |
if resp.json['data']: |
96 | 96 |
resp = app.get(subscriptions_url, status=200) |
97 | 97 |
assert resp.json['data'] == [] |
98 | ||
99 | ||
100 |
def test_simple_subscription(app, categories): |
|
101 |
payload = {'category_id': 1} |
|
102 |
resp = app.post_json('/api/subscribe/?email=john@example.net', payload, status=200) |
|
103 |
assert resp.json['data'] is True |
|
104 | ||
105 |
resp = app.get('/api/subscribe/?email=john@example.net') |
|
106 | ||
107 |
data = resp.json['data'] |
|
108 |
assert len(data) == 1 |
|
109 |
assert data[0]['id'] == '1' |
|
110 |
assert data[0]['text'] == 'Alerts' |
|
111 |
assert len(data[0]['transports']) == 1 |
|
112 |
transport = data[0]['transports'][0] |
|
113 |
assert transport['id'] == 'mailto' |
|
114 |
assert transport['text'] == 'mailto' |
|
98 |
- |