From cf66823d808971e645e38107a9c75f608d8a5059 Mon Sep 17 00:00:00 2001 From: Averrin Date: Mon, 19 Aug 2013 18:19:08 +0400 Subject: [PATCH] basic history. issue #51 --- activity/admin.py | 12 ++++++++++ activity/models.py | 11 ++++++++- activity/urls.py | 9 +++++++ activity/views.py | 31 ++++++++++++++++++------ rpg/fixtures/initial_data.json | 44 +++++++++++++++++----------------- static/js/activity.js | 38 +++++++++++++++++++---------- 6 files changed, 102 insertions(+), 43 deletions(-) create mode 100644 activity/admin.py create mode 100644 activity/urls.py diff --git a/activity/admin.py b/activity/admin.py new file mode 100644 index 0000000..3e5f96c --- /dev/null +++ b/activity/admin.py @@ -0,0 +1,12 @@ +from django.contrib import admin +from django.contrib.auth import get_user_model +from models import Event + + +class EventAdmin(admin.ModelAdmin): + class Meta(): + model = Event + #fields = ("timestamp", "user", 'type', 'message') + list_display = ("timestamp", "user", 'type', 'message') + +admin.site.register(Event, EventAdmin) \ No newline at end of file diff --git a/activity/models.py b/activity/models.py index 71a8362..8d00782 100644 --- a/activity/models.py +++ b/activity/models.py @@ -1,3 +1,12 @@ from django.db import models +from django.conf import settings +from django.contrib.auth import get_user_model -# Create your models here. + +class Event(models.Model): + timestamp = models.DateTimeField(auto_now=True) + type = models.TextField(choices=( + ("chat", "Chat"), ("other", "Other")) + ) + message = models.TextField() + user = models.ForeignKey(get_user_model()) \ No newline at end of file diff --git a/activity/urls.py b/activity/urls.py new file mode 100644 index 0000000..1acdd6a --- /dev/null +++ b/activity/urls.py @@ -0,0 +1,9 @@ +from django.conf import settings +from django.conf.urls import patterns, include, url +from views import StreamView, ChatSendView, HistoryView + +urlpatterns = patterns('', + url(r'^$', StreamView.as_view(), name='activity_stream'), + url(r'^send$', ChatSendView.as_view(), name='activity_send'), + url(r'^history$', HistoryView.as_view(), name='activity_history'), +) diff --git a/activity/views.py b/activity/views.py index e6c8b1f..c028056 100644 --- a/activity/views.py +++ b/activity/views.py @@ -1,4 +1,4 @@ -# Create your views here. +# Create your views here.from datetime import datetime import os import random @@ -8,15 +8,18 @@ from django.views.generic import TemplateView, View from braces.views import LoginRequiredMixin, JSONResponseMixin, AjaxResponseMixin import misaka as m +import pusher +from activity.models import Event +from datetime import datetime __all__ = ( 'StreamView', - 'ChatSendView' + 'ChatSendView', + 'HistoryView' ) - class StreamView(LoginRequiredMixin, TemplateView): template_name = 'activity/stream.html' @@ -25,20 +28,34 @@ def get_context_data(self, **kwargs): return context +class HistoryView(LoginRequiredMixin, JSONResponseMixin, AjaxResponseMixin, View): + def get_ajax(self, request, *args, **kwargs): + history = Event.objects.all().order_by("timestamp")[:10] + json_dict = {"history": []} + for event in history: + json_dict["history"].append({ + 'message': m.html(event.message), + 'user': event.user.username, + 'type': event.type, + 'timestamp': event.timestamp + }) + return self.render_json_response(json_dict) + + class ChatSendView(LoginRequiredMixin, JSONResponseMixin, AjaxResponseMixin, View): def post_ajax(self, request, *args, **kwargs): - import pusher - p = pusher.Pusher( app_id=settings.PUSHER_APPID, key=settings.PUSHER_KEY, secret=settings.PUSHER_SECRET ) - p['activity'].trigger('my_event', { + msg = { 'message': m.html(self.request.POST['message']), 'user': self.request.user.username, 'type': 'chat', - }) + } + Event(timestamp=datetime.now(), user=self.request.user, type='chat', message=msg['message']).save() + p['activity'].trigger('my_event', msg) json_dict = { "success": True } diff --git a/rpg/fixtures/initial_data.json b/rpg/fixtures/initial_data.json index 24ec4d4..1b4dfc6 100644 --- a/rpg/fixtures/initial_data.json +++ b/rpg/fixtures/initial_data.json @@ -1,4 +1,26 @@ [ + { + "pk": 1, + "model": "rpg.hero", + "fields": { + "username": "admin", + "first_name": "", + "last_name": "", + "titles": [], + "groups": [], + "credits": 0, + "is_active": true, + "is_superuser": true, + "is_staff": true, + "last_login": "2013-08-14T16:31:42.360Z", + "exp": 0, + "user_permissions": [], + "guilds": [], + "password": "pbkdf2_sha256$10000$iBN5bF7Ds5PY$Rz7LO1HT5zLYc6ciwM7GvHiAgJ0sZURyDSc9xWZ8n48=", + "email": "averrin@gmail.com", + "date_joined": "2013-08-14T13:02:56.435Z" + } + }, { "pk": 1, "model": "rpg.guild", @@ -48,27 +70,5 @@ "fields": { "exp": 150 } - }, - { - "pk": 1, - "model": "rpg.hero", - "fields": { - "username": "admin", - "first_name": "", - "last_name": "", - "titles": [], - "groups": [], - "credits": 0, - "is_active": true, - "is_superuser": true, - "is_staff": true, - "last_login": "2013-08-14T16:31:42.360Z", - "exp": 0, - "user_permissions": [], - "guilds": [], - "password": "pbkdf2_sha256$10000$iBN5bF7Ds5PY$Rz7LO1HT5zLYc6ciwM7GvHiAgJ0sZURyDSc9xWZ8n48=", - "email": "averrin@gmail.com", - "date_joined": "2013-08-14T13:02:56.435Z" - } } ] diff --git a/static/js/activity.js b/static/js/activity.js index 9a43233..2fbc1e2 100644 --- a/static/js/activity.js +++ b/static/js/activity.js @@ -1,30 +1,33 @@ function chat_send(editor){ - $.post("/activity/send", {message: editor.exportFile()}, function(){ - editor.importFile(); - }); + $.post("/activity/send", {message: editor.exportFile()}, function(){ + editor.importFile(); + }); } $(function(){ - var chat_message = _.template("
<%= user %> (<%= now %>):
<%= message %>
") + var chat_message = _.template( + "
" + + "<%= user %> (<%= now %>):
" + + " <%= message %>" + + "
" + ); var pusher = new Pusher('6bb5412badf09454aa87'); var channel = pusher.subscribe('activity'); channel.bind('my_event', function(data) { console.log(data); - var now = new Date().toLocaleTimeString(); - data.now = now; + data.now = new Date().toLocaleTimeString(); $("#events").append(chat_message(data)); }); var editor = new EpicEditor({ - - theme: { - base: "http://"+location.host+'/static/epiceditor/themes/base/epiceditor.css', - preview: "http://"+location.host+'/static/epiceditor/themes/preview/preview-dark.css', - editor: "http://"+location.host+'/static/epiceditor/themes/editor/epic-dark.css' - }, - focusOnLoad: true + theme: { + base: "http://"+location.host+'/static/epiceditor/themes/base/epiceditor.css', + preview: "http://"+location.host+'/static/epiceditor/themes/preview/preview-dark.css', + editor: "http://"+location.host+'/static/epiceditor/themes/editor/epic-dark.css' + }, + focusOnLoad: true }).load(); $("#msg_form").on("submit", function(e){ @@ -38,4 +41,13 @@ $(function(){ console.log("Ctrl + Enter"); chat_send(editor); }); + + $.get("/activity/history", function(data){ + console.log(data) + _.each(data.history, function(e,i){ + e.now = new Date().toLocaleTimeString(); + $("#events").append(chat_message(e)); + }) + + }) });