nighthawkcoders / flask_2025

This project is a web application for managing Classroom instruction and operations. It is built using Flask, SQLAlchemy, and other requirements.txt dependencies.
https://flask2025.nighthawkcodingsociety.com/
0 stars 6 forks source link

Setting up Kasm code #18

Open TDWolff opened 1 month ago

TDWolff commented 1 month ago

The User.py File

In user.py, I created a post method to handle the creation of new users. This method reads data from a JSON body sent via an HTTP POST request. Here's how I did it:

def post(self):
    body = request.get_json()

To ensure the integrity of the data, I implemented some error checking. I validated the name and user ID (uid) to ensure they are not missing and are at least 2 characters long. I also checked for the kasm_server_needed field, which defaults to False if not provided.

name = body.get('name')
uid = body.get('uid')
kasm_server_needed = body.get('kasm_server_needed', False)

Setting Up the User Object

After validating the input, I set up a User object. This object is initialized with the name, uid, and kasm_server_needed values.

uo = User(name=name, uid=uid, kasm_server_needed=kasm_server_needed)

I also added some additional error checking. If a password is provided, it's set for the User object. If a date of birth (dob) is provided, I attempted to convert it to a date type.

if password is not None:
    uo.set_password(password)
if dob is not None:
    try:
        uo.dob = datetime.strptime(dob, '%Y-%m-%d').date()
    except:
        return {'message': f'Date of birth format error {dob}, must be mm-dd-yyyy'}, 400

Adding User to the Database

Finally, I added the user to the database. If the user creation is successful, the method returns a JSON representation of the user. If it fails, it returns an error message.

user = uo.create()
if user:
    return jsonify(user.read())
return {'message': f'Processed {name}, either a format error or User ID {uid} is duplicate'}, 400

Interaction with Kasm.py Model

The user.py file interacts with the kasm.py model through the User class. This class is responsible for creating the user in the database and reading the user data. The set_password method is used to hash the password before storing it in the database, enhancing the security of the application.

Conclusion

Integrating user.py with the kasm.py model has been a crucial part of my project, enabling the Kasm program to interact with various API endpoints effectively. This integration has made the user creation process more robust and secure. I hope this post has given you some insight into how I achieved this. Stay tuned for more updates on my project!

jm1021 commented 1 month ago

More comprehensive series is here: https://nighthawkcoders.github.io/portfolio_2025/flask-intro

Though we may want some specifics on kasm called out in kasm series of articles, that goes with picture diagram.