Skip to main content

Django-Crud-Manager

Simple, compact and useful library to read request parameters in django framework.

Install

There are 2 methods to install this:

1- Install with pip (Recommended and the easiest way)

pip install django-crud-manager

2- Install from source code:

git clone https://github.com/sauditore/SARequest.git
python setup.py install

Usage:

To use this library just create a class and inherit from sa_request main class

from django_crud_manager import CrudManager, ParamNotFoundError


class ViewName(CrudManager):
    def check_perm(self) -> bool:
        return self.logged_in   # check if the user logged in or not

    def get_request(self):
        # Get int_name from query string
        user_int = self.get_int("int_name", False, 100)
        
        # Get str_name from query string. If data was not found then "default_data" will return
        user_str = self.get_string("str_name", False, "default_data")
        
        # Get float_name from query string. If data was not found then 1.1 will return
        user_float = self.get_float("float_name", False, 1.1)
        
        try:
            # If raise_error is set True, then default value ignored
            # and and exception will raise.
            str_data = self.get_string("parameter_name", True)
            
            # Checks for user permission. If permission not granted then
            # AuthError will raise
            if not self.authorize(perm="user.add_user"):
                raise Exception("Unauthorized")
        except ParamNotFoundError as e:
            # A response will return to user
            return e.get_response()
        # and other logics

Basic Structure:

Most of the methods have a similar signature:

method(name, raise_error, default)

  • name: Name of the parameter to read from query string or post data
  • raise_error: If data is not provided, then raise ParamNotFoundError error.
  • default: If raise_error is set to False, then this value will return as result.

Methods:

get_int:

Get "name" as int from request:

def get_int(name: str, 
            raise_error: bool = False,
            default: int = 0,
            min_value: int = None,
            max_value: int = None
            ) -> int:

get_string:

Get "name" from request as str with min / max length validation

def get_string(name: str,
               raise_error: bool = False,
               default: str = '',
               min_len: int = 0,
               max_len: int = 0) -> str:

paginate:

Paginate query set or list and returns it to use in a template. Queryset or data key name in a dictionary is the same data_name provided. Per Page parameter is read from pp querystring. Current Page is also read from cp querystring. Result contains these keys:

1- data_name: Contains paginated data

2- next: Next number to pass to cp parameter to view next page.

3- back: Back number to pass to cp parameter in querystring to view previous page

4- current_page: Current page number

5- pages: Total number of pages

6- last_page: Number of last page to pass to cp parameter to view

7- request: request object to build URL

8- is_last_page: True if reached last page

9- is_first_page: True if cp is on the first page

10- total_result: Number of records to view

11- next_pages: List of pages to view for the next pages. Each item must pass to cp param to view the page.

12- back_pages: List of pages to view for previous pages. Each item must pass to cp param to view the pate.

def paginate(
        query_set: Union[List, QuerySet],
        data_name: str,
        extra: Dict = None,
        default_per_page: int = 10)

get_decrypted_list:

Process request and find objects by name, decrypt and return in a list

def get_decrypted_list(
        name: str,
        raise_error: bool = False,
        default: List = None) -> List

get_int_list:

Process the request and get a list of int

def get_int_list(
        name: str, 
        raise_error: bool = False, 
        default: List = ()) -> List

get_file_size:

Convert user input data to file size. e.g. User enters : 1024 MB, you will receive : 1024 * 1024 bytes

def get_file_size(
        name: str,
        raise_error: bool = False, 
        default: float = 0) -> float

get_float:

Get "name" as float

def get_float(
        name: str,
        raise_error: bool = False,
        default: float = 0.0) -> float

Contribute:

You're always welcome to contribute to the project! Please file an issue and send your great PR.

TODO:

Add more useful functions

Metadata

Release files for django-crud-manager 0.10.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-crud-manager 0.10.0
File Size Uploaded
django_crud_manager-0.10.0.tar.gz 16.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-crud-manager 0.10.0
File Interpreter ABI Platform
django_crud_manager-0.10.0-py3-none-any.whl Python 3 none any Details

Total release size: 33.7 kB

Release files / django_crud_manager-0.10.0.tar.gz

Download URL django_crud_manager-0.10.0.tar.gz
Size 16.6 kB
Tags Source
SHA-256 checksum
How to use checksums
5d3f2fe7c283eb30bfe480616b57b748ae158df1c05376b10e679c3615407516
BLAKE2b-256 checksum
How to use checksums
1a4a5d0674978e54ad96cda946b80fa3c4fad2914c04b9e3435d5c3c4978dec1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.9.13

Release files / django_crud_manager-0.10.0-py3-none-any.whl

Download URL django_crud_manager-0.10.0-py3-none-any.whl
Size 17.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2a7fa451c7bfb9bb82e7788586995799af1dac4eb7e5962e875cb6db10977d6f
BLAKE2b-256 checksum
How to use checksums
51e720ba626185ac93a05df586ca5eabbf8e13bbeb15e944cc7214de042f77ca
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.9.13

Release history Release notifications | RSS feed

This release

0.10.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page