Skip to content

Instantly share code, notes, and snippets.

@litnimax
Created October 20, 2015 14:44
Show Gist options
  • Save litnimax/1a02be9091ace89f3603 to your computer and use it in GitHub Desktop.
Save litnimax/1a02be9091ace89f3603 to your computer and use it in GitHub Desktop.
Flask signal example
from flask import Flask
from flaskext.sqlalchemy import SQLAlchemy, models_comitted
app = Flask(__name__)
db = SQLAlchemy(app)
class Post(db.Model):
id = db.Column('post_id', db.Integer, primary_key=True)
title = db.Column(db.String(200))
text = db.Column(db.String)
def __init__(self, title, text):
self.title = title
self.text = text
def update_post_search_entry(post, operation):
if operation == 'insert':
searchindex.insert(post)
elif operation == 'update':
searchindex.update(post)
elif operation == 'delete':
searchindex.delete(post)
def on_models_committed(sender, changes):
for model, change in changes:
if isinstance(model, Post):
update_post_search_entry(model, change)
models_committed.connect(on_models_committed, sender=app)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment