first commit
This commit is contained in:
42
contact/views.py
Normal file
42
contact/views.py
Normal file
@@ -0,0 +1,42 @@
|
||||
from rest_framework.generics import CreateAPIView
|
||||
from rest_framework.permissions import AllowAny
|
||||
|
||||
from contact.models import Contact
|
||||
from contact.serializers import ContactSerializer
|
||||
from contact.tasks import send_contact_email
|
||||
|
||||
|
||||
# Create your views here.
|
||||
class ContactCreate(CreateAPIView):
|
||||
queryset = Contact.objects.all()
|
||||
serializer_class = ContactSerializer
|
||||
permission_classes = [AllowAny] # Herkes contact gönderebilir
|
||||
|
||||
def get_client_ip(self, request):
|
||||
"""İstemcinin IP adresini al"""
|
||||
x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
|
||||
if x_forwarded_for:
|
||||
ip = x_forwarded_for.split(',')[0]
|
||||
else:
|
||||
ip = request.META.get('REMOTE_ADDR')
|
||||
return ip
|
||||
|
||||
def perform_create(self, serializer):
|
||||
# IP adresini al
|
||||
ip_address = self.get_client_ip(self.request)
|
||||
|
||||
# Kullanıcı varsa kaydet, yoksa None olarak kaydet
|
||||
user = self.request.user if self.request.user.is_authenticated else None
|
||||
|
||||
# Contact'ı kaydet
|
||||
contact = serializer.save(user=user, ip=ip_address)
|
||||
|
||||
# Celery task ile email gönder (arka planda)
|
||||
send_contact_email.delay(
|
||||
name=contact.name,
|
||||
email=contact.email,
|
||||
subject=contact.subject,
|
||||
message=contact.message,
|
||||
ip=ip_address
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user