Skip to content

Commit

Permalink
feat(api): Add common ViewSet class for inheritence
Browse files Browse the repository at this point in the history
ref: #345 #346
  • Loading branch information
jon-nfc committed Oct 12, 2024
1 parent 247f01d commit 62696aa
Show file tree
Hide file tree
Showing 2 changed files with 183 additions and 6 deletions.
154 changes: 154 additions & 0 deletions app/api/viewsets/common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
from rest_framework import viewsets

from access.mixin import OrganizationMixin

from api.react_ui_metadata import ReactUIMetadata
from api.views.mixin import OrganizationPermissionAPI



class CommonViewSet(
OrganizationMixin,
viewsets.ModelViewSet
):
"""Common ViewSet class
This class is to be inherited by ALL viewsets.
Args:
OrganizationMixin (class): Contains the Authorization checks.
viewsets (class): Django Rest Framework base class.
"""

@property
def allowed_methods(self) -> list:
"""Allowed HTTP Methods
_Optional_, HTTP Methods allowed for the `viewSet`.
Returns:
list: Allowed HTTP Methods
"""

return super().allowed_methods


documentation: str = None
""" Viewset Documentation URL
_Optional_, if specified will be add to list view metadata
"""

filterset_fields: list = []
"""Fields to use for filtering the query
_Optional_, if specified, these fields can be used to filter the API response
"""

metadata_class = ReactUIMetadata
""" Metadata Class
_Mandatory_, required so that the HTTP/Options method is populated with the data
required to generate the UI.
"""

model: object = None
"""Django Model
_Mandatory_, Django model used for this view.
"""

model_documentation: str = None
"""Model Documentation URL
_Optional_, if specified will be add to detail view metadata"""

page_layout: list = []
""" Page layout class
_Optional_, used by metadata to add the page layout to the HTTP/Options method
for detail view, Enables the UI can setup the page layout.
"""

permission_classes = [ OrganizationPermissionAPI ]
"""Permission Class
_Mandatory_, Permission check class
"""

queryset: object = None
"""View Queryset
_Optional_, View model Query
"""

search_fields:list = []
""" Search Fields
_Optional_, Used by API text search as the fields to search.
"""



def get_model_documentation(self):

if not self.model_documentation:

if hasattr(self.model, 'documentataion'):

self.model_documentation = self.model.documentation

else:

self.model_documentation = ''

return self.model_documentation



def get_page_layout(self):

if len(self.page_layout) < 1:

if hasattr(self.model, 'page_layout'):

self.page_layout = self.model.page_layout

else:

self.page_layout = []

return self.page_layout



def get_queryset(self):

if not self.queryset:

self.queryset = self.model.objects.all()

return self.queryset



def get_serializer_class(self):

if (
self.action == 'list'
or self.action == 'retrieve'
):

return globals()[str( self.model._meta.verbose_name) + 'ViewSerializer']


return globals()[str( self.model._meta.verbose_name) + 'ModelSerializer']



def get_view_name(self):

if self.detail:

return self.model._meta.verbose_name

return self.model._meta.verbose_name_plural
35 changes: 29 additions & 6 deletions docs/projects/centurion_erp/development/views.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,35 @@
---
title: Views
description: Centurion ERP Common Views development documentation
description: Views development Documentation for Centurion ERP
date: 2024-07-12
template: project.html
about: https://gitlab.com/nofusscomputing/infrastructure/configuration-management/centurion_erp
---

Views are used with Centurion ERP to Fetch the data for rendering.

!!! info
Centurion release v1.3.0 added a feature lock to **ALL** Views and the current API. From this release, there is a new API at endpoint `api/v2`. As such we will only be using DRF `ViewSets`. This is required as the UI is being separated from the Centurion Codebase to its own repository. This means that Centurion will become an API only codebase. Release 2.0.0 will remove the current UI and api from Centurion. [See #](https://github.com/nofusscomputing/centurion_erp/issues/343) for details.


## Requirements

- Views are class based

- Inherits from base class `api.viewsets.common.CommonViewSet`

- **ALL** views are `ViewSets`

- Views are saved within the module the model is from under path `viewsets/`

- views are documented at the class level for the swagger UI.


## Pre v1.3 Docs

!!! warning
These docs are depreciated

Views are used with Centurion ERP to Fetch the data for rendering as html. We have templated our views to aid in quick development. We have done this by adding to our views the required pieces of logic so as to ensure the right information is available. The available API can be found within the [API Views](./api/common_views.md) docs.

The views that we use are:
Expand Down Expand Up @@ -33,7 +57,7 @@ The views that we use are:
Common test cases are available for views. These test cases can be found within the API docs under [model view test cases](./api/tests/model_views.md).


## Requirements
### Requirements

All views are to meet the following requirements:

Expand All @@ -50,7 +74,7 @@ All views are to meet the following requirements:
- Add and change views to use a form class


## Tests
### Tests

The following unit test cases exist for views:

Expand All @@ -70,13 +94,13 @@ The following unit test cases exist for views:
The `AllViews` test class is an aggregation of all views. This class is the recommended test class to include if the model uses all available views.


## Docs to clean up
### Docs to clean up

!!! note
The below documentation is still to be developed. As such what is written below may be incorrect.


### Templates
#### Templates

The base template includes blocks that are designed to assist in rendering your content. The following blocks are available:

Expand All @@ -100,4 +124,3 @@ your content here
{% endblock %}

```

0 comments on commit 62696aa

Please sign in to comment.