Commit 3d46e0dc21d911da1d378c14a676cdd150f156a4
0 parents
Exists in
master
init
Showing 14 changed files with 251 additions and 0 deletions Side-by-side Diff
.gitignore
View file @
3d46e0d
manage.py
View file @
3d46e0d
notes/migrations/0001_initial.py
View file @
3d46e0d
... | ... | @@ -0,0 +1,24 @@ |
1 | +# -*- coding: utf-8 -*- | |
2 | +from __future__ import unicode_literals | |
3 | + | |
4 | +from django.db import models, migrations | |
5 | + | |
6 | + | |
7 | +class Migration(migrations.Migration): | |
8 | + | |
9 | + dependencies = [ | |
10 | + ] | |
11 | + | |
12 | + operations = [ | |
13 | + migrations.CreateModel( | |
14 | + name='Note', | |
15 | + fields=[ | |
16 | + ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)), | |
17 | + ('text', models.CharField(max_length=255)), | |
18 | + ('last_updated', models.DateTimeField(auto_now=True)), | |
19 | + ], | |
20 | + options={ | |
21 | + }, | |
22 | + bases=(models.Model,), | |
23 | + ), | |
24 | + ] |
notes/models.py
View file @
3d46e0d
... | ... | @@ -0,0 +1,15 @@ |
1 | +from django.db import models | |
2 | +from django.contrib import admin | |
3 | +from swampdragon.serializers.model_serializer import ModelSerializer | |
4 | +from swampdragon.models import SelfPublishModel | |
5 | + | |
6 | +class NoteSerializer(ModelSerializer): | |
7 | + class Meta: | |
8 | + model = 'notes.Note' | |
9 | + publish_fields = ('text','last_updated') | |
10 | + | |
11 | +class Note(SelfPublishModel, models.Model): | |
12 | + text = models.CharField(max_length=255) | |
13 | + last_updated = models.DateTimeField(auto_now=True) | |
14 | + serializer_class = NoteSerializer | |
15 | +admin.site.register(Note) |
notes/routers.py
View file @
3d46e0d
... | ... | @@ -0,0 +1,17 @@ |
1 | +from swampdragon import route_handler | |
2 | +from swampdragon.route_handler import ModelRouter | |
3 | +from notes.models import Note, NoteSerializer | |
4 | + | |
5 | + | |
6 | +class NoteRouter(ModelRouter): | |
7 | + route_name = 'note-list' | |
8 | + serializer_class = NoteSerializer | |
9 | + model = Note | |
10 | + | |
11 | + def get_object(self, **kwargs): | |
12 | + return self.model.objects.get(pk=kwargs['id']) | |
13 | + | |
14 | + def get_query_set(self, **kwargs): | |
15 | + return self.model.objects.all() | |
16 | + | |
17 | +route_handler.register(NoteRouter) |
notes/settings.py
View file @
3d46e0d
... | ... | @@ -0,0 +1,96 @@ |
1 | +""" | |
2 | +Django settings for notes project. | |
3 | + | |
4 | +For more information on this file, see | |
5 | +https://docs.djangoproject.com/en/1.6/topics/settings/ | |
6 | + | |
7 | +For the full list of settings and their values, see | |
8 | +https://docs.djangoproject.com/en/1.6/ref/settings/ | |
9 | +""" | |
10 | + | |
11 | +# Build paths inside the project like this: os.path.join(BASE_DIR, ...) | |
12 | +import os | |
13 | +BASE_DIR = os.path.dirname(os.path.dirname(__file__)) | |
14 | + | |
15 | + | |
16 | +# Quick-start development settings - unsuitable for production | |
17 | +# See https://docs.djangoproject.com/en/1.6/howto/deployment/checklist/ | |
18 | + | |
19 | +# SECURITY WARNING: keep the secret key used in production secret! | |
20 | +SECRET_KEY = '2+u_)-dlz+&8(jrd!58ru-o&sg4_mj$97xmslmvnmxb=mnd@i@' | |
21 | +TEMPLATE_DIRS = [os.path.join(BASE_DIR, 'templates')] | |
22 | +STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')] | |
23 | +DRAGON_URL = os.environ['DRAGON_URL'] | |
24 | +SWAMP_DRAGON_CONNECTION = ('swampdragon.connections.sockjs_connection.DjangoSubscriberConnection', '/data') | |
25 | + | |
26 | + | |
27 | +# SECURITY WARNING: don't run with debug turned on in production! | |
28 | +DEBUG = True | |
29 | + | |
30 | +TEMPLATE_DEBUG = True | |
31 | + | |
32 | +ALLOWED_HOSTS = [] | |
33 | + | |
34 | + | |
35 | +# Application definition | |
36 | + | |
37 | +INSTALLED_APPS = ( | |
38 | + 'notes', | |
39 | + 'django.contrib.admin', | |
40 | + 'django.contrib.auth', | |
41 | + 'django.contrib.contenttypes', | |
42 | + 'django.contrib.sessions', | |
43 | + 'django.contrib.messages', | |
44 | + 'django.contrib.staticfiles', | |
45 | + 'swampdragon' | |
46 | + | |
47 | +) | |
48 | + | |
49 | +TEMPLATE_LOADERS = ( | |
50 | + 'django.template.loaders.app_directories.Loader', | |
51 | + 'django.template.loaders.filesystem.Loader', | |
52 | +) | |
53 | + | |
54 | + | |
55 | +MIDDLEWARE_CLASSES = ( | |
56 | + 'django.contrib.sessions.middleware.SessionMiddleware', | |
57 | + 'django.middleware.common.CommonMiddleware', | |
58 | + 'django.middleware.csrf.CsrfViewMiddleware', | |
59 | + 'django.contrib.auth.middleware.AuthenticationMiddleware', | |
60 | + 'django.contrib.messages.middleware.MessageMiddleware', | |
61 | + 'django.middleware.clickjacking.XFrameOptionsMiddleware', | |
62 | +) | |
63 | + | |
64 | +ROOT_URLCONF = 'notes.urls' | |
65 | + | |
66 | +WSGI_APPLICATION = 'notes.wsgi.application' | |
67 | + | |
68 | + | |
69 | +# Database | |
70 | +# https://docs.djangoproject.com/en/1.6/ref/settings/#databases | |
71 | + | |
72 | +DATABASES = { | |
73 | + 'default': { | |
74 | + 'ENGINE': 'django.db.backends.sqlite3', | |
75 | + 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), | |
76 | + } | |
77 | +} | |
78 | + | |
79 | +# Internationalization | |
80 | +# https://docs.djangoproject.com/en/1.6/topics/i18n/ | |
81 | + | |
82 | +LANGUAGE_CODE = 'en-us' | |
83 | + | |
84 | +TIME_ZONE = 'UTC' | |
85 | + | |
86 | +USE_I18N = True | |
87 | + | |
88 | +USE_L10N = True | |
89 | + | |
90 | +USE_TZ = True | |
91 | + | |
92 | + | |
93 | +# Static files (CSS, JavaScript, Images) | |
94 | +# https://docs.djangoproject.com/en/1.6/howto/static-files/ | |
95 | + | |
96 | +STATIC_URL = '/static/' |
notes/templates/add_note.html
View file @
3d46e0d
notes/templates/list_notes.html
View file @
3d46e0d
... | ... | @@ -0,0 +1,27 @@ |
1 | +{% load static swampdragon_tags %} | |
2 | +{% swampdragon_settings %} | |
3 | +<h1>List of notes</h1> | |
4 | +<a href="/notes/add">Add new</a></br> | |
5 | +<ul> | |
6 | +{% for note in notes.iterator %} | |
7 | +<li>{{ note.text }} - Last updated {{ note.last_updated }}</li> | |
8 | +{% endfor %} | |
9 | +</ul> | |
10 | + | |
11 | +<script src="//code.jquery.com/jquery-1.11.2.min.js"></script> | |
12 | +<script type="text/javascript" src="{% static 'swampdragon/js/dist/swampdragon.min.js' %}"></script> | |
13 | +<script defer> | |
14 | +swampdragon.ready(function() { | |
15 | + console.log("swampdragon ready"); | |
16 | + swampdragon.subscribe('note-list', 'local-channel', null, function(context, data) { | |
17 | + console.log("subscribed successfully"); | |
18 | + swampdragon.onChannelMessage(function(channels, message) { | |
19 | + console.log(channels); | |
20 | + console.log(message); | |
21 | + $('ul').append($('<li>'+message.data.text+'</li>')); | |
22 | + }); | |
23 | + }, function(context, data) { | |
24 | + console.log("failed to subscribe"); | |
25 | + }) | |
26 | +}); | |
27 | +</script> |
notes/urls.py
View file @
3d46e0d
... | ... | @@ -0,0 +1,15 @@ |
1 | +from django.conf.urls import patterns, include, url | |
2 | + | |
3 | +from django.contrib import admin | |
4 | +admin.autodiscover() | |
5 | + | |
6 | +from notes.views import * | |
7 | +urlpatterns = patterns('', | |
8 | + # Examples: | |
9 | + # url(r'^$', 'notes.views.home', name='home'), | |
10 | + # url(r'^blog/', include('blog.urls')), | |
11 | + | |
12 | + url(r'^admin/', include(admin.site.urls)), | |
13 | + url(r'^notes/$', list_notes), | |
14 | + url(r'^notes/add$', add_notes) | |
15 | +) |
notes/views.py
View file @
3d46e0d
... | ... | @@ -0,0 +1,14 @@ |
1 | +from django.shortcuts import render, redirect | |
2 | +from notes.models import * | |
3 | + | |
4 | +def list_notes(request): | |
5 | + return render(request, 'list_notes.html', { | |
6 | + 'notes':Note.objects | |
7 | + }) | |
8 | + | |
9 | +def add_notes(request): | |
10 | + if request.method == "POST": | |
11 | + Note(text=request.POST['text']).save() | |
12 | + return redirect('/notes/') | |
13 | + else: | |
14 | + return render(request, 'add_note.html') |
notes/wsgi.py
View file @
3d46e0d
... | ... | @@ -0,0 +1,14 @@ |
1 | +""" | |
2 | +WSGI config for notes project. | |
3 | + | |
4 | +It exposes the WSGI callable as a module-level variable named ``application``. | |
5 | + | |
6 | +For more information on this file, see | |
7 | +https://docs.djangoproject.com/en/1.6/howto/deployment/wsgi/ | |
8 | +""" | |
9 | + | |
10 | +import os | |
11 | +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "notes.settings") | |
12 | + | |
13 | +from django.core.wsgi import get_wsgi_application | |
14 | +application = get_wsgi_application() |
server.py
View file @
3d46e0d
... | ... | @@ -0,0 +1,12 @@ |
1 | +#!/usr/bin/env python | |
2 | +import os | |
3 | +import sys | |
4 | + | |
5 | + | |
6 | +from swampdragon.swampdragon_server import run_server | |
7 | + | |
8 | +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "notes.settings") | |
9 | + | |
10 | +host_port = sys.argv[1] if len(sys.argv) > 1 else None | |
11 | + | |
12 | +run_server(host_port=host_port) |