130 lines
4.5 KiB
Python
130 lines
4.5 KiB
Python
from decimal import Decimal
|
|
from django.db import transaction, models
|
|
from rest_framework import viewsets, status
|
|
from rest_framework.decorators import action
|
|
from rest_framework.response import Response
|
|
from rest_framework.permissions import IsAuthenticated
|
|
|
|
from .models import Account, Contact, Lead, Opportunity, Activity
|
|
from .serializers import (
|
|
AccountSerializer, ContactSerializer, LeadSerializer,
|
|
OpportunitySerializer, ActivitySerializer, LeadConvertSerializer
|
|
)
|
|
from .services import convert_lead
|
|
from openforce.permissions import ScopedOwnerAccessPermission
|
|
|
|
|
|
class ScopedQuerySetMixin:
|
|
permission_classes = [IsAuthenticated, ScopedOwnerAccessPermission]
|
|
|
|
def get_queryset(self):
|
|
qs = super().get_queryset()
|
|
user = self.request.user
|
|
if not user or not user.is_authenticated:
|
|
return qs.none()
|
|
if user.is_superuser or user.is_staff:
|
|
return qs
|
|
if hasattr(self.queryset.model, 'owner'):
|
|
return qs.filter(owner=user)
|
|
if hasattr(self.queryset.model, 'assigned_to'):
|
|
return qs.filter(assigned_to=user)
|
|
return qs
|
|
|
|
|
|
class AccountViewSet(ScopedQuerySetMixin, viewsets.ModelViewSet):
|
|
queryset = Account.objects.all()
|
|
serializer_class = AccountSerializer
|
|
|
|
def perform_create(self, serializer):
|
|
with transaction.atomic():
|
|
serializer.save(owner=self.request.user)
|
|
|
|
def perform_update(self, serializer):
|
|
with transaction.atomic():
|
|
serializer.save()
|
|
|
|
|
|
class ContactViewSet(ScopedQuerySetMixin, viewsets.ModelViewSet):
|
|
queryset = Contact.objects.all()
|
|
serializer_class = ContactSerializer
|
|
|
|
def perform_create(self, serializer):
|
|
with transaction.atomic():
|
|
serializer.save(owner=self.request.user)
|
|
|
|
def perform_update(self, serializer):
|
|
with transaction.atomic():
|
|
serializer.save()
|
|
|
|
|
|
class LeadViewSet(ScopedQuerySetMixin, viewsets.ModelViewSet):
|
|
queryset = Lead.objects.all()
|
|
serializer_class = LeadSerializer
|
|
|
|
def perform_create(self, serializer):
|
|
with transaction.atomic():
|
|
serializer.save(owner=self.request.user)
|
|
|
|
def perform_update(self, serializer):
|
|
with transaction.atomic():
|
|
serializer.save()
|
|
|
|
@action(detail=True, methods=['post'], url_path='convert')
|
|
def convert(self, request, pk=None):
|
|
lead = self.get_object()
|
|
serializer = LeadConvertSerializer(data=request.data)
|
|
serializer.is_valid(raise_exception=True)
|
|
validated = serializer.validated_data
|
|
|
|
account_name = validated.get('account_name') or lead.company
|
|
create_opportunity = validated.get('create_opportunity', False)
|
|
opportunity_name = validated.get('opportunity_name') or f"{account_name} - Opportunity"
|
|
opportunity_amount = validated.get('opportunity_amount')
|
|
opportunity_close_date = validated.get('opportunity_close_date')
|
|
|
|
with transaction.atomic():
|
|
result = convert_lead(
|
|
lead=lead,
|
|
account_name=account_name,
|
|
create_opportunity=create_opportunity,
|
|
opportunity_name=opportunity_name,
|
|
opportunity_amount=opportunity_amount,
|
|
opportunity_close_date=opportunity_close_date,
|
|
user=request.user
|
|
)
|
|
|
|
return Response({
|
|
'account': result['account'].id,
|
|
'contact': result['contact'].id,
|
|
'opportunity': result['opportunity'].id if result['opportunity'] else None,
|
|
'account_id': result['account'].id,
|
|
'contact_id': result['contact'].id,
|
|
'opportunity_id': result['opportunity'].id if result['opportunity'] else None,
|
|
'status': 'converted'
|
|
}, status=status.HTTP_200_OK)
|
|
|
|
|
|
class OpportunityViewSet(ScopedQuerySetMixin, viewsets.ModelViewSet):
|
|
queryset = Opportunity.objects.all()
|
|
serializer_class = OpportunitySerializer
|
|
|
|
def perform_create(self, serializer):
|
|
with transaction.atomic():
|
|
serializer.save(owner=self.request.user)
|
|
|
|
def perform_update(self, serializer):
|
|
with transaction.atomic():
|
|
serializer.save()
|
|
|
|
|
|
class ActivityViewSet(ScopedQuerySetMixin, viewsets.ModelViewSet):
|
|
queryset = Activity.objects.all()
|
|
serializer_class = ActivitySerializer
|
|
|
|
def perform_create(self, serializer):
|
|
with transaction.atomic():
|
|
serializer.save(assigned_to=self.request.user)
|
|
|
|
def perform_update(self, serializer):
|
|
with transaction.atomic():
|
|
serializer.save()
|