Commits

Kai Diefenbach committed 9c22fdd

Added more resources.

  • Participants
  • Parent commits d21ed51

Comments (0)

Files changed (2)

File lfs_rest/api.py

+# django imports
+from django.contrib.auth.models import User
+
 # tastypie imports
 from tastypie import fields
 from tastypie.authorization import Authorization
 from tastypie.authentication import BasicAuthentication
 from tastypie.resources import ModelResource
 from tastypie.resources import ALL
+from tastypie.serializers import Serializer
 
 # lfs imports
 from lfs.catalog.models import Category
 from lfs.catalog.models import Product
+from lfs.customer.models import Address
+from lfs.customer.models import Customer
+from lfs.order.models import Order
+from lfs.order.models import OrderItem
+
+
+class LFSSerializer(Serializer):
+    def to_html(self, data, options=None):
+        return self.to_json(data, options)
 
 
 class ProductResource(ModelResource):
 
     class Meta:
         queryset = Product.objects.all()
+        serializer = LFSSerializer()
         resource_name = 'product'
         authorization = Authorization()
-        authentication = BasicAuthentication()
+        # authentication = BasicAuthentication()
         excludes = ["effective_price"]
+
         filtering = {
             "sku": ALL,
             "categories": ALL,
 
     class Meta:
         queryset = Category.objects.all()
+        serializer = LFSSerializer()
         resource_name = 'category'
         authorization = Authorization()
         authentication = BasicAuthentication()
         filtering = {
             "name": ALL,
         }
+
+
+class OrderResource(ModelResource):
+    items = fields.ToManyField("lfs_rest.api.OrderItemResource", "items", null=True)
+
+    class Meta:
+        queryset = Order.objects.all()
+        serializer = LFSSerializer()
+        resource_name = 'order'
+
+        filtering = {
+            "created": ALL,
+        }
+
+
+class OrderItemResource(ModelResource):
+    product = fields.ForeignKey("lfs_rest.api.ProductResource", "product", null=True)
+    order = fields.ForeignKey("lfs_rest.api.OrderResource", "order")
+
+    class Meta:
+        queryset = OrderItem.objects.all()
+        serializer = LFSSerializer()
+        resource_name = 'order-item'
+
+        filtering = {
+            "created": ALL,
+        }
+
+
+class CustomerResource(ModelResource):
+    addresses = fields.ToManyField("lfs_rest.api.AddressResource", "addresses", null=True)
+
+    class Meta:
+        queryset = Customer.objects.all()
+        serializer = LFSSerializer()
+        resource_name = 'customer'
+
+
+class AddressResource(ModelResource):
+    customer = fields.ForeignKey("lfs_rest.api.CustomerResource", "customer", null=True)
+
+    class Meta:
+        queryset = Address.objects.all()
+        serializer = LFSSerializer()
+        resource_name = 'address'

File lfs_rest/urls.py

 from tastypie.api import Api
 
 # lfs_rest imports
+from lfs_rest.api import AddressResource
+from lfs_rest.api import CategoryResource
+from lfs_rest.api import CustomerResource
+from lfs_rest.api import OrderResource
+from lfs_rest.api import OrderItemResource
 from lfs_rest.api import ProductResource
-from lfs_rest.api import CategoryResource
-
-product_resource = ProductResource()
-category_resource = CategoryResource()
 
 v1_api = Api(api_name='api')
+v1_api.register(AddressResource())
 v1_api.register(ProductResource())
 v1_api.register(CategoryResource())
+v1_api.register(CustomerResource())
+v1_api.register(OrderResource())
+v1_api.register(OrderItemResource())
 
 urlpatterns = patterns('',
     (r'', include(v1_api.urls)),