0001-create-announces-from-external-RSS-feed-12919.patch
corbo/forms.py | ||
---|---|---|
8 | 8 | |
9 | 9 |
class Meta: |
10 | 10 |
model = Announce |
11 |
fields = '__all__'
|
|
11 |
exclude = ('identifier',)
|
|
12 | 12 |
widgets = { |
13 | 13 |
'publication_time': forms.TextInput(attrs={'class': 'datetimepicker', |
14 | 14 |
'readonly': True}), |
... | ... | |
26 | 26 | |
27 | 27 |
class CategoryForm(forms.ModelForm): |
28 | 28 |
class Meta: |
29 |
fields = ('name', ) |
|
29 |
fields = ('name', 'rss_feed_url')
|
|
30 | 30 |
model = Category |
corbo/management/commands/sync_external_feeds.py | ||
---|---|---|
1 |
# corbo - Announces Manager |
|
2 |
# Copyright (C) 2016 Entr'ouvert |
|
3 |
# |
|
4 |
# This program is free software: you can redistribute it and/or modify it |
|
5 |
# under the terms of the GNU Affero General Public License as published |
|
6 |
# by the Free Software Foundation, either version 3 of the License, or |
|
7 |
# (at your option) any later version. |
|
8 |
# |
|
9 |
# This program is distributed in the hope that it will be useful, |
|
10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
# GNU Affero General Public License for more details. |
|
13 |
# |
|
14 |
# You should have received a copy of the GNU Affero General Public License |
|
15 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 | ||
17 |
from django.core.management.base import BaseCommand, CommandError |
|
18 | ||
19 |
from corbo.models import Category |
|
20 | ||
21 |
class Command(BaseCommand): |
|
22 | ||
23 |
def handle(self, *args, **kwargs): |
|
24 |
for c in Category.objects.filter(rss_feed_url__isnull=False): |
|
25 |
c.save() |
corbo/migrations/0006_auto_20160927_1543.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', '0005_auto_20160504_1747'), |
|
11 |
] |
|
12 | ||
13 |
operations = [ |
|
14 |
migrations.AddField( |
|
15 |
model_name='announce', |
|
16 |
name='identifier', |
|
17 |
field=models.CharField(max_length=256, null=True, blank=True), |
|
18 |
), |
|
19 |
migrations.AddField( |
|
20 |
model_name='category', |
|
21 |
name='rss_feed_url', |
|
22 |
field=models.URLField(help_text='if defined, announces will be automatically created from rss items', null=True, blank=True), |
|
23 |
), |
|
24 |
] |
corbo/models.py | ||
---|---|---|
5 | 5 |
from html2text import HTML2Text |
6 | 6 |
from emails.django import Message |
7 | 7 |
from lxml.etree import HTML as HTMLTree |
8 |
import requests |
|
9 |
import feedparser |
|
8 | 10 | |
9 | 11 |
from django.utils import timezone |
10 | 12 |
from django.conf import settings |
... | ... | |
26 | 28 | |
27 | 29 |
class Category(models.Model): |
28 | 30 |
name = models.CharField(max_length=64, blank=False, null=False) |
31 |
rss_feed_url = models.URLField(blank=True, null=True, |
|
32 |
help_text=_('if defined, announces will be automatically created from rss items')) |
|
29 | 33 |
ctime = models.DateTimeField(auto_now_add=True) |
30 | 34 | |
31 | 35 |
def __unicode__(self): |
... | ... | |
37 | 41 |
def get_subscriptions_count(self): |
38 | 42 |
return self.subscription_set.all().count() |
39 | 43 | |
44 |
def save(self, *args, **kwargs): |
|
45 |
super(Category, self).save(*args, **kwargs) |
|
46 |
if not self.rss_feed_url: |
|
47 |
return |
|
48 |
feed_response = requests.get(self.rss_feed_url) |
|
49 |
if feed_response.ok: |
|
50 |
content = feedparser.parse(feed_response.content) |
|
51 |
for entry in content.get('entries', []): |
|
52 |
announce, created = Announce.objects.get_or_create(identifier=entry['id'], |
|
53 |
category=self, defaults={'title': entry['title']}) |
|
54 |
if created: |
|
55 |
announce.publication_time = entry['updated'] |
|
56 |
announce.text = entry['summary'] |
|
57 |
announce.save() |
|
58 | ||
59 | ||
40 | 60 | |
41 | 61 |
class Announce(models.Model): |
42 | 62 |
category = models.ForeignKey('Category', verbose_name=_('category')) |
43 | 63 |
title = models.CharField(_('title'), max_length=256, |
44 | 64 |
help_text=_('maximum 256 characters')) |
65 |
identifier = models.CharField(max_length=256, null=True, blank=True) |
|
45 | 66 |
text = RichTextField(_('Content')) |
46 | 67 |
publication_time = models.DateTimeField(_('publication time'), blank=True, |
47 | 68 |
null=True) |
corbo/templates/corbo/category_detail.html | ||
---|---|---|
11 | 11 |
{% block appbar %} |
12 | 12 |
<h2>{{ object.name }}</h2> |
13 | 13 |
<a href="{% url 'delete_category' object.id %}" rel="popup">{% trans 'Delete' %}</a> |
14 |
<a href="{% url 'edit_category' object.id %}" rel="popup">{% trans 'Rename' %}</a>
|
|
14 |
<a href="{% url 'edit_category' object.id %}" rel="popup">{% trans 'Edit' %}</a>
|
|
15 | 15 |
<a href="{% url 'add_announce' pk=object.pk %}">{% trans 'New announce' %}</a> |
16 | 16 |
{% endblock %} |
17 | 17 |
corbo/templates/corbo/category_form.html | ||
---|---|---|
3 | 3 | |
4 | 4 |
{% block appbar %} |
5 | 5 |
{% if object %} |
6 |
<h2>{% trans "Modify Category" %}</h2>
|
|
6 |
<h2>{% trans "Edit Category" %}</h2>
|
|
7 | 7 |
{% else %} |
8 | 8 |
<h2>{% trans "New Category" %}</h2> |
9 | 9 |
{% endif %} |
debian/control | ||
---|---|---|
12 | 12 |
python-django (>= 1.7), |
13 | 13 |
python-django-ckeditor, |
14 | 14 |
python-gadjo, |
15 |
python-emails |
|
15 |
python-emails, |
|
16 |
python-requests, |
|
17 |
python-feedparser |
|
16 | 18 |
Description: Announces Manager |
17 | 19 | |
18 | 20 |
Package: corbo |
debian/corbo.cron.d | ||
---|---|---|
1 | 1 |
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin |
2 | 2 | |
3 | 3 |
*/5 * * * * corbo corbo-manage tenant_command send_announces --all-tenants |
4 |
0 * * * * corbo corbo-manage tenant_command sync_external_feeds --all-tenants |
requirements.txt | ||
---|---|---|
3 | 3 |
djangorestframework>=3.3,<3.4 |
4 | 4 |
html2text |
5 | 5 |
emails |
6 |
feedparser |
|
7 |
requests |
|
6 | 8 |
-e git+http://repos.entrouvert.org/gadjo.git/#egg=gadjo |
setup.py | ||
---|---|---|
100 | 100 |
'gadjo', |
101 | 101 |
'emails', |
102 | 102 |
'lxml', |
103 |
'feedparser', |
|
104 |
'requests' |
|
103 | 105 |
], |
104 | 106 |
zip_safe=False, |
105 | 107 |
cmdclass={ |
tests/test_announces.py | ||
---|---|---|
1 |
import pytest |
|
2 |
import mock |
|
3 |
import feedparser |
|
4 | ||
5 |
from django.core.urlresolvers import reverse |
|
6 |
from django.core.urlresolvers import reverse |
|
7 | ||
8 |
from corbo.models import Category |
|
9 | ||
10 |
pytestmark = pytest.mark.django_db |
|
11 | ||
12 |
CATEGORIES = ('Alerts',) |
|
13 | ||
14 |
ATOM_FEED = """ |
|
15 |
<?xml version="1.0" encoding="UTF-8"?> |
|
16 |
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom" xmlns:wfw="http://wellformedweb.org/CommentAPI/"> |
|
17 |
<id>tag:linuxfr.org,2005:/news</id> |
|
18 |
<title>Sample RSS Feeds</title> |
|
19 |
<updated>2016-09-16T10:29:46+02:00</updated> |
|
20 |
<entry> |
|
21 |
<id>tag:linuxfr.org,2005:News/37537</id> |
|
22 |
<published>2016-09-16T10:29:46+02:00</published> |
|
23 |
<updated>2016-09-16T11:27:00+02:00</updated> |
|
24 |
<title>Feed entry sample</title> |
|
25 |
<content type="html"> |
|
26 |
Feed entry content |
|
27 |
</content> |
|
28 |
<author> |
|
29 |
<name>Foo Bar</name> |
|
30 |
</author> |
|
31 |
</entry> |
|
32 |
</feed> |
|
33 |
""" |
|
34 | ||
35 | ||
36 | ||
37 |
@mock.patch('corbo.models.requests.get') |
|
38 |
def test_announces_from_feed(mocked_get): |
|
39 |
feed_content = feedparser.parse(ATOM_FEED) |
|
40 |
mocked_get.return_value = mock.Mock(ok=True, content=ATOM_FEED) |
|
41 |
for category in CATEGORIES: |
|
42 |
c = Category.objects.create(name=category, rss_feed_url='http://example.com/atom') |
|
43 |
assert c.announce_set.count() == len(feed_content['entries']) |
|
44 |
for announce in c.announce_set.all(): |
|
45 |
assert announce.title in [feed['title'] for feed in feed_content['entries']] |
tox.ini | ||
---|---|---|
19 | 19 |
djangorestframework>=3.3,<3.4 |
20 | 20 |
pylint==1.4.0 |
21 | 21 |
astroid==1.3.2 |
22 |
mock |
|
22 | 23 |
commands = |
23 | 24 |
py.test {env:COVERAGE:} {posargs:tests/} |
24 |
- |