<s> import datetime <EOL> import logging <EOL> import httplib <EOL> import httplib as http <EOL> from dateutil . parser import parse as parse_date <EOL> from flask import request <EOL> import markupsafe <EOL> from modularodm . exceptions import ValidationError , NoResultsFound , MultipleResultsFound <EOL> from modularodm import Q <EOL> from framework import sentry <EOL> from framework . auth import utils as auth_utils <EOL> from framework . auth . decorators import collect_auth <EOL> from framework . auth . decorators import must_be_logged_in <EOL> from framework . auth . exceptions import ChangePasswordError <EOL> from framework . auth . views import send_confirm_email <EOL> from framework . auth . signals import user_merged <EOL> from framework . exceptions import HTTPError , PermissionsError <EOL> from framework . flask import redirect <EOL> from framework . status import push_status_message <EOL> from website import mails <EOL> from website import mailchimp_utils <EOL> from website import settings <EOL> from website . project . model import Node <EOL> from website . models import ApiOAuth2Application , ApiOAuth2PersonalToken , User <EOL> from website . oauth . utils import get_available_scopes <EOL> from website . profile import utils as profile_utils <EOL> from website . util . time import throttle_period_expired <EOL> from website . util import api_v2_url , web_url_for , paths <EOL> from website . util . sanitize import escape_html <EOL> from website . util . sanitize import strip_html <EOL> from website . views import _render_nodes <EOL> from website . addons . base import utils as addon_utils <EOL> logger = logging . getLogger ( __name__ ) <EOL> def get_public_projects ( uid = None , user = None ) : <EOL> user = user or User . load ( uid ) <EOL> nodes = Node . find_for_user ( <EOL> user , <EOL> subquery = ( <EOL> Q ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) & <EOL> Q ( '<STR_LIT>' , '<STR_LIT>' , True ) & <EOL> Q ( '<STR_LIT>' , '<STR_LIT>' , False ) & <EOL> Q ( '<STR_LIT>' , '<STR_LIT>' , False ) <EOL> ) <EOL> ) <EOL> return _render_nodes ( list ( nodes ) ) <EOL> def get_public_components ( uid = None , user = None ) : <EOL> user = user or User . load ( uid ) <EOL> nodes = list ( <EOL> Node . find_for_user ( <EOL> user , <EOL> ( <EOL> Q ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) & <EOL> Q ( '<STR_LIT>' , '<STR_LIT>' , True ) & <EOL> Q ( '<STR_LIT>' , '<STR_LIT>' , False ) & <EOL> Q ( '<STR_LIT>' , '<STR_LIT>' , False ) <EOL> ) <EOL> ) <EOL> ) <EOL> return _render_nodes ( nodes , show_path = True ) <EOL> @ must_be_logged_in <EOL> def current_user_gravatar ( size = None , ** kwargs ) : <EOL> user_id = kwargs [ '<STR_LIT>' ] . user . _id <EOL> return get_gravatar ( user_id , size = size ) <EOL> def get_gravatar ( uid , size = None ) : <EOL> return { '<STR_LIT>' : profile_utils . get_gravatar ( User . load ( uid ) , size = size ) } <EOL> def date_or_none ( date ) : <EOL> try : <EOL> return parse_date ( date ) <EOL> except Exception as error : <EOL> logger . exception ( error ) <EOL> return None <EOL> def validate_user ( data , user ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT:id>' in data : <EOL> if data [ '<STR_LIT:id>' ] != user . _id : <EOL> raise HTTPError ( httplib . FORBIDDEN ) <EOL> else : <EOL> raise HTTPError ( httplib . BAD_REQUEST , data = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> @ must_be_logged_in <EOL> def resend_confirmation ( auth ) : <EOL> user = auth . user <EOL> data = request . get_json ( ) <EOL> validate_user ( data , user ) <EOL> if not throttle_period_expired ( user . email_last_sent , settings . SEND_EMAIL_THROTTLE ) : <EOL> raise HTTPError ( httplib . BAD_REQUEST , <EOL> data = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> try : <EOL> primary = data [ '<STR_LIT:email>' ] [ '<STR_LIT>' ] <EOL> confirmed = data [ '<STR_LIT:email>' ] [ '<STR_LIT>' ] <EOL> address = data [ '<STR_LIT:email>' ] [ '<STR_LIT:address>' ] . strip ( ) . lower ( ) <EOL> except KeyError : <EOL> raise HTTPError ( httplib . BAD_REQUEST ) <EOL> if primary or confirmed : <EOL> raise HTTPError ( httplib . BAD_REQUEST , data = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> user . add_unconfirmed_email ( address ) <EOL> if settings . CONFIRM_REGISTRATIONS_BY_EMAIL : <EOL> send_confirm_email ( user , email = address ) <EOL> user . email_last_sent = datetime . datetime . utcnow ( ) <EOL> user . save ( ) <EOL> return _profile_view ( user , is_profile = True ) <EOL> @ must_be_logged_in <EOL> def update_user ( auth ) : <EOL> """<STR_LIT>""" <EOL> user = auth . user <EOL> data = request . get_json ( ) <EOL> validate_user ( data , user ) <EOL> if '<STR_LIT>' in data : <EOL> emails_list = [ x [ '<STR_LIT:address>' ] . strip ( ) . lower ( ) for x in data [ '<STR_LIT>' ] ] <EOL> if user . username . strip ( ) . lower ( ) not in emails_list : <EOL> raise HTTPError ( httplib . FORBIDDEN ) <EOL> available_emails = [ <EOL> each . strip ( ) . lower ( ) for each in <EOL> user . emails + user . unconfirmed_emails <EOL> ] <EOL> removed_emails = [ <EOL> each . strip ( ) . lower ( ) <EOL> for each in available_emails <EOL> if each not in emails_list <EOL> ] <EOL> if user . username . strip ( ) . lower ( ) in removed_emails : <EOL> raise HTTPError ( httplib . FORBIDDEN ) <EOL> for address in removed_emails : <EOL> if address in user . emails : <EOL> try : <EOL> user . remove_email ( address ) <EOL> except PermissionsError as e : <EOL> raise HTTPError ( httplib . FORBIDDEN , e . message ) <EOL> user . remove_unconfirmed_email ( address ) <EOL> added_emails = [ <EOL> each [ '<STR_LIT:address>' ] . strip ( ) . lower ( ) <EOL> for each in data [ '<STR_LIT>' ] <EOL> if each [ '<STR_LIT:address>' ] . strip ( ) . lower ( ) not in available_emails <EOL> ] <EOL> for address in added_emails : <EOL> try : <EOL> user . add_unconfirmed_email ( address ) <EOL> except ( ValidationError , ValueError ) : <EOL> raise HTTPError ( http . BAD_REQUEST , data = dict ( <EOL> message_long = "<STR_LIT>" ) <EOL> ) <EOL> if settings . CONFIRM_REGISTRATIONS_BY_EMAIL : <EOL> send_confirm_email ( user , email = address ) <EOL> primary_email = next ( <EOL> ( <EOL> each for each in data [ '<STR_LIT>' ] <EOL> if each . get ( '<STR_LIT>' ) and each . get ( '<STR_LIT>' ) <EOL> and each . get ( '<STR_LIT:address>' ) <EOL> ) <EOL> ) <EOL> if primary_email : <EOL> primary_email_address = primary_email [ '<STR_LIT:address>' ] . strip ( ) . lower ( ) <EOL> if primary_email_address not in [ each . strip ( ) . lower ( ) for each in user . emails ] : <EOL> raise HTTPError ( httplib . FORBIDDEN ) <EOL> username = primary_email_address <EOL> if username and username in user . emails and username != user . username : <EOL> mails . send_mail ( user . username , <EOL> mails . PRIMARY_EMAIL_CHANGED , <EOL> user = user , <EOL> new_address = username ) <EOL> for list_name , subscription in user . mailchimp_mailing_lists . iteritems ( ) : <EOL> if subscription : <EOL> mailchimp_utils . unsubscribe_mailchimp_async ( list_name , user . _id , username = user . username ) <EOL> user . username = username <EOL> if '<STR_LIT>' in data : <EOL> if data [ '<STR_LIT>' ] : <EOL> locale = data [ '<STR_LIT>' ] . replace ( '<STR_LIT:->' , '<STR_LIT:_>' ) <EOL> user . locale = locale <EOL> if '<STR_LIT>' in data : <EOL> if data [ '<STR_LIT>' ] : <EOL> user . timezone = data [ '<STR_LIT>' ] <EOL> user . save ( ) <EOL> for list_name , subscription in user . mailchimp_mailing_lists . iteritems ( ) : <EOL> if subscription : <EOL> mailchimp_utils . subscribe_mailchimp ( list_name , user . _id ) <EOL> return _profile_view ( user , is_profile = True ) <EOL> def _profile_view ( profile , is_profile = False ) : <EOL> from website . addons . badges . util import get_sorted_user_badges <EOL> if profile and profile . is_disabled : <EOL> raise HTTPError ( http . GONE ) <EOL> if '<STR_LIT>' in settings . ADDONS_REQUESTED : <EOL> badge_assertions = get_sorted_user_badges ( profile ) , <EOL> badges = _get_user_created_badges ( profile ) <EOL> else : <EOL> badge_assertions = [ ] <EOL> badges = [ ] <EOL> if profile : <EOL> profile_user_data = profile_utils . serialize_user ( profile , full = True , is_profile = is_profile ) <EOL> return { <EOL> '<STR_LIT>' : profile_user_data , <EOL> '<STR_LIT>' : badge_assertions , <EOL> '<STR_LIT>' : badges , <EOL> '<STR_LIT:user>' : { <EOL> '<STR_LIT>' : is_profile , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : [ ] , <EOL> } , <EOL> } <EOL> raise HTTPError ( http . NOT_FOUND ) <EOL> def _get_user_created_badges ( user ) : <EOL> from website . addons . badges . model import Badge <EOL> addon = user . get_addon ( '<STR_LIT>' ) <EOL> if addon : <EOL> return [ badge for badge in Badge . find ( Q ( '<STR_LIT>' , '<STR_LIT>' , addon . _id ) ) if not badge . is_system_badge ] <EOL> return [ ] <EOL> @ must_be_logged_in <EOL> def profile_view ( auth ) : <EOL> return _profile_view ( auth . user , True ) <EOL> @ collect_auth <EOL> def profile_view_id ( uid , auth ) : <EOL> user = User . load ( uid ) <EOL> is_profile = auth and auth . user == user <EOL> return _profile_view ( user , is_profile ) <EOL> @ must_be_logged_in <EOL> def edit_profile ( ** kwargs ) : <EOL> user = kwargs [ '<STR_LIT>' ] . user <EOL> form = request . form <EOL> ret = { '<STR_LIT>' : '<STR_LIT:success>' } <EOL> if form . get ( '<STR_LIT:name>' ) == '<STR_LIT>' and form . get ( '<STR_LIT:value>' , '<STR_LIT>' ) . strip ( ) : <EOL> user . fullname = strip_html ( form [ '<STR_LIT:value>' ] ) . strip ( ) <EOL> user . save ( ) <EOL> ret [ '<STR_LIT:name>' ] = user . fullname <EOL> return ret <EOL> def get_profile_summary ( user_id , formatter = '<STR_LIT>' ) : <EOL> user = User . load ( user_id ) <EOL> return user . get_summary ( formatter ) <EOL> @ must_be_logged_in <EOL> def user_profile ( auth , ** kwargs ) : <EOL> user = auth . user <EOL> return { <EOL> '<STR_LIT>' : user . _id , <EOL> '<STR_LIT>' : user . api_url , <EOL> } <EOL> @ must_be_logged_in <EOL> def user_account ( auth , ** kwargs ) : <EOL> user = auth . user <EOL> user_addons = addon_utils . get_addons_by_config_type ( '<STR_LIT:user>' , user ) <EOL> return { <EOL> '<STR_LIT>' : user . _id , <EOL> '<STR_LIT>' : user_addons , <EOL> '<STR_LIT>' : collect_user_config_js ( [ addon for addon in settings . ADDONS_AVAILABLE if '<STR_LIT:user>' in addon . configs ] ) , <EOL> '<STR_LIT>' : [ ] <EOL> } <EOL> @ must_be_logged_in <EOL> def user_account_password ( auth , ** kwargs ) : <EOL> user = auth . user <EOL> old_password = request . form . get ( '<STR_LIT>' , None ) <EOL> new_password = request . form . get ( '<STR_LIT>' , None ) <EOL> confirm_password = request . form . get ( '<STR_LIT>' , None ) <EOL> try : <EOL> user . change_password ( old_password , new_password , confirm_password ) <EOL> user . save ( ) <EOL> except ChangePasswordError as error : <EOL> for m in error . messages : <EOL> push_status_message ( m , kind = '<STR_LIT>' , trust = False ) <EOL> else : <EOL> push_status_message ( '<STR_LIT>' , kind = '<STR_LIT:success>' , trust = False ) <EOL> return redirect ( web_url_for ( '<STR_LIT>' ) ) <EOL> @ must_be_logged_in <EOL> def user_addons ( auth , ** kwargs ) : <EOL> user = auth . user <EOL> ret = { <EOL> '<STR_LIT>' : addon_utils . get_addons_by_config_type ( '<STR_LIT>' , user ) , <EOL> } <EOL> accounts_addons = [ addon for addon in settings . ADDONS_AVAILABLE if '<STR_LIT>' in addon . configs ] <EOL> ret . update ( { <EOL> '<STR_LIT>' : [ addon . short_name for addon in accounts_addons ] , <EOL> '<STR_LIT>' : collect_user_config_js ( accounts_addons ) , <EOL> '<STR_LIT>' : settings . ADDON_CAPABILITIES , <EOL> '<STR_LIT>' : [ ] <EOL> } ) <EOL> return ret <EOL> @ must_be_logged_in <EOL> def user_notifications ( auth , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return { <EOL> '<STR_LIT>' : dict ( auth . user . mailchimp_mailing_lists . items ( ) + auth . user . osf_mailing_lists . items ( ) ) <EOL> } <EOL> @ must_be_logged_in <EOL> def oauth_application_list ( auth , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> app_list_url = api_v2_url ( "<STR_LIT>" ) <EOL> return { <EOL> "<STR_LIT>" : app_list_url <EOL> } <EOL> @ must_be_logged_in <EOL> def oauth_application_register ( auth , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> app_list_url = api_v2_url ( "<STR_LIT>" ) <EOL> return { "<STR_LIT>" : app_list_url , <EOL> "<STR_LIT>" : '<STR_LIT>' } <EOL> @ must_be_logged_in <EOL> def oauth_application_detail ( auth , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> client_id = kwargs . get ( '<STR_LIT>' ) <EOL> try : <EOL> record = ApiOAuth2Application . find_one ( Q ( '<STR_LIT>' , '<STR_LIT>' , client_id ) ) <EOL> except NoResultsFound : <EOL> raise HTTPError ( http . NOT_FOUND ) <EOL> if record . owner != auth . user : <EOL> raise HTTPError ( http . FORBIDDEN ) <EOL> if record . is_active is False : <EOL> raise HTTPError ( http . GONE ) <EOL> app_detail_url = api_v2_url ( "<STR_LIT>" . format ( client_id ) ) <EOL> return { "<STR_LIT>" : '<STR_LIT>' , <EOL> "<STR_LIT>" : app_detail_url } <EOL> @ must_be_logged_in <EOL> def personal_access_token_list ( auth , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> token_list_url = api_v2_url ( "<STR_LIT>" ) <EOL> return { <EOL> "<STR_LIT>" : token_list_url <EOL> } <EOL> @ must_be_logged_in <EOL> def personal_access_token_register ( auth , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> token_list_url = api_v2_url ( "<STR_LIT>" ) <EOL> return { "<STR_LIT>" : token_list_url , <EOL> "<STR_LIT>" : '<STR_LIT>' , <EOL> "<STR_LIT>" : get_available_scopes ( ) } <EOL> @ must_be_logged_in <EOL> def personal_access_token_detail ( auth , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> _id = kwargs . get ( '<STR_LIT>' ) <EOL> try : <EOL> record = ApiOAuth2PersonalToken . find_one ( Q ( '<STR_LIT>' , '<STR_LIT>' , _id ) ) <EOL> except NoResultsFound : <EOL> raise HTTPError ( http . NOT_FOUND ) <EOL> if record . owner != auth . user : <EOL> raise HTTPError ( http . FORBIDDEN ) <EOL> if record . is_active is False : <EOL> raise HTTPError ( http . GONE ) <EOL> token_detail_url = api_v2_url ( "<STR_LIT>" . format ( _id ) ) <EOL> return { "<STR_LIT>" : '<STR_LIT>' , <EOL> "<STR_LIT>" : token_detail_url , <EOL> "<STR_LIT>" : get_available_scopes ( ) } <EOL> def collect_user_config_js ( addon_configs ) : <EOL> """<STR_LIT>""" <EOL> js_modules = [ ] <EOL> for addon_config in addon_configs : <EOL> js_path = paths . resolve_addon_path ( addon_config , '<STR_LIT>' ) <EOL> if js_path : <EOL> js_modules . append ( js_path ) <EOL> return js_modules <EOL> @ must_be_logged_in <EOL> def user_choose_addons ( ** kwargs ) : <EOL> auth = kwargs [ '<STR_LIT>' ] <EOL> json_data = escape_html ( request . get_json ( ) ) <EOL> auth . user . config_addons ( json_data , auth ) <EOL> @ must_be_logged_in <EOL> def user_choose_mailing_lists ( auth , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> user = auth . user <EOL> json_data = escape_html ( request . get_json ( ) ) <EOL> if json_data : <EOL> for list_name , subscribe in json_data . items ( ) : <EOL> if list_name == settings . OSF_HELP_LIST : <EOL> update_osf_help_mails_subscription ( user = user , subscribe = subscribe ) <EOL> else : <EOL> update_mailchimp_subscription ( user , list_name , subscribe ) <EOL> else : <EOL> raise HTTPError ( http . BAD_REQUEST , data = dict ( <EOL> message_long = "<STR_LIT>" ) <EOL> ) <EOL> user . save ( ) <EOL> all_mailing_lists = { } <EOL> all_mailing_lists . update ( user . mailchimp_mailing_lists ) <EOL> all_mailing_lists . update ( user . osf_mailing_lists ) <EOL> return { '<STR_LIT:message>' : '<STR_LIT>' , '<STR_LIT:result>' : all_mailing_lists } , <NUM_LIT:200> <EOL> @ user_merged . connect <EOL> def update_mailchimp_subscription ( user , list_name , subscription , send_goodbye = True ) : <EOL> """<STR_LIT>""" <EOL> if subscription : <EOL> mailchimp_utils . subscribe_mailchimp ( list_name , user . _id ) <EOL> else : <EOL> try : <EOL> mailchimp_utils . unsubscribe_mailchimp_async ( list_name , user . _id , username = user . username , send_goodbye = send_goodbye ) <EOL> except mailchimp_utils . mailchimp . ListNotSubscribedError : <EOL> raise HTTPError ( http . BAD_REQUEST , <EOL> data = dict ( message_short = "<STR_LIT>" , <EOL> message_long = "<STR_LIT>" , <EOL> error_type = "<STR_LIT>" ) <EOL> ) <EOL> def mailchimp_get_endpoint ( ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return { } , http . OK <EOL> def sync_data_from_mailchimp ( ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> key = request . args . get ( '<STR_LIT:key>' ) <EOL> if key == settings . MAILCHIMP_WEBHOOK_SECRET_KEY : <EOL> r = request <EOL> action = r . values [ '<STR_LIT:type>' ] <EOL> list_name = mailchimp_utils . get_list_name_from_id ( list_id = r . values [ '<STR_LIT>' ] ) <EOL> username = r . values [ '<STR_LIT>' ] <EOL> try : <EOL> user = User . find_one ( Q ( '<STR_LIT:username>' , '<STR_LIT>' , username ) ) <EOL> except NoResultsFound : <EOL> sentry . log_exception ( ) <EOL> sentry . log_message ( "<STR_LIT>" ) <EOL> raise HTTPError ( <NUM_LIT> , data = dict ( message_short = '<STR_LIT>' , <EOL> message_long = '<STR_LIT>' ) ) <EOL> if action == '<STR_LIT>' : <EOL> user . mailchimp_mailing_lists [ list_name ] = False <EOL> user . save ( ) <EOL> elif action == '<STR_LIT>' : <EOL> user . mailchimp_mailing_lists [ list_name ] = True <EOL> user . save ( ) <EOL> else : <EOL> raise HTTPError ( http . UNAUTHORIZED ) <EOL> @ must_be_logged_in <EOL> def impute_names ( ** kwargs ) : <EOL> name = request . args . get ( '<STR_LIT:name>' , '<STR_LIT>' ) <EOL> return auth_utils . impute_names ( name ) <EOL> def update_osf_help_mails_subscription ( user , subscribe ) : <EOL> user . osf_mailing_lists [ settings . OSF_HELP_LIST ] = subscribe <EOL> user . save ( ) <EOL> @ must_be_logged_in <EOL> def serialize_names ( ** kwargs ) : <EOL> user = kwargs [ '<STR_LIT>' ] . user <EOL> return { <EOL> '<STR_LIT>' : user . fullname , <EOL> '<STR_LIT>' : user . given_name , <EOL> '<STR_LIT>' : user . middle_names , <EOL> '<STR_LIT>' : user . family_name , <EOL> '<STR_LIT>' : user . suffix , <EOL> } <EOL> def get_target_user ( auth , uid = None ) : <EOL> target = User . load ( uid ) if uid else auth . user <EOL> if target is None : <EOL> raise HTTPError ( http . NOT_FOUND ) <EOL> return target <EOL> def fmt_date_or_none ( date , fmt = '<STR_LIT>' ) : <EOL> if date : <EOL> try : <EOL> return date . strftime ( fmt ) <EOL> except ValueError : <EOL> raise HTTPError ( <EOL> http . BAD_REQUEST , <EOL> data = dict ( message_long = '<STR_LIT>' ) <EOL> ) <EOL> return None <EOL> def append_editable ( data , auth , uid = None ) : <EOL> target = get_target_user ( auth , uid ) <EOL> data [ '<STR_LIT>' ] = auth . user == target <EOL> def serialize_social_addons ( user ) : <EOL> ret = { } <EOL> for user_settings in user . get_addons ( ) : <EOL> config = user_settings . config <EOL> if user_settings . public_id : <EOL> ret [ config . short_name ] = user_settings . public_id <EOL> return ret <EOL> @ collect_auth <EOL> def serialize_social ( auth , uid = None , ** kwargs ) : <EOL> target = get_target_user ( auth , uid ) <EOL> ret = target . social <EOL> append_editable ( ret , auth , uid ) <EOL> if ret [ '<STR_LIT>' ] : <EOL> ret [ '<STR_LIT>' ] = serialize_social_addons ( target ) <EOL> return ret <EOL> def serialize_job ( job ) : <EOL> return { <EOL> '<STR_LIT>' : job . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : job . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT:title>' : job . get ( '<STR_LIT:title>' ) , <EOL> '<STR_LIT>' : job . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : job . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : job . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : job . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : job . get ( '<STR_LIT>' , False ) , <EOL> } <EOL> def serialize_school ( school ) : <EOL> return { <EOL> '<STR_LIT>' : school . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : school . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : school . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : school . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : school . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : school . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : school . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : school . get ( '<STR_LIT>' , False ) , <EOL> } <EOL> def serialize_contents ( field , func , auth , uid = None ) : <EOL> target = get_target_user ( auth , uid ) <EOL> ret = { <EOL> '<STR_LIT>' : [ <EOL> func ( content ) <EOL> for content in getattr ( target , field ) <EOL> ] <EOL> } <EOL> append_editable ( ret , auth , uid ) <EOL> return ret <EOL> @ collect_auth <EOL> def serialize_jobs ( auth , uid = None , ** kwargs ) : <EOL> ret = serialize_contents ( '<STR_LIT>' , serialize_job , auth , uid ) <EOL> append_editable ( ret , auth , uid ) <EOL> return ret <EOL> @ collect_auth <EOL> def serialize_schools ( auth , uid = None , ** kwargs ) : <EOL> ret = serialize_contents ( '<STR_LIT>' , serialize_school , auth , uid ) <EOL> append_editable ( ret , auth , uid ) <EOL> return ret <EOL> @ must_be_logged_in <EOL> def unserialize_names ( ** kwargs ) : <EOL> user = kwargs [ '<STR_LIT>' ] . user <EOL> json_data = escape_html ( request . get_json ( ) ) <EOL> user . fullname = ( json_data . get ( '<STR_LIT>' ) or '<STR_LIT>' ) . strip ( ) <EOL> user . given_name = ( json_data . get ( '<STR_LIT>' ) or '<STR_LIT>' ) . strip ( ) <EOL> user . middle_names = ( json_data . get ( '<STR_LIT>' ) or '<STR_LIT>' ) . strip ( ) <EOL> user . family_name = ( json_data . get ( '<STR_LIT>' ) or '<STR_LIT>' ) . strip ( ) <EOL> user . suffix = ( json_data . get ( '<STR_LIT>' ) or '<STR_LIT>' ) . strip ( ) <EOL> user . save ( ) <EOL> def verify_user_match ( auth , ** kwargs ) : <EOL> uid = kwargs . get ( '<STR_LIT>' ) <EOL> if uid and uid != auth . user . _id : <EOL> raise HTTPError ( http . FORBIDDEN ) <EOL> @ must_be_logged_in <EOL> def unserialize_social ( auth , ** kwargs ) : <EOL> verify_user_match ( auth , ** kwargs ) <EOL> user = auth . user <EOL> json_data = escape_html ( request . get_json ( ) ) <EOL> for soc in user . SOCIAL_FIELDS . keys ( ) : <EOL> user . social [ soc ] = json_data . get ( soc ) <EOL> try : <EOL> user . save ( ) <EOL> except ValidationError as exc : <EOL> raise HTTPError ( http . BAD_REQUEST , data = dict ( <EOL> message_long = exc . args [ <NUM_LIT:0> ] <EOL> ) ) <EOL> def unserialize_job ( job ) : <EOL> return { <EOL> '<STR_LIT>' : job . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : job . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT:title>' : job . get ( '<STR_LIT:title>' ) , <EOL> '<STR_LIT>' : job . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : job . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : job . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : job . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : job . get ( '<STR_LIT>' ) , <EOL> } <EOL> def unserialize_school ( school ) : <EOL> return { <EOL> '<STR_LIT>' : school . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : school . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : school . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : school . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : school . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : school . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : school . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : school . get ( '<STR_LIT>' ) , <EOL> } <EOL> def unserialize_contents ( field , func , auth ) : <EOL> user = auth . user <EOL> json_data = escape_html ( request . get_json ( ) ) <EOL> setattr ( <EOL> user , <EOL> field , <EOL> [ <EOL> func ( content ) <EOL> for content in json_data . get ( '<STR_LIT>' , [ ] ) <EOL> ] <EOL> ) <EOL> user . save ( ) <EOL> @ must_be_logged_in <EOL> def unserialize_jobs ( auth , ** kwargs ) : <EOL> verify_user_match ( auth , ** kwargs ) <EOL> unserialize_contents ( '<STR_LIT>' , unserialize_job , auth ) <EOL> @ must_be_logged_in <EOL> def unserialize_schools ( auth , ** kwargs ) : <EOL> verify_user_match ( auth , ** kwargs ) <EOL> unserialize_contents ( '<STR_LIT>' , unserialize_school , auth ) <EOL> @ must_be_logged_in <EOL> def request_export ( auth ) : <EOL> user = auth . user <EOL> if not throttle_period_expired ( user . email_last_sent , settings . SEND_EMAIL_THROTTLE ) : <EOL> raise HTTPError ( httplib . BAD_REQUEST , <EOL> data = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } ) <EOL> mails . send_mail ( <EOL> to_addr = settings . SUPPORT_EMAIL , <EOL> mail = mails . REQUEST_EXPORT , <EOL> user = auth . user , <EOL> ) <EOL> user . email_last_sent = datetime . datetime . utcnow ( ) <EOL> user . save ( ) <EOL> return { '<STR_LIT:message>' : '<STR_LIT>' } <EOL> @ must_be_logged_in <EOL> def request_deactivation ( auth ) : <EOL> user = auth . user <EOL> if not throttle_period_expired ( user . email_last_sent , settings . SEND_EMAIL_THROTTLE ) : <EOL> raise HTTPError ( http . BAD_REQUEST , <EOL> data = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } ) <EOL> mails . send_mail ( <EOL> to_addr = settings . SUPPORT_EMAIL , <EOL> mail = mails . REQUEST_DEACTIVATION , <EOL> user = auth . user , <EOL> ) <EOL> user . email_last_sent = datetime . datetime . utcnow ( ) <EOL> user . save ( ) <EOL> return { '<STR_LIT:message>' : '<STR_LIT>' } <EOL> def redirect_to_twitter ( twitter_handle ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> user = User . find_one ( Q ( '<STR_LIT>' , '<STR_LIT>' , twitter_handle ) ) <EOL> except NoResultsFound : <EOL> raise HTTPError ( http . NOT_FOUND , data = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' . format ( twitter_handle ) <EOL> } ) <EOL> except MultipleResultsFound : <EOL> users = User . find ( Q ( '<STR_LIT>' , '<STR_LIT>' , twitter_handle ) ) <EOL> message_long = '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' . format ( markupsafe . escape ( twitter_handle ) ) <EOL> for user in users : <EOL> message_long += '<STR_LIT>' . format ( user . url , markupsafe . escape ( user . fullname ) ) <EOL> message_long += '<STR_LIT>' <EOL> raise HTTPError ( http . MULTIPLE_CHOICES , data = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : message_long <EOL> } ) <EOL> return redirect ( user . url ) </s>
