Commits

Yann Malet committed 74ae1bb

Improve the templates and started to add the realtime thingy

Comments (0)

Files changed (8)

django_tutorial/apps/polls/templates/polls/detail.html

+{% extends "base.html" %}
+
+{% block content %}
 <h1>{{ poll.question }}</h1>
 
 {% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
     <label for="choice{{ forloop.counter }}">{{ choice.choice }}</label><br />
 {% endfor %}
 <input type="submit" value="Vote" />
-</form>
+</form>
+{% endblock %}

django_tutorial/apps/polls/templates/polls/index.html

-{% if latest_poll_list %}
-    <ul>
-    {% for poll in latest_poll_list %}
-        <li>
-        	<a href="/polls/{{ poll.id }}/">{{ poll.question }}</a>
-        </li>
-    {% endfor %}
-    </ul>
-{% else %}
-    <p>No polls are available.</p>
-{% endif %}
+{% extends "base.html" %}
+
+{% block content %}
+    {% if latest_poll_list %}
+        <ul>
+        {% for poll in latest_poll_list %}
+            <li>
+            	<a href="/polls/{{ poll.id }}/">{{ poll.question }}</a>
+            </li>
+        {% endfor %}
+        </ul>
+    {% else %}
+        <p>No polls are available.</p>
+    {% endif %}
+{% endblock %}

django_tutorial/apps/polls/templates/polls/result.html

+{% extends "base.html" %}
+
+{% block content %}
 <h1>{{ poll.question }}</h1>
 
 <ul>
 {% endfor %}
 </ul>
 
-<a href="/polls/{{ poll.id }}/">Vote again?</a>
+<a href="/polls/{{ poll.id }}/">Vote again?</a>
+{% endblock %}

django_tutorial/conf/local/__init__.py

 
 DATABASES = {
     'default': {
-        'ENGINE': 'django.db.backends.sqlite3',
-        'NAME': os.path.join(VAR_ROOT, 'dev.db'),
+        'ENGINE': 'django.db.backends.postgresql_psycopg2',
+        'NAME': 'django_tutorial1',
+        'USER': 'django',
+        'PASSWORD': 'django',
+        'HOST': '127.0.0.1',
+        'PORT': 5432
     }
 }
 

django_tutorial/realtime_server.py

+from realtimemagic import RealTimeMagic
+
+from realtimemagic.monitors.psql import PsqlMonitor
+from realtimemagic.auth.contrib import DjangoAuthenticator, AuthenticationError
+
+from django.conf import settings
+
+from django_tutorial.apps.polls.models import Poll
+
+
+class MyAppAuthenticator(DjangoAuthenticator):
+    def check(self, conn, channel):
+        try:
+            user = self.get_user(conn)
+
+            obj = Poll.objects.get(id=channel)
+            if not obj.user_can_access(user):
+                raise AuthenticationError(self.error_message)
+        except Poll.DoesNotExist:
+            raise AuthenticationError(self.error_message)
+
+DB = settings.DATABASES['default']
+
+logs = PsqlMonitor(database=DB['NAME'],
+        user=DB['USER'], password=DB['PASSWORD'],
+        host=DB['HOST'], port=DB['PORT'])
+
+
+# To-do: Listen to redis directly. This is too complicated.
+@logs.monitor('logs')
+def log_changes(notification, pubsub):
+    print "notification"
+    print notification
+    channel, message = notification.payload.split('=>', 1)
+    pubsub.publish(channel, {'message': message})
+
+if __name__ == '__main__':
+    rtm = RealTimeMagic(local=settings.DEBUG)
+    rtm.monitors.append(log_changes)
+    rtm.authenticators['logs'].append(DjangoAuthenticator())
+    rtm.start()

django_tutorial/settings.py

 
 INSTALLED_APPS = (
     'django_tutorial.apps.polls',
+    'django_tutorial.apps.core',
 
     #'south',
 

django_tutorial/templates/base.html

     {% endblock %}
 </div>
 
+{% block js %}
+<script src="http://cdn.sockjs.org/sockjs-0.3.min.js"></script>
+{% endblock %}
 </body>
-</html>
+</html>

django_tutorial/urls.py

-from django.conf.urls.defaults import patterns, url, include
+from django.conf.urls.defaults import *
 
 from django.contrib import admin
 
 admin.autodiscover()
 
 urlpatterns = patterns('',
-    #(r'^$ ', ),
+    (r'^$', include('django_tutorial.apps.core.urls')),
     (r'^polls/', include('django_tutorial.apps.polls.urls')),
     (r'^admin/doc/', include('django.contrib.admindocs.urls')),
     (r'^admin/', include(admin.site.urls)),
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.