Flask-SQLAlchemy (mssql+pyodbc) and Existing Models with Blueprint - sql-server

I have this following Flask application/folder structure in python3.6 Enviroment:
site/
__init__.py
models.py
static/
templates/
dashboard/
index.html
login/
index.html
views/
__init__.py
login.py
config.py
run.py
My files looks like this:
run.py
from site import app
app.run()
config.py
class BaseConfig(object):
DEBUG = False
TESTING = False
WFT_CSRF_ENABLED = True
class DevConfig(BaseConfig):
DEBUG = True
SECRET_KEY = 'dev'
SQLALCHEMY_DATABASE_URI = 'DRIVER={ODBC Driver 11 for SQL Server};SERVER=myServer;DATABASE=myDB;TRUSTED_CONNECTION=yes'
SQLALCHEMY_TRACK_MODIFICATIONS = False
site/init.py
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config.from_object('config.DevConfig')
db = SQLAlchemy(app)
from site.views import login
app.register_blueprint(login.mod)
site/views/login.py
from flask import Blueprint, render_template
mod = Blueprint('login', __name__)
#mod.route('/')
def index():
return render_template('login/index.html')
site/models.py
class User(db.Model):
__table__ = db.Model.metadata.tables['users']
def __repr__(self):
return '<User %r>' % self.username
what i am trying to do is to connect and use a existing model in my ms-sql database which is called user.
I am very new with Flask, Flask-SQLAlchemy, Blueprint and Python and I am stucked on how to use the db.Model in my login.py. How to achieved this in a Blueprint paradigm?
Any help and direction will be much appriciated and thanks for the help in advanced!
regards,
maki

you can add the following lines to your login.py:
from site import db
from site.models import User
to add an new user to the database you can use this :
new_user = User()
db.session.add(new_user)
db.session.commit()
and add this in your user table :
_tablename__ = 'user'
you will need to change you database URI in the config by following the syntax suggested in the flask_sqlalchemy doc
replace what you have by this :
SQLALCHEMY_DATABASE_URI : mysql://username:password#server_adress/db_name
for user authentification i can suggest you flask_login

Related

Executing Template From App Engine using Java

I have to Trigger a Template of dataflow Job from App Engine in Fixed Interval of Time to Make Fix Interval I Have used cron job but do not have any Idea how to trigger a Template in Java Language I Need the Below Code in Form of Java.
import datetime
import logging
import os
from google.appengine.ext import ndb
import webapp2
from googleapiclient.discovery import build
from oauth2client.client import GoogleCredentials
class LaunchJob(webapp2.RequestHandler):
credentials = GoogleCredentials.get_application_default()
service = build('dataflow', 'v1b3', credentials=credentials)
# Set the following variables to your values.
JOBNAME = 'kiss-fn-dataflow-job'
PROJECT = 'testing1-18001111'
BUCKET = 'kiss-bucket'
TEMPLATE = 'Test1'
GCSPATH="gs://{bucket}/templates/{template}".format(bucket=BUCKET, template=TEMPLATE),
BODY = {
"jobName": "{jobname}".format(jobname=JOBNAME),
"parameters": {
"inputFile" : "gs://{bucket}/input/my_input.txt",
"outputFile": "gs://{bucket}/output/my_output".format(bucket=BUCKET)
},
"environment": {
"tempLocation": "gs://{bucket}/temp".format(bucket=BUCKET),
"zone": "us-central1-f"
}
}
request = service.projects().templates().launch(projectId=PROJECT, gcsPath=GCSPATH, body=BODY)
response = request.execute()
app = webapp2.WSGIApplication([
('/', LaunchJob),
], debug=True)
The Above Program Runs Perfectly But to Deploy a Single application Many Dependency in Python is Done and Some Functionality is Not Available as Per Requirement to I need to Change my App Engine Program in Java. So I Can Usee Apache beam in My App.

How to Bypass Local Login Screen with Oauth2 and GAE

I am working with the Oauth2 Decorator provided by Google. Right now I am just trying to do a very simple login via Oauth2 to Google using GAE. I am running locally for test purposes and have been successful in authenticating with Google; however, prior to the Google screen for authentication it always presents me with a local login screen running on localhost (//localhost:14080/_ah/login?continue=http%3A//localhost%3A14080/). I am not sure why I am getting this local login screen which does not appear to have any bearing on the Google login screen that comes after. I am wondering how to avoid this local login screen? Very simple code for test purposes:
import webapp2
import jinja2
from apiclient.discovery import build
from google.appengine.api import users
from oauth2client.appengine import OAuth2Decorator
template_dir = os.path.join(os.path.dirname(__file__), "templates")
jinja_env = jinja2.Environment(loader = jinja2.FileSystemLoader(template_dir))
decorator = OAuth2Decorator(
client_id='the id given by google',
client_secret='the secret given by google',
scope='https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email')
class Handler(webapp2.RequestHandler):
def write(self, *a, **kw):
self.response.out.write(*a, **kw)
def render_str(self, template, **params):
t = jinja_env.get_template(template)
return t.render(params)
def render(self, template, **kw):
self.write(self.render_str(template,**kw))
class MainHandler(Handler):
#decorator.oauth_required
def get(self):
service = build('oauth2', 'v2', http=decorator.http())
request = service.userinfo().get().execute()
self.write(request["email"])
app = webapp2.WSGIApplication([
('/', MainHandler),
(decorator.callback_path, decorator.callback_handler())
], debug=True)
The oauth2 decorator relies on having an appengine-logged-in user to function (it uses the user-id to store the oauth2 credentials), so without writing your own code, it isn't possible to avoid having the screen appear - in production, the login will be remembered for up to 30 days.

How to run app engine unit tests in pycharm?

I'm trying to run the following test ile
from google import appengine
from google.appengine.ext import testbed
from google.appengine.ext import ndb
import unittest
import passwordproperty
class MyTestCase(unittest.TestCase):
def setUp(self):
self.tb = testbed.Testbed()
self.tb.setup_env()
self.tb.activate()
self.tb.init_datastore_v3_stub()
def tearDown(self):
self.tb.deactivate()
... tests ...
if __name__ == '__main__':
unittest.main()
But when I try to run this file, I get the following error:
google.appengine.tools.devappserver2.wsgi_server.BindError: Unable to bind localhost:8080
So it looked like something was almready using port 8080, so I ran lsof -i :8080, which came up empty, and navigating to localhost without a port also doesn't give me anything. What's going on?
In the run/debug configuration create project of "python tests" and set the values like working directory , class (class name of the test class) and it will work

multiple file handlers in google app engine

I want to have multiple pages in google-app engine (python 2.7) and the followoing is my directory structure:
root contains: pujaweb.py (the main script), index.html, stylesheets(folder), port(folder)...
now my second script for the page is in the port folder and i want to configure my index.html such that it links to that page. I have tried a lot of stuff but somehow it does not work and always shows 404 page not found error and the command line says that PujaPort module does not exist (pujaport being the app handler for the second page).
the following is my app.yaml file code:
application: thepujabhalerao
version: 1
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /images
static_dir: images
- url: /stylesheets
static_dir: stylesheets
- url: /port
script: pujaport.app
- url: /.*
script: pujaweb.app
libraries:
- name: jinja2
version: latest
this is my main handler (pujaweb.py)
import cgi
import webapp2
import jinja2
import os
from google.appengine.api import xmpp
from google.appengine.ext import db
jinja_environment = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.dirname(__file__)))
class MainPage(webapp2.RequestHandler):
def get(self):
template_values = {}
template = jinja_environment.get_template('index.html')
self.response.out.write(template.render(template_values))
app = webapp2.WSGIApplication([('/', MainPage)],
debug=True)
and the href in the index filer looks like this:
P
and finally this is the second page handler pujaport.py:
import cgi
import webapp2
import jinja2
import os
from google.appengine.api import xmpp
from google.appengine.ext import db
jinja_environment = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.dirname(__file__)))
class PujaPort(webapp2.RequestHandler):
def get(self):
self.response.out.write("In handler")
app = webapp2.WSGIApplication([('/port', PujaPort)],
debug=True)
I know its a minor glitch somewhere but please help me out as after being on it for too long i maybe missing the obvious.
If you insist on using that folder structure, you'll need to make the port directory into a package by adding an __init__.py file to it (which can be empty), and reference the app as port.pujaport.app, its fully qualified name.

How to use use_library('django','1.2')

I'm learning developing in Google App Engine.
This is one of the code from the tutorial, http://code.google.com/appengine/docs/python/gettingstarted/usingwebapp.html
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
class MainPage(webapp.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/plain'
self.response.out.write('Hello, webapp World!')
application = webapp.WSGIApplication(
[('/', MainPage)],
debug=True)
def main():
run_wsgi_app(application)
if __name__ == "__main__":
main()
I've almost identical code. I sometime get warning:
WARNING 2011-06-30 13:10:44,443 init.py:851] You are using the default Django version (0.96). The default Django version will change in an App Engine release in the near future. Please call use_library() to explicitly select a Django version. For more information see http://code.google.com/appengine/docs/python/tools/libraries.html#Django
Can anyone please re factor the above code with use_library(). I'm not sure how to start and where to use use_library and what to do with webapp.
Thanks in advance.
The above code should not require you to call use_library directly.
If you create a new file in the root directory of your application named appengine_config.py and add the following line to it:
# Make webapp.template use django 1.2
webapp_django_version = '1.2'
try putting this code on top of your module :
import os
from google.appengine.dist import use_library
use_library('django', '1.2')
In the current version this is even simpler as third-party libraries are now specified in app.yaml
libraries:
- name: django
version: "1.2"
You can also use webapp2 that includes Django’s templating engine.
import webapp2
from google.appengine.ext.webapp2 import template

Resources