Skip to content

Instantly share code, notes, and snippets.

@stodge
stodge / django_tornado_handler.py
Created April 25, 2012 11:54 — forked from bdarnell/django_tornado_handler.py
django_tornado_handler.py
# NOTE: This code was extracted from a larger class and has not been
# tested in this form. Caveat emptor.
import django.conf
import django.contrib.auth
import django.core.handlers.wsgi
import django.db
import django.utils.importlib
import httplib
import json
import logging
from tastypie import fields
from tastypie.authentication import Authentication
from tastypie.authorization import Authorization
from tastypie.bundle import Bundle
from tastypie.exceptions import NotFound
from tastypie.resources import Resource
# a dummy class representing a row of data
class Row(object):
"""
module mydjangolib.bigint_patch
A fix for the rather well-known ticket #399 in the django project.
Create and link to auto-incrementing primary keys of type bigint without
having to reload the model instance after saving it to get the ID set in
the instance.
Logs:
@stodge
stodge / resources.py
Last active August 29, 2015 14:11 — forked from bohde/resources.py
class UserResource(ModelResource):
def cached_obj_get(self, request=None, **kwargs):
if request and 'id' in kwargs and kwargs['id'] == 'self':
return request.user
return super(UserResource, self).cached_obj_get(request, **kwargs)
from tastypie import fields
from tastypie.authentication import Authentication
from tastypie.authorization import Authorization
from tastypie.bundle import Bundle
from tastypie.exceptions import NotFound
from tastypie.resources import Resource
# a dummy class representing a row of data
class Row(object):
@stodge
stodge / test_ws_client.js
Created December 27, 2023 00:54
Simple websocket client test
$(document).ready(function() {
const wsClient = new WebSocket("ws://localhost/ws");
wsClient.onopen = function() {
console.debug("Connected to server");
};
wsClient.onclose = function(evt) {
console.debug("Closed: %o", evt);
};
wsClient.onmessage = function (evt) {
console.debug("Rx: %o", evt.data);
@stodge
stodge / test_ws_server.py
Created December 27, 2023 00:56
Simple websocket server test using Tornado
import tornado.ioloop
import tornado.web
import tornado.websocket
class WebSocketServer(tornado.websocket.WebSocketHandler):
"""Simple WebSocket handler to serve clients."""
# Note that `clients` is a class variable and `send_message` is a
# classmethod.
clients = set()