Monday, May 26, 2014

Heroku's Advantages Over Google App Engine

Pros:

  • Postgres (you can even use pgAdmin to view your database)
  • Uses Git to push your application into production
  • More monitoring features and 3rd party apps
  • As long as your project uses one dyno, it runs for free (similar to google, but with a less confusing billing structure)
Cons:
  • 10k row limit for your database before they start charging you $9 per month 

Saturday, May 24, 2014

AttributeError: 'QuerySelectField' object has no attribute '_sa_instance_state'

I came across this error when I was trying to make a custom validator in Flask-Admin with wtforms form objects.

This code didn't work:
def location_must_not_conflict(form, field):
if Event.query.filter(Event.location == form.location).first():
raise wtforms.validators.ValidationError('Location conflicts with another request for the same venue.')

Because I was using form.location and not form.location.data.

This will work:
def location_must_not_conflict(form, field):
if Event.query.filter(Event.location == form.location.data).first():
raise wtforms.validators.ValidationError('Location conflicts with another request for the same venue.')

Tuesday, May 20, 2014

Individual Form Fields In Jinja2 - Flask-Admin

On your edit and create templates in Flask-Admin, you can print individual form fields like this (the field's name is location):
{{ form.location }}

Sunday, May 18, 2014

Google Calendar Api bad request 400 - Python Authomatic Library

I was finally able to get the request to go through by adding a header saying it's json format. Here's an example:

requestbody = """
{
      "end": {
      "dateTime": "2014-05-18T01:35:00Z"
      },
      "start": {
      "dateTime": "2014-05-18T01:35:00Z"
      },
      "description": "test2",
      "location": "test venue",
      "summary": "test2"
      }
"""
response = authomatic.access(credentials, url, method='POST', headers={'Content-Type': 'application/json'}, body=requestbody)


Saturday, May 17, 2014

Add user_id for modified_by field before save - Flask-Admin


In Flask-Admin, you can override the "on_model_change" function to to automatically populate a "modified_by" field in your model.

class LocationView(ModelView):
def on_model_change(self, form, model):
model.user_id = g.user.id

admin = Admin(app)
admin.add_view(LocationView(Location, db.session, name="Locations"))

Favorite Flask Boilerplate/Skeleton/Templates




Those do a lot of the work of starting a new Flask project for you.

With cookiecutter flask, all you need to do to build a new project is: cookiecutter https://github.com/sloria/cookiecutter-flask.git

As a warning, cookiecutter flask and overholt include a lot of stuff. I ended up not using it because it was too much work to change everything to work with oAuth.

Saturday, May 10, 2014

Reverse PDF Page Order

I created an app that allows you to upload a pdf and it returns a copy of your PDF with the page order reversed. It's available here: http://reversepdf.appspot.com/

Initially when I googled how to do this in Adobe Acrobat, the first few results had fairly complicated solutions. After I made the app, I ended up finding an easier way to do it in Adobe (in the 6th from the top Google result on "reverse pdf order acrobat"...):

Oh well, it was a fun project.