From 8a15593cc3ff1ce004d5526df34dc1cd38ae4b53 Mon Sep 17 00:00:00 2001 From: Dean Johnson Date: Mon, 10 Feb 2014 15:19:57 -0800 Subject: [PATCH 1/3] Added example for interacting with a database. --- .gitignore | 2 ++ __init__.py | 0 create_tables.py | 3 +++ requirements.txt | 8 ++++++ systemview.py | 50 +++++++++++++++++++++++++++++++++++--- templates/proc_search.html | 13 ++++++++++ 6 files changed, 72 insertions(+), 4 deletions(-) create mode 100644 .gitignore create mode 100644 __init__.py create mode 100644 create_tables.py create mode 100644 requirements.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..085c4fe --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.pyc +*.db diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/create_tables.py b/create_tables.py new file mode 100644 index 0000000..a3d3e4d --- /dev/null +++ b/create_tables.py @@ -0,0 +1,3 @@ +from systemview import db + +db.create_all() diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..4938e53 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,8 @@ +Flask==0.10.1 +Flask-SQLAlchemy==1.0 +Jinja2==2.7.2 +MarkupSafe==0.18 +SQLAlchemy==0.9.2 +Werkzeug==0.9.4 +argparse==1.2.1 +itsdangerous==0.23 diff --git a/systemview.py b/systemview.py index 5dde9f7..ac71202 100644 --- a/systemview.py +++ b/systemview.py @@ -1,6 +1,7 @@ # all the imports from flask import Flask, request, session, g, redirect, url_for, \ abort, render_template, flash +from flask.ext.sqlalchemy import SQLAlchemy import subprocess import sys import argparse @@ -18,6 +19,28 @@ # create the application app = Flask(__name__) +# tell the app where our db is. +app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///systemview.db' + +# set up the db through SQLAlchemy +db = SQLAlchemy(app) +db.init_app(app) + + +# declare our Search model to hold previous search terms. +class Search(db.Model): + # tell the model what to name the table + __tablename__ = 'searches' + + # this is where we declare the types of our database columns + # and relationships between tables + term = db.Column(db.String(100), primary_key=True) + + # this defines what to do, and which parameters to provide when + # creating a Search object. + def __init__(self, term): + self.term = term + """ @app.route is a decorator - it tells the app what URL to respond to @@ -33,18 +56,22 @@ def show_form(): # first, let's get the host name of this vm to display on the form hostname = get_hostname() + # Now let's get all the previous searches + searches = Search.query.all() + # render the search template and send it the hostname we found - return render_template('proc_search.html', hostname=hostname) + return render_template('proc_search.html', hostname=hostname, + searches=searches) @app.route('/procs/', methods=['POST']) -def show_procs(): +def show_procs(term=None): # this method gets the system processes matching the search term # let's get the host name of this vm to display on the page hostname = get_hostname() - # first, get the search term out of the POST data + # get the search term out of the POST data term = request.form['term'] # now lets get all the system processes @@ -60,8 +87,23 @@ def show_procs(): procs.append(line) count += 1 + # check to see if the term is already in the db + search = Search.query.filter_by(term=term).first() + + # check to see that we entered a term, and it's not in the db + if term and not search: + + # create an object from our search term + search = Search(term) + + # add and commit it to the db + db.session.add(search) + db.session.commit() + + searches = Search.query.all() + # render the search template, this time with our data - return render_template('proc_search.html', + return render_template('proc_search.html', searches=searches, procs=procs, count=count, hostname=hostname) diff --git a/templates/proc_search.html b/templates/proc_search.html index d6b73d7..52a612f 100644 --- a/templates/proc_search.html +++ b/templates/proc_search.html @@ -20,4 +20,17 @@

{{ count }} processes found on {{ hostname }}

  • No processes contain that string.
  • {% endfor %} +
    + {% if searches %} +

    Previous searches: +
    +
      + {% for search in searches %} +
    • + {% endfor %} +
    +
    + {% else %} +
  • Nothing has been searched yet.
  • + {% endif %} {% endblock %} From 4a664e04edf725fff5026dc90044ac7cb90f5c44 Mon Sep 17 00:00:00 2001 From: Dean Johnson Date: Thu, 13 Feb 2014 18:40:51 -0800 Subject: [PATCH 2/3] Changed configuration to use mysql. --- systemview.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/systemview.py b/systemview.py index ac71202..cacc485 100644 --- a/systemview.py +++ b/systemview.py @@ -20,7 +20,7 @@ app = Flask(__name__) # tell the app where our db is. -app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///systemview.db' +app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://vagrant:password@localhost/systemview' # set up the db through SQLAlchemy db = SQLAlchemy(app) From f94f302c9e885b8d2485ab35faa1388bcc57b3d3 Mon Sep 17 00:00:00 2001 From: Dean Johnson Date: Thu, 20 Feb 2014 16:21:33 -0800 Subject: [PATCH 3/3] Added MySQL-python to requirements. --- requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/requirements.txt b/requirements.txt index 4938e53..ea840eb 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,6 +2,7 @@ Flask==0.10.1 Flask-SQLAlchemy==1.0 Jinja2==2.7.2 MarkupSafe==0.18 +MySQL-python==1.2.5 SQLAlchemy==0.9.2 Werkzeug==0.9.4 argparse==1.2.1