From anonymouscodar at gmail.com Wed Mar 1 01:51:56 2017 From: anonymouscodar at gmail.com (Anonymous Coder) Date: Wed, 1 Mar 2017 06:51:56 +0000 Subject: [Flask] Populating child models with parent model Fields in Views Message-ID: Hi, I need to populate model fields from parent model fields with ForeignKey relation. Following is the example: class Parent(db.Model): __tablename__='parent' ....... id = db.Column(db.Integer, primary_key=True, autoincrement=True) child = relationship('Child", backref=('parent')) .... class Child(db.Model): __tablename__='child' ..... id = db.Column(db.Integer, primary_key=True, autoincrement=True) parent_id = db.Column(db.Integer, ForeignKey('parent.id')) .... Id does shows the list but when I create a child model from views it is not getting relevant parent_id automatically loaded into child model in parent_id. For example in my view if I create child model based on any condition: if something: child = Child(other_filed=created_values, ....) db.session.add() db.session.commit() It shows all the fields other than the relevant parent model automatically attached to it. Is there a way to bind the session with parent and get attributes of parent to automatically associated with child model creation? Thanks -------------- next part -------------- An HTML attachment was scrubbed... URL: From hcmr.verdonck.christof at gmail.com Wed Mar 1 04:48:07 2017 From: hcmr.verdonck.christof at gmail.com (Christof Verdonck) Date: Wed, 1 Mar 2017 11:48:07 +0200 Subject: [Flask] Add authentication to auto generated URLs Message-ID: Dear I am a quiet new developer for both Python and Flask. To authenticate my Flask application, I am using the Flask extension: flask-httpauth/ To have a secured endpoint, I just add the @auth.login_required decorator to the @app.route endpoint definition as shown here under: @app.route('/secret_page') @auth.login_required def secret_page(): return send_from_directory(app.static_folder, 'secret_page.html') Now, I have some endpoints that I add dynamically as here under: current_app.add_url_rule( 'secret_page_url/', view_func=view_func, methods=['POST', ]) I would like to add also authentication for these URLs but I can't figure out how this is done. It seems that the solution I have now, adds authentication on the adding of the rule instead of on the accessing of the URL. Can someone please give me a hint/solution how this is done? Thank you in advance Christof -------------- next part -------------- An HTML attachment was scrubbed... URL: From gergely at polonkai.eu Wed Mar 1 16:42:54 2017 From: gergely at polonkai.eu (Gergely Polonkai) Date: Wed, 01 Mar 2017 21:42:54 +0000 Subject: [Flask] Add authentication to auto generated URLs In-Reply-To: References: Message-ID: As it?s a plain decorator, you can use current_app.add_url_rule( 'secret_page_url/', view_func=auth.login_required(view_func), methods=['POST', ]) Hint: if you need to use a decorator that requires parameters, use something like this: a_decorator(decor_arg, decor_arg)(view_func) You can also nest them: a_decorator(arg, arg)(auth.login_required(view_func)) Best, Gergely On Wed, Mar 1, 2017, 10:48 Christof Verdonck < hcmr.verdonck.christof at gmail.com> wrote: > Dear > > > I am a quiet new developer for both Python and Flask. > > > To authenticate my Flask application, I am using the Flask extension: > flask-httpauth/ > To have a secured endpoint, I just add the @auth.login_required decorator > to the @app.route endpoint definition as shown here under: > > @app.route('/secret_page') > @auth.login_required > def secret_page(): > return send_from_directory(app.static_folder, 'secret_page.html') > > > Now, I have some endpoints that I add dynamically as here under: > > > current_app.add_url_rule( > 'secret_page_url/', view_func=view_func, methods=['POST', ]) > > > I would like to add also authentication for these URLs but I can't figure out how this is done. > > It seems that the solution I have now, adds authentication on the adding of the rule instead > > of on the accessing of the URL. > > Can someone please give me a hint/solution how this is done? > > > Thank you in advance > > > Christof > > _______________________________________________ > Flask mailing list > Flask at python.org > https://mail.python.org/mailman/listinfo/flask > -------------- next part -------------- An HTML attachment was scrubbed... URL: From scott.werner.vt at gmail.com Fri Mar 3 15:43:05 2017 From: scott.werner.vt at gmail.com (Scott Werner) Date: Fri, 3 Mar 2017 15:43:05 -0500 Subject: [Flask] Populating child models with parent model Fields in Views In-Reply-To: References: Message-ID: Assuming a One to Many relationship: class Parent(Base): __tablename__ = 'parent' id = Column(Integer, primary_key=True) children = relationship("Child", backref="parent") class Child(Base): __tablename__ = 'child' id = Column(Integer, primary_key=True) parent_id = Column(Integer, ForeignKey('parent.id')) child = Child() parent = Parent() parent.child.append(child) http://docs.sqlalchemy.org/en/latest/orm/basic_relationships.html -- Scott Werner scott.werner.vt at gmail.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From scott.werner.vt at gmail.com Fri Mar 3 15:46:47 2017 From: scott.werner.vt at gmail.com (Scott Werner) Date: Fri, 3 Mar 2017 15:46:47 -0500 Subject: [Flask] Populating child models with parent model Fields in Views In-Reply-To: References: Message-ID: Sorry hit send to early... Assuming a One to Many relationship: class Parent(Base): __tablename__ = 'parent' id = Column(Integer, primary_key=True) children = relationship("Child", backref="parent") # backref="parent" not backref=("parent") class Child(Base): __tablename__ = 'child' id = Column(Integer, primary_key=True) parent_id = Column(Integer, ForeignKey('parent.id')) c = Child() p = Parent() p.children.append(c) session.add(p) session.commit() # or p = Parent() # if new: session.add(p) c = Child(parent=p) session.add(c) session.commit() http://docs.sqlalchemy.org/en/latest/orm/basic_relationships.html -- Scott Werner scott.werner.vt at gmail.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From anonymouscodar at gmail.com Sun Mar 5 06:55:31 2017 From: anonymouscodar at gmail.com (Anonymous Coder) Date: Sun, 5 Mar 2017 11:55:31 +0000 Subject: [Flask] Deleting Flask tables conditionally Message-ID: Hi, I've created a view and I attached my table creation with subscription. Now I can create a table with subscription but I can't delete same table if subscription is cancelled. Following are my views for creating subscription and adding subscription data to model table successfully. @app.route('/yearlychargedrec', methods=['GET', 'POST']) def yearly_charged_rec(): if not user_authorized(): return redirect('/') current_package = Package.query.all() if request.method == 'POST': data = get_profile_data(session['auth_token']) profile_data = data['StudentProfile'] student = get_profile_data(session['auth_token'])['StudentProfile'] stripe_token = request.form['stripeToken'] email = request.form['stripeEmail'] customer = stripe.Customer.create( email=email, source=request.form['stripeToken'] ) try: subscription = stripe.Subscription.create( customer=customer.id, plan="yearlyrec", ) student_id = profile_data.id student.stripe_customer_id = customer.id student.stripe_subscription_id = subscription.id package = Package( student_id=student_id, stripe_id = customer.id, student_email=request.form['stripeEmail'], is_active=True, package_type='yearlyrec', subscription_id=subscription.id ) dbase.session.add(package) dbase.session.commit() except stripe.error.CardError as e: body = e.json_body err = body['error'] return render_template('/profile/charge/monthlycharge.html') Now following is the view where I need to delete table if user choose to delete the subscription. @app.route('/cancelannualsub', methods=['GET', 'POST']) def cancel_yearly_rec(): if not user_authorized(): return redirect('/') data = get_profile_data(session['auth_token']) profile_data = data['StudentProfile'] student_id = profile_data.id package = Package() subscription_id = package.subscription_id if request.method == 'POST': if student_id in package: key = stripe_keys['publishable_key'] subscription = stripe.Subscription.retrieve(subscription_id) subscription.delete() package = Package() dbase.session.delete(package) dbase.session.commit() return redirect(url_for('new_template')) Problem is deletion is not deleting table in fact no impact on anything at all. Please advise. -------------- next part -------------- An HTML attachment was scrubbed... URL: From coreybrett at gmail.com Sun Mar 5 18:18:28 2017 From: coreybrett at gmail.com (Corey Boyle) Date: Sun, 5 Mar 2017 18:18:28 -0500 Subject: [Flask] Deleting Flask tables conditionally In-Reply-To: References: Message-ID: Please clarify... by "table", do you mean "row" or "record"? Regarding deleteing, I think this line package = Package() Needs to be replaced with a query that returns the single object that you want to delete. From anonymouscodar at gmail.com Mon Mar 6 01:54:26 2017 From: anonymouscodar at gmail.com (Anonymous Coder) Date: Mon, 6 Mar 2017 06:54:26 +0000 Subject: [Flask] Deleting Flask tables conditionally In-Reply-To: References: Message-ID: I your solution but this isn't working at all to my surprise. I can create rows in tables but I can't delete. I tried a dozen solutions from google. But mine don't work at all. Please advise. On Sun, Mar 5, 2017 at 11:18 PM, Corey Boyle wrote: > Please clarify... by "table", do you mean "row" or "record"? > > Regarding deleteing, I think this line > package = Package() > Needs to be replaced with a query that returns the single object that > you want to delete. > -------------- next part -------------- An HTML attachment was scrubbed... URL: From spenceryoung at ufl.edu Mon Mar 6 15:45:59 2017 From: spenceryoung at ufl.edu (Young,Spencer P) Date: Mon, 6 Mar 2017 20:45:59 +0000 Subject: [Flask] Deleting Flask tables conditionally In-Reply-To: References: Message-ID: <2A372EDB-AA8E-423B-A27D-ACF080EDB568@ufl.edu> Suppose you add a record to your `Package` table? student_id = 12345 new_package = Package() new_package.student_id = student_id db.session.add(new_package) db.session.commit() Now suppose you want to remove that record from your table? You must query and retrieve the record you want to delete, then delete it. package_to_delete = Package.query.filter_by(student_id=12345).first() db.session.delete(package_to_delete) db.session.commit() Does that make any sense? -Spencer From: Flask on behalf of Anonymous Coder Date: Monday, March 6, 2017 at 1:54 AM To: Corey Boyle Cc: flask Subject: Re: [Flask] Deleting Flask tables conditionally I your solution but this isn't working at all to my surprise. I can create rows in tables but I can't delete. I tried a dozen solutions from google. But mine don't work at all. Please advise. On Sun, Mar 5, 2017 at 11:18 PM, Corey Boyle > wrote: Please clarify... by "table", do you mean "row" or "record"? Regarding deleteing, I think this line package = Package() Needs to be replaced with a query that returns the single object that you want to delete. -------------- next part -------------- An HTML attachment was scrubbed... URL: From anonymouscodar at gmail.com Tue Mar 7 05:28:02 2017 From: anonymouscodar at gmail.com (Anonymous Coder) Date: Tue, 7 Mar 2017 10:28:02 +0000 Subject: [Flask] Deleting Flask tables conditionally In-Reply-To: <2A372EDB-AA8E-423B-A27D-ACF080EDB568@ufl.edu> References: <2A372EDB-AA8E-423B-A27D-ACF080EDB568@ufl.edu> Message-ID: Thank you Spencer I was able to make it work with your help. On Mon, Mar 6, 2017 at 8:45 PM, Young,Spencer P wrote: > Suppose you add a record to your `Package` table? > > > > > > student_id = 12345 > > new_package = Package() > > new_package.student_id = student_id > > db.session.add(new_package) > > db.session.commit() > > > > > > Now suppose you want to remove that record from your table? You must query > and retrieve the record you want to delete, then delete it. > > > > package_to_delete = Package.query.filter_by(student_id=12345).first() > > db.session.delete(package_to_delete) > > db.session.commit() > > > > > > Does that make any sense? > > > > -Spencer > > > > *From: *Flask on behalf > of Anonymous Coder > *Date: *Monday, March 6, 2017 at 1:54 AM > *To: *Corey Boyle > *Cc: *flask > *Subject: *Re: [Flask] Deleting Flask tables conditionally > > > > I your solution but this isn't working at all to my surprise. I can create > rows in tables but I can't delete. I tried a dozen solutions from google. > But mine don't work at all. Please advise. > > > > On Sun, Mar 5, 2017 at 11:18 PM, Corey Boyle wrote: > > Please clarify... by "table", do you mean "row" or "record"? > > Regarding deleteing, I think this line > package = Package() > Needs to be replaced with a query that returns the single object that > you want to delete. > > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From anonymouscodar at gmail.com Tue Mar 7 11:01:13 2017 From: anonymouscodar at gmail.com (Anonymous Coder) Date: Tue, 7 Mar 2017 16:01:13 +0000 Subject: [Flask] Creating table only if its not created for user already Message-ID: I was trying to find a way to process/commit a model table for view only if it doesn't exists already. But I couldn't find the way to do it. I tried if/else but it doesn't create model at all if I try it that way. Following is view and I need to create sqlalchymy table only if it doesn't exist alrady. @app.route('/yearlychargedrec', methods=['GET', 'POST']) def yearly_charged_rec(): if not user_authorized(): return redirect('/') current_package = Package.query.all() if request.method == 'POST': data = get_profile_data(session['auth_token']) profile_data = data['StudentProfile'] student = get_profile_data(session['auth_token'])['StudentProfile'] stripe_token = request.form['stripeToken'] email = request.form['stripeEmail'] customer = stripe.Customer.create( email=email, source=request.form['stripeToken'] ) try: subscription = stripe.Subscription.create( customer=customer.id, plan="yearlyrec", ) student_id = profile_data.id student.stripe_customer_id = customer.id student.stripe_subscription_id = subscription.id package = Package( student_id=student_id, stripe_id = customer.id, student_email=request.form['stripeEmail'], is_active=True, package_type='yearlyrec', subscription_id=subscription.id ) dbase.session.add(package) dbase.session.commit() except stripe.error.CardError as e: body = e.json_body err = body['error'] return render_template('/profile/charge/monthlycharge.html', current_package=current_package) Please advise. -------------- next part -------------- An HTML attachment was scrubbed... URL: From ziirish at ziirish.info Tue Mar 7 11:21:38 2017 From: ziirish at ziirish.info (Ziirish) Date: Tue, 7 Mar 2017 17:21:38 +0100 Subject: [Flask] Creating table only if its not created for user already In-Reply-To: References: Message-ID: <20170307162137.GD1899@mail.ziirish.info> It would be better if you showed us some of your tries and/or error messages. Regarding the latest emails you sent it looks like you want us to do your job... Anyway, the "if" you are looking for is probably something like: package = Package.query.filter_by( student_id=student_id, stripe_id = customer.id, student_email=request.form['stripeEmail'], is_active=True, package_type='yearlyrec', subscription_id=subscription.id ).first() if not package: package = Package(...) dbase.session.add(package) dbase.session.commit() * On Tuesday, March 07, 2017 at 04:01 PM +0000, Anonymous Coder wrote: > I was trying to find a way to process/commit a model table for view only if > it doesn't exists already. But I couldn't find the way to do it. I tried > if/else but it doesn't create model at all if I try it that way. Following > is view and I need to create sqlalchymy table only if it doesn't exist > alrady. > > @app.route('/yearlychargedrec', methods=['GET', 'POST']) > def yearly_charged_rec(): > if not user_authorized(): > return redirect('/') > > current_package = Package.query.all() > > if request.method == 'POST': > data = get_profile_data(session['auth_token']) > profile_data = data['StudentProfile'] > student = get_profile_data(session['auth_token'])['StudentProfile'] > > stripe_token = request.form['stripeToken'] > email = request.form['stripeEmail'] > customer = stripe.Customer.create( > email=email, > source=request.form['stripeToken'] > ) > try: > subscription = stripe.Subscription.create( > customer=customer.id, > plan="yearlyrec", > ) > student_id = profile_data.id > student.stripe_customer_id = customer.id > student.stripe_subscription_id = subscription.id > > package = Package( > student_id=student_id, > stripe_id = customer.id, > student_email=request.form['stripeEmail'], > is_active=True, > package_type='yearlyrec', > subscription_id=subscription.id > ) > dbase.session.add(package) > dbase.session.commit() > > except stripe.error.CardError as e: > body = e.json_body > err = body['error'] > > return render_template('/profile/charge/monthlycharge.html', > current_package=current_package) > > Please advise. From anonymouscodar at gmail.com Tue Mar 7 12:30:39 2017 From: anonymouscodar at gmail.com (Anonymous Coder) Date: Tue, 7 Mar 2017 17:30:39 +0000 Subject: [Flask] Creating table only if its not created for user already In-Reply-To: <20170307162137.GD1899@mail.ziirish.info> References: <20170307162137.GD1899@mail.ziirish.info> Message-ID: Thanks for help but all I am trying to do here is create a udemy clone with some changes I would like my solution to have. Its not a job I am doing it for myself. I like python and that's why I chose Flask. For your comment above, official documentation sucks that's why I have no other option as this is my very first project. Thanks for help though, I will try it. Cheers. On Tue, Mar 7, 2017 at 4:21 PM, Ziirish wrote: > It would be better if you showed us some of your tries and/or error > messages. > Regarding the latest emails you sent it looks like you want us to do your > job... > > Anyway, the "if" you are looking for is probably something like: > > > package = Package.query.filter_by( > student_id=student_id, stripe_id = customer.id, > student_email=request.form['stripeEmail'], is_active=True, > package_type='yearlyrec', subscription_id=subscription.id > ).first() > > if not package: > package = Package(...) > dbase.session.add(package) > dbase.session.commit() > > > > * On Tuesday, March 07, 2017 at 04:01 PM +0000, Anonymous Coder < > anonymouscodar at gmail.com> wrote: > > I was trying to find a way to process/commit a model table for view only > if > > it doesn't exists already. But I couldn't find the way to do it. I tried > > if/else but it doesn't create model at all if I try it that way. > Following > > is view and I need to create sqlalchymy table only if it doesn't exist > > alrady. > > > > @app.route('/yearlychargedrec', methods=['GET', 'POST']) > > def yearly_charged_rec(): > > if not user_authorized(): > > return redirect('/') > > > > current_package = Package.query.all() > > > > if request.method == 'POST': > > data = get_profile_data(session['auth_token']) > > profile_data = data['StudentProfile'] > > student = get_profile_data(session[' > auth_token'])['StudentProfile'] > > > > stripe_token = request.form['stripeToken'] > > email = request.form['stripeEmail'] > > customer = stripe.Customer.create( > > email=email, > > source=request.form['stripeToken'] > > ) > > try: > > subscription = stripe.Subscription.create( > > customer=customer.id, > > plan="yearlyrec", > > ) > > student_id = profile_data.id > > student.stripe_customer_id = customer.id > > student.stripe_subscription_id = subscription.id > > > > package = Package( > > student_id=student_id, > > stripe_id = customer.id, > > student_email=request.form['stripeEmail'], > > is_active=True, > > package_type='yearlyrec', > > subscription_id=subscription.id > > ) > > dbase.session.add(package) > > dbase.session.commit() > > > > except stripe.error.CardError as e: > > body = e.json_body > > err = body['error'] > > > > return render_template('/profile/charge/monthlycharge.html', > > current_package=current_package) > > > > Please advise. > -------------- next part -------------- An HTML attachment was scrubbed... URL: From coreybrett at gmail.com Tue Mar 7 16:56:15 2017 From: coreybrett at gmail.com (Corey Boyle) Date: Tue, 7 Mar 2017 16:56:15 -0500 Subject: [Flask] Creating table only if its not created for user already In-Reply-To: References: <20170307162137.GD1899@mail.ziirish.info> Message-ID: FWIW, I think the official documentation is actually quite good. I also highly recommend the following book. http://a.co/0zFaCVl Pause whatever project you are currently working on (because your code is probably a mess), and work through this book chapter by chapter. On Tue, Mar 7, 2017 at 12:30 PM, Anonymous Coder wrote: > Thanks for help but all I am trying to do here is create a udemy clone with > some changes I would like my solution to have. Its not a job I am doing it > for myself. I like python and that's why I chose Flask. > > For your comment above, official documentation sucks that's why I have no > other option as this is my very first project. > Thanks for help though, I will try it. > > Cheers. > > On Tue, Mar 7, 2017 at 4:21 PM, Ziirish wrote: >> >> It would be better if you showed us some of your tries and/or error >> messages. >> Regarding the latest emails you sent it looks like you want us to do your >> job... >> >> Anyway, the "if" you are looking for is probably something like: >> >> >> package = Package.query.filter_by( >> student_id=student_id, stripe_id = customer.id, >> student_email=request.form['stripeEmail'], is_active=True, >> package_type='yearlyrec', subscription_id=subscription.id >> ).first() >> >> if not package: >> package = Package(...) >> dbase.session.add(package) >> dbase.session.commit() >> >> >> >> * On Tuesday, March 07, 2017 at 04:01 PM +0000, Anonymous Coder >> wrote: >> > I was trying to find a way to process/commit a model table for view only >> > if >> > it doesn't exists already. But I couldn't find the way to do it. I tried >> > if/else but it doesn't create model at all if I try it that way. >> > Following >> > is view and I need to create sqlalchymy table only if it doesn't exist >> > alrady. >> > >> > @app.route('/yearlychargedrec', methods=['GET', 'POST']) >> > def yearly_charged_rec(): >> > if not user_authorized(): >> > return redirect('/') >> > >> > current_package = Package.query.all() >> > >> > if request.method == 'POST': >> > data = get_profile_data(session['auth_token']) >> > profile_data = data['StudentProfile'] >> > student = >> > get_profile_data(session['auth_token'])['StudentProfile'] >> > >> > stripe_token = request.form['stripeToken'] >> > email = request.form['stripeEmail'] >> > customer = stripe.Customer.create( >> > email=email, >> > source=request.form['stripeToken'] >> > ) >> > try: >> > subscription = stripe.Subscription.create( >> > customer=customer.id, >> > plan="yearlyrec", >> > ) >> > student_id = profile_data.id >> > student.stripe_customer_id = customer.id >> > student.stripe_subscription_id = subscription.id >> > >> > package = Package( >> > student_id=student_id, >> > stripe_id = customer.id, >> > student_email=request.form['stripeEmail'], >> > is_active=True, >> > package_type='yearlyrec', >> > subscription_id=subscription.id >> > ) >> > dbase.session.add(package) >> > dbase.session.commit() >> > >> > except stripe.error.CardError as e: >> > body = e.json_body >> > err = body['error'] >> > >> > return render_template('/profile/charge/monthlycharge.html', >> > current_package=current_package) >> > >> > Please advise. > > > > _______________________________________________ > Flask mailing list > Flask at python.org > https://mail.python.org/mailman/listinfo/flask > From ford.anthonyj at gmail.com Tue Mar 7 17:48:53 2017 From: ford.anthonyj at gmail.com (Anthony Ford) Date: Tue, 7 Mar 2017 16:48:53 -0600 Subject: [Flask] Creating table only if its not created for user already In-Reply-To: References: <20170307162137.GD1899@mail.ziirish.info> Message-ID: I totally second the sentiment that the docs are good, as well as the book. Definitely take a few days or more to set down and work through some of the Flask & Flask-SQLAlchemy tutorials. Without that, you'll have a much harder time getting things going. Anthony Ford, KF5IBN, ford.anthonyj at gmail.com On Tue, Mar 7, 2017 at 3:56 PM, Corey Boyle wrote: > FWIW, I think the official documentation is actually quite good. > > I also highly recommend the following book. > > http://a.co/0zFaCVl > > Pause whatever project you are currently working on (because your code > is probably a mess), and work through this book chapter by chapter. > > On Tue, Mar 7, 2017 at 12:30 PM, Anonymous Coder > wrote: > > Thanks for help but all I am trying to do here is create a udemy clone > with > > some changes I would like my solution to have. Its not a job I am doing > it > > for myself. I like python and that's why I chose Flask. > > > > For your comment above, official documentation sucks that's why I have no > > other option as this is my very first project. > > Thanks for help though, I will try it. > > > > Cheers. > > > > On Tue, Mar 7, 2017 at 4:21 PM, Ziirish wrote: > >> > >> It would be better if you showed us some of your tries and/or error > >> messages. > >> Regarding the latest emails you sent it looks like you want us to do > your > >> job... > >> > >> Anyway, the "if" you are looking for is probably something like: > >> > >> > >> package = Package.query.filter_by( > >> student_id=student_id, stripe_id = customer.id, > >> student_email=request.form['stripeEmail'], is_active=True, > >> package_type='yearlyrec', subscription_id=subscription.id > >> ).first() > >> > >> if not package: > >> package = Package(...) > >> dbase.session.add(package) > >> dbase.session.commit() > >> > >> > >> > >> * On Tuesday, March 07, 2017 at 04:01 PM +0000, Anonymous Coder > >> wrote: > >> > I was trying to find a way to process/commit a model table for view > only > >> > if > >> > it doesn't exists already. But I couldn't find the way to do it. I > tried > >> > if/else but it doesn't create model at all if I try it that way. > >> > Following > >> > is view and I need to create sqlalchymy table only if it doesn't exist > >> > alrady. > >> > > >> > @app.route('/yearlychargedrec', methods=['GET', 'POST']) > >> > def yearly_charged_rec(): > >> > if not user_authorized(): > >> > return redirect('/') > >> > > >> > current_package = Package.query.all() > >> > > >> > if request.method == 'POST': > >> > data = get_profile_data(session['auth_token']) > >> > profile_data = data['StudentProfile'] > >> > student = > >> > get_profile_data(session['auth_token'])['StudentProfile'] > >> > > >> > stripe_token = request.form['stripeToken'] > >> > email = request.form['stripeEmail'] > >> > customer = stripe.Customer.create( > >> > email=email, > >> > source=request.form['stripeToken'] > >> > ) > >> > try: > >> > subscription = stripe.Subscription.create( > >> > customer=customer.id, > >> > plan="yearlyrec", > >> > ) > >> > student_id = profile_data.id > >> > student.stripe_customer_id = customer.id > >> > student.stripe_subscription_id = subscription.id > >> > > >> > package = Package( > >> > student_id=student_id, > >> > stripe_id = customer.id, > >> > student_email=request.form['stripeEmail'], > >> > is_active=True, > >> > package_type='yearlyrec', > >> > subscription_id=subscription.id > >> > ) > >> > dbase.session.add(package) > >> > dbase.session.commit() > >> > > >> > except stripe.error.CardError as e: > >> > body = e.json_body > >> > err = body['error'] > >> > > >> > return render_template('/profile/charge/monthlycharge.html', > >> > current_package=current_package) > >> > > >> > Please advise. > > > > > > > > _______________________________________________ > > Flask mailing list > > Flask at python.org > > https://mail.python.org/mailman/listinfo/flask > > > _______________________________________________ > Flask mailing list > Flask at python.org > https://mail.python.org/mailman/listinfo/flask > -------------- next part -------------- An HTML attachment was scrubbed... URL: From anonymouscodar at gmail.com Wed Mar 8 03:25:06 2017 From: anonymouscodar at gmail.com (Anonymous Coder) Date: Wed, 8 Mar 2017 08:25:06 +0000 Subject: [Flask] Creating table only if its not created for user already In-Reply-To: References: <20170307162137.GD1899@mail.ziirish.info> Message-ID: Thanks for the recommendation and I am going to read and practice it thoroughly. I apologise to bother you all on and off and will make sure to go through it line by line. Thanks again. On Tue, Mar 7, 2017 at 10:48 PM, Anthony Ford wrote: > I totally second the sentiment that the docs are good, as well as the book. > > Definitely take a few days or more to set down and work through some of > the Flask & Flask-SQLAlchemy tutorials. Without that, you'll have a much > harder time getting things going. > > > > Anthony Ford, > KF5IBN, > ford.anthonyj at gmail.com > > On Tue, Mar 7, 2017 at 3:56 PM, Corey Boyle wrote: > >> FWIW, I think the official documentation is actually quite good. >> >> I also highly recommend the following book. >> >> http://a.co/0zFaCVl >> >> Pause whatever project you are currently working on (because your code >> is probably a mess), and work through this book chapter by chapter. >> >> On Tue, Mar 7, 2017 at 12:30 PM, Anonymous Coder >> wrote: >> > Thanks for help but all I am trying to do here is create a udemy clone >> with >> > some changes I would like my solution to have. Its not a job I am doing >> it >> > for myself. I like python and that's why I chose Flask. >> > >> > For your comment above, official documentation sucks that's why I have >> no >> > other option as this is my very first project. >> > Thanks for help though, I will try it. >> > >> > Cheers. >> > >> > On Tue, Mar 7, 2017 at 4:21 PM, Ziirish wrote: >> >> >> >> It would be better if you showed us some of your tries and/or error >> >> messages. >> >> Regarding the latest emails you sent it looks like you want us to do >> your >> >> job... >> >> >> >> Anyway, the "if" you are looking for is probably something like: >> >> >> >> >> >> package = Package.query.filter_by( >> >> student_id=student_id, stripe_id = customer.id, >> >> student_email=request.form['stripeEmail'], is_active=True, >> >> package_type='yearlyrec', subscription_id=subscription.id >> >> ).first() >> >> >> >> if not package: >> >> package = Package(...) >> >> dbase.session.add(package) >> >> dbase.session.commit() >> >> >> >> >> >> >> >> * On Tuesday, March 07, 2017 at 04:01 PM +0000, Anonymous Coder >> >> wrote: >> >> > I was trying to find a way to process/commit a model table for view >> only >> >> > if >> >> > it doesn't exists already. But I couldn't find the way to do it. I >> tried >> >> > if/else but it doesn't create model at all if I try it that way. >> >> > Following >> >> > is view and I need to create sqlalchymy table only if it doesn't >> exist >> >> > alrady. >> >> > >> >> > @app.route('/yearlychargedrec', methods=['GET', 'POST']) >> >> > def yearly_charged_rec(): >> >> > if not user_authorized(): >> >> > return redirect('/') >> >> > >> >> > current_package = Package.query.all() >> >> > >> >> > if request.method == 'POST': >> >> > data = get_profile_data(session['auth_token']) >> >> > profile_data = data['StudentProfile'] >> >> > student = >> >> > get_profile_data(session['auth_token'])['StudentProfile'] >> >> > >> >> > stripe_token = request.form['stripeToken'] >> >> > email = request.form['stripeEmail'] >> >> > customer = stripe.Customer.create( >> >> > email=email, >> >> > source=request.form['stripeToken'] >> >> > ) >> >> > try: >> >> > subscription = stripe.Subscription.create( >> >> > customer=customer.id, >> >> > plan="yearlyrec", >> >> > ) >> >> > student_id = profile_data.id >> >> > student.stripe_customer_id = customer.id >> >> > student.stripe_subscription_id = subscription.id >> >> > >> >> > package = Package( >> >> > student_id=student_id, >> >> > stripe_id = customer.id, >> >> > student_email=request.form['stripeEmail'], >> >> > is_active=True, >> >> > package_type='yearlyrec', >> >> > subscription_id=subscription.id >> >> > ) >> >> > dbase.session.add(package) >> >> > dbase.session.commit() >> >> > >> >> > except stripe.error.CardError as e: >> >> > body = e.json_body >> >> > err = body['error'] >> >> > >> >> > return render_template('/profile/charge/monthlycharge.html', >> >> > current_package=current_package) >> >> > >> >> > Please advise. >> > >> > >> > >> > _______________________________________________ >> > Flask mailing list >> > Flask at python.org >> > https://mail.python.org/mailman/listinfo/flask >> > >> _______________________________________________ >> Flask mailing list >> Flask at python.org >> https://mail.python.org/mailman/listinfo/flask >> > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From coreybrett at gmail.com Wed Mar 8 08:33:02 2017 From: coreybrett at gmail.com (Corey Boyle) Date: Wed, 8 Mar 2017 08:33:02 -0500 Subject: [Flask] Creating table only if its not created for user already In-Reply-To: References: <20170307162137.GD1899@mail.ziirish.info> Message-ID: I would also like to suggest this book. https://www.amazon.com/dp/1449355730/ref=cm_sw_r_cp_awdb_RDaWybA5REM4Z It will give you a great introduction to the Python language itself. On Mar 8, 2017 3:25 AM, "Anonymous Coder" wrote: Thanks for the recommendation and I am going to read and practice it thoroughly. I apologise to bother you all on and off and will make sure to go through it line by line. Thanks again. On Tue, Mar 7, 2017 at 10:48 PM, Anthony Ford wrote: > I totally second the sentiment that the docs are good, as well as the book. > > Definitely take a few days or more to set down and work through some of > the Flask & Flask-SQLAlchemy tutorials. Without that, you'll have a much > harder time getting things going. > > > > Anthony Ford, > KF5IBN, > ford.anthonyj at gmail.com > > On Tue, Mar 7, 2017 at 3:56 PM, Corey Boyle wrote: > >> FWIW, I think the official documentation is actually quite good. >> >> I also highly recommend the following book. >> >> http://a.co/0zFaCVl >> >> Pause whatever project you are currently working on (because your code >> is probably a mess), and work through this book chapter by chapter. >> >> On Tue, Mar 7, 2017 at 12:30 PM, Anonymous Coder >> wrote: >> > Thanks for help but all I am trying to do here is create a udemy clone >> with >> > some changes I would like my solution to have. Its not a job I am doing >> it >> > for myself. I like python and that's why I chose Flask. >> > >> > For your comment above, official documentation sucks that's why I have >> no >> > other option as this is my very first project. >> > Thanks for help though, I will try it. >> > >> > Cheers. >> > >> > On Tue, Mar 7, 2017 at 4:21 PM, Ziirish wrote: >> >> >> >> It would be better if you showed us some of your tries and/or error >> >> messages. >> >> Regarding the latest emails you sent it looks like you want us to do >> your >> >> job... >> >> >> >> Anyway, the "if" you are looking for is probably something like: >> >> >> >> >> >> package = Package.query.filter_by( >> >> student_id=student_id, stripe_id = customer.id, >> >> student_email=request.form['stripeEmail'], is_active=True, >> >> package_type='yearlyrec', subscription_id=subscription.id >> >> ).first() >> >> >> >> if not package: >> >> package = Package(...) >> >> dbase.session.add(package) >> >> dbase.session.commit() >> >> >> >> >> >> >> >> * On Tuesday, March 07, 2017 at 04:01 PM +0000, Anonymous Coder >> >> wrote: >> >> > I was trying to find a way to process/commit a model table for view >> only >> >> > if >> >> > it doesn't exists already. But I couldn't find the way to do it. I >> tried >> >> > if/else but it doesn't create model at all if I try it that way. >> >> > Following >> >> > is view and I need to create sqlalchymy table only if it doesn't >> exist >> >> > alrady. >> >> > >> >> > @app.route('/yearlychargedrec', methods=['GET', 'POST']) >> >> > def yearly_charged_rec(): >> >> > if not user_authorized(): >> >> > return redirect('/') >> >> > >> >> > current_package = Package.query.all() >> >> > >> >> > if request.method == 'POST': >> >> > data = get_profile_data(session['auth_token']) >> >> > profile_data = data['StudentProfile'] >> >> > student = >> >> > get_profile_data(session['auth_token'])['StudentProfile'] >> >> > >> >> > stripe_token = request.form['stripeToken'] >> >> > email = request.form['stripeEmail'] >> >> > customer = stripe.Customer.create( >> >> > email=email, >> >> > source=request.form['stripeToken'] >> >> > ) >> >> > try: >> >> > subscription = stripe.Subscription.create( >> >> > customer=customer.id, >> >> > plan="yearlyrec", >> >> > ) >> >> > student_id = profile_data.id >> >> > student.stripe_customer_id = customer.id >> >> > student.stripe_subscription_id = subscription.id >> >> > >> >> > package = Package( >> >> > student_id=student_id, >> >> > stripe_id = customer.id, >> >> > student_email=request.form['stripeEmail'], >> >> > is_active=True, >> >> > package_type='yearlyrec', >> >> > subscription_id=subscription.id >> >> > ) >> >> > dbase.session.add(package) >> >> > dbase.session.commit() >> >> > >> >> > except stripe.error.CardError as e: >> >> > body = e.json_body >> >> > err = body['error'] >> >> > >> >> > return render_template('/profile/charge/monthlycharge.html', >> >> > current_package=current_package) >> >> > >> >> > Please advise. >> > >> > >> > >> > _______________________________________________ >> > Flask mailing list >> > Flask at python.org >> > https://mail.python.org/mailman/listinfo/flask >> > >> _______________________________________________ >> Flask mailing list >> Flask at python.org >> https://mail.python.org/mailman/listinfo/flask >> > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From woon at mit.edu Sat Mar 11 05:08:52 2017 From: woon at mit.edu (Michael Woon) Date: Sat, 11 Mar 2017 11:08:52 +0100 Subject: [Flask] WTForms / Validation / Dynamically set fields help Message-ID: Hi Flask community, I have a question, but don't trust that posting source code to the mailing list will be easy to read, so maybe it's better here: http://stackoverflow.com/questions/42721564/flask-python-wtforms-validation-and-dynamically-set-selectfield-choices A brief description: I'm struggling a little bit right now with creating choices for a SelectField dynamically on a POST and then POSTing again, using data from the field, all to the same page. I think it might be validation related. Much more detail via the link above. Thanks! Michael -------------- next part -------------- An HTML attachment was scrubbed... URL: From anonymouscodar at gmail.com Thu Mar 16 05:50:54 2017 From: anonymouscodar at gmail.com (Anonymous Coder) Date: Thu, 16 Mar 2017 09:50:54 +0000 Subject: [Flask] Deleting rows from Flask-Admin panel Message-ID: I am not able to delete rows from flask-admin and getting following error: OperationalError: (_mysql_exceptions.OperationalError) (1048, "Column 'student_id' cannot be null") [SQL: u'UPDATE student_attributes SET student_id=%s WHERE student_attributes.id = %s'] [parameters: ((None, 1888L), (None, 1889L))] I am using a fix at but couldn't make it work: https://gist.github.com/pawl/14e6ca2dcdf0435925af Will appreciate some help. My models are: class StudentProfile(db.Model): __tablename__='student_profile' packages = relationship('Package', backref=('student_profile', cascade="all,delete")) attributes = relationship("StudentAttribute", backref=('student_profile', cascade="all,delete")) groupsubs = relationship("GroupSubscription", backref=('student_profile', cascade="all,delete")) def __init__(self, **kwargs): for key, value in kwargs.items(): setattr(self, key, value) class StudentAttribute(db.Model): __tablename__ = 'student_attributes' student_id = db.Column(db.Integer, ForeignKey('student_profile.id', ondelete='CASCADE', onupdate='CASCADE')) class Package(db.Model): __tablename__ = 'package' student_id = db.Column(db.Integer, ForeignKey('student_profile.id', ondelete='CASCADE', onupdate='CASCADE')) class GroupSubscription(db.Model): student_id = db.Column(db.Integer, ForeignKey('student_profile.id', ondelete='CASCADE', onupdate='CASCADE')) -------------- next part -------------- An HTML attachment was scrubbed... URL: From anonymouscodar at gmail.com Mon Mar 27 11:05:57 2017 From: anonymouscodar at gmail.com (Anonymous Coder) Date: Mon, 27 Mar 2017 16:05:57 +0100 Subject: [Flask] Accessing Jquery Key/Values from Python Message-ID: Can anyone help with the question below? http://stackoverflow.com/questions/43041003/accessing-jquery-key-values-in-python Thanks. -------------- next part -------------- An HTML attachment was scrubbed... URL: From skip.montanaro at gmail.com Fri Mar 31 08:48:46 2017 From: skip.montanaro at gmail.com (Skip Montanaro) Date: Fri, 31 Mar 2017 07:48:46 -0500 Subject: [Flask] Subclassing flask.Flask? Message-ID: I'm getting to the point with a smallish Flask application that I really don't want a bunch of module-level global variables sitting around. My initial inclination would be to subclass flask.Flask, but picking through the documentation and the prominent examples (minitwit, etc), I didn't see any examples of this. This leads me to believe that maybe that's not the correct route for corralling my data. I'll give you one small example. My database is actually constructed from a set of files which are updated outside of my control. I'd like to run a separate thread to incrementally update the relevant bits of my database as individual files change. The straightforward (to me) way to do this is to have a separate thread which keeps things up-to-date. As I'm currently using sqlite3, that suggests I need to lock access. Storing the necessary threading.{RLock,Lock} object at the module level seems iffy. I'd prefer to keep it as an instance attribute. Once I've got a subclass, why not put all the implementation in instance methods with the necessary @app.route decorators? So, am I off-base in thinking I should subclass the Flask class? Is there a more prevalent pattern? Thx, Skip Montanaro