def get_queryset(self): try: data = self.request.query_params lead = Lead.objects.get(id=data['lead']) opportunities = Opportunity.objects.filter( id=data.get('opportunity_id')) if 'suminsured' in data and not opportunities.exists(): raise mixins.APIException(Constants.OPPORTUNITY_ID_REQUIRED) if opportunities.exists(): opportunity = opportunities.get() if 'suminsured' in data: category_oppor = opportunity.category_opportunity category_oppor.predicted_suminsured = data['suminsured'] category_oppor.save() opportunity.refresh_from_db() queryset = opportunity.get_quotes() else: queryset = lead.get_quotes() if not queryset.exists(): raise mixins.NotFound(Constants.NO_QUOTES_FOUND) return queryset except (KeyError, Lead.DoesNotExist): raise mixins.APIException(Constants.LEAD_ERROR) except ValueError: raise mixins.APIException(Constants.INVALID_INPUT)
def get_queryset(self): try: data = self.request.query_params Lead.objects.get(id=data['lead']) opportunities = Opportunity.objects.filter( id=data.get('opportunity_id'), lead_id=data['lead']) if not opportunities.exists(): raise mixins.APIException(Constants.OPPORTUNITY_DOES_NOT_EXIST) with transaction.atomic(): opportunity = opportunities.get() if data.get('suminsured'): opportunity = opportunities.get() opportunity.category_opportunity.update_fields( **dict(predicted_suminsured=self.request. query_params['suminsured'])) opportunity.refresh_from_db() return opportunity.get_recommendated_quotes() except (KeyError, Lead.DoesNotExist): raise mixins.APIException(Constants.LEAD_ERROR) except ValueError: raise mixins.APIException(Constants.INVALID_INPUT) except IntegrityError: pass raise mixins.APIException( 'Curently we are unable to suggest any quote. please try again.')
def get_serializer_class(self): serializer_cls = self.insurance_serializer_classes.get( self.application_type) if serializer_cls: return serializer_cls raise mixins.APIException(constants.APPLICATION_UNMAPPED)
def post(self, request, *args, **kwargs): application = self.get_object() try: with transaction.atomic(): application.active_members.filter( relation__in=['son', 'daughter']).update(ignore=True) application.active_members.exclude( relation__in=['son', 'daughter']).update(ignore=None) for member in request.data: serializer = self.get_serializer_class()(data=member) serializer.is_valid(raise_exception=True) serializer.save(application_id=application.id) from sales.tasks import update_insurance_fields update_insurance_fields.delay(application_id=application.id) getattr(application, application.application_type).switch_premium( application.adults, application.childrens) application.member_set.filter(ignore=None).update(ignore=True) application.refresh_from_db() application.update_fields(**dict(stage='nominee_details')) except (IntegrityError, mixins.RecommendationException) as e: raise mixins.APIException(e) return Response(dict(premium=application.quote.premium.amount, member=MemberSerializer( application.active_members, many=True).data), status=status.HTTP_201_CREATED)
def get_queryset(self): try: data = self.request.query_params opportunity = Opportunity.objects.get(id=data['opportunity_id']) if not self.request.query_params.get('quotes') or len( self.request.query_params['quotes'].split(',')) < 2: raise mixins.NotAcceptable(Constants.COMPARISION_ERROR) quotes_ids = self.request.query_params['quotes'].split(',') queryset = opportunity.get_quotes().filter(id__in=quotes_ids) if not queryset.exists() or queryset.count() != len(quotes_ids): raise mixins.APIException(Constants.INVALID_INPUT) return queryset except (KeyError, Opportunity.DoesNotExist): raise mixins.APIException(Constants.OPPORTUNITY_ID_REQUIRED) except ValueError: raise mixins.APIException(Constants.INVALID_INPUT)
def perform_update(self, serializer): with transaction.atomic(): data = self.request.data if 'opportunity_id' not in data and 'category_id' not in data: raise mixins.APIException( Constants.OPPORTUNITY_OR_OPPERATION_ID_REQUIRED) serializer.save(user_id=self.request.user.id)
def post(self, request, pk, version): try: instance = Application.objects.get(id=pk) serializer = VerifyProposerPhonenoSerializer(instance, data=request.data) serializer.is_valid(raise_exception=True) serializer.save(proposer_verified=True) response = serializer.data response['payment_status'] = instance.payment_mode != 'offline' return Response(serializer.data) except Application.DoesNotExist: raise mixins.APIException(constants.INVALID_APPLICATION_ID)
def retrieve(self, request, *args, **kwargs): instance = self.get_object() if not hasattr(instance, instance.application_type): raise mixins.APIException(constants.APPLICATION_UNMAPPED) serializer = self.get_serializer(instance) data = serializer.data insurance = getattr(instance, instance.application_type) data['%s_fields' % (instance.application_type)] = insurance.get_insurance_fields() return Response(data)
def update(self, instance, validated_data): if instance.contact_id and 'contact_id' in validated_data: raise mixins.APIException(Constants.CONTACT_FORBIDDEN) fields = dict.fromkeys(Constants.GENERIC_LEAD_FIELDS, None) for field in fields.keys(): fields[field] = validated_data.get( field, getattr(self.instance, field)) self.instance = super(self.__class__, self).update(instance, fields) if 'opportunity_id' in validated_data: self.opportunity.update_category_opportunity(validated_data) elif 'category_id' in validated_data: self.opportunity = self.instance.create_opportunity(validated_data) return self.instance
def create(self, validated_data): fields = dict.fromkeys(Constants.GENERIC_LEAD_FIELDS, None) for field in fields.keys(): fields[field] = validated_data.get(field, None) if field in ['bookmark', 'ignore']: fields[field] = False if 'contact_id' in validated_data and self.Meta.model.objects.filter( contact_id=fields['contact_id']).exists(): raise mixins.APIException(Constants.DUPLICATE_LEAD) self.instance = super(self.__class__, self).create(fields) if 'category_id' in validated_data: self.opportunity_id = self.instance.create_opportunity( validated_data).id return self.instance
def get_contact(self, validated_data): first_name = validated_data['first_name'].lower() last_name = validated_data['last_name'].lower() valid, validated_data['phone_no'] = parse_phone_no( validated_data['phone_no']) if not valid: raise mixins.APIException(Constants.INVALID_PHONE_NO) instance, created = Contact.objects.get_or_create( phone_no=validated_data['phone_no'], first_name=first_name, last_name=last_name, middle_name='') if created: instance.user_id = validated_data['user_id'] instance.save() return instance
def retrieve(self, request, *args, **kwargs): instance = self.get_object() # nominee = instance.nominee_set.first() # if not nominee: # raise mixins.APIException( # constants.INCOMPLETE_APPLICATION % 'nominee details') if not hasattr(instance, instance.application_type): raise mixins.APIException(constants.APPLICATION_UNMAPPED) serializer = self.get_serializer(instance) data = serializer.data insurance = getattr(instance, instance.application_type) data['%s_fields' % (instance.application_type)] = insurance.get_summary() return Response(data)
def get_contact(self, validated_data, **kwargs): name = validated_data['contact_name'].lower().split(' ') valid, validated_data['contact_phone_no'] = parse_phone_no( validated_data['contact_phone_no']) if not valid: raise mixins.APIException(Constants.INVALID_PHONE_NO) first_name = name[0] middle_name = name[1] if len(name) == 3 else '' last_name = name[2] if len(name) > 2 else ( name[1] if len(name) == 2 else '') instance, created = Contact.objects.get_or_create( phone_no=validated_data['contact_phone_no'], first_name=first_name, middle_name=middle_name, last_name=last_name) if created: instance.user_id = validated_data['user_id'] instance.save() return instance
def create(self, request, *args, **kwargs): try: with transaction.atomic(): lead = self.get_object() data = self.request.data if isinstance(data, dict): data = [data] for row in data: serializer = self.get_serializer(data=row) serializer.is_valid(raise_exception=True) serializer.save(lead_id=lead.id) return Response(dict(message='Notes added successfully', lead_id=lead.id), status=status.HTTP_201_CREATED) except IntegrityError: pass raise mixins.APIException( 'Unable to process request currently. Please try again')
def retrieve(self, request, *args, **kwargs): instance = self.get_object() if not hasattr(instance, instance.application_type): raise mixins.APIException(constants.APPLICATION_UNMAPPED) insurance = getattr(instance, instance.application_type) return Response(insurance.get_insurance_fields())