← flask-blog

sqlalchemy -l16

0e1499ba38745dcde7e84d249a73f4e78a218f87 · ovidiucs · May 14, 2015 11:19

+24 −1 · 1 file changed

  • flask-blog.py +24 −1
diff --git a/flask-blog.py b/flask-blog.py
index cdf5888..6ef8095 100644
--- a/flask-blog.py
+++ b/flask-blog.py
@@ -7,11 +7,14 @@ from flask.ext.bootstrap import Bootstrap
 from flask.ext.wtf import Form
 from wtforms import StringField, SubmitField, FileField, ValidationError
 from wtforms.validators import Required, Length
+from flask.ext.sqlalchemy import SQLAlchemy
 
 
 app = Flask(__name__)
 app.config['SECRET_KEY'] = 'top secret!'
-
+# where the db is
+app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///data.sqlite3'
+db = SQLAlchemy(app)
 # initialize extension
 bootstrap = Bootstrap(app)
 
@@ -21,6 +24,7 @@ class NameForm(Form):
                                                          Length(1, 16)])
     submit = SubmitField('Submit')
 
+
 class UploadForm(Form):
     image_file = FileField('Image file')
     submit = SubmitField('Submit')
@@ -31,6 +35,23 @@ class UploadForm(Form):
         if imghdr.what(field.data) != 'jpeg':
             raise ValidationError('Invalid image format')
 
+
+# Define a class that represents our users in the database
+# This class inherits from  a base model classes
+# Models > Classes > Associated with entities within a database.
+
+
+class User(db.Model):
+    # custom table name
+    __tablename__ = 'users'
+
+    id = db.Column(db.Integer, primary_key=True)
+    name = db.Column(db.String(16), index=True, unique=True)
+
+    def __repr__(self):
+        return '<User {0}>'.format(self.name)
+
+
 @app.route('/')
 def hello_world():
     return render_template('index.html')
@@ -75,6 +96,7 @@ def response():
     resp.headers['Content-Type'] = 'text/plain'
     return resp
 
+
 @app.route('/user/<name>')
 def user(name):
     return render_template('user.html', name=name)
@@ -151,6 +173,7 @@ def index5():
 def index6():
     return render_template('other.html', count=session['count'], when=g.when)
 
+
 @app.errorhandler(404)
 def not_found(e):
     return render_template('404.html')