0001-api-use-categories-slugs-instead-of-ids-13791.patch
corbo/api_views.py | ||
---|---|---|
33 | 33 |
newsletters = [] |
34 | 34 |
transports = [{'id': identifier, 'text': name} for identifier, name in channel_choices] |
35 | 35 |
for c in Category.objects.all(): |
36 |
newsletter = {'id': str(c.pk), 'text': c.name,
|
|
36 |
newsletter = {'id': c.slug, 'text': c.name,
|
|
37 | 37 |
'transports': transports} |
38 | 38 |
newsletters.append(newsletter) |
39 | 39 |
return Response({'data': newsletters}) |
... | ... | |
46 | 46 |
identifier = 'mailto:' + email |
47 | 47 |
for s in Subscription.objects.filter(Q(identifier=identifier) | Q(uuid=uuid)): |
48 | 48 |
cat_id = s.category.pk |
49 |
subscriptions[cat_id]['id'] = str(cat_id)
|
|
49 |
subscriptions[cat_id]['id'] = s.category.slug
|
|
50 | 50 |
subscriptions[cat_id]['text'] = s.category.name |
51 | 51 |
transport_id, transport_name = identifier.split(':') |
52 | 52 |
transport = {'id': transport_id, |
... | ... | |
59 | 59 |
return subscriptions.values() |
60 | 60 | |
61 | 61 |
@transaction.atomic |
62 |
def update_subscriptions(self, category_id, transports, email, uuid=None):
|
|
62 |
def update_subscriptions(self, category_slug, transports, email, uuid=None):
|
|
63 | 63 |
uuid = uuid or u'' |
64 | 64 |
identifier = u'mailto:' |
65 | 65 |
if email: |
66 | 66 |
identifier += email |
67 | 67 |
try: |
68 |
cat = Category.objects.get(pk=category_id)
|
|
68 |
cat = Category.objects.get(slug=category_slug)
|
|
69 | 69 |
except Category.DoesNotExist: |
70 | 70 |
return |
71 | 71 |
corbo/forms.py | ||
---|---|---|
1 | 1 |
from django import forms |
2 | 2 |
from django.utils.translation import ugettext_lazy as _ |
3 |
from django.utils.text import slugify |
|
3 | 4 | |
4 | 5 |
from .models import Announce, Category, Broadcast, channel_choices |
5 | 6 | |
... | ... | |
28 | 29 |
class Meta: |
29 | 30 |
fields = ('name', 'rss_feed_url') |
30 | 31 |
model = Category |
32 | ||
33 |
def save(self, commit=True): |
|
34 |
if not self.instance.slug: |
|
35 |
self.instance.slug = slugify(self.instance.name) |
|
36 |
return super(CategoryForm, self).save(commit=commit) |
corbo/migrations/0008_category_slug.py | ||
---|---|---|
1 |
# -*- coding: utf-8 -*- |
|
2 |
from __future__ import unicode_literals |
|
3 | ||
4 |
from django.db import migrations, models |
|
5 | ||
6 | ||
7 |
class Migration(migrations.Migration): |
|
8 | ||
9 |
dependencies = [ |
|
10 |
('corbo', '0007_auto_20160928_1454'), |
|
11 |
] |
|
12 | ||
13 |
operations = [ |
|
14 |
migrations.AddField( |
|
15 |
model_name='category', |
|
16 |
name='slug', |
|
17 |
field=models.SlugField(default='', verbose_name='Slug'), |
|
18 |
preserve_default=False, |
|
19 |
), |
|
20 |
] |
corbo/models.py | ||
---|---|---|
37 | 37 | |
38 | 38 |
class Category(models.Model): |
39 | 39 |
name = models.CharField(_('Name'), max_length=64, blank=False, null=False) |
40 |
slug = models.SlugField(_('Slug')) |
|
40 | 41 |
rss_feed_url = models.URLField(_('Feed URL'), blank=True, null=True, |
41 | 42 |
help_text=_('if defined, announces will be automatically created from rss items')) |
42 | 43 |
ctime = models.DateTimeField(auto_now_add=True) |
tests/test_api.py | ||
---|---|---|
5 | 5 |
from django.core.urlresolvers import reverse |
6 | 6 |
from django.utils.http import urlencode |
7 | 7 |
from django.contrib.auth import get_user_model |
8 |
from django.utils.text import slugify |
|
8 | 9 | |
9 | 10 |
from corbo.models import Category, Announce, Broadcast, Subscription |
10 | 11 |
from corbo.models import channel_choices |
11 | 12 | |
12 | 13 |
pytestmark = pytest.mark.django_db |
13 | 14 | |
14 |
CATEGORIES = ('Alerts', 'News')
|
|
15 |
CATEGORIES = (u'Alerts', u'News')
|
|
15 | 16 | |
16 | 17 | |
17 | 18 |
@pytest.fixture |
18 | 19 |
def categories(): |
19 | 20 |
categories = [] |
20 | 21 |
for category in CATEGORIES: |
21 |
c, created = Category.objects.get_or_create(name=category) |
|
22 |
c, created = Category.objects.get_or_create(name=category, slug=slugify(category))
|
|
22 | 23 |
categories.append(c) |
23 | 24 |
return categories |
24 | 25 | |
... | ... | |
55 | 56 |
for category in data['data']: |
56 | 57 |
assert 'id' in category |
57 | 58 |
assert 'text' in category |
59 |
assert category['id'] in [slugify(c) for c in CATEGORIES] |
|
58 | 60 |
assert category['text'] in CATEGORIES |
59 | 61 |
assert 'transports' in category |
60 | 62 |
assert category['transports'] == [{'id': 'mailto', 'text': 'Email'}] |
... | ... | |
73 | 75 |
assert 'data' in resp.json |
74 | 76 |
data = resp.json['data'] |
75 | 77 |
for d in data: |
76 |
assert d['id'] in [str(category.id) for category in categories]
|
|
78 |
assert d['id'] in [category.slug for category in categories]
|
|
77 | 79 |
assert d['text'] in [category.name for category in categories] |
78 | 80 |
for t in d['transports']: |
79 | 81 |
assert t['id'] == identifier |
... | ... | |
114 | 116 | |
115 | 117 | |
116 | 118 |
def test_simple_subscription(app, categories, user): |
117 |
payload = {'category_id': 1}
|
|
119 |
payload = {'category_id': 'alerts'}
|
|
118 | 120 |
url = '/api/subscribe/?email=john@example.net' |
119 | 121 | |
120 | 122 |
# anonymous |
... | ... | |
135 | 137 | |
136 | 138 |
data = resp.json['data'] |
137 | 139 |
assert len(data) == 1 |
138 |
assert data[0]['id'] == '1'
|
|
140 |
assert data[0]['id'] == 'alerts'
|
|
139 | 141 |
assert data[0]['text'] == 'Alerts' |
140 | 142 |
assert len(data[0]['transports']) == 1 |
141 | 143 |
transport = data[0]['transports'][0] |
142 |
- |