diff options
| author | Yuval Adam <yuv.adm@gmail.com> | 2013-12-31 16:31:40 +0200 |
|---|---|---|
| committer | Yuval Adam <yuv.adm@gmail.com> | 2013-12-31 16:31:40 +0200 |
| commit | e5e134396c15ee7504588bfe42134a3e72aaa2fd (patch) | |
| tree | 800f40cb16c8cafa34fa648787838cba8025ac5b | |
| parent | 06dc00525acdc0021685aa58fc65075c0b50c355 (diff) | |
Add docs app
| -rw-r--r-- | newsdiff/core/parsers/haaretz.py | 4 | ||||
| -rw-r--r-- | newsdiff/core/utils.py | 2 | ||||
| -rw-r--r-- | newsdiff/docs/__init__.py | 0 | ||||
| -rw-r--r-- | newsdiff/docs/admin.py | 5 | ||||
| -rw-r--r-- | newsdiff/docs/migrations/0001_initial.py | 38 | ||||
| -rw-r--r-- | newsdiff/docs/migrations/__init__.py | 0 | ||||
| -rw-r--r-- | newsdiff/docs/models.py | 11 | ||||
| -rw-r--r-- | newsdiff/docs/tasks.py | 14 | ||||
| -rw-r--r-- | newsdiff/docs/tests.py | 15 | ||||
| -rw-r--r-- | newsdiff/docs/views.py | 3 | ||||
| -rw-r--r-- | newsdiff/rain/tasks.py | 5 | ||||
| -rw-r--r-- | newsdiff/settings/base.py | 1 |
12 files changed, 92 insertions, 6 deletions
diff --git a/newsdiff/core/parsers/haaretz.py b/newsdiff/core/parsers/haaretz.py index 360cf25..58f7dd5 100644 --- a/newsdiff/core/parsers/haaretz.py +++ b/newsdiff/core/parsers/haaretz.py @@ -6,7 +6,7 @@ from datetime import datetime from .base import HtmlSoupParser from ..models import HaaretzArticle, HaaretzImage -from ..utils import get_image_from_url +from ..utils import get_file_from_url class HaaretzParser(HtmlSoupParser): @@ -60,7 +60,7 @@ class HaaretzParser(HtmlSoupParser): img_url = 'http://www.haaretz.co.il{}'.format(img['src'].split('_gen')[0]) caption = img.title - name, image_file = get_image_from_url(img_url) + name, image_file = get_file_from_url(img_url) article_image, created = self.IMAGE_MODEL.objects.get_or_create(article=article, origin_url=img_url, defaults={'caption': caption}) diff --git a/newsdiff/core/utils.py b/newsdiff/core/utils.py index 514bf67..d104a81 100644 --- a/newsdiff/core/utils.py +++ b/newsdiff/core/utils.py @@ -6,7 +6,7 @@ from django.core.files.temp import NamedTemporaryFile from urlparse import urlparse -def get_image_from_url(url): +def get_file_from_url(url): tmp = NamedTemporaryFile(delete=True) req = requests.get(url) if not req.ok: diff --git a/newsdiff/docs/__init__.py b/newsdiff/docs/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/newsdiff/docs/__init__.py diff --git a/newsdiff/docs/admin.py b/newsdiff/docs/admin.py new file mode 100644 index 0000000..115e8a0 --- /dev/null +++ b/newsdiff/docs/admin.py @@ -0,0 +1,5 @@ +from django.contrib import admin + +from .models import * + +admin.site.register(Document) diff --git a/newsdiff/docs/migrations/0001_initial.py b/newsdiff/docs/migrations/0001_initial.py new file mode 100644 index 0000000..3fa5fef --- /dev/null +++ b/newsdiff/docs/migrations/0001_initial.py @@ -0,0 +1,38 @@ +# -*- coding: utf-8 -*- +from south.utils import datetime_utils as datetime +from south.db import db +from south.v2 import SchemaMigration +from django.db import models + + +class Migration(SchemaMigration): + + def forwards(self, orm): + # Adding model 'Document' + db.create_table(u'docs_document', ( + (u'id', self.gf('django.db.models.fields.AutoField')(primary_key=True)), + ('name', self.gf('django.db.models.fields.CharField')(default='(untitled)', max_length=100)), + ('url', self.gf('django.db.models.fields.URLField')(max_length=200)), + ('created', self.gf('django.db.models.fields.DateTimeField')(auto_now_add=True, blank=True)), + ('document', self.gf('django.db.models.fields.files.FileField')(max_length=100)), + )) + db.send_create_signal(u'docs', ['Document']) + + + def backwards(self, orm): + # Deleting model 'Document' + db.delete_table(u'docs_document') + + + models = { + u'docs.document': { + 'Meta': {'object_name': 'Document'}, + 'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}), + 'document': ('django.db.models.fields.files.FileField', [], {'max_length': '100'}), + u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'name': ('django.db.models.fields.CharField', [], {'default': "'(untitled)'", 'max_length': '100'}), + 'url': ('django.db.models.fields.URLField', [], {'max_length': '200'}) + } + } + + complete_apps = ['docs']
\ No newline at end of file diff --git a/newsdiff/docs/migrations/__init__.py b/newsdiff/docs/migrations/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/newsdiff/docs/migrations/__init__.py diff --git a/newsdiff/docs/models.py b/newsdiff/docs/models.py new file mode 100644 index 0000000..717eb3d --- /dev/null +++ b/newsdiff/docs/models.py @@ -0,0 +1,11 @@ +from django.db import models + + +class Document(models.Model): + name = models.CharField(max_length=100, default='(untitled)') + url = models.URLField() + created = models.DateTimeField(auto_now_add=True, editable=False) + document = models.FileField(upload_to='documents') + + def __unicode__(self): + return '{} ({})'.format(self.name, self.url) diff --git a/newsdiff/docs/tasks.py b/newsdiff/docs/tasks.py new file mode 100644 index 0000000..c40e9ff --- /dev/null +++ b/newsdiff/docs/tasks.py @@ -0,0 +1,14 @@ +import logging + +from newsdiff.celery import app +from newsdiff.core.utils import get_file_from_url + +from .models import Document + + +@app.task() +def import_document(url): + name, doc = get_file_from_url(url) + document = Document(url=url) + document.document.save(name, doc) + document.save() diff --git a/newsdiff/docs/tests.py b/newsdiff/docs/tests.py new file mode 100644 index 0000000..dee311c --- /dev/null +++ b/newsdiff/docs/tests.py @@ -0,0 +1,15 @@ +from django.test import TestCase + +from .models import Document +from .tasks import import_document + + +class DocumentTestCase(TestCase): + def setUp(self): + pass + + def test_import_document(self): + import_document('http://www.google.com/webmasters/docs/search-engine-optimization-starter-guide.pdf') + document = Document.objects.all()[0] + self.assertTrue(document.created) + self.assertTrue(document.document.url) diff --git a/newsdiff/docs/views.py b/newsdiff/docs/views.py new file mode 100644 index 0000000..91ea44a --- /dev/null +++ b/newsdiff/docs/views.py @@ -0,0 +1,3 @@ +from django.shortcuts import render + +# Create your views here. diff --git a/newsdiff/rain/tasks.py b/newsdiff/rain/tasks.py index 582acbf..3ae0eb2 100644 --- a/newsdiff/rain/tasks.py +++ b/newsdiff/rain/tasks.py @@ -1,9 +1,8 @@ import logging -import requests from datetime import datetime from newsdiff.celery import app -from newsdiff.core.utils import get_image_from_url +from newsdiff.core.utils import get_file_from_url from .models import RainRadarImage @@ -13,7 +12,7 @@ RAIN_RADAR_IMAGE_URL = 'http://www.ims.gov.il/Ims/Pages/RadarImage.aspx' @app.task() def import_rain_radar_image(): logging.info('run') - _name, image = get_image_from_url(RAIN_RADAR_IMAGE_URL) + _name, image = get_file_from_url(RAIN_RADAR_IMAGE_URL) name = datetime.now().strftime('%H%M.gif') image_obj = RainRadarImage() image_obj.image.save(name, image) diff --git a/newsdiff/settings/base.py b/newsdiff/settings/base.py index 93bac16..e185a7a 100644 --- a/newsdiff/settings/base.py +++ b/newsdiff/settings/base.py @@ -88,6 +88,7 @@ INSTALLED_APPS = ( 'reversion', 'newsdiff.core', + 'newsdiff.docs', 'newsdiff.rain' ) |
