The module contains several features that provide functionality for various aspects of your application. Here is a breakdown of each feature:
-
Activity Events:
- SqlAlchemy events are used to log user activity logs to Elasticsearch.
- The
dcg_service.core.events.pyfile contains more details about these events. - It includes SqlAlchemy model events such as
before_create,after_create,before_update, andafter_update. - The module includes an
ActivityEventclass and registers the event class in the__init__.pyfile. - You can exclude specific model names from activity events by adding them to the
EXCLUDE_MODEL_EVENTSlist in thesettings.pyfile. - You can add events to log activity by specifying them in the
EVENTSlist in thesettings.pyfile. - The model file path can be specified using the
APP_MODELSvariable in thesettings.pyfile.
-
Authentication/Permission:
- User authentication is handled using JWT tokens.
- You need to set the signing key and JWT algorithms in your settings file.
- The
is_authenticatedfunction fromdcg_service.core.permissionscan be used as a decorator to enforce authentication on API views.
-
Service Cache:
- The module includes a
ServicePriceclass that fetches service fee structure from an Elasticsearch index. - To retrieve service fee details, you can call
ServicePrice(service_id, units).get_fee_details(). - The
get_fee_detailsfunction has an optionalinclude_taxparameter to include/exclude tax details. - Additionally, you can use
get_coupon_detailsto retrieve the total discount and coupon code.
- The module includes a
-
Filters:
- The module provides a Flask filter class that can be inherited to apply filters on querysets.
- You can create a custom filter class by inheriting from
Filterand implementing theadd_filtermethod. - In your API views or functions, you can use the filter class by setting the
filter_classattribute and callingapply()on an instance of the filter class.
-
Custom Response Handler:
- You can change the default API response format by overriding the
CustomResponseclass fromdcg_service.core.response. - The overridden class should provide a response JSON with the following structure:
{"data": data, "message": message, "title": self.response.status, "code": self.response.status_code}.
- You can change the default API response format by overriding the
-
Logger:
- The module provides an overridden logger for better app debugging.
-
Encryption:
- The module includes functions for encrypting and decrypting data.
- You need to set the
CIPHER_PRIVATE_KEYenvironment variable with a 16-character key for AES128 encryption.
-
Elastic-search DSL:
- The module utilizes Elastic-search DSL for connecting and querying Elastic search indexes.
- You need to set the
ELASTICSEARCH_URLenvironment variable in your settings file. - The
ACTIVITY_INDEXandSERVICE_CACHE_INDEXvariables can be set to specify the indexes to be used for activity logs and service caching, respectively.
Elastic-search DSL
Elastic-search DSL is used to connect/query on elastic search indexes.
Must add following in your settings.py file:
ELASTICSEARCH_URL = os.environ.get('ELASTICSEARCH_URL')
ACTIVITY_INDEX = os.environ.get('ACTIVITY_INDEX', '').lower() # must be lower
SERVICE_CACHE_INDEX = os.environ.get('SERVICE_CACHE_INDEX')
Activity Events
SqlAlchemy events are used to log user activity logs to 'Elasticsearch'.
You can check dcg_service.core.events.py file for details.
Contains: SqlAlchemy model events before_create, after_create, before_update, after_update.
Activity event class,
registerd event class in __init__.py file
e.g.
def register_events():
from . import events
activity_events = events.ActivityEvent()
activity_events.initialise_events()
register_events()
Add models names of your apps in settings.py to exclude from activity events.
e.g. EXCLUDE_MODEL_EVENTS = ['Cart']
Add events to log activity.
e.g. EVENTS = ['after_insert', 'before_update']
Add model file path.
e.g APP_MODELS = 'order_app.models'
Authentication/Permission
User authentication JWT token, set following details in your settings file.
SIGNING_KEY = os.environ.get('SIGNING_KEY')
JWT_ALGORITHMS = ['HS256']
Start using:
from dcg_service.core.permissions import is_authenticated
class ExampleView(views.MethodView):
"""
APIs Methods
-----------
GET: Get list.
"""
decorators = [is_authenticated]
def get(self):
Service cache
Service price class to fetch service fee structure from `Elasticsearch` index.
service_id: Respective service id,
units: Units/quantity of service
-----------------------------------------------------------------------------
Call `ServicePrice(service_id, units).get_fee_details()` to get service fee details.
For more information check file function docs.
Start using:
from dcg_service.core.services import ServicePrice
service = ServicePrice(service_id, units)
price_details = service.get_fee_details(include_tax=False)
total_discount, coupon_code = service.get_coupon_details()
Filters
Flask filter class, Inherit this class to apply filters on queryset.
e.g.
class ModelFilter(Filter):
model = model_name
def add_filter(self, *args, **kwargs):
queryset = self.get_queryset()
queryset = queryset.filter_by(**self.request_params)
return queryset
use this filter class in your API view or function.
class APIView():
filter_class = ModelFilter
def get():
queryset = self.filter_class(queryset).apply()
Custom Response handler
Change default API response, override dcg_service.core.response.CustomResponse class according to your need.
returns:
response_json = {
"data": data,
"message": message,
"title": self.response.status,
"code": self.response.status_code
}
Logger
Logger override for maintain app debugging.
Encryption
Contain encrypt, decrypt functions for encryption.
Must add "CIPHER_PRIVATE_KEY" in your env:
key = os.environ['CIPHER_PRIVATE_KEY'] # Must Be 16 char for AES128
Metadata
Release files for dcg-service 1.9.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| dcg_service-1.9.2.tar.gz | 36.7 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| dcg_service-1.9.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 81.2 kB
Release files / dcg_service-1.9.2.tar.gz
| Download URL | dcg_service-1.9.2.tar.gz |
|---|---|
| Size | 36.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ffeb72439892a2b47bc1d0e10fe60c0051ed0235e38fb2985b91e873fceee8fc
|
|
BLAKE2b-256 checksum How to use checksums |
8ad2a648a54a97f0ab1a245aceeabd0ca67e885e18f9e7972b9cdc3c22de1256
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.9.6
|
Release files / dcg_service-1.9.2-py3-none-any.whl
| Download URL | dcg_service-1.9.2-py3-none-any.whl |
|---|---|
| Size | 44.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
5463599cdc3270d5727bca91dbc003a0da29183c9c0e000c80502961d8a81511
|
|
BLAKE2b-256 checksum How to use checksums |
622610655546a4d087d482d3614624d0086c479fc8de225c96d13e41fe5f1430
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.9.6
|