def favour_propos_user(user_id): form = CheckAccessTokenForm() if not form.validate_on_submit(): return generate_error_response(form.errors) return generate_data_response(json.dumps([p.as_dict() for p in User.get_favour_propos(user_id)]))
def auth_user(): form = AuthUserForm() data = dict(form.data.copy()) data.pop('social_id') # This secret key will validate client that try sent auth data to server secret_key = data.pop('secret_key') if form.validate_on_submit(): if secret_key != '666': return generate_error_response(field='secret_key', text='Invalid secret key') user = User(**data) social_id = int(form.social_id.data) # Search old profile user_orig = User.query.filter(or_(User.vk_id == social_id, User.fb_id == social_id)).first() # Update old profile if exist if user_orig: user.id = user_orig.id user.access_token = (str(uuid.uuid4()) + str(uuid.uuid4())).replace('-', '') User.query.filter_by(id=user_orig.id).update(dict( first_name=user.first_name, last_name=user.last_name, access_token=user.access_token, login_mode=user.login_mode, about=user.about, avatar_url=user.avatar_url, country=user.country, city=user.city, gender=user.gender )) else: if form.login_mode.data == User.LOGIN_MODE_ENUM.vk: user.vk_id = form.social_id.data elif form.login_mode.data == User.LOGIN_MODE_ENUM.fb: user.fb_id = form.social_id.data user.access_token = (str(uuid.uuid4()) + str(uuid.uuid4())).replace('-', '') db.session.add(user) db.session.commit() return generate_data_response(dict(id=user.id, access_token=user.access_token)) else: return generate_error_response(form.errors)
def propos_user(user_id): form = CheckAccessTokenForm() if not form.validate_on_submit(): return generate_error_response(form.errors) user = User.query.filter(User.id == user_id).first() if user: propos = list((p.as_dict_without_author() for p in user.propositions)) return generate_data_response(propos) else: return generate_error_response(text=errors.NOT_FOUND)
def get_user(user_id): form = CheckAccessTokenForm() if not form.validate_on_submit(): return generate_error_response(form.errors) if not User.query.filter_by(access_token=form.access_token.data).first(): return generate_error_response(text=errors.UNATHORIZED) user = User.query.filter(User.id == user_id).first() if user: return generate_data_response(user.as_dict()) else: return generate_error_response(text=errors.NOT_FOUND)
def update_user(user_id): form = UpdateUserForm() if form.validate_on_submit(): User.query.filter_by(id=user_id).update( dict( first_name=form.first_name.data, last_name=form.last_name.data, login_mode=form.login_mode.data, about=form.about.data, mobile_number=form.mobile_number.data, country=form.country.data, city=form.city.data, gender=form.gender.data, vk_id=form.vk_id.data, fb_id=form.fb_id.data, ) ) db.session.commit() return generate_data_response(dict(id=user_id, access_token=form.access_token.data)) else: return generate_error_response(form.errors)