def plans_charge(namespace): if current_user is None: redirect('/users/login') if current_user.stripe_id is None: redirect('/mypage') plan = session.query(Plan).filter(Plan.namespace == namespace).first() user = session.query(User).join( Plan, Plan.id == User.plan_id).filter(current_user.id == User.id).first() if plan is None: redirect('/plans') if current_user.plan_id is None: #新規 subscription = stripe.Subscription.create( customer=current_user.stripe_id, items=[{ 'plan': plan.stripe_plan_id, }]) current_user.plan_id = plan.id current_user.stripe_subscription_id = subscription.id session.commit() else: #既にサブスクリプションを購入しているので変更 if plan.id == current_user.plan_id: redirect('/plans') #同じプランには変更できない subscription = stripe.Subscription.retrieve( current_user.stripe_subscription_id) item_id = subscription['items']['data'][0].id stripe.Subscription.modify( current_user.stripe_subscription_id, cancel_at_period_end=False, items=[{ "id": item_id, "plan": plan.stripe_plan_id, }], ) current_user.plan_id = plan.id current_user.stripe_subscription_id = subscription.id session.commit() return template('templates/charge', url=url, current_user=current_user, plan=plan)
def plans(): plans = session.query(Plan).all() return template('templates/plan', url=url, current_user=current_user, plans=plans)
def update_user(current_user, form): user = session.query(User).get(current_user.id) if form.getunicode('name'): user.name = form.getunicode('name') if form.getunicode('age'): user.age = int( form.getunicode('age')) if form.getunicode('age') else None if form.getunicode('email'): user.email = form.getunicode('email') session.commit()
def plans_detail(namespace): plan = session.query(Plan).filter(Plan.namespace == namespace).first() if plan is None: redirect('/plans') return template('templates/plans_detail', url=url, current_user=current_user, plan=plan)
def check_socials(data, provider): if provider == 'facebook': social = session.query(Social).filter( Social.provider == 'facebook', Social.provider_id == data['id']).first() if social is None: return False else: login_user(social.user_id) return True
def authenticate(form): auth_user = session.query(User).filter( User.email == form.getunicode('email'), User.password == _encrypt_password( form.getunicode('password'))).first() if auth_user is not None: login_user(auth_user.id) return True else: return False
def mypage(): if current_user.email is None: redirect('/mypage/edit') socials = get_socials_info(current_user) plan = session.query(Plan).get( current_user.plan_id) if current_user.plan_id else None return template('templates/users/mypage', url=url, current_user=current_user, socials=socials, plan=plan, publish_key=app_setting.STRIPE_PUBLISHABLE)
def get_current_user(): user_id = request.get_cookie('user_id', secret=app_setting.SECRET_KEY) if user_id: return session.query(User).get(user_id) else: return None
def is_duplicate_email(email): user = session.query(User).filter(User.email == email).first() if user is None: return False else: return True
def update_password(current_user, form): user = session.query(User).get(current_user.id) if form.getunicode('password1'): user.password = _encrypt_password(form.getunicode('password1')) session.commit()
def get_socials_info(user): return session.query(Social).filter(Social.user_id == user.id)