Skip to content

Commit

Permalink
Merge pull request #1 from JahongirHakimjonov/improvement/performance…
Browse files Browse the repository at this point in the history
…-optimization

fix: Add configuration files and improve code formatting
  • Loading branch information
Muhammadali-Akbarov authored Jan 18, 2025
2 parents 49a5916 + 60a9251 commit d6b81d4
Show file tree
Hide file tree
Showing 10 changed files with 176 additions and 141 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ __pycache__/

dist/
paynet_pkg.egg-info/
.idea/
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Paynet configuration settings.py
PAYNET_USERNAME = "your-paynet-username"
PAYNET_PASSWORD = "your-paynet-password"
PAYNET_ACCOUNT_FIELD = "order_id"
PAYNET_ACCOUNT_INFO_FIELDS = ("id", "amount") # this is optional (default is None)
PAYNET_ACCOUNT_MODEL = "order.models.Order"
```

Expand Down
21 changes: 10 additions & 11 deletions paynet/admin.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
from django.contrib import admin


from paynet.utils import get_admin_model
from paynet.models import PaynetTransaction

from paynet.utils import get_admin_model

ModelAdmin = get_admin_model(name="unfold")

Expand All @@ -12,15 +10,16 @@ class PaynetTransactionUI(ModelAdmin):
"""
Admin Model for Transaction
"""

list_display = (
'id',
'amount',
'account_id',
'transaction_id',
'service_id',
'status',
'created_at',
'updated_at'
"id",
"amount",
"account_id",
"transaction_id",
"service_id",
"status",
"created_at",
"updated_at",
)


Expand Down
46 changes: 38 additions & 8 deletions paynet/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
"""
the paynet exceptions
"""

import logging

from rest_framework import status
from rest_framework.exceptions import APIException


logger = logging.getLogger(__name__)


# pylint: disable=W1203


class JSONRPCException(APIException):
"""
Base exception class for JSON-RPC 2.0 formatted errors.
Ensures that all exceptions return a JSON-RPC compliant structure.
"""

code = None

# pylint: disable=super-init-not-called
def __init__(self, detail=None, code=None, rpc_id=None, exc=None):
"""
Initialize the exception with custom detail, code, and JSON-RPC ID.
Args:
detail (str): Custom error message.
code (int): Error code (overrides default code).
Expand All @@ -33,10 +35,7 @@ def __init__(self, detail=None, code=None, rpc_id=None, exc=None):
self.detail = {
"jsonrpc": "2.0",
"id": rpc_id or None,
"error": {
"code": self.code,
"message": detail or self.default_detail
}
"error": {"code": self.code, "message": detail or self.default_detail},
}
logger.error(f"Paynet RPC error detail: {self.detail} exc: {exc}")

Expand All @@ -46,6 +45,7 @@ class MethodNotPOST(JSONRPCException):
"""
Exception raised when the HTTP method is not POST.
"""

code = -32300
default_detail = "Request method must be POST."

Expand All @@ -54,23 +54,28 @@ class JSONParsingError(JSONRPCException):
"""
Exception raised when there is an error parsing the JSON request.
"""

code = -32700
default_detail = "Error parsing JSON."


class InvalidRPCRequest(JSONRPCException):
"""
Exception raised when the RPC request is missing required fields
Exception raised when the RPC request is missing required fields
or fields have invalid types.
"""

code = -32600
default_detail = "Required fields are missing or have invalid types in the RPC request."
default_detail = (
"Required fields are missing or have invalid types in the RPC request."
)


class MethodNotFound(JSONRPCException):
"""
Exception raised when the requested RPC method does not exist.
"""

code = -32601
default_detail = "Requested method not found."

Expand All @@ -79,6 +84,7 @@ class MissingRPCParameters(JSONRPCException):
"""
Exception raised when required parameters are missing in the request.
"""

code = -32602
default_detail = "Missing required fields in parameters."

Expand All @@ -88,6 +94,7 @@ class InternalSystemError(JSONRPCException):
Exception raised when an internal system error occurs.
Typically used for database or file system failures.
"""

code = -32603
default_detail = "System error due to internal failure."

Expand All @@ -96,6 +103,7 @@ class OperationCompletedSuccessfully(JSONRPCException):
"""
Exception raised to indicate that the operation was completed successfully.
"""

code = 0
default_detail = "Operation completed successfully."

Expand All @@ -104,6 +112,7 @@ class InsufficientFunds(JSONRPCException):
"""
Exception raised when a client has insufficient funds to cancel a payment.
"""

code = 77
default_detail = "Insufficient funds to cancel payment."

Expand All @@ -112,6 +121,7 @@ class ServiceTemporarilyUnavailable(JSONRPCException):
"""
Exception raised when the requested service is temporarily unavailable.
"""

code = 100
default_detail = "Service temporarily unavailable."

Expand All @@ -120,6 +130,7 @@ class QuotaExceeded(JSONRPCException):
"""
Exception raised when the user has exceeded their quota.
"""

code = 101
default_detail = "Quota exceeded."

Expand All @@ -128,6 +139,7 @@ class SystemErrorExc(JSONRPCException):
"""
Exception raised for a generic system error.
"""

code = 102
default_detail = "System error."

Expand All @@ -136,6 +148,7 @@ class UnknownError(JSONRPCException):
"""
Exception raised for an unknown error.
"""

code = 103
default_detail = "Unknown error."

Expand All @@ -144,6 +157,7 @@ class WalletNotIdentified(JSONRPCException):
"""
Exception raised when the wallet cannot be identified.
"""

code = 113
default_detail = "Wallet not identified."

Expand All @@ -161,6 +175,7 @@ class DailyLimitExceeded(JSONRPCException):
"""
Exception raised when the daily limit for an account is exceeded.
"""

code = 141
default_detail = "The daily limit is exceeded for this account."

Expand All @@ -169,6 +184,7 @@ class TransactionAlreadyExists(JSONRPCException):
"""
Exception raised when attempting to create a transaction that already exists.
"""

code = 201
default_detail = "Transaction already exists."

Expand All @@ -178,6 +194,7 @@ class TransactionAlreadyCancelled(JSONRPCException):
Exception raised when a transaction that is already cancelled
is attempted to be cancelled again.
"""

code = 202
default_detail = "Transaction already cancelled."

Expand All @@ -186,6 +203,7 @@ class TransactionNotFound(JSONRPCException):
"""
Exception raised when a requested transaction cannot be found.
"""

code = 203
default_detail = "Transaction not found."

Expand All @@ -194,6 +212,7 @@ class NumberDoesNotExist(JSONRPCException):
"""
Exception raised when a specified number does not exist.
"""

code = 301
default_detail = "Number does not exist."

Expand All @@ -202,6 +221,7 @@ class ClientNotFound(JSONRPCException):
"""
Exception raised when a specified client cannot be found.
"""

code = 302
default_detail = "Client not found."

Expand All @@ -210,6 +230,7 @@ class ProductNotFound(JSONRPCException):
"""
Exception raised when a specified product cannot be found.
"""

code = 304
default_detail = "Product not found."

Expand All @@ -218,6 +239,7 @@ class ServiceNotFound(JSONRPCException):
"""
Exception raised when a specified service cannot be found.
"""

code = 305
default_detail = "Service not found."

Expand All @@ -226,6 +248,7 @@ class RequiredParametersMissing(JSONRPCException):
"""
Exception raised when one or more required parameters are missing in the request.
"""

code = 411
default_detail = "One or more required parameters are missing."

Expand All @@ -234,6 +257,7 @@ class InvalidLoginOrPassword(JSONRPCException):
"""
Exception raised for invalid login or password.
"""

code = 412
default_detail = "Invalid login or password."

Expand All @@ -242,6 +266,7 @@ class InvalidAmount(JSONRPCException):
"""
Exception raised when the provided amount is invalid.
"""

code = 413
default_detail = "Invalid amount."

Expand All @@ -250,6 +275,7 @@ class InvalidDateTimeFormat(JSONRPCException):
"""
Exception raised when the provided date and time format is invalid.
"""

code = 414
default_detail = "Invalid date and time format."

Expand All @@ -258,6 +284,7 @@ class AmountExceedsLimit(JSONRPCException):
"""
Exception raised when the provided amount exceeds the allowed limit.
"""

code = 415
default_detail = "Amount exceeds the maximum limit."

Expand All @@ -266,6 +293,7 @@ class TransactionsProhibited(JSONRPCException):
"""
Exception raised when transactions are prohibited for a specific payer.
"""

code = 501
default_detail = "Transactions are prohibited for this payer."

Expand All @@ -274,6 +302,7 @@ class AccessDenied(JSONRPCException):
"""
Exception raised when access is denied to a resource or operation.
"""

code = 601
default_detail = "Access denied."

Expand All @@ -282,6 +311,7 @@ class InvalidCommandCode(JSONRPCException):
"""
Exception raised when an invalid command code is provided.
"""

code = 603
default_detail = "Invalid command code."

Expand Down
9 changes: 5 additions & 4 deletions paynet/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@
class PaynetTransaction(models.Model):
"""
Represents a transaction made by a user on a specific service.
"""

CREATED = 0
SUCCESSFUL = 1
CANCELLED = 2
NOT_FOUND = 3

STATUS_CHOICES = [
(SUCCESSFUL, 'Successfully completed'),
(CANCELLED, 'Cancelled transaction'),
(NOT_FOUND, 'Transaction not found'),
(SUCCESSFUL, "Successfully completed"),
(CANCELLED, "Cancelled transaction"),
(NOT_FOUND, "Transaction not found"),
]
amount = models.IntegerField()
account_id = models.BigIntegerField()
Expand Down
9 changes: 7 additions & 2 deletions paynet/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class PerformTransactionSerializer(serializers.Serializer):
"""
Serializer for performing a transaction
"""

amount = serializers.DecimalField(max_digits=10, decimal_places=2)
serviceId = serializers.IntegerField()
transactionId = serializers.IntegerField()
Expand All @@ -16,6 +17,7 @@ class CheckTransactionSerializer(serializers.Serializer):
"""
Serializer for check transaction
"""

serviceId = serializers.IntegerField()
transactionId = serializers.IntegerField()

Expand All @@ -24,6 +26,7 @@ class CancelTransactionSerializer(serializers.Serializer):
"""
Serializer for cancel transaction
"""

serviceId = serializers.IntegerField()
transactionId = serializers.IntegerField()

Expand All @@ -32,13 +35,15 @@ class GetStatementSerializer(serializers.Serializer):
"""
Serializer for get statement
"""

serviceId = serializers.IntegerField()
dateFrom = serializers.DateTimeField(format='%Y-%m-%d %H:%M:%S')
dateTo = serializers.DateTimeField(format='%Y-%m-%d %H:%M:%S')
dateFrom = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S")
dateTo = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S")


class ChangePasswordSerializer(serializers.Serializer):
"""
Serializer for change password
"""

newPassword = serializers.CharField(max_length=128)
Loading

0 comments on commit d6b81d4

Please sign in to comment.