Skip to content

Instantly share code, notes, and snippets.

@adimania
Created December 9, 2014 06:39
Show Gist options
  • Save adimania/96960173f906c9830e22 to your computer and use it in GitHub Desktop.
Save adimania/96960173f906c9830e22 to your computer and use it in GitHub Desktop.
Flask + SQLAlchemy for pyfacts
# get this from https://gist.github.com/adimania/96960173f906c9830e22
import json
import datetime
from flask import Flask, request
from flask.ext.sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////home/app/inventory.db'
db = SQLAlchemy(app)
base_uri = '/inv/v1'
class Inventory(db.Model):
ipaddress = db.Column(db.String(16), primary_key=True)
memory = db.Column(db.Integer)
processor_speed = db.Column(db.Integer)
cpu_cores = db.Column(db.Integer)
cpu_threads = db.Column(db.Integer)
cpu_name = db.Column(db.String(30))
last_updated = db.Column(db.DateTime)
def __init__(self, ipaddress, memory, processor_speed, cpu_cores, cpu_threads, cpu_name, last_updated):
self.ipaddress = ipaddress
self.memory = memory
self.processor_speed = processor_speed
self.cpu_cores = cpu_cores
self.cpu_threads = cpu_threads
self.cpu_name = cpu_name
self.last_updated = last_updated
def __repr__(self):
return "IP address %s" % (ipaddress,)
@app.route(base_uri, methods=["GET", "POST"])
def set_data():
if request.method == 'POST':
data = json.loads(request.data)
machine = Inventory(data['ipaddress'], int(data['memory']), int(data['processor_speed']), int(data['cpu_cores']), int(data['cpu_threads']), data['cpu_name'], datetime.datetime.strptime(data['time'][:-7], "%Y-%m-%d %H:%M:%S"))
db.session.merge(machine)
db.session.commit()
return "machine saved"
else:
return "Hmmm.. There seems to be a disturbance in the matrix!"
if __name__ == "__main__":
app.debug = True
app.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment