8000 Finish adding starter code to section 5. · SWCodeG/testing-python-apps@ab4b188 · GitHub
[go: up one dir, main page]

Skip to content

Commit ab4b188

Browse files
committed
Finish adding starter code to section 5.
1 parent 73be874 commit ab4b188

File tree

9 files changed

+85
-0
lines changed

9 files changed

+85
-0
lines changed

section5/starter_code/__init__.py

Whitespace-only changes.

section5/starter_code/app.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import os
2+
3+
from flask import Flask
4+
from flask_restful import Api
5+
6+
from resources.item import Item, ItemList
7+
8+
app = Flask(__name__)
9+
10+
app.config['DEBUG'] = True
11+
12+
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('DATABASE_URL', 'sqlite:///data.db')
13+
api = Api(app)
14+
15+
api.add_resource(Item, '/item/<string:name>')
16+
17+
if __name__ == '__main__':
18+
from db import db
19+
20+
db.init_app(app)
21+
22+
if app.config['DEBUG']:
23+
@app.before_first_request
24+
def create_tables():
25+
db.create_all()
26+
27+
app.run(port=5000)

section5/starter_code/db.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from flask_sqlalchemy import SQLAlchemy
2+
3+
db = SQLAlchemy()

section5/starter_code/models/__init__.py

Whitespace-only changes.

section5/starter_code/models/item.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from db import db
2+
3+
4+
class ItemModel(db.Model):
5+
__tablename__ = 'items'
6+
7+
id = db.Column(db.Integer, primary_key=True)
8+
name = db.Column(db.String(80))
9+
price = db.Column(db.Float(precision=2))
10+
11+
def __init__(self, name, price):
12+
self.name = name
13+
self.price = price
14+
15+
def json(self):
16+
return {'name': self.name, 'price': self.price}
17+
18+
@classmethod
19+
def find_by_name(cls, name):
20+
return cls.query.filter_by(name=name).first()
21+
22+
def save_to_db(self):
23+
db.session.add(self)
24+
db.session.commit()
25+
26+
def delete_from_db(self):
27+
db.session.delete(self)
28+
db.session.commit()

section5/starter_code/readme.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Stores REST Api
2+
3+
This is built with Flask, Flask-RESTful, Flask-JWT, and Flask-SQLAlchemy.
4+
5+
To get started:
6+
7+
- Create a virtualenv for this project
8+
- Install requirements using `pip install -r requirements.txt`
9+
10+
When you've created the first test, you'll also need to create a correct runtime configuration in PyCharm.
11+
12+
Create a sample unittest configuration, and choose:
13+
14+
- `Path` as as target, with your project's `/tests` folder.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Flask
2+
Flask-RESTful
3+
Flask-SQLAlchemy
4+
psycopg2

section5/starter_code/run.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from app import app
2+
from db import db
3+
4+
db.init_app(app)
5+
6+
7+
@app.before_first_request
8+
def create_tables():
9+
db.create_all()

section5/starter_code/tests/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)
0