<s> from oauth2_provider . oauth2_backends import OAuthLibCore , get_oauthlib_core <EOL> from django . utils . http import urlencode <EOL> import ast <EOL> from django . utils import timezone <EOL> from rest_framework . pagination import PageNumberPagination <EOL> from rest_framework . renderers import JSONRenderer <EOL> from django . http import HttpResponse <EOL> from csp import settings <EOL> import string <EOL> import random <EOL> import datetime <EOL> def get_delimiter ( filename , * args , ** kwargs ) : <EOL> delimiter_map = { '<STR_LIT>' : '<STR_LIT:U+002C>' , '<STR_LIT>' : '<STR_LIT:\t>' } <EOL> delimiter = None <EOL> extension = filename . split ( '<STR_LIT:.>' ) [ - <NUM_LIT:1> ] <EOL> if extension in delimiter_map : <EOL> delimiter = delimiter_map [ extension ] <EOL> return delimiter <EOL> def get_model_or_none ( model , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return model . objects . get ( * args , ** kwargs ) <EOL> except model . DoesNotExist : <EOL> return None <EOL> def get_next_unique_id ( model , field , value ) : <EOL> """<STR_LIT>""" <EOL> condition = { } <EOL> condition [ '<STR_LIT>' % field ] = r'<STR_LIT>' % value <EOL> values = model . objects . filter ( ** condition ) . values_list ( field , flat = True ) <EOL> integers = map ( lambda x : int ( x . replace ( value , '<STR_LIT>' ) ) , values ) <EOL> all_values = range ( <NUM_LIT:1> , len ( integers ) + <NUM_LIT:2> ) <EOL> gap = list ( set ( all_values ) - set ( integers ) ) [ <NUM_LIT:0> ] <EOL> new_field_value = '<STR_LIT>' % ( value , gap ) <EOL> return new_field_value <EOL> def get_time_delta ( time_stamp ) : <EOL> difference = timezone . now ( ) - time_stamp <EOL> days = difference . days <EOL> hours = difference . seconds // <NUM_LIT> <EOL> minutes = ( difference . seconds // <NUM_LIT> ) % <NUM_LIT> <EOL> if minutes > <NUM_LIT:0> and hours == <NUM_LIT:0> and days == <NUM_LIT:0> : <EOL> minutes_calculated = str ( minutes ) + "<STR_LIT>" <EOL> elif minutes > <NUM_LIT:0> and ( hours != <NUM_LIT:0> or days != <NUM_LIT:0> ) : <EOL> minutes_calculated = "<STR_LIT>" <EOL> else : <EOL> minutes_calculated = "<STR_LIT>" <EOL> return "<STR_LIT>" . format ( days = str ( days ) + "<STR_LIT>" if days > <NUM_LIT:0> else "<STR_LIT>" , <EOL> hours = str ( hours ) + "<STR_LIT>" if hours > <NUM_LIT:0> and days == <NUM_LIT:0> else "<STR_LIT>" , <EOL> minutes = minutes_calculated ) + "<STR_LIT>" <EOL> class Oauth2Backend ( OAuthLibCore ) : <EOL> def _extract_params ( self , request ) : <EOL> """<STR_LIT>""" <EOL> uri = self . _get_escaped_full_path ( request ) <EOL> http_method = request . method <EOL> headers = { } <EOL> body = urlencode ( self . extract_body ( request ) ) <EOL> return uri , http_method , body , headers <EOL> def create_token_response ( self , request ) : <EOL> """<STR_LIT>""" <EOL> uri , http_method , body , headers = self . _extract_params ( request ) <EOL> headers , body , status = get_oauthlib_core ( ) . server . create_token_response ( uri , http_method , body , <EOL> headers ) <EOL> uri = headers . get ( "<STR_LIT>" , None ) <EOL> return uri , headers , body , status <EOL> def extract_body ( self , request ) : <EOL> """<STR_LIT>""" <EOL> return request . data . items ( ) <EOL> class Oauth2Utils : <EOL> def create_client ( self , request , user ) : <EOL> from oauth2_provider . models import Application <EOL> oauth2_client = Application . objects . create ( user = user , <EOL> client_type = Application . CLIENT_CONFIDENTIAL , <EOL> authorization_grant_type = Application . GRANT_PASSWORD ) <EOL> return oauth2_client <EOL> def get_token ( self , request ) : <EOL> oauth2_backend = Oauth2Backend ( ) <EOL> uri , headers , body , status = oauth2_backend . create_token_response ( request ) <EOL> response_data = { } <EOL> response_data [ "<STR_LIT:message>" ] = "<STR_LIT:OK>" <EOL> response_data . update ( ast . literal_eval ( body ) ) <EOL> return response_data , status <EOL> def get_refresh_token ( self , request ) : <EOL> pass <EOL> class SmallResultSetPagination ( PageNumberPagination ) : <EOL> page_size = <NUM_LIT> <EOL> page_size_query_param = '<STR_LIT>' <EOL> max_page_size = <NUM_LIT:100> <EOL> class JSONResponse ( HttpResponse ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , data , ** kwargs ) : <EOL> content = JSONRenderer ( ) . render ( data ) <EOL> kwargs [ '<STR_LIT>' ] = '<STR_LIT:application/json>' <EOL> super ( JSONResponse , self ) . __init__ ( content , ** kwargs ) <EOL> class PayPalBackend : <EOL> def __init__ ( self ) : <EOL> import paypalrestsdk <EOL> paypalrestsdk . configure ( { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : settings . PAYPAL_CLIENT_ID , <EOL> "<STR_LIT>" : settings . PAYPAL_CLIENT_SECRET <EOL> } ) <EOL> self . paypalrestsdk = paypalrestsdk <EOL> def generate_random_id ( length = <NUM_LIT:8> , chars = string . ascii_lowercase + string . digits ) : <EOL> return '<STR_LIT>' . join ( random . choice ( chars ) for _ in range ( length ) ) <EOL> def get_relative_time ( date_time ) : <EOL> delta = datetime . timedelta ( days = <NUM_LIT:7> ) <EOL> current = timezone . now ( ) <EOL> difference = current - date_time <EOL> if difference . total_seconds ( ) - delta . total_seconds ( ) > <NUM_LIT:0> : <EOL> return date_time . strftime ( "<STR_LIT>" ) + '<STR_LIT:U+0020>' + str ( date_time . day ) <EOL> else : <EOL> one_day = datetime . timedelta ( days = <NUM_LIT:1> ) <EOL> if difference . total_seconds ( ) - one_day . total_seconds ( ) > <NUM_LIT:0> : <EOL> return date_time . strftime ( "<STR_LIT>" ) <EOL> else : <EOL> return date_time . strftime ( '<STR_LIT>' ) . lstrip ( '<STR_LIT:0>' ) </s>
