67 lines
2.1 KiB
Python
67 lines
2.1 KiB
Python
from rest_framework.response import Response
|
|
from rest_framework import status
|
|
import string
|
|
import random
|
|
import logging
|
|
import os
|
|
import stat
|
|
|
|
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_400_BAD_REQUEST):
|
|
response_data = {"success": False, "status": status, "message": message}
|
|
if errors is not None:
|
|
response_data["errors"] = errors
|
|
return Response(response_data, status=status)
|
|
|
|
# @staticmethod
|
|
# def validation_error(errors, status_code=status.HTTP_422_UNPROCESSABLE_ENTITY):
|
|
# return ApiResponse.error("Validation error", errors, status_code)
|
|
|
|
|
|
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)
|
|
)
|
|
|
|
|
|
class CapacityError(Exception):
|
|
"""Exception raised for errors in the capacity of an event."""
|
|
def __init__(self, message="Venue capacity must be greater than 0"):
|
|
self.message = message
|
|
super().__init__(self.message)
|