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.
108 lines
4.5 KiB
108 lines
4.5 KiB
from django.contrib import admin
|
|
from django.utils.translation import gettext_lazy as _
|
|
from django.utils.html import format_html
|
|
from django.contrib import messages
|
|
|
|
from unfold.admin import ModelAdmin, StackedInline
|
|
from unfold.decorators import display
|
|
|
|
from apps.transaction.models import TransactionParticipant, ParticipantInfo
|
|
from apps.course.models import Participant
|
|
|
|
from utils.admin import project_admin_site
|
|
|
|
class ParticipantInfoInline(StackedInline):
|
|
model = ParticipantInfo
|
|
extra = 1
|
|
fields = ['fullname', 'email', 'phone_number', 'gender', 'birthdate']
|
|
# readonly_fields = ['email', 'phone_number']
|
|
classes = ['collapse']
|
|
tab = True
|
|
show_change_link = True
|
|
|
|
|
|
@admin.register(TransactionParticipant)
|
|
class TransactionParticipantAdmin(ModelAdmin):
|
|
list_display = ('user', 'course', 'payment_status', 'price_display', 'participant_status', 'created_at', 'updated_at')
|
|
list_filter = ('status', 'course', 'created_at')
|
|
search_fields = ('user__email', 'course__title')
|
|
readonly_fields = [ 'created_at', 'updated_at']
|
|
inlines = [ParticipantInfoInline]
|
|
autocomplete_fields = ['user',]
|
|
show_change_link = True
|
|
ordering = ('-created_at',)
|
|
|
|
fieldsets = (
|
|
(None, {
|
|
'fields': ('user', 'course', 'status', 'price')
|
|
}),
|
|
(_('Timestamps'), {
|
|
'fields': ('created_at', 'updated_at'),
|
|
'classes': ('collapse',)
|
|
}),
|
|
)
|
|
|
|
@display(description=_("Payment Status"), ordering="status")
|
|
def payment_status(self, obj):
|
|
if obj.status == 'success':
|
|
return format_html('<span class="unfold-badge unfold-badge--success">Paid</span>')
|
|
elif obj.status == 'failed':
|
|
return format_html('<span class="unfold-badge unfold-badge--danger">Failed</span>')
|
|
return format_html('<span class="unfold-badge unfold-badge--warning">Pending</span>')
|
|
|
|
@display(description=_("Price"), ordering="price")
|
|
def price_display(self, obj):
|
|
return format_html('<span class="unfold-badge unfold-badge--info">${}</span>', obj.price)
|
|
|
|
@display(description=_("Course Participant Status"))
|
|
def participant_status(self, obj):
|
|
"""نمایش وضعیت شرکتکننده در دوره"""
|
|
if obj.status == TransactionParticipant.TransactionStatus.SUCCESS:
|
|
participant_exists = Participant.objects.filter(
|
|
student=obj.user,
|
|
course=obj.course
|
|
).exists()
|
|
if participant_exists:
|
|
return format_html('<span class="unfold-badge unfold-badge--success">✓ Enrolled</span>')
|
|
else:
|
|
return format_html('<span class="unfold-badge unfold-badge--warning">⚠ Not Enrolled</span>')
|
|
else:
|
|
return format_html('<span class="unfold-badge unfold-badge--secondary">- Not Applicable</span>')
|
|
|
|
def save_model(self, request, obj, form, change):
|
|
"""Override save_model to show messages when participant is created"""
|
|
if change:
|
|
# Store the old status before saving
|
|
old_obj = TransactionParticipant.objects.get(pk=obj.pk)
|
|
old_status = old_obj.status
|
|
|
|
# Save the object
|
|
super().save_model(request, obj, form, change)
|
|
|
|
# Check if status changed to SUCCESS
|
|
if (old_status != TransactionParticipant.TransactionStatus.SUCCESS and
|
|
obj.status == TransactionParticipant.TransactionStatus.SUCCESS):
|
|
|
|
participant_exists = Participant.objects.filter(
|
|
student=obj.user,
|
|
course=obj.course
|
|
).exists()
|
|
|
|
if participant_exists:
|
|
messages.success(
|
|
request,
|
|
f"Transaction status updated to SUCCESS. User {obj.user.email} is now enrolled in course '{obj.course.title}'."
|
|
)
|
|
else:
|
|
messages.warning(
|
|
request,
|
|
f"Transaction status updated to SUCCESS, but there was an issue enrolling user {obj.user.email} in course '{obj.course.title}'. Please check the logs."
|
|
)
|
|
else:
|
|
super().save_model(request, obj, form, change)
|
|
|
|
def get_queryset(self, request):
|
|
# Filter out deleted transactions
|
|
return super().get_queryset(request).filter(is_deleted=False)
|
|
|
|
project_admin_site.register(TransactionParticipant, TransactionParticipantAdmin)
|