April 12, 2024, Friday work

Continuing to follow the Python + JavaScript – Full Stack App Tutorial

As an example, we’ll use only a few as the rest of the specifications are similar.

class Contact(db.Model):
    id = db.Column(db.Integer, primary_key=True)

primary_key is defining id as the indexer, our tool to help keep these contacts in order and accessible in order, as well as it must be unique. Unique means it cannot be repeated within that category of that database. Integer just means number, and that is the type of data it will accept.

class Contact(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    first_name = db.Column(db.String(80), unique=False, nullable=False)

This part introduces two new things. String and Nullable. String means it accepts a string datatype, and the number in the parenthesis is the max number of characters allowed into that string. Nullable means you cannot enter nothing/leave it blank when entering a piece of data there.

We then make the function to_json. This will help store our data in something similar to a python dictionary, which will be able to be accessed by our API.

def to_json(self):
    return{
        "id" : self.id,
        "firstName" : self.first_name,
        "lastName" : self.last_name,
        "email" : self.email,
    }

We’re using something called Camel Case to index these. This is because it will be going to the JSON, also called Javascript Object Notation. It’s general convention with this type of file to use Camel Case, aka the start of words are separated by capitals instead of underscores in variables. Words separated by underscores instead of capitals is called Snake Case. This is the convention for Python. While using Camel Case and Snake Case are not required, it is recommended.

from config import db

class Contact(db.Model):
    id = id.Column(db.Integer, primary_key=True)
    first_name = db.Column(db.String(80), unique=False, nullable=False)
    last_name = db.Column(db.String(80), unique=False, nullable=False)
    email = db.Column(db.String(120), unique=True, nullable=False)
    
    def to_json(self):
        return{
            "id" : self.id,
            "firstName" : self.first_name,
            "lastName" : self.last_name,
            "email" : self.email,
        }

With that we have finished models.py for now. We are now going to start on main.py.

if __name__ == "__main__":
    
    app.run(debug = True)

This code helps prevent accidentally starting our code from another file. If we run the file directly (main.py), only then will it run the application.

if __name__ == "__main__":
    with app.app_context():
        db.create_all()
    
    app.run(debug = True)

This new piece of code tells the database to create all the specific categories in models.py and itself if they’re not already created.

@app.route("/contacts", methods=["GET"])

This is called a decorator. This will allow us to limit a URL to a valid set of functions or function. The valid methods are only the ones we list.

def get_contacts():
    contacts = Contact.query.all()

This will use Flask SQLAlchemy to fetch all the contacts within our database.

@app.route("/contacts", methods={"GET"})
def get_contacts():
    contacts = Contact.query.all()
    json_contacts = list(map(lambda x: x.to_json(), contacts))
    return jsonify({"contacts" : json_contacts}) 

This will convert the information into json and have our contacts in the database listed.

(left off on 31:34)

Leave a Reply

Your email address will not be published. Required fields are marked *