<s> import base64 <EOL> import re <EOL> import unicodedata <EOL> import json <EOL> from django . core . exceptions import ImproperlyConfigured <EOL> from django . core . validators import validate_email , ValidationError <EOL> from django . core import urlresolvers <EOL> from django . contrib . sites . models import Site <EOL> from django . db . models import FieldDoesNotExist <EOL> from django . db . models . fields import ( DateTimeField , DateField , <EOL> EmailField , TimeField , <EOL> BinaryField ) <EOL> from django . utils import six , dateparse <EOL> from django . utils . six . moves . urllib . parse import urlsplit <EOL> from django . core . serializers . json import DjangoJSONEncoder <EOL> try : <EOL> from django . utils . encoding import force_text , force_bytes <EOL> except ImportError : <EOL> from django . utils . encoding import force_unicode as force_text <EOL> from allauth . compat import importlib <EOL> def _generate_unique_username_base ( txts , regex = None ) : <EOL> from . account . adapter import get_adapter <EOL> adapter = get_adapter ( ) <EOL> username = None <EOL> regex = regex or '<STR_LIT>' <EOL> for txt in txts : <EOL> if not txt : <EOL> continue <EOL> username = unicodedata . normalize ( '<STR_LIT>' , force_text ( txt ) ) <EOL> username = username . encode ( '<STR_LIT:ascii>' , '<STR_LIT:ignore>' ) . decode ( '<STR_LIT:ascii>' ) <EOL> username = force_text ( re . sub ( regex , '<STR_LIT>' , username ) . lower ( ) ) <EOL> username = username . split ( '<STR_LIT:@>' ) [ <NUM_LIT:0> ] <EOL> username = username . strip ( ) <EOL> username = re . sub ( '<STR_LIT>' , '<STR_LIT:_>' , username ) <EOL> try : <EOL> username = adapter . clean_username ( username , shallow = True ) <EOL> break <EOL> except ValidationError : <EOL> pass <EOL> return username or '<STR_LIT:user>' <EOL> def get_username_max_length ( ) : <EOL> from . account . app_settings import USER_MODEL_USERNAME_FIELD <EOL> if USER_MODEL_USERNAME_FIELD is not None : <EOL> User = get_user_model ( ) <EOL> max_length = User . _meta . get_field ( USER_MODEL_USERNAME_FIELD ) . max_length <EOL> else : <EOL> max_length = <NUM_LIT:0> <EOL> return max_length <EOL> def generate_unique_username ( txts , regex = None ) : <EOL> from . account . adapter import get_adapter <EOL> adapter = get_adapter ( ) <EOL> username = _generate_unique_username_base ( txts , regex ) <EOL> max_length = get_username_max_length ( ) <EOL> i = <NUM_LIT:0> <EOL> while True : <EOL> try : <EOL> if i : <EOL> pfx = str ( i + <NUM_LIT:1> ) <EOL> else : <EOL> pfx = '<STR_LIT>' <EOL> ret = username [ <NUM_LIT:0> : max_length - len ( pfx ) ] + pfx <EOL> return adapter . clean_username ( ret ) <EOL> except ValidationError : <EOL> i += <NUM_LIT:1> <EOL> def valid_email_or_none ( email ) : <EOL> ret = None <EOL> try : <EOL> if email : <EOL> validate_email ( email ) <EOL> if len ( email ) <= EmailField ( ) . max_length : <EOL> ret = email <EOL> except ValidationError : <EOL> pass <EOL> return ret <EOL> def email_address_exists ( email , exclude_user = None ) : <EOL> from . account import app_settings as account_settings <EOL> from . account . models import EmailAddress <EOL> emailaddresses = EmailAddress . objects <EOL> if exclude_user : <EOL> emailaddresses = emailaddresses . exclude ( user = exclude_user ) <EOL> ret = emailaddresses . filter ( email__iexact = email ) . exists ( ) <EOL> if not ret : <EOL> email_field = account_settings . USER_MODEL_EMAIL_FIELD <EOL> if email_field : <EOL> users = get_user_model ( ) . objects <EOL> if exclude_user : <EOL> users = users . exclude ( pk = exclude_user . pk ) <EOL> ret = users . filter ( ** { email_field + '<STR_LIT>' : email } ) . exists ( ) <EOL> return ret <EOL> def import_attribute ( path ) : <EOL> assert isinstance ( path , six . string_types ) <EOL> pkg , attr = path . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) <EOL> ret = getattr ( importlib . import_module ( pkg ) , attr ) <EOL> return ret <EOL> def import_callable ( path_or_callable ) : <EOL> if not hasattr ( path_or_callable , '<STR_LIT>' ) : <EOL> ret = import_attribute ( path_or_callable ) <EOL> else : <EOL> ret = path_or_callable <EOL> return ret <EOL> try : <EOL> from django . contrib . auth import get_user_model <EOL> except ImportError : <EOL> def get_user_model ( ) : <EOL> from . import app_settings <EOL> from django . db . models import get_model <EOL> try : <EOL> app_label , model_name = app_settings . USER_MODEL . split ( '<STR_LIT:.>' ) <EOL> except ValueError : <EOL> raise ImproperlyConfigured ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> user_model = get_model ( app_label , model_name ) <EOL> if user_model is None : <EOL> raise ImproperlyConfigured ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % app_settings . USER_MODEL ) <EOL> return user_model <EOL> def get_current_site ( request = None ) : <EOL> """<STR_LIT>""" <EOL> if request and hasattr ( Site . objects , '<STR_LIT>' ) : <EOL> site = Site . objects . get_current ( request = request ) <EOL> else : <EOL> site = Site . objects . get_current ( ) <EOL> return site <EOL> def resolve_url ( to ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return urlresolvers . reverse ( to ) <EOL> except urlresolvers . NoReverseMatch : <EOL> if '<STR_LIT:/>' not in to and '<STR_LIT:.>' not in to : <EOL> raise <EOL> return to <EOL> def serialize_instance ( instance ) : <EOL> """<STR_LIT>""" <EOL> data = { } <EOL> for k , v in instance . __dict__ . items ( ) : <EOL> if k . startswith ( '<STR_LIT:_>' ) or callable ( v ) : <EOL> continue <EOL> try : <EOL> if isinstance ( instance . _meta . get_field ( k ) , BinaryField ) : <EOL> v = force_text ( base64 . b64encode ( v ) ) <EOL> except FieldDoesNotExist : <EOL> pass <EOL> data [ k ] = v <EOL> return json . loads ( json . dumps ( data , cls = DjangoJSONEncoder ) ) <EOL> def deserialize_instance ( model , data ) : <EOL> ret = model ( ) <EOL> for k , v in data . items ( ) : <EOL> if v is not None : <EOL> try : <EOL> f = model . _meta . get_field ( k ) <EOL> if isinstance ( f , DateTimeField ) : <EOL> v = dateparse . parse_datetime ( v ) <EOL> elif isinstance ( f , TimeField ) : <EOL> v = dateparse . parse_time ( v ) <EOL> elif isinstance ( f , DateField ) : <EOL> v = dateparse . parse_date ( v ) <EOL> elif isinstance ( f , BinaryField ) : <EOL> v = force_bytes ( <EOL> base64 . b64decode ( <EOL> force_bytes ( v ) ) ) <EOL> except FieldDoesNotExist : <EOL> pass <EOL> setattr ( ret , k , v ) <EOL> return ret <EOL> def set_form_field_order ( form , fields_order ) : <EOL> if hasattr ( form . fields , '<STR_LIT>' ) : <EOL> form . fields . keyOrder = fields_order <EOL> else : <EOL> from collections import OrderedDict <EOL> assert isinstance ( form . fields , OrderedDict ) <EOL> form . fields = OrderedDict ( ( f , form . fields [ f ] ) <EOL> for f in fields_order ) <EOL> def build_absolute_uri ( request , location , protocol = None ) : <EOL> """<STR_LIT>""" <EOL> from . account import app_settings as account_settings <EOL> if request is None : <EOL> site = get_current_site ( ) <EOL> bits = urlsplit ( location ) <EOL> if not ( bits . scheme and bits . netloc ) : <EOL> uri = '<STR_LIT>' . format ( <EOL> proto = account_settings . DEFAULT_HTTP_PROTOCOL , <EOL> domain = site . domain , <EOL> url = location ) <EOL> else : <EOL> uri = location <EOL> else : <EOL> uri = request . build_absolute_uri ( location ) <EOL> if not protocol and account_settings . DEFAULT_HTTP_PROTOCOL == '<STR_LIT>' : <EOL> protocol = account_settings . DEFAULT_HTTP_PROTOCOL <EOL> if protocol : <EOL> uri = protocol + '<STR_LIT::>' + uri . partition ( '<STR_LIT::>' ) [ <NUM_LIT:2> ] <EOL> return uri <EOL> def get_form_class ( forms , form_id , default_form ) : <EOL> form_class = forms . get ( form_id , default_form ) <EOL> if isinstance ( form_class , six . string_types ) : <EOL> form_class = import_attribute ( form_class ) <EOL> return form_class <EOL> def get_request_param ( request , param , default = None ) : <EOL> return request . POST . get ( param ) or request . GET . get ( param , default ) </s>
