Commits

magik cypress  committed 768bcff

import initial version

  • Participants

Comments (0)

Files changed (19)

+Simple api get info of NASA http://data.nasa.gov/api-info/

File __init__.py

Empty file added.

File context_processors.py

+from django.conf import settings
+
+def site_info(context):
+    title = getattr(settings, 'SITE_INFO_TITLE', 'NASA API')
+    description = getattr(settings, 'SITE_INFO_DESC', 'NASA API - OpenData')
+    return {
+        'SITE_INFO_TITLE': title,
+        'SITE_INFO_DESC': description,
+    }
+
+def get_current_path(request):
+    return {
+       'CURRENT_PATH': request.get_full_path()
+}

File example/__init__.py

Empty file added.

File example/models.py

+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+from django.db import models

File example/templatetags/__init__.py

Empty file added.

File example/templatetags/get_data.py

+from django.template import Library
+from django import template
+from django.utils import simplejson
+import urllib2
+
+register = template.Library()
+
+class data():
+    def __init__(self, id, slug, url, title_plain, 
+                    title, content, tags, date, 
+                    categories, custom_fields):
+        self.id = id
+        self.slug = slug
+        self.url = url
+        self.title_plain = title_plain
+        self.title = title
+        self.content = content
+        self.tags = tags
+        self.date = date
+        self.categories = categories
+        self.custom_fields = custom_fields
+
+@register.inclusion_tag("example/get_recent_datasets.html")
+def get_recent_datasets(count=12):
+    datas = []
+    try:
+        url = "http://data.nasa.gov/api/get_recent_datasets?count=%s" % (count)
+        file = urllib2.urlopen(url)
+        content = file.read()
+        if content:
+            json = simplejson.loads(content)
+            if json["status"] == 'ok':                
+                for item in json['posts']:
+                    id = item['id']
+                    slug = item['slug']
+                    url = item['url']
+                    title_plain = item['title_plain']
+                    title = item['title']
+                    content = item['content']
+                    tags = item['tags']
+                    date = item['date']
+                    categories = item['categories']
+                    custom_fields = item['custom_fields']
+                
+                    thisdata = data(id, slug, url, title_plain, 
+                                    title, content, tags, date, 
+                                    categories, custom_fields)
+                    datas.append(thisdata)
+    except: 
+        HttpResponse("Error")
+
+    return {'datas': datas}

File example/tests.py

+"""
+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)

File example/urls.py

+from django.conf.urls.defaults import *
+
+from example.views import (home)
+
+urlpatterns = patterns('',
+    url('^$', home, name='home'),
+   
+    url('^i18n/', include('django.conf.urls.i18n')),
+)

File example/views.py

+from django.http import (Http404, HttpResponseRedirect, HttpResponseNotFound,
+                        HttpResponse)
+from django.conf import settings
+from django.shortcuts import render_to_response
+from django.template import RequestContext
+
+from django.utils.translation import gettext_lazy as _
+
+def _lookup_template(name):
+    return 'example/%s.html' % name
+
+def home(request):
+    """Show homepage.
+    """       
+
+    context = {
+    }
+    return render_to_response(_lookup_template('index'), context,
+                              context_instance=RequestContext(request))    
+#!/usr/bin/env python
+from django.core.management import execute_manager
+import imp
+try:
+    imp.find_module('settings') # Assumed to be in the same directory.
+except ImportError:
+    import sys
+    sys.stderr.write("Error: Can't find the file 'settings.py' in the directory containing %r. It appears you've customized things.\nYou'll have to run django-admin.py, passing it your settings module.\n" % __file__)
+    sys.exit(1)
+
+import settings
+
+if __name__ == "__main__":
+    execute_manager(settings)

File requirements.txt

+#########################################################################
+### this pip requirements file is kept to the bare minimum (django)
+### 
+### uncomment or add your own packages here to have them installed easily
+### with pip:
+###
+### pip -E /path/to/venv/ install -r pip_requirements.txt
+###
+#########################################################################
+
+# Django latest STABLE version
+#-e git+https://github.com/django/django.git#egg=django
+django
+"""Settings"""
+
+# Django settings for simpletemplate project.
+
+import os
+from django.utils.translation import gettext_lazy as _
+
+PROJECT_PATH = os.path.abspath(os.path.dirname(__file__))
+
+ADMINS = (
+    ('Magik', 'magik.cypress@gmail.com'),
+)
+
+MANAGERS = ADMINS
+
+TIME_ZONE = 'Europe/Paris'
+LANGUAGE_CODE = 'fr-fr'
+USE_I18N = True
+USE_L10N = True
+gettext = lambda s: s
+LANGUAGES = (
+    ('fr', gettext(u'French')),
+    ('en', gettext('English')),
+)
+DEFAULT_LANGUAGE = 2
+
+SECRET_KEY = 'gn7l2a+rvk4uh7(188nswg3n86&_tasqczh)vr#)_b45gge6x!'
+
+ROOT_URLCONF = 'urls'
+
+TEMPLATE_DIRS = (
+    os.path.join(PROJECT_PATH, 'templates'),
+)
+
+MEDIA_ROOT = os.path.join(PROJECT_PATH, 'static')
+MEDIA_URL = '/static/'
+
+# List of callables that know how to import templates from various sources.
+TEMPLATE_LOADERS = (
+    ('django.template.loaders.cached.Loader', (
+        'django.template.loaders.filesystem.Loader',
+        'django.template.loaders.app_directories.Loader',
+    )),
+)
+
+TEMPLATE_CONTEXT_PROCESSORS = (
+    'django.core.context_processors.media',
+    'django.core.context_processors.i18n',
+    'django.core.context_processors.request',
+    'nasa.context_processors.site_info',    
+    'django.contrib.auth.context_processors.auth'
+)
+
+MIDDLEWARE_CLASSES = (
+    'django.middleware.common.CommonMiddleware',
+    'django.middleware.csrf.CsrfViewMiddleware',
+    'django.contrib.sessions.middleware.SessionMiddleware',
+    'django.contrib.auth.middleware.AuthenticationMiddleware',
+    'django.contrib.messages.middleware.MessageMiddleware',
+)
+
+INSTALLED_APPS = (
+    'django.contrib.sessions',
+    'example'
+)
+
+
+DEBUG = True
+TEMPLATE_DEBUG = DEBUG

File templates/404.html

+{% extends "base.html" %}
+
+{% load i18n %}
+
+{% block title %}{% trans "You lost it. (404 is a old beautiful car)" %}{% endblock %}
+
+{% block content %}
+<h2>{% trans "You lost it." %}</h2>
+<p><a href="http://en.wikipedia.org/wiki/Peugeot_404">404 is a old beautiful car</a></p>
+&lt;sorry&gt; ;)
+{% endblock %}

File templates/500.html

+{% extends "base.html" %}
+
+{% load i18n %}
+
+{% block title %}{% trans "You lost it. (500 is not a joke)" %}{% endblock %}
+
+{% block content %}
+<h2>{% trans "You lost it." %}</h2>
+<p><a href="http://en.wikipedia.org/wiki/500_%28number%29">Very large possibility</a></p>
+&lt;sorry&gt; ;)
+{% endblock %}

File templates/base.html

+<!DOCTYPE html>
+<html>
+<head>
+    <meta charset="utf-8" />
+    <title> {% block title %}{{ SITE_INFO_TITLE }}{% endblock title %}</title>
+    {% block css %}{% endblock css %}
+    {% block extrahead %}{% endblock extrahead %}
+</head>
+
+<body>
+    {% block content %}{% endblock content %}
+
+    {% block js %}{% endblock js %}
+</body>
+</html>
+

File templates/example/get_recent_datasets.html

+	<div id="results">
+	{% for d in datas %}	
+		<div class="date">{{ d.date }}</div>
+		<a href="{{ d.url }}" title="{{ d.title }}">{{ d.title_plain }}</a> (ID : {{ d.id }})
+		
+		<div class="tags">Tags :</div>
+		<ul class="inline">
+
+		{% for t in d.tags %}
+		 	<li>{{ t.title }}</li>
+		{% endfor %}
+		</ul>
+
+		{{ d.content|safe }}
+
+		Categories : 
+		<ul class="inline">
+		{% for c in d.categories %}
+		 	<li style="color: {{ c.description }}">{{ c.title }}</li>
+		{% endfor %}
+		</ul>
+
+		{% for cf in d.custom_fields %}
+		 		<div class="custom_fields">{{ cf.more_info_link }}</div>
+		{% endfor %}
+		<hr />
+	{% endfor %}		 
+	</div>	

File templates/example/index.html

+{% extends "base.html" %}
+{% load get_data %}
+
+{% block content %}
+
+<h1>{{ SITE_INFO_TITLE }}</h1>
+<h2>{{ SITE_INFO_DESC }}</h2>
+
+{% get_recent_datasets 14 %}
+
+{% endblock content %}
+from django.conf.urls.defaults import *
+from django.conf import settings
+
+urlpatterns = patterns('',
+    (r'^', include('example.urls')),
+)
+
+if settings.DEBUG:
+    urlpatterns += patterns('', 
+                            (r'^' + settings.MEDIA_URL.lstrip('/') + r'(.*)$', 
+                            'django.views.static.serve',
+                            {'document_root': settings.MEDIA_ROOT}))