GAE Django Transaction - python

I have been developing on the GAE dev_appserver and my code relied heavily on Django's transactionmiddleware. I have tested it locally and it works.
After deployment to GAE, however, model saves that are committed are not rolled back.
Sample code:
#transaction.commit_on_success
def get(self, request):
name = request.GET.get('name')
d = Department(name=name)
d.save()
raise Exception('Failed')
Is this because Django transaction API is not honored by GAE or is it a problem on my app settings?
FYI django.middleware.transaction.TransactionMiddleware is currently last on the list of MIDDLEWARE_CLASSES

According to this website, the Django database backend for Google App Engine does not support Django transactions. You can however use the run_in_transaction method from the App Engine's SDK.

Related

having trouble setting up python social auth to log in to facebook, facebook giving error that test user is not registered

I am running my django app on a remote server. When I try to log in to facebook using python-social-auth, I get the following error
App Not Setup: This app is still in development mode, and you don't have access to it. Switch to a registered test user or ask an app admin for permissions.
I'm using a tutorial from simpleisbetterthancomplex.com
I've named the app that demonstrates the login "test_social" with a prefix "tsoc" as in /tsoc/login/facebook, etc.
I've managed to get it working on my localhost, I got it ssl certification using letsencrypt.org
it's just getting it to work remotely is very hard, I'm sure there is something I'm missing here. I can't even use the test users I made to log in.
If I am already logged in as a nomral user I get the following error:
App Not Setup: This app is still in development mode, and you don't have access to it. Switch to a registered test user or ask an app admin for permissions.
screenshot of attempted sign-in while already logged in
This happens if I'm signed in as the app administrator or if I'm signed in as an an accepted app developer.
If I'm NOT already signed in to facebook and I try to use the log in I get the following error:
Error Accessing App - We're sorry but the application you are trying to use doesn't exist or has been disabled
screenshot of attempted sign-in while NOT already logged in
here are some of the settings I already have on my facebook app
==========================================================
Basic Settings
(note: I changed the domain name, but the format is the same)
Settings->Basic (top part)
Settings->Basic (bottom part)
==========================================================
Facebook login settings
Products->Facebook Login ->Settings
==========================================================
** test user settings **
==========================================================
django settings
TEMPLATES[0]['OPTIONS']['context_processors'].append(
'social_django.context_processors.backends')
TEMPLATES[0]['OPTIONS']['context_processors'].append(
'social_django.context_processors.login_redirect')
INSTALLED_APPS.append('social_django')
MIDDLEWARE.append('social_django.middleware.SocialAuthExceptionMiddleware')
(tsoc is the django app I'm using to login - as I said above it's based on a tutorial by simpleisbetterthancomplex.com
I've named the app that demonstrates the login "test_social" with a prefix "tsoc" as in /tsoc/login/facebook, etc.
)
SOCIAL_AUTH_LOGIN_ERROR_URL = '/tsoc/settings/'
SOCIAL_AUTH_LOGIN_REDIRECT_URL = '/tsoc/settings/'
SOCIAL_AUTH_RAISE_EXCEPTIONS = False
SOCIAL_AUTH_FACEBOOK_SCOPE = ['email']
SOCIAL_AUTH_URL_NAMESPACE = 'social'
AUTHENTICATION_BACKENDS = (
'social_core.backends.github.GithubOAuth2',
'social_core.backends.twitter.TwitterOAuth',
'social_core.backends.facebook.FacebookOAuth2',
'django.contrib.auth.backends.ModelBackend',)
SOCIAL_AUTH_FACEBOOK_KEY = "XXXX" # they exist, just hidden
SOCIAL_AUTH_FACEBOOK_SECRET = "XXXX"
It would seem that your redirect URL defined in Facebook has /oauth/ where your app is defined as /tsoc/. Other than that, the app is clearly defined as "in development" -- did you try to access it while logged into facebook as one of your test users?

url_for in celery doesn't work

We are building a notofication mailing system as part of a python/flask system, as for now it has been run using cronjobs but we're moving everything to celery to make it more performant and easier to maintain.
However the templates which have been working so far suddendly start throwing the following error:
[2017-05-29 20:30:30,411: WARNING/PoolWorker-7] [2017-05-29 20:30:30,411]
ERROR in mails: ERROR errorString => Could not build url for endpoint
'page.start' with values ['from_email']. Did you mean 'static' instead?
The url_for is called in an external template as follows:
{{ url_for('page.start', _external=True) }}
and rendered as follows:
message = render_template('notifs/user_notif.html',
subject=subject,
entries = grouped,
user=u,
unsubscribe_hash=pw_hash,
list_id = str(notif_list.id),
timestamp = today)
Now if we rip out all the url_for in the template it works. But why?
My hypothesis, which i can't test or proof: Somehow celery does not have access to the Blueprints (even though it is running in the application context, as the tasks actually accesses all kinds of models and the db etc.). How do I make celery understand url_for?
Just ran into the same issue:
The database and models are fronted by by your ORM (Flask-sqlalchemy?), not the Flask application itself. Flask the application provides the context for things like url_for, current_user, etc. Your orm just provides the database abstraction and is not contingent on the actual application context.

Google App Engine: Creating an Ecommerce website

I'm working on an ecommerce website, using Google App Engine with Python. Now, since its an ecommerce website, we would be having dozens of products displayed, each having its own webpage. Now, my question is, if we have about 400 web pages on our server, won't it make the site pretty heavy and bulky? Won't that affect the user experience?
How can we manage the 400-odd web pages on Google App Engine? Is there something I don't know about making a web application less bulky in spite of hosting multiple web pages on the server?
You can use webapp2 framework and fetch product info from the datastore and render it with a template. You make an entity for your product e.g.
class Product(db.Model):
tags = db.ListProperty(db.Category)
category = db.CategoryProperty(verbose_name='Category')
title = db.StringProperty(verbose_name='title') # required
text = db.TextProperty(verbose_name='text')
Then you have a handler class using webapp2, flask, bottle or similar to fetch your product data and render it with for instance the jinja 2 templating engine.
class ProductHandler(BaseHandler):
def get_product(self, key):
data = memcache.get(key)
if data is not None:
return data
else:
data = Product.get_by_id(long(key))
memcache.add(key, data, 6000)
return data
def get(self, id, html):
product= self.get_product(id)
if not product:
self.error(404)
return
self.render_jinja(
'view_product',
product=product)
Then in your template you can use the variables on the view_product.html e.g.
{{product.title}
And the routing is done with a config object for webapp2 (this will be different if you use flask or bottle but if you use flask or bottle you can't leverage webapp2's extras such as User models and i18n)
app = webapp2.WSGIApplication([('/view/(\d+)(\.html?)?', ProductHandler),

"Table flask_db.user doesn't exist" error when doing db.session.commit()

I am building my first app with Flask Python micro-framework and I have a problem with committing my models to the database. When I test my User model on the command line, all works well. But when I do a db.session.commit(), I have error 1146 : "Table doesn't exist."
I'm using a MySQL database in local mode and there is no error with login/password
Maybe I'm doing it wrong on the configuration or something else. So here is my config application config file
from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:admin#localhost/flask_db'
db = SQLAlchemy(app)
from app import views
The error explains it all-- while you may have the models for your data, you haven't yet created the tables in the database to store and query them. Simply import your models then run db.create_all() to generate the tables and you should be good to go.
It'll be worth you reading the quickstart guide for Flask-SQLAlchemy to get your head around the general flow.

GAE and django same users

I'm running a GAE and Django helper project and i ran into some issues when creating a User from de django shell. After exploring the code under the helper directory i came across with the following code:
class User(BaseModel):
user = db.UserProperty(required=True)
My question is, do i have to bind the django user to a google account if so, how do i create a django or google user from my code?
UPDATE: I meant to say create the account without having the google account itself. For instace not having:
users.get_current_user()
UPDATE: A user must be able to create a new account just like regular django users, with a register page for that matter.
For AppEngine, you can get the users who are logged into your application.
from google.appengine.api import users
And then you do
username = users.get_current_user()
And set your user property
User(user=username)
You could lookup the tutorial on how to do redirect to login to ask the application to login to get the user.

Categories

Resources