def send_otp_email(self, auth_code): """ send otp email to user """ from core.tasks import send_custom_notification_task send_custom_notification_task.delay('USER_SEND_OTP_EMAIL', {'name': self.name, 'otp': auth_code}, params_dict={'sent_for_account_id': self.id}, user_id=self.id)
def handle_car_pickup_incentive(booking): from core.models.booking import UserVendorCash, Booking from core.models.users import ScratchFinderLead sflead = ScratchFinderLead.objects.filter(phone=booking.user.phone ).exclude(status=ScratchFinderLead.STATUS_REJECTED).first() if not sflead: return if Booking.objects.filter(status__flow_order_num__range=(9,24), user=booking.user).exclude(id=booking.id).exists(): logger.info("No SF Incentive - Existing converted bookings for this booking ({}) user ({})".format(booking.id, booking.user_id)) return # create incentives if any incentive_data = {"name": "INCENTIVE_EVENT_SFLEAD_CREATE", "entity": UserVendorCash.ENTITY_SFLEAD, "entity_id": sflead.id, "user": sflead.user, "transferred": True, "transfer_info": "For Booking: {}".format(booking.id) } from core.managers import userManager uvc = userManager.handle_incentive_events(incentive_data) if uvc: from core.tasks import send_custom_notification_task send_custom_notification_task.delay('USER_SMS_SCRATCH_LEAD_CONVERTED', {'amount': uvc.amount}, user_id=sflead.user_id)
def update_followup(instance, validated_data): followup = validated_data.pop('followup', []) updated_by = validated_data.pop('updated_by', None) from core.tasks import send_custom_notification_task for f in followup: f['updated_by'] = updated_by tags = f.pop('tags', None) followup_obj = instance.followup.create(**f) if tags: followup_obj.tags.add(*tags) result = f.get('result') if result and result.notification: notification = result.notification send_custom_notification_task.delay( notification.name, { 'instance_id': instance.id, 'result_name': result.name, 'followup_for_type': 'Inquiry' }) is_instance_updated = False for attr, value in validated_data.items(): if not isinstance(value, (list, dict)): is_instance_updated = True setattr(instance, attr, value) if is_instance_updated: instance.updated_by = updated_by instance.save() return instance
def handle_referral_campaign(flow_name, referral): campaign = ReferralCampaign.objects.filter(name=flow_name, active=True).first() user_id = None amount = None if campaign: if campaign.referrer_credit: add_user_credits(referral.referrer, campaign.referrer_credit, CreditTransaction.ENTITY_REFERRAL, referral.id, campaign.name) user_id = referral.referrer_id amount = campaign.referrer_credit elif campaign.referred_credit: add_user_credits(referral.referred, campaign.referred_credit, CreditTransaction.ENTITY_REFERRAL, referral.id, campaign.name) user_id = referral.referred_id amount = campaign.referred_credit notifications = campaign.notifications.all() for notification in notifications: send_custom_notification_task.delay( notification.name, { 'amount': amount, 'referrer_name': referral.referrer.name, 'referred_name': referral.referred.name, }, user_id=user_id)
def make_payment(self, request, pk=None): """ pay cash on delivery - This is only allowed for Ops User or Driver. """ logger.info("Make Payment : data: %s, booking_id: %s" % (request.data, pk)) booking = self.get_object() serializer = ProcessPaymentSerializer(data=request.data) if serializer.is_valid(): data = serializer.validated_data data['booking_id'] = booking.id data['device_type'] = self.request.META.get('HTTP_SOURCE') if paymentManager.process_payment_response( data, data.get('vendor_tx_data')): if data.get('device_type') == 'opsPanel': send_custom_notification_task.delay( 'OPS_ADD_PAYMENT_OPS_PANEL', { 'booking': booking.id, 'amount': data.get('net_amount_debit'), 'reason': data.get('tx_data'), }) return Response(status=status.HTTP_200_OK) else: return Response({"message": "Missing critical data"}, status=status.HTTP_400_BAD_REQUEST) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def save_model(self, request, obj, form, change): # Overwritten to send email whenever a car panel is added/changed from admin panel. obj.name = capitalize_words(obj.name) obj.save() add_or_change = 'changed' if change else 'added' from core.tasks import send_custom_notification_task send_custom_notification_task.delay( 'OPS_MODEL_ADD_CHANGE_EMAIL', { 'model': 'CarPanel', 'updated_by': request.user.name, 'id': obj.id, 'add_or_change': add_or_change })
def create(self, validated_data): followup = validated_data.pop('followup', None) updated_by = validated_data.pop('updated_by', None) instance = super(UserInquirySerializer, self).create(validated_data) if followup: data = {"followup": followup, "updated_by": updated_by} userManager.update_followup(instance, data) from core.tasks import send_user_inquiry_to_ops send_user_inquiry_to_ops.delay(instance.id) from core.tasks import send_custom_notification_task send_custom_notification_task.delay('USER_INQUIRY_NOTIFICATION', {'user_name': instance.user.name}, user_id=instance.user_id) return instance
def create(self, validated_data): try: with transaction.atomic(): m = validated_data.pop('media') if not m.get('file'): m['file'] = m.pop('image_name') media = Media.objects.create(**m) validated_data['media'] = media instance = super(ScratchFinderLeadSerializer, self).create(validated_data) # create incentives if any incentive_data = { "name": "INCENTIVE_EVENT_SFLEAD_CREATE", "entity": UserVendorCash.ENTITY_SFLEAD, "entity_id": instance.id, "promise_info": "Scratch Finder Lead Created.", "user": validated_data.get('user') } userManager.handle_incentive_events(incentive_data) # send notifications send_custom_notification_task.delay( 'USER_SMS_SCRATCH_LEAD_CREATED', {}, user_id=instance.user_id) # Send promo message only if user not exist in our system already. User = get_user_model() if not User.objects.filter(phone=instance.phone).exists(): if instance.car_model: send_custom_notification_task.delay( 'USER_SMS_SCRATCH_LEAD_PROMO1', { 'user_name': instance.user.name, 'lead_name': instance.name, 'car_model': instance.car_model.name }, params_dict={'phone': instance.phone}) send_custom_notification_task.apply_async( ('USER_SMS_SCRATCH_LEAD_PROMO2', {}), {"params_dict": { 'phone': instance.phone }}, countdown=7200) return instance except: # may need to send trigger to dev about issue. logger.exception("SF Lead not created.") raise serializers.ValidationError("Not able to create lead.")
def update(self, instance, validated_data): lead_approved = False if validated_data.get('status') == ScratchFinderLead.STATUS_APPROVED: lead_approved = True new_instance = super(ScratchFinderLeadSerializer, self).update(instance, validated_data) create_event = IncentiveEvent.objects.filter( name="INCENTIVE_EVENT_SFLEAD_CREATE").first() if create_event and lead_approved: uvc = UserVendorCash.objects.filter( entity=UserVendorCash.ENTITY_SFLEAD, entity_id=instance.id, event=create_event).first() if uvc: # send notifications approved_event = IncentiveEvent.objects.filter( name="INCENTIVE_EVENT_SFLEAD_APPROVED").first() if approved_event: send_custom_notification_task.delay( 'USER_SMS_SCRATCH_LEAD_APPROVED', { 'amount': uvc.amount, 'approved_amount': approved_event.tp_cash }, user_id=instance.user_id) if lead_approved: incentive_data = { "name": "INCENTIVE_EVENT_SFLEAD_APPROVED", "entity": UserVendorCash.ENTITY_SFLEAD, "entity_id": instance.id, "promise_info": "Scratch Finder Lead Approved.", "user": new_instance.user, "promise_and_transfer": True } userManager.handle_incentive_events(incentive_data) if validated_data.get('status') == ScratchFinderLead.STATUS_REJECTED: send_custom_notification_task.delay( 'USER_SMS_SCRATCH_LEAD_REJECTED', {}, user_id=instance.user_id) return new_instance