<s> from django import forms <EOL> from django . forms import widgets <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from userena . utils import get_user_model <EOL> class CommaSeparatedUserInput ( widgets . Input ) : <EOL> input_type = '<STR_LIT:text>' <EOL> def render ( self , name , value , attrs = None ) : <EOL> if value is None : <EOL> value = '<STR_LIT>' <EOL> elif isinstance ( value , ( list , tuple ) ) : <EOL> value = ( '<STR_LIT:U+002CU+0020>' . join ( [ user . username for user in value ] ) ) <EOL> return super ( CommaSeparatedUserInput , self ) . render ( name , value , attrs ) <EOL> class CommaSeparatedUserField ( forms . Field ) : <EOL> """<STR_LIT>""" <EOL> widget = CommaSeparatedUserInput <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> recipient_filter = kwargs . pop ( '<STR_LIT>' , None ) <EOL> self . _recipient_filter = recipient_filter <EOL> super ( CommaSeparatedUserField , self ) . __init__ ( * args , ** kwargs ) <EOL> def clean ( self , value ) : <EOL> super ( CommaSeparatedUserField , self ) . clean ( value ) <EOL> names = set ( value . split ( '<STR_LIT:U+002C>' ) ) <EOL> names_set = set ( [ name . strip ( ) for name in names ] ) <EOL> users = list ( get_user_model ( ) . objects . filter ( username__in = names_set ) ) <EOL> unknown_names = names_set ^ set ( [ user . username for user in users ] ) <EOL> recipient_filter = self . _recipient_filter <EOL> invalid_users = [ ] <EOL> if recipient_filter is not None : <EOL> for r in users : <EOL> if recipient_filter ( r ) is False : <EOL> users . remove ( r ) <EOL> invalid_users . append ( r . username ) <EOL> if unknown_names or invalid_users : <EOL> humanized_usernames = '<STR_LIT:U+002CU+0020>' . join ( list ( unknown_names ) + invalid_users ) <EOL> raise forms . ValidationError ( _ ( "<STR_LIT>" ) % { '<STR_LIT>' : humanized_usernames } ) <EOL> return users </s>
