Tobias Bartel 2 years ago
parent
commit
2ff2968205

+ 0 - 0
__init__.py View File


+ 15 - 0
admin.py View File

@@ -0,0 +1,15 @@
1
+# -*- coding: utf-8 -*-
2
+from __future__ import unicode_literals
3
+
4
+from django.contrib import admin
5
+from .models import *
6
+
7
+@admin.register(Feed)
8
+class FeedAdmin(admin.ModelAdmin):
9
+    pass
10
+
11
+@admin.register(FeedItem)
12
+class FeedItemAdmin(admin.ModelAdmin):
13
+    pass
14
+
15
+# Register your models here.

+ 8 - 0
apps.py View File

@@ -0,0 +1,8 @@
1
+# -*- coding: utf-8 -*-
2
+from __future__ import unicode_literals
3
+
4
+from django.apps import AppConfig
5
+
6
+
7
+class NewsConfig(AppConfig):
8
+    name = 'news'

+ 0 - 0
management/__init__.py View File


+ 0 - 0
management/commands/__init__.py View File


+ 28 - 0
management/commands/update_feeds.py View File

@@ -0,0 +1,28 @@
1
+from django.core.management.base import BaseCommand, CommandError
2
+from ...models import *
3
+from prettyprint import pp
4
+from feedparser import parse
5
+from datetime import datetime
6
+from time import mktime
7
+
8
+class Command(BaseCommand):
9
+
10
+    def handle(self, *args, **options):
11
+        timestamp = datetime.now()
12
+        for my_feed in Feed.objects.all():
13
+            try:
14
+                parsed_feed = parse(my_feed.url)
15
+                my_feed.last_update = timestamp
16
+                my_feed.save()
17
+            except:
18
+                pass
19
+
20
+            FeedItem.objects.all().filter(feed=my_feed).delete()
21
+
22
+            for entry in parsed_feed.entries:
23
+                new_item = FeedItem.objects.create(feed=my_feed, headline=entry.title, body=entry.description, uuid=entry.id, added=timestamp)
24
+                new_item.published = datetime.fromtimestamp(mktime(entry.updated_parsed))
25
+                new_item.save()
26
+
27
+        for item in FeedItem.objects.all().order_by('-published')[:5]:
28
+            pp("%s - %s" % (item.published, item))

+ 42 - 0
migrations/0001_initial.py View File

@@ -0,0 +1,42 @@
1
+# -*- coding: utf-8 -*-
2
+# Generated by Django 1.11 on 2017-05-03 21:33
3
+from __future__ import unicode_literals
4
+
5
+from django.db import migrations, models
6
+import django.db.models.deletion
7
+
8
+
9
+class Migration(migrations.Migration):
10
+
11
+    initial = True
12
+
13
+    dependencies = [
14
+    ]
15
+
16
+    operations = [
17
+        migrations.CreateModel(
18
+            name='Feed',
19
+            fields=[
20
+                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
21
+                ('name', models.CharField(max_length=100)),
22
+                ('url', models.URLField(unique=True)),
23
+                ('last_update', models.DateTimeField(null=True)),
24
+            ],
25
+        ),
26
+        migrations.CreateModel(
27
+            name='FeedItem',
28
+            fields=[
29
+                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
30
+                ('added', models.DateTimeField(auto_created=True)),
31
+                ('headline', models.CharField(max_length=250)),
32
+                ('body', models.TextField(blank=True, null=True)),
33
+                ('published', models.DateTimeField(blank=True, null=True)),
34
+                ('uuid', models.CharField(blank=True, max_length=100, null=True)),
35
+                ('feed', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='news.Feed')),
36
+            ],
37
+        ),
38
+        migrations.AlterUniqueTogether(
39
+            name='feeditem',
40
+            unique_together=set([('feed', 'headline')]),
41
+        ),
42
+    ]

+ 30 - 0
migrations/0002_auto_20170505_1933.py View File

@@ -0,0 +1,30 @@
1
+# -*- coding: utf-8 -*-
2
+# Generated by Django 1.11 on 2017-05-05 19:33
3
+from __future__ import unicode_literals
4
+
5
+from django.db import migrations, models
6
+
7
+
8
+class Migration(migrations.Migration):
9
+
10
+    dependencies = [
11
+        ('news', '0001_initial'),
12
+    ]
13
+
14
+    operations = [
15
+        migrations.AddField(
16
+            model_name='feed',
17
+            name='background_color',
18
+            field=models.CharField(default='#000', max_length=6),
19
+        ),
20
+        migrations.AddField(
21
+            model_name='feed',
22
+            name='foreground_color',
23
+            field=models.CharField(default='#fff', max_length=6),
24
+        ),
25
+        migrations.AddField(
26
+            model_name='feed',
27
+            name='image',
28
+            field=models.ImageField(default=None, upload_to='news_logo/'),
29
+        ),
30
+    ]

+ 32 - 0
migrations/0002_auto_20170505_1933_squashed_0003_auto_20170505_1934.py View File

@@ -0,0 +1,32 @@
1
+# -*- coding: utf-8 -*-
2
+# Generated by Django 1.11 on 2017-05-05 19:38
3
+from __future__ import unicode_literals
4
+
5
+from django.db import migrations, models
6
+
7
+
8
+class Migration(migrations.Migration):
9
+
10
+    replaces = [(b'news', '0002_auto_20170505_1933'), (b'news', '0003_auto_20170505_1934')]
11
+
12
+    dependencies = [
13
+        ('news', '0001_initial'),
14
+    ]
15
+
16
+    operations = [
17
+        migrations.AddField(
18
+            model_name='feed',
19
+            name='background_color',
20
+            field=models.CharField(default='#000', max_length=6),
21
+        ),
22
+        migrations.AddField(
23
+            model_name='feed',
24
+            name='foreground_color',
25
+            field=models.CharField(default='#fff', max_length=6),
26
+        ),
27
+        migrations.AddField(
28
+            model_name='feed',
29
+            name='image',
30
+            field=models.ImageField(default=None, null=True, upload_to='news_logo/'),
31
+        ),
32
+    ]

+ 20 - 0
migrations/0003_auto_20170505_1934.py View File

@@ -0,0 +1,20 @@
1
+# -*- coding: utf-8 -*-
2
+# Generated by Django 1.11 on 2017-05-05 19:34
3
+from __future__ import unicode_literals
4
+
5
+from django.db import migrations, models
6
+
7
+
8
+class Migration(migrations.Migration):
9
+
10
+    dependencies = [
11
+        ('news', '0002_auto_20170505_1933'),
12
+    ]
13
+
14
+    operations = [
15
+        migrations.AlterField(
16
+            model_name='feed',
17
+            name='image',
18
+            field=models.ImageField(default=None, null=True, upload_to='news_logo/'),
19
+        ),
20
+    ]

+ 20 - 0
migrations/0003_auto_20170810_0942.py View File

@@ -0,0 +1,20 @@
1
+# -*- coding: utf-8 -*-
2
+# Generated by Django 1.11.3 on 2017-08-10 09:42
3
+from __future__ import unicode_literals
4
+
5
+from django.db import migrations, models
6
+
7
+
8
+class Migration(migrations.Migration):
9
+
10
+    dependencies = [
11
+        ('news', '0002_auto_20170505_1933_squashed_0003_auto_20170505_1934'),
12
+    ]
13
+
14
+    operations = [
15
+        migrations.AlterField(
16
+            model_name='feeditem',
17
+            name='uuid',
18
+            field=models.CharField(blank=True, max_length=500, null=True),
19
+        ),
20
+    ]

+ 0 - 0
migrations/__init__.py View File


+ 31 - 0
models.py View File

@@ -0,0 +1,31 @@
1
+# -*- coding: utf-8 -*-
2
+from __future__ import unicode_literals
3
+
4
+from django.db import models
5
+
6
+# Create your models here.
7
+class Feed(models.Model):
8
+    name = models.CharField(max_length=100, blank=False, null=False)
9
+    url = models.URLField(blank=False, null=False, unique=True)
10
+    last_update = models.DateTimeField(null=True)
11
+    image = models.ImageField(upload_to='news_logo/', default=None, null=True)
12
+    foreground_color = models.CharField(max_length=6, default='#fff')
13
+    background_color = models.CharField(max_length=6, default='#000')
14
+
15
+    def __unicode__(self):
16
+        return self.name
17
+
18
+
19
+class FeedItem(models.Model):
20
+    feed = models.ForeignKey('feed', null=False)
21
+    headline = models.CharField(max_length=250, blank=False)
22
+    body = models.TextField(blank=True, null=True)
23
+    published = models.DateTimeField(blank=True, null=True)
24
+    uuid = models.CharField(max_length=500, blank=True, null=True)
25
+    added = models.DateTimeField(auto_created=True)
26
+
27
+    class Meta:
28
+        unique_together = ('feed', 'headline')
29
+
30
+    def __unicode__(self):
31
+        return "%s: %s" % (self.feed.name, self.headline)

+ 11 - 0
templates/dash/news_slider_item.tpl.html View File

@@ -0,0 +1,11 @@
1
+<div>
2
+<div style="color: #{{ item.feed.foreground_color }}; background: #{{ item.feed.background_color }}; ">
3
+    <div class="col-xs-1 center-block">
4
+        <img class="img-responsive center-block" style="margin: 0 auto; height: 1.5em; width: auto; border: none; "
5
+             src="{{ MEDIA_URL }}{{ item.feed.image.url }}">
6
+    </div>
7
+    <div class="col-xs-auto" style="overflow: hidden;white-space:nowrap;">
8
+        {{ item.headline }}
9
+    </div>
10
+</div>
11
+    </div>

+ 6 - 0
tests.py View File

@@ -0,0 +1,6 @@
1
+# -*- coding: utf-8 -*-
2
+from __future__ import unicode_literals
3
+
4
+from django.test import TestCase
5
+
6
+# Create your tests here.

+ 7 - 0
urls.py View File

@@ -0,0 +1,7 @@
1
+from django.conf.urls import url
2
+
3
+from .views import NewsItemsJson
4
+
5
+urlpatterns = [
6
+    url(r'^', NewsItemsJson, name='NewsItems'),
7
+]

+ 20 - 0
views.py View File

@@ -0,0 +1,20 @@
1
+# -*- coding: utf-8 -*-
2
+from __future__ import unicode_literals
3
+
4
+from django.shortcuts import render, HttpResponse
5
+from django.template.loader import render_to_string
6
+from django.http import JsonResponse
7
+from .models import FeedItem
8
+import json
9
+from prettyprint import pp
10
+
11
+def NewsItemsJson(request):
12
+    headlines = FeedItem.objects.all().order_by('-published')[:6]
13
+    output = []
14
+    i = 0
15
+    for  line in headlines:
16
+        html = render_to_string('dash/news_slider_item.tpl.html', {'item': line, 'key': i}, request,  )
17
+        output.append(html)
18
+        i += 1
19
+    return JsonResponse(output, safe=False)
20
+