def read(self, request, id=None): ready_data_true = {"success": True} ready_data_false = {"success": False} if not request.user.is_authenticated(): ready_data_false["error"] = "Authorization is failed" return ready_data_false try: if id: req = check(request, id, attrs=None) if req == True: ready_data_true["items"] = self.model.objects.get(pk=id) ready_data_true["total"] = 1 return ready_data_true else: ready_data_false["errors"] = req return ready_data_false else: role = getProfile(request).get_role_display() if role == "Customer" or role == "Sub-customer": ready_data_true["items"] = self.model.objects.filter(customer=getParentProfile(request)) ready_data_true["total"] = len(ready_data_true["items"]) return ready_data_true elif role == "Vendor" or role == "Vendor salesmen": ready_data_true["items"] = self.model.objects.filter(vendor=getParentProfile(request)) ready_data_true["total"] = len(ready_data_true["items"]) return ready_data_true except Exception, err: ready_data_false["errors"] = err return ready_data_false
def delete(self, request, id=None): ready_data_true = {"success": True} ready_data_false = {"success": False} if not request.user.is_authenticated(): ready_data_false["error"] = "Authorization is failed" return ready_data_false role = getProfile(request).get_role_display() reason = [] if id: req = check(request, id, attrs=None) if req == True: if role == "Vendor" or role == "Vendor salesmen": table = self.model.objects.filter(vendor=getParentProfile(request)).delete() return ready_data_true else: return "Permission denied" else: ready_data_false["errors"] = req return ready_data_false else: reason.append(u"Unit ID is required") ready_data_false["errors"] = reason return ready_data_false
def update(self, request, id=None): ready_data_true = {"success": True} ready_data_false = {"success": False} if not request.user.is_authenticated(): ready_data_false["error"] = "Authorization is failed" return ready_data_false role = getProfile(request).get_role_display() ext_posted_data = simplejson.loads(request.POST.get("items")) attrs = self.flatten_dict(ext_posted_data) try: del attrs["id"] except KeyError: pass reason = [] if id: req = check(request, id, attrs=attrs) if req == True: if role == "Vendor" or role == "Vendor salesmen": try: del attrs["id"] except KeyError: pass if "customer_id" in attrs: attrs["customer"] = attrs["customer_id"] del attrs["customer_id"] for x in attrs: if attrs[x] == "": attrs[x] = None else: attrs[x] = attrs[x] table = self.model.objects.filter(pk=id).update(**attrs) table = self.model.objects.get(pk=id) ready_data_true["items"] = table ready_data_true["total"] = 1 return ready_data_true else: return "False" else: ready_data_false["errors"] = req return ready_data_false else: reason.append(u"Unit ID is required") ready_data_false["errors"] = reason return ready_data_false
def create(self, request): ready_data_true = {"success": True} ready_data_false = {"success": False} if not request.user.is_authenticated(): ready_data_false["error"] = "Authorization is failed" return ready_data_false role = getProfile(request).get_role_display() ext_posted_data = simplejson.loads(request.POST.get("items")) attrs = self.flatten_dict(ext_posted_data) try: del attrs["id"] except KeyError: pass for x in attrs: if attrs[x] == "": attrs[x] = None else: attrs[x] = attrs[x] req = check(request, id=None, attrs=attrs) if req == True: if role == "Vendor" or role == "Vendor salesmen": reason = [] try: table = self.model.objects.create(vendor=getParentProfile(request), **attrs) ready_data_true["items"] = table ready_data_true["total"] = 1 return ready_data_true except ValueError: reason.append(u"some of fields do not correspond to their type") ready_data_false["errors"] = reason return ready_data_false else: return "Permission denied" else: ready_data_false["errors"] = req return ready_data_false