Permalink
Browse files

Passing shape id to client to be able to remove them.

  • Loading branch information...
1 parent 746142e commit c7a1b980a8294c35e8bd15343bddd640c1803b10 @frontierpsycho committed Aug 22, 2012
Showing with 25 additions and 11 deletions.
  1. +8 −2 canvas/form_generator/form_generator.py
  2. +2 −2 canvas/static/socket.js
  3. +1 −1 canvas/urls.py
  4. +12 −4 canvas/views.py
  5. +2 −2 templates/canvas/canvas.html
@@ -5,6 +5,7 @@
import logging
import time
import datetime
+import urllib
import urllib2
sys.path.append('../../../')
@@ -48,9 +49,14 @@ def add_feeling(self):
self.feelingdata = list(FeelingData.objects.filter(postdatetime__gt = self.latest_postdatetime).order_by("postdatetime")[:200])
self.counter = 0
- def broadcast(self, id, channel):
+ def broadcast(self, id, channel, remove_list=[]):
try:
- urllib2.urlopen("http://localhost:9000/canvas/refresh/%d" % id).read()
+ url = "http://localhost:9000/canvas/refresh/"
+
+ data = { "id" : id, "remove": remove_list }
+ req = urllib2.Request(url, urllib.urlencode(data))
+
+ return urllib2.urlopen(req).read()
except urllib2.HTTPError as e:
logger.error("Error broadcasting: %s " % e)
View
@@ -1,5 +1,5 @@
-var addShape = function(shape) {
- paper.path(shape["shape"]).attr({ fill: shape["colour"], "stroke" : "none" }).transform(Raphael.matrix(shape['transform'][0], shape['transform'][1], shape['transform'][2], shape['transform'][3], shape['transform'][4], shape['transform'][5]).toTransformString());
+var addShape = function(data) {
+ paper.path(data["shape"]).attr({ fill: data["colour"], "stroke" : "none" }).transform(Raphael.matrix(data['transform'][0], data['transform'][1], data['transform'][2], data['transform'][3], data['transform'][4], data['transform'][5]).toTransformString());
};
var socket = new io.Socket();
View
@@ -6,7 +6,7 @@
urlpatterns = patterns('canvas.views',
url(r'^$', CanvasView.as_view()),
- url(r'^refresh/(?P<id>\d+)/', 'broadcast'),
+ url(r'^refresh/', 'broadcast'),
url(r'^playground/$', PlaygroundView.as_view()),
url(r'^feeling/(?P<pk>\d+)/', FeelingDataDetailView.as_view()),
url(r'^feelings$', ListView.as_view(template_name="canvas/feelings.html", model=FeelingData)),
View
@@ -3,11 +3,13 @@
import random
from multiprocessing import Process,Manager
import datetime
+from collections import OrderedDict
from django.http import HttpResponse
from django.views.generic import ListView,DetailView
from django.conf import settings
from django.db.models import Q
+from django.views.decorators.csrf import csrf_exempt
from django_socketio import broadcast_channel, NoSocket
@@ -34,8 +36,10 @@ class CanvasView(ListView):
template_name="canvas/canvas.html"
def get_queryset(self):
+ shapes = OrderedDict()
fd_ids = [subitem for sublist in grid for item in sublist for subitem in item]
- shapes = [cells[fd_id] for fd_id in fd_ids]
+ for fd_id in fd_ids:
+ shapes[fd_id] = cells[fd_id]
return shapes
def get_context_data(self, **kwargs):
@@ -95,12 +99,16 @@ def get_context_data(self, **kwargs):
context["stroke"] = self.stroke
return context
-def broadcast(request, id):
+@csrf_exempt
+def broadcast(request):
global cells
if len(cells) > 0:
- shape = cells[int(id)]
+ fd_id = request.POST["id"]
+ remove_list = request.POST["remove"]
+
+ shape = cells[int(fd_id)]
try:
- broadcast_channel({ 'shape': shape.path, 'colour': shape.colour, 'transform': shape.transformation_matrix } , "shapes")
+ broadcast_channel({ 'fd_id': fd_id, 'shape': shape.path, 'colour': shape.colour, 'transform': shape.transformation_matrix, "remove": remove_list } , "shapes")
except NoSocket:
logger.error("No subscribers on channel shapes")
return HttpResponse()
@@ -24,8 +24,8 @@
{% block inline-javascript %}
<script type="text/javascript">
var paper = Raphael(10, 10, {{ width }}, {{ height }});
-{% for shape in shapes %}
- {{ shape|to_raphael:"paper"|safe }}.click(function(event) {
+{% for fd_id,shape in shapes.items %}
+ var shape{{ fd_id }} = {{ shape|to_raphael:"paper"|safe }}.click(function(event) {
var new_size = this.getBBox().width + 200;
this.attr({ width: new_size } );
alert(this.getBBox().width);

0 comments on commit c7a1b98

Please sign in to comment.