You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
190 lines
7.3 KiB
190 lines
7.3 KiB
"""
|
|
URL configuration for backend project.
|
|
|
|
The `urlpatterns` list routes URLs to views. For more information please see:
|
|
https://docs.djangoproject.com/en/5.0/topics/http/urls/
|
|
Examples:
|
|
Function views
|
|
1. Add an import: from my_app import views
|
|
2. Add a URL to urlpatterns: path('', views.home, name='home')
|
|
Class-based views
|
|
1. Add an import: from other_app.views import Home
|
|
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
|
|
Including another URLconf
|
|
1. Import the include() function: from django.urls import include, path
|
|
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
|
|
"""
|
|
from django.contrib import admin
|
|
from django.urls import path, include, re_path
|
|
from django.conf import settings
|
|
from django.conf.urls.static import static
|
|
from django.conf.urls.i18n import i18n_patterns
|
|
from django.contrib.admin.views.decorators import staff_member_required
|
|
from utils import UploadTmpMedia, UploadChatMedia
|
|
from django.http import JsonResponse
|
|
from django.shortcuts import render
|
|
from django.views.decorators.csrf import csrf_exempt
|
|
from rest_framework.decorators import api_view
|
|
from rest_framework.response import Response
|
|
|
|
from utils import absolute_url
|
|
|
|
from utils.admin import project_admin_site, HomeView ,dovoodi_admin_site
|
|
|
|
from drf_yasg.views import get_schema_view
|
|
from drf_yasg import openapi
|
|
from rest_framework import permissions
|
|
import requests
|
|
from filer import views
|
|
|
|
# Import custom API views
|
|
from apps.api.views import CustomAPIDocumentationView, CustomSwaggerView, SwaggerTokenAuthView, clear_swagger_auth ,admin_dashboard, ProfessorDashboardStatsView
|
|
from apps.api.permissions import IsAdminOrSwaggerToken
|
|
from apps.api.decorators import swagger_auth_required
|
|
|
|
# Restricted schema view for admin users and authenticated swagger users
|
|
schema_view = get_schema_view(
|
|
openapi.Info(
|
|
title="Imam Javad API",
|
|
default_version='v1',
|
|
description="Comprehensive API documentation for the Imam Javad educational platform",
|
|
contact=openapi.Contact(email="contact@imamjavad.com"),
|
|
license=openapi.License(name="MIT License"),
|
|
),
|
|
public=False,
|
|
permission_classes=(IsAdminOrSwaggerToken,),
|
|
)
|
|
|
|
|
|
def oneapi_translate(request):
|
|
dist_lang = request.GET.get('dist_lang')
|
|
q = request.GET.get('q')
|
|
url = f"https://one-api.ir/translate/?token=169700:6485a38c34b00&action=google&lang={dist_lang}&q={q}"
|
|
try:
|
|
data = requests.get(url).json()
|
|
except Exception as e:
|
|
data = {}
|
|
|
|
return JsonResponse(data)
|
|
|
|
|
|
api_patterns = [
|
|
path('list/', include('apps.api.urls')),
|
|
|
|
path('account/', include('apps.account.urls')),
|
|
path('courses/', include('apps.course.urls')),
|
|
path('quiz/', include('apps.quiz.urls')),
|
|
path('transaction/', include('apps.transaction.urls')),
|
|
path('certificates/', include('apps.certificate.urls')),
|
|
path('hadis/', include('apps.hadis.urls')),
|
|
path('library/', include('apps.library.urls')),
|
|
path('videos/', include('apps.video.urls')),
|
|
path('article/', include('apps.article.urls')),
|
|
path('podcast/', include('apps.podcast.urls')),
|
|
path('agent/', include('apps.agent.urls')),
|
|
path('bookmarks/', include('apps.bookmark.urls')),
|
|
path('calendar/', include('apps.dobodbi_calendar.urls')),
|
|
path('blog/', include('apps.blog.urls')),
|
|
path('admin/dashboard-stats/' , admin_dashboard.AdminDashboardStatsView.as_view()),
|
|
path('professor/dashboard-stats/' , ProfessorDashboardStatsView.as_view()),
|
|
|
|
path('settings/', include('dynamic_preferences.urls')),
|
|
|
|
path('upload-chat-media/', UploadChatMedia.as_view()), # دائمی در /media/chat/
|
|
path('upload-tmp-media/', UploadTmpMedia.as_view()), # موقت در /static/tmp/
|
|
|
|
]
|
|
|
|
|
|
# Base URL patterns (common to all domains)
|
|
# These patterns are shared by both Imam Javad and Dovoodi sites
|
|
|
|
def trigger_error(request):
|
|
division_by_zero = 1 / 0
|
|
|
|
urlpatterns = [
|
|
path("admin/", HomeView.as_view(), name="home"), # Redirect to appropriate admin based on domain
|
|
path("i18n/", include("django.conf.urls.i18n")),
|
|
path('api/', include(api_patterns)),
|
|
path('oneapi-translation/', oneapi_translate),
|
|
path('admin/filer/', include('filer.urls')),
|
|
path('filer/', include('filer.urls')),
|
|
path('sentry-debug/', trigger_error),
|
|
]
|
|
|
|
# Protected swagger URL patterns (to be used in domain-specific configs)
|
|
swagger_urlpatterns = [
|
|
path('swagger-auth/', SwaggerTokenAuthView.as_view(), name='swagger-token-auth'),
|
|
path('swagger-auth/clear/', clear_swagger_auth, name='clear-swagger-auth'),
|
|
re_path(r'^swagger(?P<format>\.json|\.yaml)$',
|
|
swagger_auth_required(schema_view.without_ui(cache_timeout=0)),
|
|
name='schema-json'),
|
|
path('swagger/', CustomSwaggerView.as_view(), name='schema-swagger-ui'),
|
|
re_path(r'^redoc/$',
|
|
swagger_auth_required(schema_view.with_ui('redoc', cache_timeout=0)),
|
|
name='schema-redoc'),
|
|
]
|
|
|
|
if settings.DEBUG:
|
|
import os
|
|
import re
|
|
import mimetypes
|
|
from django.http import StreamingHttpResponse, HttpResponse, Http404
|
|
from django.views.static import serve as django_static_serve
|
|
|
|
def ranged_static_serve(request, path, document_root=None, **kwargs):
|
|
fullpath = os.path.join(document_root, path)
|
|
if not os.path.exists(fullpath) or os.path.isdir(fullpath):
|
|
raise Http404("File not found")
|
|
|
|
range_header = request.META.get('HTTP_RANGE', '').strip()
|
|
if not range_header:
|
|
return django_static_serve(request, path, document_root=document_root, **kwargs)
|
|
|
|
size = os.path.getsize(fullpath)
|
|
content_type, encoding = mimetypes.guess_type(fullpath)
|
|
content_type = content_type or 'application/octet-stream'
|
|
|
|
match = re.match(r'bytes=(\d+)-(\d*)', range_header)
|
|
if not match:
|
|
return HttpResponse("Invalid Range Header", status=400)
|
|
|
|
first_byte, last_byte = match.groups()
|
|
first_byte = int(first_byte) if first_byte else 0
|
|
last_byte = int(last_byte) if last_byte else size - 1
|
|
|
|
if first_byte >= size:
|
|
return HttpResponse("Requested Range Not Satisfiable", status=416)
|
|
|
|
if last_byte >= size:
|
|
last_byte = size - 1
|
|
|
|
length = last_byte - first_byte + 1
|
|
|
|
def file_iterator(file_path, offset, bytes_to_read, chunk_size=8192):
|
|
with open(file_path, 'rb') as f:
|
|
f.seek(offset)
|
|
remaining = bytes_to_read
|
|
while remaining > 0:
|
|
to_read = min(chunk_size, remaining)
|
|
data = f.read(to_read)
|
|
if not data:
|
|
break
|
|
yield data
|
|
remaining -= len(data)
|
|
|
|
response = StreamingHttpResponse(
|
|
file_iterator(fullpath, first_byte, length),
|
|
status=206,
|
|
content_type=content_type
|
|
)
|
|
response['Content-Range'] = f'bytes {first_byte}-{last_byte}/{size}'
|
|
response['Content-Length'] = str(length)
|
|
response['Accept-Ranges'] = 'bytes'
|
|
return response
|
|
|
|
urlpatterns += [
|
|
re_path(r'^%s(?P<path>.*)$' % re.escape(settings.MEDIA_URL.lstrip('/')), ranged_static_serve, {'document_root': settings.MEDIA_ROOT}),
|
|
]
|
|
|
|
# Force reload trigger
|