Files
atabackend/contact/views.py
Beyhan Oğur d50f14bcb1 first commit
2026-04-26 22:20:45 +03:00

43 lines
1.3 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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
)