0% found this document useful (0 votes)
4 views2 pages

API Endpoints

The document describes a Django API view for uploading and processing PDF files, specifically for extracting license information. It outlines the steps involved, including file validation, text extraction from the PDF, identification of license types, and saving the extracted data to a database model. The API responds with success or error messages based on the processing outcome.

Uploaded by

alisuleimann4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

API Endpoints

The document describes a Django API view for uploading and processing PDF files, specifically for extracting license information. It outlines the steps involved, including file validation, text extraction from the PDF, identification of license types, and saving the extracted data to a database model. The API responds with success or error messages based on the processing outcome.

Uploaded by

alisuleimann4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

This code defines a Django API view for uploading and processing PDF files.

The view accepts POST


requests, processes the uploaded PDF to extract text from it, and then attempts to extract specific
details based on the type of license mentioned in the document. Here's a step-by-step breakdown of the
code:

1. Imports:

fitz: A module from PyMuPDF, used to handle PDF files (to open and extract text).

Django Imports: Various Django modules for handling HTTP requests and file operations (HttpResponse,
JsonResponse, HttpResponseBadRequest, etc.).

Rest Framework Imports: For handling API views and requests (api_view decorator).

File Handling Imports: For handling file storage and conversion (ContentFile, default_storage).

Model Import: License, which represents the model that stores the extracted data.

2. license_upload API View:

@api_view(http_method_names=['POST']): This decorator ensures that the view only accepts POST
requests.

3. Retrieve the PDF File:

The uploaded file is fetched using request.FILES.get('my_file').

Validation:

If no file is uploaded, an error response with HttpResponseBadRequest is returned.

If the file is not a PDF (doesn't end with .pdf), another error response is returned.

4. Saving the Uploaded File:

The file is saved temporarily using Django's default_storage.save(). The file is saved in a temp/ directory
within the storage system.

5. Opening the PDF:

The fitz.open() method is used to open the uploaded PDF file. If there’s an error opening the PDF (e.g.,
corrupt file), a JsonResponse with error details is returned.

6. Extracting Text from the PDF:

The text from all pages of the PDF is extracted using page.get_text("text") and joined into a single string
(text).
7. Extracting License Information:

Based on the text content, the license type is identified:

Driving License

Business License

Tourism License

The script searches for specific keywords (e.g., "Driving License:", "Business Name:", etc.) to extract data
from the PDF.

8. Creating Records in the Database:

If the license_type is identified, relevant fields (like business_name, license_no, etc.) are extracted from
the PDF text and saved to the License model.

For Business License, it extracts:

Business name, business ID, and national ID.

For Driving License, it extracts:

Surname, license number, sex, and other names.

For Tourism License, it extracts:

Names and license number.

After extracting and creating the relevant records, it saves the data to the License model (Django ORM).

9. Cleaning Up:

After processing, the uploaded file is deleted from storage using default_storage.delete(file_path).

10. Returning the Response:

If everything goes smoothly, the API returns an HttpResponse confirming that the file has been uploaded
and processed successfully.

If any exception occurs, a JsonResponse with the error details is returned.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy