Authentication system, online and real-time chat, SMS & BANK system for Iranian SMS services.
Project description
django-chelseru Django Package
Overview
django-chelseru is a Django package developed by Sobhan Bahman Rashnu for real-time chatting via WebSocket, OTP-based SMS verification, and SMS sending with Iranian providers. It also supports payment integrations.
Useful for building applications requiring secure authentication, messaging, real-time chat, and online payments.
Features
- Authentication: OTP and PASSWD methods using rest_framework_simplejwt.
- SMS Services: PARSIAN_WEBCO_IR, MELI_PAYAMAK_COM, KAVENEGAR_COM.
- Payment Gateways: PAYPING_IR, ZARINPAL_COM.
- Real-time Chat: WebSocket/Channels for messaging.
- Session management and API endpoints for OTP, authentication, SMS, payments, and chat.
Requirements
- Python 3.8+
- Django 4.x
- django-rest-framework
- djangorestframework-simplejwt
- django-channels
- user-agents
- requests
- Other dependencies: See
requirements.txt
Installation
-
Install the package:
pip install django-chelseru
-
Add to
INSTALLED_APPSinsettings.py:INSTALLED_APPS = [ ... 'rest_framework', 'rest_framework_simplejwt', 'channels', 'drfchelseru', ]
-
Configure middleware and ASGI in
settings.py. -
Apply migrations:
python manage.py makemigrations python manage.py migrate
-
Run the server (use ASGI for WebSocket):
daphne -b 0.0.0.0 -p 8000 project.asgi:application
Project Structure
- models.py: Defines models for users, OTP codes, sessions, SMS messages, organizations, chat rooms, chat permissions, messages, wallets, and payments.
- views.py: Handles API endpoints for OTP authentication, SMS, payments, and chat.
- urls.py: Maps URL patterns to views for API routes.
- consumers.py: Implements WebSocket consumer for real-time chat.
- middlewares.py: Includes
TakeUserSessionMiddlawarefor session management andJWTAuthMiddlewarefor WebSocket authentication.
Models
- User: Extends Django's User model with mobile number and group.
- OTPCode: Stores OTP codes for authentication with expiration logic.
- Session: Tracks user sessions with IP, device, and browser info.
- MessageSMS: Logs SMS messages sent to users.
- Organization: Represents organizations owned by users.
- ChatRoomPermissions: Defines access levels for chat room actions.
- ChatRoom: Manages chat rooms with users, status, and permissions.
- MessageChat: Stores chat messages with sender and timestamp.
- Wallet: Tracks user wallet balances for payments.
- Payment: Manages payment transactions with gateway integration.
Configuration
Update your Django settings.py:
MIDDLEWARE = [
...
'drfchelseru.middlewares.TakeUserSessionMiddlaware',
]
ASGI_APPLICATION = 'yourproject.asgi.application'
CHANNEL_LAYERS = {
'default': {
'BACKEND': 'channels_redis.core.RedisChannelLayer',
'CONFIG': {
'hosts': [('127.0.0.1', 6379)],
},
},
}
DJANGO_CHELSERU = {
'AUTH': {
'AUTH_METHOD': 'OTP',
'AUTH_SERVICE': 'rest_framework_simplejwt',
'OPTIONS': {
'OTP_LENGTH': 8,
'OTP_EXPIRE_PER_MINUTES': 4,
'OTP_SMS_TEMPLATE_ID': 5,
}
},
'SMS': {
'SMS_SERVICE': 'PARSIAN_WEBCO_IR',
'SETTINGS': {
'PARSIAN_WEBCO_IR_API_KEY': '',
'MELI_PAYAMAK_COM_USERNAME': '',
'MELI_PAYAMAK_COM_PASSWORD': '',
'MELI_PAYAMAK_COM_FROM': '',
'KAVENEGAR_COM_API_KEY': '',
'KAVENEGAR_COM_FROM': '',
},
'TEMPLATES': {
'T1': 1,
'T2': 2,
'T3': 3,
}
},
'BANK': {
'GATEWAY': 'PAYPING_IR',
'SETTINGS': {
'MERCHANT_ID': '',
'CALLBACK_URL': '',
'CURRENCY': 'IRT'
}
}
}
API Endpoints
-
POST /drfchelseru/message/send/: Send SMS.
- Request:
{ "mobile_number": "09211892425", "message_text": "Hello", "template_id": 1 } - Response:
{ "details": "The Message was sent correctly." }(200 OK)
- Request:
-
POST /drfchelseru/otp/send/: Request OTP code.
- Request:
{ "mobile_number": "09211892425" } - Response:
{ "details": "The OTP code was sent correctly." }(200 OK)
- Request:
-
POST /drfchelseru/authenticate/: Authenticate with OTP.
- Request:
{ "mobile_number": "09211892425", "code": "652479", "group": 0 } - Response:
{ "access": "<access_token>", "refresh": "<refresh_token>" }(200 OK)
- Request:
-
GET /drfchelseru/sessions/: List user sessions (authenticated users only).
- Headers:
Authorization: Bearer <access_token> - Response: List of sessions (200 OK)
- Headers:
-
POST /drfchelseru/payment/create/: Initiate payment.
- Request:
{ "order_id": "123", "amount": 1000.0, "description": "Test payment", "callback_url": "http://example.com/callback", "mobile": "09211892425", "email": "user@example.com", "currency": "IRT" } - Response:
{ "details": { "gateway_url": "<url>", ... } }(200 OK)
- Request:
-
GET/POST /drfchelseru/payment/verify/: Verify payment.
- GET Query:
?Authority=<authority>&Status=OK - POST Body:
{ "paymentCode": "<code>", "paymentRefId": "<refid>", ... } - Response:
{ "details": { "is_pay": 1, ... } }(200 OK)
- GET Query:
-
/drfchelseru/chat/chatrooms/: CRUD for chat rooms (authenticated users only).
- POST Request:
{ "user": 2 }(to create room with user ID 2) - Headers:
Authorization: Bearer <access_token> - Response: Chat room details (201 Created)
- POST Request:
-
/drfchelseru/chat/messages/: CRUD for chat messages (authenticated users only).
- POST Request:
{ "chat_room": 1, "text": "Hello" } - GET Query:
?chat_room=1 - Headers:
Authorization: Bearer <access_token> - Response: Message details or list (200 OK / 201 Created)
- POST Request:
WebSocket Usage
-
Connect to a chat room for real-time messaging:
wscat -c wss://<your-domain>/drfchelseru/chat/<chat_room_id>/?token=<jwt_token>
- Example token:
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoxNzYxMDE5NTY3LCJpYXQiOjE3NTU4MzU1NjcsImp0aSI6IjhhYTY1Y2I3ZDhmMjRmMzliYjFmNDFkZmJiYjcyYmVmIiwidXNlcl9pZCI6Mzl9.mHmIjbTl3X1cd3Ky5HFCD6gy4kGxMVcActo9JXtT9JQ - Replace
<chat_room_id>(e.g., 17) with the desired room ID and provide a valid JWT token.
- Example token:
Middleware
- TakeUserSessionMiddlaware: Logs user session data (IP, user agent, device, browser) for HTTP requests.
- JWTAuthMiddleware: Authenticates WebSocket connections using JWT tokens.
Usage
- Access the API at
http://<your-domain>/drfchelseru/. - Connect to WebSocket at
ws://<your-domain>/drfchelseru/chat/<chat_room_id>/?token=<jwt_token>. - Admin panel: Create a superuser with
python manage.py createsuperuser.
Contributing
- Fork the repository.
- Create a new branch (
git checkout -b feature-branch). - Commit your changes (
git commit -m "Add feature"). - Push to the branch (
git push origin feature-branch). - Open a pull request.
License
MIT License
django-chelseru پکیج جنگو
بررسی اجمالی
django-chelseru یک پکیج جنگو است که توسط سبحان بهمن رشنو توسعه یافته برای گفتگوهای همزمان از راه وبسوکت، تایید پیامکی بر پایه OTP، و فرستادن پیامک با فراهمکنندگان ایرانی. همچنین از یکپارچگی پرداخت پشتیبانی میکند.
کاربردی برای ساخت برنامههایی که نیاز به تایید امن، پیامرسانی، گفتگو همزمان، و پرداخت برخط دارند.
ویژگیها
- تایید: روشهای OTP و PASSWD با بهرهگیری از rest_framework_simplejwt.
- خدمات پیامک: PARSIAN_WEBCO_IR، MELI_PAYAMAK_COM، KAVENEGAR_COM.
- درگاههای پرداخت: PAYPING_IR، ZARINPAL_COM.
- گفتگو همزمان: وبسوکت/کانالها برای پیامرسانی.
- مدیریت نشست و نقاط پایانی API برای OTP، تایید، پیامک، پرداختها، و گفتگو.
نیازمندیها
- Python 3.8+
- Django 4.x
- django-rest-framework
- djangorestframework-simplejwt
- django-channels
- user-agents
- requests
- دیگر وابستگیها: ببینید
requirements.txt
نصب
-
پکیج را نصب کنید:
pip install django-chelseru
-
به
INSTALLED_APPSدرsettings.pyبیفزایید:INSTALLED_APPS = [ ... 'rest_framework', 'rest_framework_simplejwt', 'channels', 'drfchelseru', ]
-
میانگیر و ASGI را در
settings.pyپیکربندی کنید. -
مهاجرتها را اعمال کنید:
python manage.py makemigrations python manage.py migrate
-
کارساز را اجرا کنید (برای وبسوکت از ASGI بهره ببرید):
daphne -b 0.0.0.0 -p 8000 project.asgi:application
ساختار پروژه
- models.py: مدلها را برای کاربران، کدهای OTP، نشستها، پیامکها، سازمانها، اتاقهای گفتگو، دسترسیهای اتاق گفتگو، پیامها، کیفپولها، و پرداختها تعریف میکند.
- views.py: نقاط پایانی API برای تایید OTP، پیامک، پرداختها، و گفتگو را مدیریت میکند.
- urls.py: الگوهای URL را به نماها برای مسیرهای API نگاشت میکند.
- consumers.py: مصرفکننده وبسوکت را برای گفتگو همزمان پیادهسازی میکند.
- middlewares.py: شامل
TakeUserSessionMiddlawareبرای مدیریت نشست وJWTAuthMiddlewareبرای تایید وبسوکت.
مدلها
- User: مدل کاربر جنگو را با شماره همراه و گروه گسترش میدهد.
- OTPCode: کدهای OTP را برای تایید با منطق انقضا ذخیره میکند.
- Session: نشستهای کاربر را با IP، دستگاه، و دادههای مرورگر پیگیری میکند.
- MessageSMS: پیامکهای فرستادهشده به کاربران را ثبت میکند.
- Organization: سازمانهایی را که مالک کاربران هستند نشان میدهد.
- ChatRoomPermissions: سطوح دسترسی برای کارهای اتاق گفتگو را تعریف میکند.
- ChatRoom: اتاقهای گفتگو را با کاربران، وضعیت، و دسترسیها مدیریت میکند.
- MessageChat: پیامهای گفتگو را با فرستنده و زماننگار ذخیره میکند.
- Wallet: مانده کیفپول کاربران را برای پرداختها پیگیری میکند.
- Payment: تراکنشهای پرداخت را با یکپارچگی درگاه مدیریت میکند.
پیکربندی
settings.py جنگو را بهروزرسانی کنید:
MIDDLEWARE = [
...
'drfchelseru.middlewares.TakeUserSessionMiddlaware',
]
ASGI_APPLICATION = 'yourproject.asgi.application'
CHANNEL_LAYERS = {
'default': {
'BACKEND': 'channels_redis.core.RedisChannelLayer',
'CONFIG': {
'hosts': [('127.0.0.1', 6379)],
},
},
}
DJANGO_CHELSERU = {
'AUTH': {
'AUTH_METHOD': 'OTP',
'AUTH_SERVICE': 'rest_framework_simplejwt',
'OPTIONS': {
'OTP_LENGTH': 8,
'OTP_EXPIRE_PER_MINUTES': 4,
'OTP_SMS_TEMPLATE_ID': 5,
}
},
'SMS': {
'SMS_SERVICE': 'PARSIAN_WEBCO_IR',
'SETTINGS': {
'PARSIAN_WEBCO_IR_API_KEY': '',
'MELI_PAYAMAK_COM_USERNAME': '',
'MELI_PAYAMAK_COM_PASSWORD': '',
'MELI_PAYAMAK_COM_FROM': '',
'KAVENEGAR_COM_API_KEY': '',
'KAVENEGAR_COM_FROM': '',
},
'TEMPLATES': {
'T1': 1,
'T2': 2,
'T3': 3,
}
},
'BANK': {
'GATEWAY': 'PAYPING_IR',
'SETTINGS': {
'MERCHANT_ID': '',
'CALLBACK_URL': '',
'CURRENCY': 'IRT'
}
}
}
نقاط پایانی API
-
POST /drfchelseru/message/send/: فرستادن پیامک.
- درخواست:
{ "mobile_number": "09211892425", "message_text": "Hello", "template_id": 1 } - پاسخ:
{ "details": "The Message was sent correctly." }(200 OK)
- درخواست:
-
POST /drfchelseru/otp/send/: درخواست کد OTP.
- درخواست:
{ "mobile_number": "09211892425" } - پاسخ:
{ "details": "The OTP code was sent correctly." }(200 OK)
- درخواست:
-
POST /drfchelseru/authenticate/: تایید با OTP.
- درخواست:
{ "mobile_number": "09211892425", "code": "652479", "group": 0 } - پاسخ:
{ "access": "<access_token>", "refresh": "<refresh_token>" }(200 OK)
- درخواست:
-
GET /drfchelseru/sessions/: فهرست نشستهای کاربر (فقط کاربران تاییدشده).
- سرآیندها:
Authorization: Bearer <access_token> - پاسخ: فهرست نشستها (200 OK)
- سرآیندها:
-
POST /drfchelseru/payment/create/: آغاز پرداخت.
- درخواست:
{ "order_id": "123", "amount": 1000.0, "description": "Test payment", "callback_url": "http://example.com/callback", "mobile": "09211892425", "email": "user@example.com", "currency": "IRT" } - پاسخ:
{ "details": { "gateway_url": "<url>", ... } }(200 OK)
- درخواست:
-
GET/POST /drfchelseru/payment/verify/: تایید پرداخت.
- جستجوی GET:
?Authority=<authority>&Status=OK - بدنه POST:
{ "paymentCode": "<code>", "paymentRefId": "<refid>", ... } - پاسخ:
{ "details": { "is_pay": 1, ... } }(200 OK)
- جستجوی GET:
-
/drfchelseru/chat/chatrooms/: CRUD برای اتاقهای گفتگو (فقط کاربران تاییدشده).
- درخواست POST:
{ "user": 2 }(برای ساخت اتاق با شناسه کاربر 2) - سرآیندها:
Authorization: Bearer <access_token> - پاسخ: جزئیات اتاق گفتگو (201 Created)
- درخواست POST:
-
/drfchelseru/chat/messages/: CRUD برای پیامهای گفتگو (فقط کاربران تاییدشده).
- درخواست POST:
{ "chat_room": 1, "text": "Hello" } - جستجوی GET:
?chat_room=1 - سرآیندها:
Authorization: Bearer <access_token> - پاسخ: جزئیات پیام یا فهرست (200 OK / 201 Created)
- درخواست POST:
بهرهگیری از وبسوکت
-
پیوستن به اتاق گفتگو برای پیامرسانی همزمان:
wscat -c wss://<your-domain>/drfchelseru/chat/<chat_room_id>/?token=<jwt_token>
- نمونه نشانه:
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoxNzYxMDE5NTY3LCJpYXQiOjE3NTU4MzU1NjcsImp0aSI6IjhhYTY1Y2I3ZDhmMjRmMzliYjFmNDFkZmJiYjcyYmVmIiwidXNlcl9pZCI6Mzl9.mHmIjbTl3X1cd3Ky5HFCD6gy4kGxMVcActo9JXtT9JQ - <chat_room_id> (مانند 17) را با شناسه اتاق دلخواه جایگزین کنید و نشانه JWT معتبر فراهم کنید.
- نمونه نشانه:
میانگیرها
- TakeUserSessionMiddlaware: دادههای نشست کاربر (IP، عامل کاربر، دستگاه، مرورگر) را برای درخواستهای HTTP ثبت میکند.
- JWTAuthMiddleware: اتصالهای وبسوکت را با نشانههای JWT تایید میکند.
بهرهگیری
- به API در
http://<your-domain>/drfchelseru/دسترسی یابید. - به وبسوکت در
ws://<your-domain>/drfchelseru/chat/<chat_room_id>/?token=<jwt_token>وصل شوید. - پنل مدیر: ابرکاربر را با
python manage.py createsuperuserبسازید.
همکاری
- مخزن را فورک کنید.
- شاخه نو بسازید (
git checkout -b feature-branch). - تغییرات را کامیت کنید (
git commit -m "Add feature"). - به شاخه بفرستید (
git push origin feature-branch). - درخواست کشیدن باز کنید.
پروانه
MIT License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_chelseru-3.0.1.tar.gz.
File metadata
- Download URL: django_chelseru-3.0.1.tar.gz
- Upload date:
- Size: 36.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f6b0b015ddfdc8a3933e76416e09262d9e9f931ef89e147336b3d62b32548d91
|
|
| MD5 |
a1ce1f08f8ac16fe2c7a0696f851b333
|
|
| BLAKE2b-256 |
7c3b8ae4df9f41ec59ddbb65702784e7a46b4e520844a97503dfe1e7b945362e
|
File details
Details for the file django_chelseru-3.0.1-py3-none-any.whl.
File metadata
- Download URL: django_chelseru-3.0.1-py3-none-any.whl
- Upload date:
- Size: 51.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
388b777426997a39f525824b023b53d1d84b3b4ac21e0d21a67051351ab55843
|
|
| MD5 |
6f2463ee06d0b29a08610746a30456a8
|
|
| BLAKE2b-256 |
eab7970c115ccd1d18daace97d1349aeb6ed0b90f3bd2f7481c847a4738fd53a
|