2024-02-26 13:28:32 +05:30
|
|
|
import logging
|
|
|
|
|
import os
|
2024-03-21 13:09:14 +05:30
|
|
|
import random
|
2024-02-26 13:28:32 +05:30
|
|
|
import stat
|
2024-03-21 13:09:14 +05:30
|
|
|
import string
|
|
|
|
|
|
|
|
|
|
from django.http import JsonResponse
|
|
|
|
|
from rest_framework import status
|
|
|
|
|
from rest_framework.exceptions import NotFound
|
|
|
|
|
from rest_framework.response import Response
|
|
|
|
|
|
|
|
|
|
from . import constants
|
|
|
|
|
|
2024-02-26 13:28:32 +05:30
|
|
|
|
|
|
|
|
class GroupWriteRotatingFileHandler(logging.handlers.RotatingFileHandler):
|
|
|
|
|
|
|
|
|
|
def doRollover(self):
|
|
|
|
|
"""
|
|
|
|
|
Overrides base class method to make the new log file group writable.
|
|
|
|
|
"""
|
|
|
|
|
# Rotate the file first.
|
|
|
|
|
logging.handlers.RotatingFileHandler.doRollover(self)
|
|
|
|
|
|
|
|
|
|
# Add group write to the current permissions.
|
|
|
|
|
currMode = os.stat(self.baseFilename).st_mode
|
|
|
|
|
os.chmod(self.baseFilename, currMode | stat.S_IWGRP)
|
|
|
|
|
|
|
|
|
|
class ApiResponse:
|
|
|
|
|
@staticmethod
|
|
|
|
|
def success(message, data=None, status=status.HTTP_200_OK):
|
|
|
|
|
response_data = {"success": True, "status": status, "message": message}
|
|
|
|
|
if data is not None:
|
|
|
|
|
response_data["data"] = data
|
|
|
|
|
return Response(response_data, status=status)
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def error(message, errors=None, status=status.HTTP_403_FORBIDDEN):
|
|
|
|
|
response_data = {"success": False, "status": status, "message": message}
|
|
|
|
|
if errors is not None:
|
|
|
|
|
response_data["errors"] = errors
|
|
|
|
|
return Response(response_data, status=status)
|
2024-03-11 14:48:48 +05:30
|
|
|
|
2024-02-26 13:28:32 +05:30
|
|
|
# @staticmethod
|
2024-03-11 14:48:48 +05:30
|
|
|
# def validation_error(errors, status=status.HTTP_422_UNPROCESSABLE_ENTITY):
|
|
|
|
|
# return ApiResponse.error("Validation error", errors, status)
|
2024-02-26 13:28:32 +05:30
|
|
|
|
|
|
|
|
class JsonResponseUtil:
|
|
|
|
|
@staticmethod
|
2024-03-11 14:48:48 +05:30
|
|
|
def success(message, data=None, status=200):
|
|
|
|
|
response_data = {"success": True, "status": status, "message": message}
|
2024-02-26 13:28:32 +05:30
|
|
|
if data is not None:
|
|
|
|
|
response_data["data"] = data
|
2024-03-11 14:48:48 +05:30
|
|
|
return JsonResponse(response_data, status=status)
|
2024-02-26 13:28:32 +05:30
|
|
|
|
|
|
|
|
@staticmethod
|
2024-03-11 14:48:48 +05:30
|
|
|
def error(message, errors=None, status=403):
|
|
|
|
|
response_data = {"success": False, "status": status, "message": message}
|
2024-02-26 13:28:32 +05:30
|
|
|
if errors is not None:
|
|
|
|
|
response_data["errors"] = errors
|
2024-03-11 14:48:48 +05:30
|
|
|
return JsonResponse(response_data, status=status)
|
2024-02-26 13:28:32 +05:30
|
|
|
|
|
|
|
|
|
|
|
|
|
class RandomGenerator:
|
|
|
|
|
@staticmethod
|
|
|
|
|
def number(start, end):
|
|
|
|
|
# import random
|
|
|
|
|
return random.randint(start, end)
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def password_reset_code():
|
|
|
|
|
return RandomGenerator.number(10000000, 99999999)
|
|
|
|
|
|
|
|
|
|
def random_otp():
|
|
|
|
|
return RandomGenerator.number(1000, 9999)
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def random_alphnum(length):
|
|
|
|
|
return "".join(
|
|
|
|
|
random.choice(string.ascii_uppercase + string.digits) for _ in range(length)
|
|
|
|
|
)
|