from order.models import OrderModel
from product.models import DollorModel, CommentModel
from ticket.models import Ticket, ContactUsModel
from home.models import LearnVideoModel
from account.models import SecurityBreachAttemptModel

def admin_pending_count(request):
    pending_count = OrderModel.objects.filter(status='ADMIN_PENDING').count()
    return str(pending_count)

def dollor_price(request):
    dollor_object, _ = DollorModel.objects.get_or_create(unique_filed='unique')
    return str(dollor_object.price)[:3]

def comment_count(request):
    return CommentModel.objects.filter(review_status='not_reviwed').count()

def new_ticket_count(request):
    return Ticket.objects.filter(status__in=['open', 'in_progress']).count()

def new_learn_video_count(request):
    return LearnVideoModel.objects.filter(viewd=False).count()

def new_attck_count(request):
    return SecurityBreachAttemptModel.objects.filter(viewd=False).count()

def new_contact_us_count(request):
    return ContactUsModel.objects.filter(is_reviewed=False).count()

from django.contrib import admin, messages
from unfold.admin import ModelAdmin
from home.models import LearnVideoModel
from import_export.admin import ImportExportModelAdmin
from unfold.contrib.import_export.forms import ExportForm, ImportForm, SelectableFieldsExportForm
from unfold.decorators import action, display
from django.shortcuts import redirect,reverse
from django.contrib.contenttypes.models import ContentType


class ModelAdmin(ModelAdmin):
    actions_list = ['redirect_to_learn']
    @action(description=f"چگونگی استفاده این بخش")
    def redirect_to_learn(self, request):
        content_type = ContentType.objects.get_for_model(self.model)
        try:
            learn_video = LearnVideoModel.objects.get(
                content_type=content_type,
            )
            return redirect(reverse("admin:home_learnvideomodel_change", args=[learn_video.pk]))
        except Exception as e:
            messages.error(request, f"برای بخش {content_type} ویدیویی اپلود نشده است")
            return redirect("admin:home_learnvideomodel_changelist")