def new_orders(request):
    venue = Venue.get_by_username(request.user.username)
    last_time = request.GET.get('last_time')
    today = datetime.utcnow().replace(hour=0, minute=0, second=0)
    if timestamp(today) > int(last_time):
        last_time = timestamp(today)
    if last_time:
        logging.error(last_time)
        last_time = datetime.utcfromtimestamp(int(last_time))
        orders = Order.objects.filter(created__gt=last_time, venue=venue)
        if orders:
            orders, last_time = _prepare_orders(orders)
            order_dicts = [order.dict() for order in orders]
            for order_dict in order_dicts:
                order_dict.update({
                    'created_time': datetime.utcfromtimestamp(order_dict['created']).strftime('%H:%M:%S'),
                    'updated_time': datetime.utcfromtimestamp(order_dict['updated']).strftime('%H:%M:%S')
                })
                for product in order_dict.get('products', []):
                    if product.get('status') is not None:
                        product['status'] = OrderProduct.STATUS_CHOICES[product['status']][1]
                for status in Order.STATUS_CHOICES:
                    if order_dict.get('status') in status:
                        order_dict.update({
                            'status_class': status[0]
                        })
        else:
            order_dicts = []
            last_time = timestamp(last_time)
        return JsonResponse({
            'orders': order_dicts,
            'last_time': last_time if not order_dicts else last_time + 1
        })
    else:
        return HttpResponseBadRequest()
Example #2
0
def _prepare_products(products):
    last_time = 0
    for product in products:
        if timestamp(product.created) > last_time:
            last_time = timestamp(product.created)
        product.name = product.product.venue_product.product.name
        product.status_name = CookedOrderedProduct.STATUS_CHOICES[product.status][1]
        product.number = product.product.id
    return products, last_time
def last_modified_menu(request):
    venue_id = request.GET.get('venue_id')
    if venue_id:
        venue_id = int(venue_id)
        try:
            venue = Venue.objects.get(id=venue_id)
            return JsonResponse({
                'category_time':
                timestamp(venue.first_category.updated),
                'venue_time':
                timestamp(venue.updated)
            })
        except Venue.DoesNotExist:
            return HttpResponseBadRequest()
    else:
        return HttpResponseBadRequest()
def _prepare_orders(orders):
    last_time = 0
    for order in orders:
        if timestamp(order.created) > last_time:
            last_time = timestamp(order.created)
        order.status_class = order.status
        order.status = Order.STATUS_CHOICES[order.status][1]
        order.created_time = order.created.strftime('%H:%M:%S')
        order.updated_time = order.updated.strftime('%H:%M:%S')
        order_products = order.get_products()
        for product in order_products:
            product.dict = product.venue_product.product_dict()
            product.status = OrderProduct.STATUS_CHOICES[product.status][1]
        order.products = order_products
        if order.user.address:
            order.user.address_str = order.user.address.to_str()
    return orders, last_time
Example #5
0
 def dict(self):
     return {
         'order_id':
         self.id,
         'created':
         timestamp(self.created),
         'updated':
         timestamp(self.updated),
         'total_sum':
         self.total_sum,
         'products': [
             product.dict()
             for product in OrderProduct.objects.filter(order=self)
         ],
         'status':
         self.status,
         'user':
         self.user.dict()
     }
Example #6
0
def new_products(request):
    cook = Cook.get_cook_by_username(request.user.username)
    last_time = request.GET.get('last_time')
    today = datetime.utcnow().replace(hour=0, minute=0, second=0)
    if timestamp(today) > int(last_time):
        last_time = timestamp(today)
    if last_time:
        last_time = datetime.utcfromtimestamp(int(last_time))
        products = CookedOrderedProduct.objects.filter(created__gt=last_time, cook=cook)
        if products:
            products, last_time = _prepare_products(products)
            product_dicts = [product.dict() for product in products]
        else:
            product_dicts = []
            last_time = timestamp(last_time)
        return JsonResponse({
            'products': product_dicts,
            'last_time': last_time if not product_dicts else last_time + 1
        })
    else:
        return HttpResponseBadRequest()
Example #7
0
 def dict(self):
     return {
         'id':
         self.id,
         'name':
         self.name,
         'description':
         self.description,
         'image':
         self.image_url,
         'last_updated':
         timestamp(self.updated) if self.parent is None else None
     }
def send_push(channels, device_type, data):
    url = 'https://api.parse.com/1/push'
    payload = {
        'channels': channels,
        'type': DEVICE_TYPE_MAP[device_type],
        'expiry': timestamp(datetime.utcnow() + timedelta(days=365)),
        'data': data
    }
    headers = {
        'Content-Type': 'application/json',
        'X-Parse-Application-Id': PARSE_APPLICATION_ID,
        'X-Parse-REST-API-Key': PARSE_API_KEY
    }
    result = requests.post(url, data=json.dumps(payload), headers=headers).text
    logging.error(result)
 def updated_dict(self):
     return {'id': self.id, 'last_updated': timestamp(self.updated)}