Skip to content

What did we do for the Asset Class (Assignment 5)?

  • We added a model to models.py and our database
  • We added a form to forms.py
  • We created a new asset_class.html file and extended it from base.html
  • We updated the navigation
  • We created three routes to CRUD our asset classes.

Replicate for Ticker

Step 1. Create the Ticker Model (models.py)

Here is the code for ticker, put it into the models.py file at the bottom.

class Ticker(db.Model):
    ticker_id = db.Column(db.Integer, primary_key=True)
    ticker_symbol = db.Column(db.String(64), index=True, unique=True)
    company_name = db.Column(db.String(164), index=True)
    current_price = db.Column(db.Float)
    asset_class_id = db.Column(db.Integer, db.ForeignKey('asset_class.asset_class_id'), nullable=False)
    user_id = db.Column(db.Integer)

Step 2. Create the Table in MySQL Workbench

When you create your database using MySQL Workbench, make sure you add a foreign key constraint on asset class.

asset_class(asset_class_id -> asset_class_id)
On Update CASCADE
On Delete CASCADE

The on update and on delete cascade essentially says to delete ALL tickers that are associated with an asset class when you delete the asset class. It cascades the delete. I like this option, then you wont have orphaned data that may no longer be relevant. The key though is in the python code model, you need to set it up with the code:

asset_class_id = db.Column(db.Integer, db.ForeignKey(‘asset_class.asset_class_id’), nullable=False)

Step 3. Add the Form Code (forms.py)

Not much different here, except we have a new datatype called SelectField. Make sure you import at the top of the forms.py page from wtforms.

from wtforms import StringField, PasswordField, SubmitField, SelectField
############ TICKERS ############
class TickerForm(FlaskForm):
	ticker_symbol = StringField('Ticker Symbol', validators=[DataRequired()])
	company_name = StringField('Company Name', validators=[DataRequired()])
	asset_class = SelectField('Asset Class', choices=[])
	submit = SubmitField()

Step 4. Add the Three (3) Routes for CRUD

Make sure you add TickerForm and Ticker to the import statements at the top.

Ticker Delete

Ticker Update

Ticker Create and Read

If you can’t read this save this file to your computer (right click save image) and open it an zoom in.

For ticker create and read, I use a function I created called get_tickers(user_id). Put this inside the functions.py file. Also in this form we pass back to the form on the webpage the asset classes to a drop down known as a select field.

You won’t need to import it since the top of your routes.py page you already included everything in that file with:

from app.functions import *

Step 5. Navigation and HTML Pages (tickers.html)

The last thing you need to do is add a link in your base.html files navigation. Then create a new file called tickers.html. Use the asset_class.html file as a template to build tickers.html.

Put this code in the navigation below Asset Class in the base.html page.

<li class="nav-item"><a class="nav-link {% block nav_ticker %}{% endblock %}" href="{{ url_for('tickers') }}">Tickers</a></li>

Tickers.html Page

I would just duplicate the asset_class.html file and name it tickers.html.

Make sure at the very top extends the base file.

{% extends 'base.html' %}

In the block head add/change the “tickers” in the title.

Then go line by line through the block body, looking for places to change the code from asset class to ticker.

This is some sample code I used in my form for the select field asset class.

Look where the <table class code starts and this is how I did it in my page.

Down in the Update section, this is how I put the dropdown for asset class.

You should be good at this point. Make sure you set up times to meet with me if you have any issues.


Submitting to GeorgiaView

Click here for an example of completed project up to this point.

Turn in your working Render URL and GitHub repository link. Make sure it is shared with me correctly.