from django.contrib.auth import get_user_model
from django.core.mail import send_mail
from django.contrib import admin

from .models import SupportRequest



@admin.register(SupportRequest)
class SupportAdmin(admin.ModelAdmin):
    list_display = ('email', 'respondent', 'is_answered')

    def save_model(self, request, obj, form, change) -> None:
        
        try:
            obj.user = get_user_model().objects.get(email=obj.email)
        except get_user_model().DoesNotExist:
            pass

        obj.respondent = request.user
        if obj.response_text:
            obj.is_answered = True
            send_mail(
                subject='Subject here',
                message=obj.response_text,
                from_email='<EMAIL>',
                recipient_list=[obj.email],
                fail_silently=False,
            )

        return super().save_model(request, obj, form, change)

from django.contrib.auth import get_user_model
from django.core.mail import send_mail
from django.contrib import admin

from .models import SupportRequest



@admin.register(SupportRequest)
class SupportAdmin(admin.ModelAdmin):
    list_display = ('email', 'respondent', 'is_answered')

    def save_model(self, request, obj, form, change) -> None:
        
        try:
            obj.user = get_user_model().objects.get(email=obj.email)
        except get_user_model().DoesNotExist:
            pass

        obj.respondent = request.user
        if obj.response_text:
            obj.is_answered = True
            send_mail(
                subject='Subject here',
                message=obj.response_text,
                from_email='<EMAIL>',
                recipient_list=[obj.email],
                fail_silently=False,
            )

        return super().save_model(request, obj, form, change)
