def login_social(self): # self._json_data # social_alias = self.post('social_alias') # token = self.post('token') token = self._json_data.get('token') social_alias = self._json_data.get('social_alias') is_agree = True if self._json_data and self._json_data.get( 'is_agree') else False # if is_agree is not True: # self.make_message('Please confirm terms agreement.') if social_alias: from core.helpers.Social import Social if token or social_alias == 'twitter': # response = Social.get_facebook_user(token) response = Social.get_user(social_alias, token, self._json_data) # if social_alias == 'twitter': # @TODO: make method in Social helper for getting user avatar if response is not None: social_config = Social.get_config() user_social = None for field in Social.get_social_fields(social_alias): if field[0] == 'social_ident': ident_key = field[1] ident_value = Config.get(ident_key, response) user_social = UserSocial.find_by_ident( ident_value, social_alias) if user_social is None: if is_agree is not True: self.make_message( 'Please confirm terms agreement.', ['goto_auth']) else: user_social = UserSocial() user = User() user_social.alias = social_alias for field in Social.get_social_fields( social_alias): social_key = field[1] if len( field) >= 2 else field[0] # value = response.get(social_key) value = Config.get(social_key, response) # execute all function defined in fields if len(field) > 2: for i, func in enumerate(field): if i > 1: value = func(value) if value is not None: setattr(user_social, field[0], value) user_field_val = social_config[ social_alias]['social_to_user'].get( field[0]) if user_field_val is not None: setattr(user, user_field_val, value) # social media returned not empty user unique identifier if hasattr(user_social, 'social_ident' ) and user_social.social_ident: user.generate_token() from core.models.Connection import Connection with Connection.get_db().transaction(): user.is_social = True user.save() if user.add_role_group('api'): user_social.user_id = user.id user_social.save() self.make_response( user_social.user.format_default()) else: user.delete() raise Exception( 'cant create user role.') else: self.make_message( 'Cant get social media user unique identifier.' ) else: # user already exists. wee need return existing user self.make_response(user_social.user.format_default()) else: self.make_actions(['invalid_oauth2_token']) else: self.make_actions(['parameter_token_required']) else: self.make_actions(['parameter_social_alias_required'])
def delete(self, id): return User.delete(id)