<s> from grouper import public_key <EOL> from grouper . email_util import send_email <EOL> from grouper . fe . forms import PublicKeyForm <EOL> from grouper . fe . settings import settings <EOL> from grouper . fe . util import GrouperHandler <EOL> from grouper . model_soup import User <EOL> from grouper . models . audit_log import AuditLog <EOL> class PublicKeyAdd ( GrouperHandler ) : <EOL> def get ( self , user_id = None , name = None ) : <EOL> user = User . get ( self . session , user_id , name ) <EOL> if not user : <EOL> return self . notfound ( ) <EOL> if ( user . name != self . current_user . name ) and not self . current_user . user_admin : <EOL> return self . forbidden ( ) <EOL> self . render ( "<STR_LIT>" , form = PublicKeyForm ( ) , user = user ) <EOL> def post ( self , user_id = None , name = None ) : <EOL> user = User . get ( self . session , user_id , name ) <EOL> if not user : <EOL> return self . notfound ( ) <EOL> if ( user . name != self . current_user . name ) and not self . current_user . user_admin : <EOL> return self . forbidden ( ) <EOL> form = PublicKeyForm ( self . request . arguments ) <EOL> if not form . validate ( ) : <EOL> return self . render ( <EOL> "<STR_LIT>" , form = form , user = user , <EOL> alerts = self . get_form_alerts ( form . errors ) , <EOL> ) <EOL> try : <EOL> pubkey = public_key . add_public_key ( self . session , user , form . data [ "<STR_LIT>" ] ) <EOL> except public_key . PublicKeyParseError : <EOL> form . public_key . errors . append ( <EOL> "<STR_LIT>" <EOL> ) <EOL> return self . render ( <EOL> "<STR_LIT>" , form = form , user = user , <EOL> alerts = self . get_form_alerts ( form . errors ) , <EOL> ) <EOL> except public_key . DuplicateKey : <EOL> form . public_key . errors . append ( <EOL> "<STR_LIT>" <EOL> ) <EOL> return self . render ( <EOL> "<STR_LIT>" , form = form , user = user , <EOL> alerts = self . get_form_alerts ( form . errors ) , <EOL> ) <EOL> AuditLog . log ( self . session , self . current_user . id , '<STR_LIT>' , <EOL> '<STR_LIT>' . format ( pubkey . fingerprint ) , <EOL> on_user_id = user . id ) <EOL> email_context = { <EOL> "<STR_LIT>" : self . current_user . name , <EOL> "<STR_LIT>" : user . name , <EOL> "<STR_LIT:action>" : "<STR_LIT>" , <EOL> } <EOL> send_email ( self . session , [ user . name ] , '<STR_LIT>' , '<STR_LIT>' , <EOL> settings , email_context ) <EOL> return self . redirect ( "<STR_LIT>" . format ( user . name ) ) </s>
