<s> import json <EOL> import re <EOL> import sys <EOL> from contextlib import contextmanager <EOL> from datetime import datetime <EOL> from django . conf import settings <EOL> from django . contrib . sites . models import Site <EOL> from django . db import models <EOL> from django . db . models . signals import pre_delete <EOL> from django . utils import translation <EOL> from django . utils . http import urlencode , is_safe_url <EOL> import ratelimit . helpers <EOL> from kitsune . sumo import paginator <EOL> from kitsune . journal . models import Record <EOL> def paginate ( request , queryset , per_page = <NUM_LIT:20> , count = None ) : <EOL> """<STR_LIT>""" <EOL> p = paginator . Paginator ( queryset , per_page , count = count ) <EOL> try : <EOL> page = int ( request . GET . get ( '<STR_LIT>' , <NUM_LIT:1> ) ) <EOL> except ValueError : <EOL> page = <NUM_LIT:1> <EOL> try : <EOL> paginated = p . page ( page ) <EOL> except ( paginator . EmptyPage , paginator . InvalidPage ) : <EOL> paginated = p . page ( <NUM_LIT:1> ) <EOL> paginated . url = build_paged_url ( request ) <EOL> return paginated <EOL> def simple_paginate ( request , queryset , per_page = <NUM_LIT:20> ) : <EOL> """<STR_LIT>""" <EOL> p = paginator . SimplePaginator ( queryset , per_page ) <EOL> page = p . page ( request . GET . get ( '<STR_LIT>' , <NUM_LIT:1> ) ) <EOL> page . url = build_paged_url ( request ) <EOL> return page <EOL> def build_paged_url ( request ) : <EOL> """<STR_LIT>""" <EOL> base = request . build_absolute_uri ( request . path ) <EOL> items = [ ( k , v ) for k in request . GET if k != '<STR_LIT>' <EOL> for v in request . GET . getlist ( k ) if v ] <EOL> qsa = urlencode ( items ) <EOL> return u'<STR_LIT>' % ( base , qsa ) <EOL> def chunked ( seq , n , length = None ) : <EOL> """<STR_LIT>""" <EOL> if not length : <EOL> length = len ( seq ) <EOL> for i in xrange ( <NUM_LIT:0> , length , n ) : <EOL> yield seq [ i : i + n ] <EOL> def smart_int ( string , fallback = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return int ( float ( string ) ) <EOL> except ( ValueError , TypeError , OverflowError ) : <EOL> return fallback <EOL> def delete_files_for_obj ( sender , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> obj = kwargs . pop ( '<STR_LIT>' ) <EOL> for field_name in sender . _meta . get_all_field_names ( ) : <EOL> if not hasattr ( obj , field_name ) : <EOL> continue <EOL> try : <EOL> field_class = sender . _meta . get_field ( field_name ) <EOL> except models . FieldDoesNotExist : <EOL> continue <EOL> field_value = getattr ( obj , field_name ) <EOL> if isinstance ( field_class , models . FileField ) and field_value : <EOL> field_value . delete ( ) <EOL> def auto_delete_files ( cls ) : <EOL> """<STR_LIT>""" <EOL> pre_delete . connect ( delete_files_for_obj , sender = cls ) <EOL> return cls <EOL> def get_next_url ( request ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT>' in request . POST : <EOL> url = request . POST . get ( '<STR_LIT>' ) <EOL> elif '<STR_LIT>' in request . GET : <EOL> url = request . GET . get ( '<STR_LIT>' ) <EOL> else : <EOL> url = request . META . get ( '<STR_LIT>' ) <EOL> if not settings . DEBUG and not is_safe_url ( url , Site . objects . get_current ( ) . domain ) : <EOL> return None <EOL> return url <EOL> class TruncationException ( Exception ) : <EOL> pass <EOL> def truncated_json_dumps ( obj , max_length , key , ensure_ascii = False ) : <EOL> """<STR_LIT>""" <EOL> orig = json . dumps ( obj , ensure_ascii = ensure_ascii ) <EOL> diff = len ( orig ) - max_length <EOL> if diff < <NUM_LIT:0> : <EOL> return orig <EOL> dupe = json . loads ( orig ) <EOL> if len ( dupe [ key ] ) < diff : <EOL> raise TruncationException ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> dupe [ key ] = dupe [ key ] [ : - diff ] <EOL> return json . dumps ( dupe , ensure_ascii = ensure_ascii ) <EOL> def user_or_ip ( key_prefix ) : <EOL> """<STR_LIT>""" <EOL> def _user_or_ip ( request ) : <EOL> if hasattr ( request , '<STR_LIT:user>' ) and request . user . is_authenticated ( ) : <EOL> key = str ( request . user . pk ) <EOL> else : <EOL> key = request . META . get ( '<STR_LIT>' , <EOL> request . META [ '<STR_LIT>' ] ) <EOL> return '<STR_LIT>' % ( key_prefix , key ) <EOL> return _user_or_ip <EOL> @ contextmanager <EOL> def uselocale ( locale ) : <EOL> """<STR_LIT>""" <EOL> currlocale = translation . get_language ( ) <EOL> translation . activate ( locale ) <EOL> yield <EOL> translation . activate ( currlocale ) <EOL> def rabbitmq_queue_size ( ) : <EOL> """<STR_LIT>""" <EOL> from celery import current_app <EOL> app = current_app . _get_current_object ( ) <EOL> conn = app . connection ( ) <EOL> chan = conn . default_channel <EOL> queue = chan . queue_declare ( '<STR_LIT>' , passive = True ) <EOL> return queue . message_count <EOL> class Progress ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , total , milestone_stride = <NUM_LIT:10> ) : <EOL> """<STR_LIT>""" <EOL> self . current = <NUM_LIT:0> <EOL> self . total = total <EOL> self . milestone_stride = milestone_stride <EOL> self . milestone_time = datetime . now ( ) <EOL> self . estimated = '<STR_LIT:?>' <EOL> def tick ( self , incr = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> self . current += incr <EOL> if self . current and self . current % self . milestone_stride == <NUM_LIT:0> : <EOL> now = datetime . now ( ) <EOL> duration = now - self . milestone_time <EOL> duration = duration . seconds + duration . microseconds / <NUM_LIT> <EOL> rate = self . milestone_stride / duration <EOL> remaining = self . total - self . current <EOL> self . estimated = int ( remaining / rate / <NUM_LIT> ) <EOL> self . milestone_time = now <EOL> self . draw ( ) <EOL> def draw ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _wr ( '<STR_LIT>' <EOL> . format ( self ) ) <EOL> def _wr ( self , s ) : <EOL> sys . stdout . write ( s ) <EOL> sys . stdout . flush ( ) <EOL> def is_ratelimited ( request , name , rate , method = [ '<STR_LIT:POST>' ] , skip_if = lambda r : False ) : <EOL> """<STR_LIT>""" <EOL> if skip_if ( request ) or request . user . has_perm ( '<STR_LIT>' ) : <EOL> request . limited = False <EOL> else : <EOL> ratelimit . helpers . is_ratelimited ( <EOL> request , increment = True , ip = False , rate = rate , keys = user_or_ip ( name ) ) <EOL> if request . limited : <EOL> if hasattr ( request , '<STR_LIT:user>' ) and request . user . is_authenticated ( ) : <EOL> key = '<STR_LIT>' . format ( request . user . username ) <EOL> else : <EOL> ip = request . META . get ( '<STR_LIT>' , request . META [ '<STR_LIT>' ] ) <EOL> key = '<STR_LIT>' . format ( ip ) <EOL> Record . objects . info ( '<STR_LIT>' , '<STR_LIT>' , <EOL> key = key , name = name ) <EOL> return request . limited <EOL> def get_browser ( user_agent ) : <EOL> """<STR_LIT>""" <EOL> match = re . search ( r'<STR_LIT>' , user_agent , re . IGNORECASE ) <EOL> if match : <EOL> browser = match . group ( <NUM_LIT:1> ) <EOL> else : <EOL> browser = None <EOL> return browser </s>
