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 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: 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: 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 ext_posted_data = simplejson.loads(request.POST.get('items')) attrs = self.flatten_dict(ext_posted_data) try: del attrs['id'] except KeyError: pass req = check(request, id=None, attrs=attrs) if req == True: reason=[] try: table = self.model.objects.create(user_id=getParentuser(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: ready_data_false['errors'] = req return ready_data_false
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 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: ready_data_true['items'] = self.model.objects.filter(user=getParentuser(request)).order_by('name') ready_data_true['total'] = len(ready_data_true['items']) return ready_data_true
def delete(self, request, id=None): ready_data_true = {'success': True } ready_data_false = {'success': False} ready_data_false['errors'] = 'DELETE IS NOT ALLOWED' return ready_data_false reason = [] if id: req = check(request, id, attrs=None) if req == True: table = self.model.objects.get(pk=id) table.delete() return ready_data_true 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