I am trying to convert the mini chat flask application to web2py.
link: https://github.com/sdiehl/minichat
(1) in the flask code, I can access rooms. and users.
%flask output.
users
{u'test': <__main__.User object at 0x0000000003A7F198>}
When I do a conversion in web2py, I can't access the class variables users.
or room. I needed to redefine rooms in main in the default.py (action).
How do I access the class variable or make them global?
Here is my web2py conversion.
# -*- coding: utf-8 -*-
# this file is released under public domain and you can use without
limitations
#########################################################################
## This is a sample controller
## - index is the default action of any application
## - user is required for authentication and authorization
## - download is for downloading files uploaded in the db (does streaming)
#########################################################################
from gevent import monkey
from flask import Flask, json
from gevent import queue
from gevent.pywsgi import WSGIServer
app = Flask(__name__)
app.debug = True
class Room(object):
def __init__(self):
self.users = set()
self.messages = []
def backlog(self, size=25):
return self.messages[-size:]
def subscribe(self, user):
self.users.add(user)
def add(self, message):
for user in self.users:
print user
user.queue.put_nowait(message)
self.messages.append(message)
class User(object):
def __init__(self):
self.queue = queue.Queue()
rooms = {
'python': Room(),
'django': Room(),}
users = {}
def index():
return dict(message=T('Welcome to web2py!'))
def choose():
form = FORM('Your name:', INPUT(_name='name'), INPUT(_type='submit'))
if form.process().accepted:
name = form.vars.name;
uname = unicode(unicode(form.vars.name))
redirect(URL('default','main', args = uname, extension = ''),
client_side = True);
return dict(form = form)
def main():
rooms = {
'python': Room(),
'django': Room(),}
uname = unicode(request.args(0))
rooms=rooms.keys()
return dict(rooms = rooms, uname = uname)
def room():
room = []
messages = []
room = unicode(request.args(0))
uid = unicode(request.args(1))
user = users.get(uid, None)
if not user:
users[uid] = user = User()
active_room = rooms[room]
print 'subscribe', active_room, user
messages = active_room.backlog()
return dict(room = room, uid = uid, messages = messages, users = users)
def put():
uid = unicode(request.args(0))
room = unicode(request.args(1))
users = request.args(2)
user = users[uid]
room = rooms[room]
message = request.vars['message']
room.add(':'.join([uid, message]))
return locals()
def poll():
uid = unicode(request.args(0))
msg = []
#users = request.args(1)
#try:
# msg = users[uid].queue.get(timeout=10)
#msg = auth.user.id.queue.get(timeout=10)
#except queue.Empty:
# msg = []
return json.dumps(msg)
--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.