import logging

import flask.ext.restless

import db
import db.schema as schema
import db.query as query

from handlers import app
import services.fb as fb

# Create the Flask application and the Flask-SQLAlchemy object.
app.config['DEBUG'] = True
app.config['SQLALCHEMY_DATABASE_URI'] = db.connection_string

# Create the Flask-Restless API manager.
manager = flask.ext.restless.APIManager(app, session=query.session)

# POST /submission post-processor
def post_submission_postprocessor(result=None, **kw):
  """Accepts a single argument, `result`, which is the dictionary
  representation of the created instance of the model.
  """
  submission = result
  logging.info("Entered POST submission %s postprocessor" % submission['id'])
  # Post submission to Facebook
  fb_response = fb.post_submission(submission)
  # Insert fb_post_id into Database
  query.update_submission_fb_post_id(submission['id'], fb_response['id'])
  # Insert fb_post_id into outgoing result
  result['fb_post_id'] = fb_response['id']
  logging.info("Finished POST submission %s postprocessor" % submission['id'])

# Create API endpoints, which will be available at /api/<tablename> by
# default. Allowed HTTP methods can be specified as well.
manager.create_api(schema.Round, methods=["GET"])
manager.create_api(schema.Perspective, methods=["GET"])
manager.create_api(schema.Submission, methods=["GET", "POST", "PUT"],
  postprocessors={
    'POST': [post_submission_postprocessor]
  })

@@ -628,16 +628,49 @@
 .%0A  %22%22%22%0A
+  session = schema.new_session()%0A
   submis
@@ -757,24 +757,27 @@
 sion%5B'id'%5D)%0A
+  %0A
   # Post sub
@@ -947,24 +947,59 @@
 sponse%5B'id'%5D
+, session=session)%0A  session.close(
 )%0A  # Insert
@@ -1074,16 +1074,17 @@
 e%5B'id'%5D%0A
+%0A
   loggin

Handle session properly when posting new submissions