Skip to content

Commit

Permalink
basic history. issue #51
Browse files Browse the repository at this point in the history
  • Loading branch information
averrin committed Aug 19, 2013
1 parent c389e08 commit cf66823
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 43 deletions.
12 changes: 12 additions & 0 deletions activity/admin.py
Original file line number Diff line number Diff line change
@@ -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)
11 changes: 10 additions & 1 deletion activity/models.py
Original file line number Diff line number Diff line change
@@ -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())
9 changes: 9 additions & 0 deletions activity/urls.py
Original file line number Diff line number Diff line change
@@ -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'),
)
31 changes: 24 additions & 7 deletions activity/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Create your views here.
# Create your views here.from datetime import datetime

import os
import random
Expand All @@ -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'

Expand All @@ -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
}
Expand Down
44 changes: 22 additions & 22 deletions rpg/fixtures/initial_data.json
Original file line number Diff line number Diff line change
@@ -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": "[email protected]",
"date_joined": "2013-08-14T13:02:56.435Z"
}
},
{
"pk": 1,
"model": "rpg.guild",
Expand Down Expand Up @@ -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": "[email protected]",
"date_joined": "2013-08-14T13:02:56.435Z"
}
}
]
38 changes: 25 additions & 13 deletions static/js/activity.js
Original file line number Diff line number Diff line change
@@ -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("<div class='chat_message'><strong><a href='/users/<%= user %>'><%= user %></a></strong> (<%= now %>): <br> <%= message %></div>")
var chat_message = _.template(
"<div class='chat_message'>" +
"<strong><a href='/users/<%= user %>'><%= user %></a></strong> (<%= now %>): <br>" +
" <%= message %>" +
"</div>"
);

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){
Expand All @@ -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));
})

})
});

0 comments on commit cf66823

Please sign in to comment.