<s> import datetime <EOL> from collections import namedtuple <EOL> import json <EOL> import re <EOL> import redis <EOL> from django . conf import settings <EOL> from django . core . cache import caches <EOL> from django . core . exceptions import ImproperlyConfigured <EOL> from django . dispatch import receiver <EOL> from django . test . signals import setting_changed <EOL> from django . utils . crypto import get_random_string <EOL> from django . utils . timezone import utc <EOL> _client = None <EOL> _settings = None <EOL> @ receiver ( setting_changed ) <EOL> def reset_caches ( ** kwargs ) : <EOL> global _client , _settings <EOL> if kwargs [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> _client = None <EOL> _settings = None <EOL> def get_redis_client ( ) : <EOL> global _client <EOL> if _client is not None : <EOL> return _client <EOL> try : <EOL> cache = caches [ '<STR_LIT>' ] <EOL> except KeyError : <EOL> raise ImproperlyConfigured ( "<STR_LIT>" ) <EOL> try : <EOL> try : <EOL> _client = cache . client <EOL> except AttributeError : <EOL> _client = cache . _client <EOL> assert isinstance ( _client , redis . StrictRedis ) <EOL> except ( AssertionError , AttributeError ) : <EOL> raise ImproperlyConfigured ( "<STR_LIT>" ) <EOL> return _client <EOL> def convert_timestamp ( ts ) : <EOL> if settings . USE_TZ : <EOL> return datetime . datetime . utcfromtimestamp ( ts ) . replace ( tzinfo = utc ) <EOL> else : <EOL> return datetime . datetime . fromtimestamp ( ts ) <EOL> def get_log ( username ) : <EOL> """<STR_LIT>""" <EOL> redis = get_redis_client ( ) <EOL> log_key = '<STR_LIT>' . format ( username ) <EOL> raw_log = redis . lrange ( log_key , <NUM_LIT:0> , - <NUM_LIT:1> ) <EOL> log = [ ] <EOL> for raw_item in raw_log : <EOL> item = json . loads ( raw_item . decode ( ) ) <EOL> item [ '<STR_LIT>' ] = convert_timestamp ( item . pop ( '<STR_LIT:time>' ) ) <EOL> log . append ( item ) <EOL> return log <EOL> def get_token ( username , length = <NUM_LIT:20> , timeout = <NUM_LIT:20> ) : <EOL> """<STR_LIT>""" <EOL> redis = get_redis_client ( ) <EOL> token = get_random_string ( length ) <EOL> token_key = '<STR_LIT>' . format ( token ) <EOL> redis . set ( token_key , username ) <EOL> redis . expire ( token_key , timeout ) <EOL> return token <EOL> UserLogSettings = namedtuple ( <EOL> '<STR_LIT>' , <EOL> [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def get_userlog_settings ( ) : <EOL> global _settings <EOL> if _settings is not None : <EOL> return _settings <EOL> def get_setting ( name , default ) : <EOL> return getattr ( settings , '<STR_LIT>' + name , default ) <EOL> _settings = UserLogSettings ( <EOL> timeout = int ( settings . CACHES [ '<STR_LIT>' ] . get ( '<STR_LIT>' , <NUM_LIT> ) ) , <EOL> max_size = int ( get_setting ( '<STR_LIT>' , <NUM_LIT> ) ) , <EOL> publish = bool ( get_setting ( '<STR_LIT>' , True ) ) , <EOL> ignore_urls = [ re . compile ( pattern ) <EOL> for pattern in get_setting ( '<STR_LIT>' , [ ] ) ] , <EOL> websocket_address = str ( get_setting ( '<STR_LIT>' , <EOL> '<STR_LIT>' ) ) , <EOL> ) <EOL> return _settings </s>
