Webapp2 custom tags - google-app-engine

I am migrating from webapp1 to webapp2 and from python 2.5 to python2.7
I had some custom tags I used with
webapp.template.register_template_library('common.templatetags')
and registered with:
from google.appengine.ext.webapp import template
register = template.create_template_register()
and the corresponding decorator and everything else worked fine
now I need to use webapp2 which does not have template.register.....
I tried doing it this way : http://www.john-smith.me/Tag/webapp2
but it didn't work
thanks

solved by using add_to_builtins()
from django.template.loader import add_to_builtins
add_to_builtins('myapp.templatetags.mytagslib')

Related

Wrong dependency in Google Cloud SDK for google-auth?

i am using google cloud storage and google cloud datastore api (locally, for now), in a Standard Environment app.
I am having a very weird behaviour trying to running my tests (with pytest): i have discovered that, during tests, the dev_appserver.fix_sys_path command run by pytest-beds that i am using (https://github.com/kaste/pytest-beds/blob/master/testbeds/plugin.py#L35) puts the bundled google-cloud libraries before my custom versions:
/Users/username/google-cloud-sdk/platform/google_appengine
/Users/username/google-cloud-sdk/platform/google_appengine/lib/antlr3
/Users/username/google-cloud-sdk/platform/google_appengine/lib/fancy_urllib
/Users/username/google-cloud-sdk/platform/google_appengine/lib/ipaddr
/Users/username/google-cloud-sdk/platform/google_appengine/lib/yaml-3.10
/Users/username/google-cloud-sdk/platform/google_appengine/lib/rsa
/Users/username/google-cloud-sdk/platform/google_appengine/lib/pyasn1
/Users/username/google-cloud-sdk/platform/google_appengine/lib/pyasn1_modules
/Users/username/google-cloud-sdk/platform/google_appengine/lib/httplib2
/Users/username/google-cloud-sdk/platform/google_appengine/lib/oauth2client_devserver
/Users/username/google-cloud-sdk/platform/google_appengine/lib/six-1.9.0
/Users/username/google-cloud-sdk/platform/google_appengine
/Users/username/google-cloud-sdk/platform/google_appengine/lib/simplejson
/Users/username/google-cloud-sdk/platform/google_appengine/lib/django-1.4
/Users/username/google-cloud-sdk/platform/google_appengine/lib/endpoints-1.0
/Users/username/google-cloud-sdk/platform/google_appengine/lib/jinja2-2.6
/Users/username/google-cloud-sdk/platform/google_appengine/lib/protorpc-1.0
/Users/username/google-cloud-sdk/platform/google_appengine/lib/PyAMF-0.6.1
/Users/username/google-cloud-sdk/platform/google_appengine/lib/markupsafe-0.15
/Users/username/google-cloud-sdk/platform/google_appengine/lib/webob-1.2.3
/Users/username/google-cloud-sdk/platform/google_appengine/lib/webapp2-2.5.2
/Users/username/dev/project
/Users/username/.virtualenvs/project/bin
/Users/username/dev/project/app
/Users/username/.virtualenvs/project/lib/python27.zip
/Users/username/.virtualenvs/project/lib/python2.7
/Users/username/.virtualenvs/project/lib/python2.7/plat-darwin
/Users/username/.virtualenvs/project/lib/python2.7/plat-mac
/Users/username/.virtualenvs/project/lib/python2.7/plat-mac/lib-scriptpackages
/Users/username/.virtualenvs/project/Extras/lib/python
/Users/username/.virtualenvs/project/lib/python2.7/lib-tk
/Users/username/.virtualenvs/project/lib/python2.7/lib-old
/Users/username/.virtualenvs/project/lib/python2.7/lib-dynload
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages
/Users/username/.virtualenvs/project/lib/python2.7/site-packages
This causes the import of google.auth.crypt.base module import to fail, because it does not exist in the package version shipped with google-cloud-sdk, while i can see that version 1.3.0 on github, that is apparently the same version of the one shipped with the sdk, does.
Same for pyasn1_modules package: version 0.2.1 existing, shipped 0.11
The import errors I am having are:
from google.auth.crypt import base
ImportError: cannot import name base
and:
from pyasn1_modules import pem
ImportError: cannot import name pem
Last, but not least, the environment: MACOSX.
Google Cloud SDK 184.0.0
app-engine-python 1.9.65
app-engine-python-extras 1.9.63
beta 2017.09.15
bq 2.0.28
cloud-datastore-emulator 1.3.0
core 2018.01.05
gcloud
gsutil 4.28
UPDATE: apparently, i was using the google.cloud.storage while instead i should be using, according to the examples for standard environment (https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/appengine/standard/storage/appengine-client/main.py#L23), the package cloudstorage. I will change the code, try and update the post here.
UPDATE 2: i have created a specific github repo that should replicate the problem, just in case. Here it is:
https://github.com/brunoripa/gae_import_error
If the problem is entirely on localhost during testing, you can run:
pip install cryptography
so that the Google libraries won't even need pem to begin with.
We typically add libs to remote API with vendor system.
from google.appengine.ext import vendor
vendor.add('lib')
Module pyasn1_modules gets installed as a dependency when you install google-cloud and I am not sure of the reason behind it but, in remote API, I have observed it misses pem.py. The simple workaround would be:
# within remote API console
>>> import sys
>>> sys.modules.pop('pyasn1_modules')
<module 'pyasn1_modules' from '/Users/pjamkhande001/google-cloud-sdk/platform/google_appengine/lib/pyasn1_modules/pyasn1_modules/__init__.pyc'>
About your other issue of not being able to import from google.auth.crypt import base, you are required to gcloud auth login.
gcloud auth application-default login restores your default authentication credentials but if you want to login to the application as user, you have to gcloud auth login. Hope this helps.

What is correct way to import the angularjs library when using typescript?

I work on a large application that imports the angularjs library like so:
import * as angular from 'angular';
However, I just fixed a problem in our test runner by changing the import style in the spec files to:
import angular from 'angular';
What is the difference and which should I use for AngularJS v1.5.7?

Bulk Upload with Google App Engine Datastore via CSV

I am building a GAE webapp using Python. I am also using the Datastore and trying to bulk upload data to the DB using the terminal and a CSV file as per:
https://developers.google.com/appengine/docs/python/tools/uploadingdata
I have created a Loader class in a separate.py file in my app root directory. I am not really sure if this loader class should be in my main.py webapp file, or another file in the root directory.
Loader class:
import datetime
from google.appengine.ext import db
from google.appengine.tools import bulkloader
import models
class FTSELoader(bulkloader.Loader):
def __init__(self):
bulkloader.Loader.__init__(self, 'FTSE',
[('date', lambda x: datetime.datetime.strptime(x, '%Y/%m/%d')),
('close', float)])
loaders = [FTSELoader]
My kind class (i.e. my Datastore table) I am trying to create/upload is called "FTSE". I then run this command in Terminal:
appcfg.py upload_data --config_file=FTSEdataloader.py --filename=FTSEdata.csv -- kind=FTSE --url=http://<myapp.appspot.com>/_ah/remote_api
I get the following error:
File "FTSEdataloader.py", line 4, in
import models
ImportError: No module named models
I do not have a "models.py" like in the GAE demonstration. What should take its place?
Thanks
I had the same problem. I'm not sure why the appcfg.py can't find the models module when running the upload script. I got around the problem by doing this:
import datetime
from google.appengine.ext import db
from google.appengine.tools import bulkloader
class FTSE(db.Model):
date = DateTimeProperty()
close = FloatProperty()
class FTSELoader(bulkloader.Loader):
def __init__(self):
bulkloader.Loader.__init__(self, 'FTSE',
[('date', lambda x: datetime.datetime.strptime(x, '%Y/%m/%d')),
('close', float)])
loaders = [FTSELoader]
Basically it is just putting your model definition in the bulkloader. It certainly isn't the best way to do this, but it will work around the PYTHONPATH problem that appcfg.py seems to have when it is running the bulk upload.
You do this using a file of Python code. The file imports or defines the Model classes for the entities being created, defines a loader class for each kind you wish to import, and declares the available loader classes in a global variable.
For example, say you have a Model class named "FTSE" defined in a file named models.py (which is in your PYTHON PATH, such as the directory where you'll run the tool Ex: C:\Python27) that resembles the following:
models.py
from google.appengine.ext import db
class FTSE(db.Model):
date = db.DateProperty()
close = db.FloatProperty()

bulkloader not importing ndb.model

I am still new to Python and GAE. I have an application on local server that is running just fine. I can add entity to my datastore, I can view my website, etc: everything is fine.
Now I am trying to use bulkloader to add entities to my datastore. I followed the tutorial at https://developers.google.com/appengine/docs/python/tools/uploadingdata. My loader is below:
from google.appengine.ext import ndb
from google.appengine.tools import bulkloader
import my_model
class ArticleLoader(bulkloader.Loader):
def __init__(self):
bulkloader.Loader.__init__(self, 'Article',
[('title', str),
('author', str)
])
loaders = [ArticleLoader]
I am getting the error:
No module named my_model
Does anyone have a fix for this?
Note: I am only using one directory. So my loader is in the same location as the other file that imports the my_model module.
This can also happen if your PYTHONPATH is not properly set up. If you're on Linux, try running this before you run the Bulkloader:
export PYTHONPATH=$PYTHONPATH:.
This appends your current directory to your PYTHONPATH and should make your my_model module visible. Since my memory is terrible and I always forget to do it, I've ended up using a simple shell script that includes this at the beginning and then the bulkload command itself.
If you're on Windows, you should be able to modify your path by using sys.path.append. Haven't tested this, but you could try adding this to your script (note that this should work on Linux as well):
import sys
# ...
sys.path.append('.')
Your code should be located in a file named my_model.py. You are getting that error because there is no module named my_module. Might be worth a read of the Python module and package docs.

How do you add and use just the Django version 1.2.1 template?

Thanks for the help.
Currently I import in gae:
from google.appengine.ext.webapp import template
then use this to render:
self.response.out.write(template.render('tPage1.htm', templateInfo ))
I believe the template that Google supplied for Django templete is version 0.96.
How do I setup and import the newer version of only the Django template version 1.2.1?
Brian
Until Issue 3230 http://code.google.com/p/googleappengine/issues/detail?id=3230 is resolved and Django 1.2 is added as a library, you need to zip up django 1.2.1 and include it with your project.
Is there something specific you want out of 1.2 that isn't in 1.1? Because you could use 1.1 since it is available as a library already by following this http://code.google.com/intl/en-US/appengine/docs/python/tools/libraries.html#Django
which is:
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
from google.appengine.dist import use_library
use_library('django', '1.1')

Resources