Search code examples
djangorestful-architecturetastypie

django views with django tastypie


I am building a shop kiosk in django and I have a view that displays some sold products within a time range like so;

def product_sold_report(request):
    response = {}
    id_list = []
    try:
        _start = _get_parameter(request, "_start")
    except Exception, e:
        _start = None

    try:
        _end = _get_parameter(request, "_end")
    except Exception, e:
        _end = None

    if _start and _end:
        orders = Order.objects.filter(created__range=[datetime.datetime.fromtimestamp(float(_start)),datetime.datetime.fromtimestamp(float(_end))]).filter(status = 4).order_by("-created")
    else:
        orders = Order.objects.all().filter(status=4).order_by("-created")

    for order in orders:
        id_list.append(order.id)

    for item in OrderItem.objects.filter(order__in = id_list):
        i = Order.objects.get(id = item.order_id)
        try:
            product = Product.objects.get(id = item.product_reference)
            barcode = product.barcode
        except Exception,e:
            barcode = None



        if item.product_name in response:
            response[item.product_name]["product_quantity"] += item.quantity
        else:
            response[item.product_name] = {
                "product_quantity":item.quantity,
                "product_barcode":barcode


            }

    return HttpResponse(simplejson.dumps(response), mimetype="text/json")

  

However, I want to use a rest framework so that I can have a different web app on another server query this app and get the same products sold results , I have considered django tastypie but it seems to major a lot on models resources. Is it possible to have this done using django tastypie or django rest framework.

Thanks


Solution

  • You don't NEED a full restful service to get the same results. As long as your views are returning well formed JSON data - Just use good URL patterns in your url.py that maps to your view functions.

    You can craft URL patterns just like tastypie /api/v1/products_sold/ Just work with some regex - and make sure the URLS just make sense