qmsk/dmx/web.py
changeset 88 c923295ee520
parent 84 9ebf1a2cee3a
child 96 e6dfc98ec50f
--- a/qmsk/dmx/web.py	Mon Jun 02 18:26:41 2014 +0300
+++ b/qmsk/dmx/web.py	Mon Jun 02 18:27:08 2014 +0300
@@ -1,6 +1,7 @@
 import pvl.web
 from pvl.web import urls, html
 
+import collections
 import logging; log = logging.getLogger('qmsk.dmx.web')
 
 def colorize (x, red, green, blue, alpha=1.0) :
@@ -61,18 +62,17 @@
 
     def process (self) :
         if self.request.method == 'POST' :
+            update = collections.defaultdict(dict)
+
             for name, value in self.request.form.iteritems() :
                 head_name, attr = name.split('-', 1)
-                head = self.app.heads[head_name]
+                head = self.app.universe[head_name]
 
-                if value :
-                    value = int(value)
-                    
-                    # update head
-                    head[attr] = value
+                if value:
+                    update[head][attr] = int(value)
 
             # update dmx
-            self.app.update() 
+            self.app.update(update)
 
     def render_head (self, name, head) :
         if head.alpha() is None :
@@ -118,7 +118,7 @@
                         html.th(class_='dmx-control')(u"Control"),
                         html.th(class_='dmx-head-control')(u"Head Control"),
                     ),
-                    html.tbody(self.render_head(name, head) for name, head in sorted(self.app.heads.iteritems())),
+                    html.tbody(self.render_head(name, head) for name, head in sorted(self.app.universe)),
                 ),
 
                 html.button(type='submit', class_='btn btn-primary')("Go"),
@@ -130,16 +130,20 @@
         urls.rule('/',          Handler),
     ))
 
-    def __init__ (self, dmx, heads, **opts) :
-        super(DMXWebApplication, self).__init__(**opts)
-
-        self.dmx = dmx
-        self.heads = heads
+    def __init__ (self, universe, **opts) :
+        """
+            universe    - qmsk.dmx.heads.Universe
+        """
 
-    def update (self) :
-        if not self.dmx :
-            return
+        super(DMXWebApplication, self).__init__(**opts)
+        
+        self.universe = universe
 
-        for head in self.heads.itervalues() :
-            self.dmx[head.channel] = tuple(head)
+    def update (self, update) :
+        """
+            Update given {head: {attr: value}}
+        """
+        
+        for head, attrs in update.iteritems():
+            self.universe.update(head, attrs)