Projet

Général

Profil

0001-create-announces-from-external-RSS-feed-12919.patch

Serghei Mihai, 04 octobre 2016 15:23

Télécharger (7,04 ko)

Voir les différences:

Subject: [PATCH] create announces from external RSS feed (#12919)

 corbo/forms.py                              |  4 ++--
 corbo/migrations/0007_auto_20160928_1454.py | 24 ++++++++++++++++++++++++
 corbo/models.py                             | 21 +++++++++++++++++++++
 corbo/templates/corbo/category_detail.html  |  2 +-
 corbo/templates/corbo/category_form.html    |  2 +-
 debian/control                              |  5 ++++-
 debian/corbo.cron.d                         |  1 +
 requirements.txt                            |  3 +++
 setup.py                                    |  3 +++
 9 files changed, 60 insertions(+), 5 deletions(-)
 create mode 100644 corbo/migrations/0007_auto_20160928_1454.py
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/migrations/0007_auto_20160928_1454.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', '0006_auto_20160928_0833'),
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, verbose_name='Feed URL', blank=True),
23
        ),
24
    ]
corbo/models.py
6 6
from html2text import HTML2Text
7 7
from emails.django import Message
8 8
from lxml.etree import HTML as HTMLTree
9
from dateutil import parser as dateutil_parser
10
import requests
11
import feedparser
9 12

  
10 13
from django.utils import timezone
11 14
from django.conf import settings
......
37 40

  
38 41
class Category(models.Model):
39 42
    name = models.CharField(_('Name'), max_length=64, blank=False, null=False)
43
    rss_feed_url = models.URLField(_('Feed URL'), blank=True, null=True,
44
                help_text=_('if defined, announces will be automatically created from rss items'))
40 45
    ctime = models.DateTimeField(auto_now_add=True)
41 46

  
42 47
    def __unicode__(self):
......
48 53
    def get_subscriptions_count(self):
49 54
        return self.subscription_set.all().count()
50 55

  
56
    def save(self, *args, **kwargs):
57
        super(Category, self).save(*args, **kwargs)
58
        if not self.rss_feed_url:
59
            return
60
        feed_response = requests.get(self.rss_feed_url)
61
        if feed_response.ok:
62
            content = feedparser.parse(feed_response.content)
63
            for entry in content.get('entries', []):
64
                announce, created = Announce.objects.get_or_create(identifier=entry['id'],
65
                                    category=self, defaults={'title': entry['title'],
66
                                    'text': entry['summary'],
67
                                    'publication_time': dateutil_parser.parse(entry['updated'])})
68
                if created:
69
                    Broadcast.objects.get_or_create(announce=announce)
70

  
51 71

  
52 72
class Announce(models.Model):
53 73
    category  = models.ForeignKey('Category', verbose_name=_('category'))
54 74
    title = models.CharField(_('title'), max_length=256,
55 75
                             help_text=_('maximum 256 characters'))
76
    identifier = models.CharField(max_length=256, null=True, blank=True)
56 77
    text = RichTextField(_('Content'))
57 78
    publication_time = models.DateTimeField(_('publication time'), blank=True,
58 79
                                            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,
18
    python-dateutil
16 19
Description: Announces Manager
17 20

  
18 21
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
9
dateutil
setup.py
100 100
        'gadjo',
101 101
        'emails',
102 102
        'lxml',
103
        'feedparser',
104
        'requests',
105
        'dateutil'
103 106
        ],
104 107
    zip_safe=False,
105 108
    cmdclass={
106
-