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 12 forks source link

Adding Kasm user creation to backend #14

Closed TDWolff closed 4 months ago

TDWolff commented 4 months ago

In the user.py api class, I implemented a post method to create a new user. This method is designed to handle the creation of a new user both in our local database and on the Kasm server if needed.

First, I extract the data from the request's JSON body from the login portal. This data includes the user's name, user ID (uid), and a flag indicating whether a Kasm server is needed . I perform some basic validation on the name and uid to ensure they are present and at least 2 characters long.

If kasm_server_needed is set to True, I prepare a POST request to the Kasm server's /api/public/create_user endpoint at https://kasm.nighthawkcodingsociety.com/api/public/create_user. The data for this request is assembled into a dictionary, kasm_data, which includes the API key and secret, as well as the details of the user to be created. These details are nested under the target_user key, as specified in the Kasm API documentation.

I then send the POST request to the Kasm server using the requests.post function, passing in the URL and the kasm_data dictionary. The response from the Kasm server is printed to the console for debugging purposes.

After handling the Kasm server interaction, I proceed to create the user in our local database. I instantiate a new User object, passing in the name, uid, and kasm_server_needed flag. If a password and date of birth (dob) were provided in the request, I also set these on the User object.

Finally, I call the create method on the User object to add the new user to our database. If the creation is successful, I return a JSON representation of the new user. If not, I return an error message indicating that there was a problem with the user data or that the uid is a duplicate.

Throughout this process, I referred to the Kasm API documentation to understand the requirements for creating a user on the Kasm server. This helped me to structure the kasm_data dictionary correctly and to handle the response from the Kasm server.

Heres some screenshots to see further what I changed and how it works in runtime development: Screenshot 2024-07-20 at 5 07 42 PM Screenshot 2024-07-20 at 5 08 01 PM Screenshot 2024-07-20 at 5 08 13 PM

jm1021 commented 4 months ago

No kasm.py, starting to flood user.py with code that is behind the scenes.