MarisiaS / SMM

0 stars 0 forks source link

BE - Add School model, serializer and view with search and sort features #43

Closed viriponce closed 7 months ago

viriponce commented 7 months ago

This PR addresses issue #16

Implementation

  1. backend/api/models.py Create School model with the following fields:

Note: In this first model definition, the name field does not have Unique and db_collation attributes.

  1. backend/api/migrations/0006_school.py After creating the model, I ran the makemigrations command and the initial migration for the School model was created.

  2. backend/api/migrations/0007_add_collation_unique_to_name_school.py This is a manual migration file created to add Unique and case-insensitive attributes to name field in School model.

  3. backend/api/CustomField.py Created a custom TimeField that allows blank values calle BlankableTimeField. Django by default doesn't allow blank for TimeFields.

  4. backend/api/serializers/SchoolSerializer.py Created a Model serializer for School. Note that we are using the BlankableTimeField for open and close hour.

Added the validate_name method to check if the name is unique, doing so at Serializer level allow us to handle the exception returned as a Validation Error with a Bad request status code. If we don't have this validation, it will happen at model level and an Internal Server Error will be thrown.

  1. backend/api/views/SiteView.py Created a Model View set for School with the following features:

The data is returned paginated.

  1. backend/api/urls.py Added a router to generate the endpoints for School.

  2. backend/api/fixtures/setup_data.json

Added data for an school object.

I applied the migrations running the migrate command. Then load the fixture data with loaddata command.

Swagger

The endpoints are accessible through Swagger:

Screenshot 2024-02-12 at 10 14 29 AM

I tried each of the new endpoints successfully. I was able to create, delete, patch and get an school. I tried the search and sort functionality while getting the schools. When posting and patching, I got the expected error for entering invalid data.