Ejemplo n.º 1
0
def watchdog():

    try:
        orders = Order.objects.exclude(status__in=['delivered', 'cancelled'])
        delayed_orders = []
        ontrack_orders = []
        pusher = PushMessage(channel="order_list")

        for order in orders:
            # Get expected time for the current status to have changed
            expected_time = window[order.status]

            log = OrderLog.objects.filter(order=order, message=dict(STOCK_MESSAGES)[order.status])

            if len(log):
                assigned_time = log[0].created_on

                #Time aware timestamp fubar
                current_time = datetime.utcnow().replace(tzinfo=pytz.utc)

                if current_time - assigned_time > timedelta(minutes=expected_time):

                    delayed_by = int((current_time - assigned_time).total_seconds()) - (expected_time*60)

                    #If an alarm exists for the current status update the delay time otherwise create a new alarmnom
                    alarm = Alarm.objects.get_or_create(order=order, for_status=order.status)
                    alarm[0].delayed_by = delayed_by
                    alarm[0].save()

                    #Set order delayed time for "current step"
                    order.delayed_by = delayed_by
                    order.save()

                    delayed_orders.append(order)

                    order = OrderDetailViewSerializer(instance=order).data

                    pusher.push(json.dumps(order))
                    print "Order delayed \n"

                else:
                    ontrack_orders.append(order)
                    print "Order on track \n"

        orders = delayed_orders + ontrack_orders

        return orders

    except Exception as e:
        beam(e)
Ejemplo n.º 2
0
    def get(self, request):

        """


            summary: This endpoint is called by the ASMX server. It then publishes a push notification of
            the concerned agent on their cogent_agent_id

            Auth: Doesn't require the auth token in request headers

            ---

            serializer: CustomerDetailSerializer
            parameters:
                - name: contact_number
                  required: true
                  type: string
                - name: forwarded_for
                  required: true
                  type: string
                - name: agent_id
                  required: true
                  type: integer

            """

        client, order = None, False
        print "request from asmx server", request.query_params
        # TODO  Check for a B2B customer
        try:
            forwarded_for = request.query_params["forwarded_for"]
            if forwarded_for in B2BCall.keys():
                print forwarded_for
            # client = PhoneNumber.objects.filter(number= forwarded_for, number_type=NUMBER_TYPE[0][0])[0]
            from restaurant.models import Restaurant

            restaurant = Restaurant.objects.get(id=B2BCall[forwarded_for])
            client = ClientSerializer(instance=restaurant).data
        except (KeyError, IndexError):
            pass

        try:
            contact_number = request.query_params["contact_number"]
            customer = Customer.find_by_contact_number(contact_number=contact_number)

            customer_info = {"primary_number": contact_number, "name": "New Customer", "call_count": 1}

            if customer:
                customer.call_count += 1
                customer.save()
                customer_info = CustomerDetailSerializer(instance=customer).data

                order = customer.get_pending_order()

                if order:
                    order = OrderDetailViewSerializer(instance=order).data

                CallLog.log_call(customer, request.query_params["agent_id"])

            pusher = PushMessage(channel=request.query_params["agent_id"])
            print "pusher order ", order
            pusher.push(
                json.dumps({"client": client, "customer": customer_info, "order": order}, default=self.date_handler)
            )

            return JSONResponse({"customer": customer_info}, status=200)
        except Exception as e:
            print traceback.format_exc(e)
            beam(e, request)
            return JSONResponse({"Success": False}, status=200)