<s> import json <EOL> from time import sleep <EOL> from django . http import HttpResponse , HttpRequest <EOL> from treeio . core . conf import settings <EOL> import threading <EOL> from datetime import datetime <EOL> from time import strftime <EOL> from hashlib import md5 <EOL> import sys <EOL> from django . core . cache import cache <EOL> import cPickle <EOL> from django . contrib . messages . storage import default_storage <EOL> def get_key ( postfix = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> domain = getattr ( settings , '<STR_LIT>' , '<STR_LIT:default>' ) <EOL> key = "<STR_LIT>" % ( domain , postfix ) <EOL> return key <EOL> def create_lock ( key ) : <EOL> try : <EOL> while True : <EOL> if cache . add ( key + '<STR_LIT>' , '<STR_LIT:1>' , <NUM_LIT:10> ) : <EOL> break <EOL> return True <EOL> except : <EOL> print "<STR_LIT>" , sys . exc_info ( ) <EOL> return False <EOL> def delete_lock ( key ) : <EOL> cache . delete ( key + '<STR_LIT>' ) <EOL> def set_memcached ( key , obj , lock = True ) : <EOL> """<STR_LIT>""" <EOL> if lock : <EOL> if create_lock ( key ) : <EOL> cache . set ( key , cPickle . dumps ( obj ) , <NUM_LIT> ) <EOL> delete_lock ( key ) <EOL> else : <EOL> cache . set ( key , cPickle . dumps ( obj ) , <NUM_LIT> ) <EOL> def get_memcached ( key ) : <EOL> """<STR_LIT>""" <EOL> data = cache . get ( key ) <EOL> if not data : <EOL> set_memcached ( key , { } ) <EOL> obj = cPickle . loads ( cache . get ( key ) ) <EOL> return obj <EOL> def get_notifications ( user ) : <EOL> """<STR_LIT>""" <EOL> notifications = [ ] <EOL> try : <EOL> user . id <EOL> except : <EOL> return [ ] <EOL> try : <EOL> if not getattr ( settings , '<STR_LIT>' , False ) : <EOL> return notifications <EOL> request = HttpRequest ( ) <EOL> request . user = user <EOL> storage = default_storage ( request ) <EOL> for msg in storage . _get ( ) [ <NUM_LIT:0> ] : <EOL> notifications . append ( { '<STR_LIT:message>' : msg . message , <EOL> '<STR_LIT>' : msg . _get_tags ( ) } ) <EOL> storage . _store ( None ) <EOL> except : <EOL> pass <EOL> return notifications <EOL> def get_user_profile ( user ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> listeners = get_memcached ( get_key ( "<STR_LIT>" ) ) <EOL> return listeners [ user ] [ "<STR_LIT>" ] <EOL> except : <EOL> return user <EOL> def update_user ( user , location ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> listeners = get_memcached ( get_key ( "<STR_LIT>" ) ) <EOL> _user_profile = str ( user . profile ) <EOL> _user = str ( user ) <EOL> listeners [ _user ] = { <EOL> "<STR_LIT>" : datetime . now ( ) , "<STR_LIT>" : location , "<STR_LIT>" : _user_profile } <EOL> set_memcached ( get_key ( "<STR_LIT>" ) , listeners ) <EOL> except : <EOL> print "<STR_LIT>" , sys . exc_info ( ) <EOL> def remove_user ( id , user ) : <EOL> """<STR_LIT>""" <EOL> conferences = get_memcached ( get_key ( "<STR_LIT>" ) ) <EOL> if verification_user ( id , user ) : <EOL> del conferences [ id ] [ "<STR_LIT>" ] [ user ] <EOL> set_memcached ( get_key ( "<STR_LIT>" ) , conferences ) <EOL> return get_new_message_for_user ( user ) <EOL> def verification_user ( id , user ) : <EOL> """<STR_LIT>""" <EOL> conferences = get_memcached ( get_key ( "<STR_LIT>" ) ) <EOL> if user not in conferences [ id ] [ "<STR_LIT>" ] . keys ( ) : <EOL> return False <EOL> return True <EOL> def checking_conference ( id_conference ) : <EOL> """<STR_LIT>""" <EOL> conferences = get_memcached ( get_key ( "<STR_LIT>" ) ) <EOL> if id_conference in conferences . keys ( ) : <EOL> return True <EOL> return False <EOL> def is_owner_user ( id , user ) : <EOL> """<STR_LIT>""" <EOL> conferences = get_memcached ( get_key ( "<STR_LIT>" ) ) <EOL> if conferences [ id ] [ "<STR_LIT:info>" ] [ "<STR_LIT>" ] == user : <EOL> return True <EOL> return False <EOL> def exit_from_conference ( id , user ) : <EOL> """<STR_LIT>""" <EOL> if checking_conference ( id ) : <EOL> if verification_user ( id , user ) : <EOL> conferences = get_memcached ( get_key ( "<STR_LIT>" ) ) <EOL> if is_owner_user ( id , user ) : <EOL> delete_conference ( id , user ) <EOL> del conferences [ id ] [ "<STR_LIT>" ] [ user ] <EOL> set_memcached ( get_key ( "<STR_LIT>" ) , conferences ) <EOL> return get_new_message_for_user ( user ) <EOL> def delete_conference ( id , user ) : <EOL> """<STR_LIT>""" <EOL> if is_owner_user ( id , user ) : <EOL> conferences = get_memcached ( get_key ( "<STR_LIT>" ) ) <EOL> del conferences [ id ] <EOL> set_memcached ( get_key ( "<STR_LIT>" ) , conferences ) <EOL> return get_new_message_for_user ( user ) <EOL> def remove_users_in_conference ( id , user , users ) : <EOL> """<STR_LIT>""" <EOL> if checking_conference ( id ) and is_owner_user ( id , user ) : <EOL> conferences = get_memcached ( get_key ( "<STR_LIT>" ) ) <EOL> for val in users : <EOL> del conferences [ id ] [ "<STR_LIT>" ] [ val ] <EOL> set_memcached ( get_key ( "<STR_LIT>" ) , conferences ) <EOL> return get_new_message_for_user ( user ) <EOL> def add_users_in_conference ( id , user , users ) : <EOL> """<STR_LIT>""" <EOL> if checking_conference ( id ) : <EOL> conferences = get_memcached ( get_key ( "<STR_LIT>" ) ) <EOL> for val in users : <EOL> conferences [ id ] [ "<STR_LIT>" ] [ val ] = { "<STR_LIT>" : [ ] , "<STR_LIT>" : [ ] } <EOL> set_memcached ( get_key ( "<STR_LIT>" ) , conferences ) <EOL> return get_new_message_for_user ( user ) <EOL> def create_conference ( user , users , title ) : <EOL> """<STR_LIT>""" <EOL> id = md5 ( ) <EOL> id . update ( str ( datetime . now ( ) ) ) <EOL> id = user + "<STR_LIT:_>" + id . hexdigest ( ) <EOL> users . append ( user ) <EOL> conferences = get_memcached ( get_key ( "<STR_LIT>" ) ) <EOL> if id in conferences . keys ( ) : <EOL> return get_new_message_for_user ( user ) <EOL> conferences [ id ] = { } <EOL> conferences [ id ] [ "<STR_LIT>" ] = { } <EOL> conferences [ id ] [ "<STR_LIT:info>" ] = { <EOL> "<STR_LIT>" : user , <EOL> "<STR_LIT:title>" : title , <EOL> "<STR_LIT>" : datetime . now ( ) <EOL> } <EOL> set_memcached ( get_key ( "<STR_LIT>" ) , conferences ) <EOL> add_users_in_conference ( id , user , users ) <EOL> return get_new_message_for_user ( user ) <EOL> def get_active_conferences ( user ) : <EOL> """<STR_LIT>""" <EOL> conferences = get_memcached ( get_key ( "<STR_LIT>" ) ) <EOL> list_conferences = [ ] <EOL> for key in conferences . keys ( ) : <EOL> if user in conferences [ key ] [ "<STR_LIT>" ] . keys ( ) : <EOL> list_conferences . append ( <EOL> dict ( id = key , <EOL> title = conferences [ key ] [ "<STR_LIT:info>" ] [ "<STR_LIT:title>" ] , <EOL> creator = conferences [ key ] [ "<STR_LIT:info>" ] [ "<STR_LIT>" ] , <EOL> creation_date = str ( <EOL> conferences [ key ] [ "<STR_LIT:info>" ] [ "<STR_LIT>" ] ) , <EOL> users = [ dict ( username = username , profile = get_user_profile ( username ) ) for username in conferences [ key ] [ "<STR_LIT>" ] . keys ( ) if not username == user ] ) <EOL> ) <EOL> return list_conferences <EOL> def get_new_message_for_user ( user , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> def __update_data ( _data ) : <EOL> conferences = get_memcached ( get_key ( "<STR_LIT>" ) ) <EOL> for key in conferences . keys ( ) : <EOL> if user in conferences [ key ] [ "<STR_LIT>" ] . keys ( ) : <EOL> try : <EOL> msg = conferences [ key ] [ "<STR_LIT>" ] [ user ] [ "<STR_LIT>" ] <EOL> except : <EOL> msg = [ ] <EOL> if msg : <EOL> _data [ "<STR_LIT>" ] . append ( { key : { "<STR_LIT>" : msg } } ) <EOL> conferences [ key ] [ "<STR_LIT>" ] [ user ] [ "<STR_LIT>" ] = [ ] <EOL> set_memcached ( get_key ( "<STR_LIT>" ) , conferences ) <EOL> notifications = get_notifications ( kwargs [ "<STR_LIT>" ] ) <EOL> if notifications : <EOL> _data [ "<STR_LIT>" ] = _data [ "<STR_LIT>" ] + notifications <EOL> return _data <EOL> def __get_new_data ( ) : <EOL> listeners = get_memcached ( get_key ( "<STR_LIT>" ) ) <EOL> _new_data = { <EOL> "<STR_LIT>" : [ dict ( name = key , <EOL> location = listeners [ key ] [ "<STR_LIT>" ] , <EOL> profile = listeners [ key ] [ "<STR_LIT>" ] ) for key in listeners . keys ( ) if not key == str ( user ) ] , <EOL> "<STR_LIT>" : [ ] , <EOL> "<STR_LIT>" : get_active_conferences ( user ) , <EOL> "<STR_LIT>" : get_notifications ( kwargs [ "<STR_LIT>" ] ) <EOL> } <EOL> return _new_data . copy ( ) <EOL> if "<STR_LIT>" not in kwargs . keys ( ) : <EOL> kwargs [ "<STR_LIT>" ] = None <EOL> if "<STR_LIT>" not in kwargs . keys ( ) : <EOL> kwargs [ "<STR_LIT>" ] = False <EOL> if "<STR_LIT>" in kwargs . keys ( ) and "<STR_LIT:location>" in kwargs . keys ( ) : <EOL> _location = kwargs [ '<STR_LIT:location>' ] <EOL> else : <EOL> kwargs [ "<STR_LIT>" ] = None <EOL> data = __get_new_data ( ) <EOL> if settings . CHAT_LONG_POLLING and kwargs [ "<STR_LIT>" ] : <EOL> out_time = <NUM_LIT:0> <EOL> while not data [ "<STR_LIT>" ] and out_time < settings . CHAT_TIMEOUT : <EOL> if kwargs [ "<STR_LIT>" ] : <EOL> if _location : <EOL> update_user ( kwargs [ "<STR_LIT>" ] , _location ) <EOL> out_time += <NUM_LIT:1> <EOL> _temp_data = __get_new_data ( ) <EOL> _temp_data = __update_data ( _temp_data ) <EOL> if not _temp_data == data : <EOL> data = _temp_data . copy ( ) <EOL> break <EOL> if kwargs [ "<STR_LIT>" ] == "<STR_LIT>" : <EOL> break <EOL> if not _temp_data [ "<STR_LIT>" ] : <EOL> sleep ( settings . CHAT_TIME_SLEEP_NEWDATA ) <EOL> else : <EOL> data = _temp_data . copy ( ) <EOL> data = json . dumps ( data ) <EOL> return HttpResponse ( data , content_type = '<STR_LIT:application/json>' , status = <NUM_LIT:200> ) <EOL> def add_new_message ( id , user , user_profile , text ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> if not verification_user ( id , user ) : <EOL> return get_new_message_for_user ( user ) <EOL> if checking_conference ( id ) : <EOL> conferences = get_memcached ( get_key ( "<STR_LIT>" ) ) <EOL> for key in conferences [ id ] [ "<STR_LIT>" ] . keys ( ) : <EOL> conferences [ id ] [ "<STR_LIT>" ] [ key ] [ "<STR_LIT>" ] . append ( <EOL> dict ( user = user , <EOL> text = text , <EOL> time = strftime ( "<STR_LIT>" ) , <EOL> date = strftime ( "<STR_LIT>" ) , <EOL> profile = user_profile ) <EOL> ) <EOL> set_memcached ( get_key ( "<STR_LIT>" ) , conferences ) <EOL> except : <EOL> data = json . dumps ( <EOL> { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT:data>" : { "<STR_LIT>" : str ( sys . exc_info ( ) ) } } ) <EOL> return HttpResponse ( data , content_type = '<STR_LIT:application/json>' , status = <NUM_LIT:200> ) <EOL> return get_new_message_for_user ( user ) <EOL> def connect ( user , location ) : <EOL> update_user ( user , location ) <EOL> return get_new_message_for_user ( str ( user ) . lower ( ) , location = location , long_polling = False , user_obj = user ) <EOL> def disconnect ( user ) : <EOL> listeners = get_memcached ( get_key ( "<STR_LIT>" ) ) <EOL> del listeners [ user ] <EOL> set_memcached ( get_key ( "<STR_LIT>" ) , listeners ) <EOL> return HttpResponse ( json . dumps ( { "<STR_LIT>" : "<STR_LIT>" } ) , content_type = '<STR_LIT:application/json>' , status = <NUM_LIT:200> ) <EOL> def cmd ( message , user ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> user_obj = user <EOL> user_profile = str ( user . profile ) <EOL> user = str ( user ) . lower ( ) <EOL> data = json . loads ( message [ "<STR_LIT>" ] ) <EOL> except : <EOL> print "<STR_LIT>" , sys . exc_info ( ) <EOL> data = json . dumps ( <EOL> { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT:data>" : { "<STR_LIT>" : str ( sys . exc_info ( ) ) } } ) <EOL> return HttpResponse ( data , content_type = '<STR_LIT:application/json>' , status = <NUM_LIT:200> ) <EOL> try : <EOL> update_user ( user_obj , data [ '<STR_LIT:location>' ] ) <EOL> if data [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> return connect ( user_obj , data [ '<STR_LIT:location>' ] ) <EOL> if data [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> return disconnect ( user ) <EOL> if data [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> return get_new_message_for_user ( user , user_obj = user_obj , location = data [ '<STR_LIT:location>' ] , long_polling = True ) <EOL> if data [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> return add_new_message ( data [ "<STR_LIT:data>" ] [ "<STR_LIT:id>" ] , user , user_profile , data [ "<STR_LIT:data>" ] [ "<STR_LIT:text>" ] ) <EOL> if data [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> return exit_from_conference ( data [ "<STR_LIT:data>" ] [ "<STR_LIT:id>" ] , user ) <EOL> if data [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> return delete_conference ( data [ '<STR_LIT:data>' ] [ '<STR_LIT:id>' ] , user ) <EOL> if data [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> return remove_users_in_conference ( data [ '<STR_LIT:data>' ] [ '<STR_LIT:id>' ] , user , data [ '<STR_LIT:data>' ] [ '<STR_LIT>' ] ) <EOL> if data [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> return add_users_in_conference ( data [ '<STR_LIT:data>' ] [ '<STR_LIT:id>' ] , user , data [ '<STR_LIT:data>' ] [ '<STR_LIT>' ] ) <EOL> if data [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> return create_conference ( user , data [ '<STR_LIT:data>' ] [ '<STR_LIT>' ] , data [ '<STR_LIT:data>' ] [ '<STR_LIT:title>' ] ) <EOL> except : <EOL> print "<STR_LIT>" , sys . exc_info ( ) <EOL> data = json . dumps ( <EOL> { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT:data>" : { "<STR_LIT>" : str ( sys . exc_info ( ) ) } } ) <EOL> return HttpResponse ( data , content_type = '<STR_LIT:application/json>' , status = <NUM_LIT:200> ) <EOL> return HttpResponse ( json . dumps ( { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT:data>" : { "<STR_LIT>" : "<STR_LIT>" } } ) , <EOL> content_type = '<STR_LIT:application/json>' , status = <NUM_LIT:200> ) <EOL> class Search_Inactive_Users ( threading . Thread ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> threading . Thread . __init__ ( self ) <EOL> def run ( self ) : <EOL> while True : <EOL> try : <EOL> sleep ( settings . CHAT_TIME_SLEEP_THREAD ) <EOL> listeners = get_memcached ( get_key ( "<STR_LIT>" ) ) <EOL> for user in listeners . keys ( ) : <EOL> time = datetime . now ( ) - listeners [ user ] [ '<STR_LIT>' ] <EOL> if time . seconds > settings . CHAT_TIMEOUT : <EOL> del listeners [ user ] <EOL> set_memcached ( get_key ( "<STR_LIT>" ) , listeners ) <EOL> except : <EOL> print "<STR_LIT>" , sys . exc_info ( ) <EOL> class ChatAjaxMiddleware ( object ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> if not settings . HARDTREE_CRON_DISABLED : <EOL> Search_Inactive_Users ( ) . start ( ) <EOL> pass <EOL> super ( ChatAjaxMiddleware , self ) . __init__ ( * args , ** kwargs ) <EOL> def process_request ( self , request ) : <EOL> if not request . META [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> return <EOL> if not request . user . is_authenticated ( ) : <EOL> data = json . dumps ( <EOL> { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT:data>" : "<STR_LIT>" } ) <EOL> response = HttpResponse ( content_type = '<STR_LIT:application/json>' ) <EOL> response . write ( data ) <EOL> return response <EOL> if request . method == "<STR_LIT:POST>" : <EOL> return cmd ( request . POST , request . user ) <EOL> return </s>
