Commits

Marcin Lulek  committed e4321f0

DRY

  • Participants
  • Parent commits 65238ae

Comments (0)

Files changed (1)

File gevent_cometd/app_views.py

          'total_unique_messages':0,
          'started_on':datetime.datetime.utcnow()}
 
+def add_cors_headers(res):
+    # allow CORS
+    res.headers.add('Access-Control-Allow-Origin', '*')
+    res.headers.add('Access-Control-Allow-Methods', 'GET, POST, OPTIONS')
+    res.headers.add('Access-Control-Allow', 'GET, POST, OPTIONS')
+    res.headers.add('Access-Control-Allow-Credentials', 'true')
+    res.headers.add('Access-Control-Allow-Headers', 'Content-Type, Depth, User-Agent, X-File-Size, X-Requested-With, If-Modified-Since, X-File-Name, Cache-Control, Pragma, Origin')
+    res.headers.add('Access-Control-Max-Age', '86400')
+
 def get_channel(channel):
     # create new channel if it doesnt exist yet
     if channel and channel not in channels:
         self.is_active = True
         self.last_update = datetime.datetime.utcnow() 
         self.clients = []
+        self.persistance = False
         
     def add_message(self, message, exclude_user=None):
         message.update({'channel':self.name})
             channel = get_channel(chan)
             #user gets assigned to a channel
             channel.add_client(client)
-            message = {'user':client.user,
-               'message':None,
-               'type':'join',
-               'timestamp':timestmp
-               }
-            stats['total_unique_messages'] += 1
-            channel.add_message(message, exclude_user=client.user)
+            if channel.persistance:
+                message = {'user':client.user,
+                   'message':None,
+                   'type':'join',
+                   'timestamp':timestmp
+                   }
+                stats['total_unique_messages'] += 1
+                channel.add_message(message, exclude_user=client.user)
                         
         res = Response(json.dumps({'conn_id':client.conn_id}), request=request)
     return res
     client.last_active = datetime.datetime.utcnow()
     client.gc_marker()
     if request.method.upper() == 'OPTIONS':
+        #preflight
         res = Response(' '*2048, request=request,
                        content_type='application/json')
-        # allow CORS
-        res.headers.add('Access-Control-Allow-Origin', '*')
-        res.headers.add('Access-Control-Allow-Methods', 'GET, POST, OPTIONS')
-        res.headers.add('Access-Control-Allow', 'GET, POST, OPTIONS')
-        res.headers.add('Access-Control-Allow-Credentials', 'true')
-        res.headers.add('Access-Control-Allow-Headers', 'Content-Type, Depth, User-Agent, X-File-Size, X-Requested-With, If-Modified-Since, X-File-Name, Cache-Control, Pragma')
-        res.headers.add('Access-Control-Max-Age', '86400')
+        add_cors_headers(res)
         return res
     
     def yield_response():
     
     res = Response(app_iter=yield_response(), request=request,
                    content_type='application/json')
-    res.headers.add('Access-Control-Allow-Origin', '*')
-    res.headers.add('Access-Control-Allow-Methods', 'GET, POST, OPTIONS')
-    res.headers.add('Access-Control-Allow', 'GET, POST, OPTIONS')
-    res.headers.add('Access-Control-Allow-Headers', 'Content-Type, Depth, User-Agent, X-File-Size, X-Requested-With, If-Modified-Since, X-File-Name, Cache-Control, Pragma')
-    res.headers.add('Access-Control-Allow-Credentials', 'true')
-    res.headers.add('Access-Control-Max-Age', '86400')
+    add_cors_headers(res)
     return res
 
 def static(request, *args):