datasources: start a csv data source
This commit is contained in:
parent
5e65ba8758
commit
5e718d3379
|
@ -1,3 +1,4 @@
|
|||
*.pyc
|
||||
local_settings.py
|
||||
passerelle.sqlite3
|
||||
media
|
||||
|
|
|
@ -0,0 +1,8 @@
|
|||
from django.contrib import admin
|
||||
|
||||
from models import CsvDataSource
|
||||
|
||||
class CsvDataSourceAdmin(admin.ModelAdmin):
|
||||
prepopulated_fields = {'slug': ('title',)}
|
||||
|
||||
admin.site.register(CsvDataSource, CsvDataSourceAdmin)
|
|
@ -0,0 +1,26 @@
|
|||
import csv
|
||||
|
||||
from django.db import models
|
||||
|
||||
from model_utils.managers import InheritanceManager
|
||||
|
||||
|
||||
class BaseDataSource(models.Model):
|
||||
title = models.CharField(max_length=50)
|
||||
slug = models.SlugField()
|
||||
|
||||
objects = InheritanceManager()
|
||||
|
||||
|
||||
class CsvDataSource(BaseDataSource):
|
||||
csv_file = models.FileField(upload_to='csv')
|
||||
|
||||
def get_data(self):
|
||||
data = []
|
||||
self.csv_file.open()
|
||||
dialect = csv.Sniffer().sniff(self.csv_file.read(1024))
|
||||
self.csv_file.seek(0)
|
||||
reader = csv.reader(self.csv_file, dialect)
|
||||
for row in reader:
|
||||
data.append(unicode(row[0], 'utf-8')) # 1st column only
|
||||
return data
|
|
@ -0,0 +1,16 @@
|
|||
"""
|
||||
This file demonstrates writing tests using the unittest module. These will pass
|
||||
when you run "manage.py test".
|
||||
|
||||
Replace this with more appropriate tests for your application.
|
||||
"""
|
||||
|
||||
from django.test import TestCase
|
||||
|
||||
|
||||
class SimpleTest(TestCase):
|
||||
def test_basic_addition(self):
|
||||
"""
|
||||
Tests that 1 + 1 always equals 2.
|
||||
"""
|
||||
self.assertEqual(1 + 1, 2)
|
|
@ -0,0 +1,5 @@
|
|||
from django.conf.urls import patterns, url, include
|
||||
|
||||
urlpatterns = patterns('passerelle.datasources.views',
|
||||
url(r'(?P<datasource>\w+)/json$', 'json'),
|
||||
)
|
|
@ -0,0 +1,8 @@
|
|||
from jsonresponse import to_json
|
||||
|
||||
from models import BaseDataSource
|
||||
|
||||
@to_json('api')
|
||||
def json(request, datasource):
|
||||
ds = BaseDataSource.objects.get_subclass(slug=datasource)
|
||||
return ds.get_data()
|
|
@ -45,12 +45,12 @@ USE_TZ = True
|
|||
|
||||
# Absolute filesystem path to the directory that will hold user-uploaded files.
|
||||
# Example: "/home/media/media.lawrence.com/media/"
|
||||
MEDIA_ROOT = ''
|
||||
MEDIA_ROOT = os.path.join(PROJECT_PATH, 'media')
|
||||
|
||||
# URL that handles the media served from MEDIA_ROOT. Make sure to use a
|
||||
# trailing slash.
|
||||
# Examples: "http://media.lawrence.com/media/", "http://example.com/media/"
|
||||
MEDIA_URL = ''
|
||||
MEDIA_URL = '/media/'
|
||||
|
||||
# Absolute path to the directory static files should be collected to.
|
||||
# Don't put anything in this directory yourself; store your static files
|
||||
|
@ -119,6 +119,7 @@ INSTALLED_APPS = (
|
|||
'django.contrib.admin',
|
||||
# Uncomment the next line to enable admin documentation:
|
||||
# 'django.contrib.admindocs',
|
||||
'passerelle.datasources',
|
||||
)
|
||||
|
||||
# A sample logging configuration. The only tangible logging
|
||||
|
|
|
@ -4,9 +4,6 @@ from django.contrib import admin
|
|||
admin.autodiscover()
|
||||
|
||||
urlpatterns = patterns('',
|
||||
# Examples:
|
||||
# url(r'^$', 'passerelle.views.home', name='home'),
|
||||
# url(r'^passerelle/', include('passerelle.foo.urls')),
|
||||
|
||||
url(r'^data/', include('passerelle.datasources.urls')),
|
||||
url(r'^admin/', include(admin.site.urls)),
|
||||
)
|
||||
|
|
|
@ -1,2 +1,3 @@
|
|||
django == 1.4
|
||||
http://pypi.python.org/packages/source/d/django-jsonresponse/django-jsonresponse-0.5.tar.gz
|
||||
django-model-utils
|
||||
|
|
1
setup.py
1
setup.py
|
@ -13,6 +13,7 @@ setup(name='passerelle',
|
|||
packages=find_packages(os.path.dirname(__file__) or '.'),
|
||||
install_requires=[
|
||||
'django >= 1.4.2, < 1.5',
|
||||
'django-model-utils',
|
||||
],
|
||||
dependency_links = [
|
||||
'http://pypi.python.org/packages/source/d/django-jsonresponse/django-jsonresponse-0.5.tar.gz',
|
||||
|
|
Loading…
Reference in New Issue