def signup(self): """ Client: - Payload: { "command": "user/signup", "params" : {email, password} } Server: - { "code" : 200, "data": {"rent": "0.37"}, "actions" : [] } """ email = self._json_data['email'] password = self._json_data['password'] existing_user = User.where('email', '=', email).first() if existing_user is None: user = User() user.generate_token() user.email = email user.set_password(password) # @TODO: write universal validator helper values_to_save = self.left_keys(self._json_data, {'first_name', 'last_name'}) for key, val in values_to_save.items(): setattr(user, key, val) user.save() if user.add_role_group('api'): self.make_response(user.format_default()) else: raise Exception('cant create user role.') else: self.make_message('User with such email already exists.')
def register(): # check if current user is already logged in if current_user.is_authenticated: return redirect(url_for('main.index')) form = RegistrationForm() # Action after received form data if form.validate_on_submit(): user = User(user_name=form.username.data, email=form.email.data, about_me=form.about_me.data) user.set_password(form.password.data) db.session.add(user) db.session.commit() token = user.generate_token('confirm') send_email(user.email, 'Confirm Your Account', 'mail/confirm', user=user, token=token) # Login automatically login_user(user) flash(alerts['login_success']) return redirect(url_for('main.index')) return render_template('auth/register.html', form=form, title='Register')
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'])