def setup(): if not User.query.count() == 0: abort(400) email = request.form.get("email") password = request.form.get("password") if not email or not password: return redirect("..") # TODO: Tell them what they did wrong (i.e. being stupid) user = User(email, password) user.admin = True db.add(user) db.commit() login_user(user) return redirect("admin?first-run=1")
def donate(): email = request.form.get("email") stripe_token = request.form.get("stripe_token") amount = request.form.get("amount") type = request.form.get("type") comment = request.form.get("comment") project_id = request.form.get("project") # validate and rejigger the form inputs if not email or not stripe_token or not amount or not type: return {"success": False, "reason": "Invalid request"}, 400 try: if project_id is None or project_id == "null": project = None else: project_id = int(project_id) project = Project.query.filter(Project.id == project_id).first() if type == "once": type = DonationType.one_time else: type = DonationType.monthly amount = int(amount) except: return {"success": False, "reason": "Invalid request"}, 400 new_account = False user = User.query.filter(User.email == email).first() if not user: new_account = True user = User(email, binascii.b2a_hex(os.urandom(20)).decode("utf-8")) user.password_reset = binascii.b2a_hex(os.urandom(20)).decode("utf-8") user.password_reset_expires = datetime.now() + timedelta(days=1) customer = stripe.Customer.create(email=user.email, card=stripe_token) user.stripe_customer = customer.id db.add(user) else: customer = stripe.Customer.retrieve(user.stripe_customer) new_source = customer.sources.create(source=stripe_token) customer.default_source = new_source.id customer.save() donation = Donation(user, type, amount, project, comment) db.add(donation) try: charge = stripe.Charge.create(amount=amount, currency=_cfg("currency"), customer=user.stripe_customer, description="Donation to " + _cfg("your-name")) except stripe.error.CardError as e: db.rollback() db.close() return {"success": False, "reason": "Your card was declined."} db.commit() send_thank_you(user, amount, type == DonationType.monthly) if new_account: return { "success": True, "new_account": new_account, "password_reset": user.password_reset } else: return {"success": True, "new_account": new_account}
def create_project(): name = request.form.get("name") project = Project(name) db.add(project) db.commit() return redirect("admin")
def donate(): email = request.form.get("email") stripe_token = request.form.get("stripe_token") amount = request.form.get("amount") type = request.form.get("type") comment = request.form.get("comment") project_id = request.form.get("project") # validate and rejigger the form inputs if not email or not stripe_token or not amount or not type: return {"success": False, "reason": "Invalid request"}, 400 try: if project_id is None or project_id == "null": project = None else: project_id = int(project_id) project = Project.query.filter(Project.id == project_id).first() if type == "once": type = DonationType.one_time else: type = DonationType.monthly amount = int(amount) except: return {"success": False, "reason": "Invalid request"}, 400 new_account = False user = User.query.filter(User.email == email).first() if not user: new_account = True user = User(email, binascii.b2a_hex(os.urandom(20)).decode("utf-8")) user.password_reset = binascii.b2a_hex(os.urandom(20)).decode("utf-8") user.password_reset_expires = datetime.now() + timedelta(days=1) customer = stripe.Customer.create(email=user.email, card=stripe_token) user.stripe_customer = customer.id db.add(user) else: customer = stripe.Customer.retrieve(user.stripe_customer) new_source = customer.sources.create(source=stripe_token) customer.default_source = new_source.id customer.save() donation = Donation(user, type, amount, project, comment) db.add(donation) try: charge = stripe.Charge.create( amount=amount, currency="usd", customer=user.stripe_customer, description="Donation to " + _cfg("your-name") ) except stripe.error.CardError as e: db.rollback() db.close() return {"success": False, "reason": "Your card was declined."} db.commit() send_thank_you(user, amount, type == DonationType.monthly) if new_account: return {"success": True, "new_account": new_account, "password_reset": user.password_reset} else: return {"success": True, "new_account": new_account}