def change(self, request, pk=None): obj = get_object_or_404(Staff, pk=pk) data = Tools.parse_user_related_data(request.data) userSr = UserSr(obj.user, data=data['user']) if userSr.is_valid(raise_exception=True): userSr.save() remain = data['remain'] remain.update({'user': obj.user_id}) serializer = StaffBaseSr(obj, data=remain) if serializer.is_valid(raise_exception=True): serializer.save() return res(serializer.data)
def add(self, request): data = Tools.parse_user_related_data(request.data) userSr = UserSr(data=data['user']) if userSr.is_valid(raise_exception=True): userSr.save() remain = data['remain'] remain.update({'user': userSr.data['id']}) serializer = StaffBaseSr(data=remain) if serializer.is_valid(raise_exception=True): serializer.save() serializer.data.update({'user': userSr.data}) return res(serializer.data)
def change(self, request, pk=None): obj = get_object_or_404(Customer, pk=pk) data = Tools.parse_user_related_data(request.data) userSr = UserSr(obj.user, data=data['user']) if userSr.is_valid(raise_exception=True): userSr.save() remain = data['remain'] remain.update({'user': obj.user_id}) if type(remain.get('avatar')) is not InMemoryUploadedFile: remain.pop('avatar', None) serializer = CustomerBaseSr(obj, data=remain) if serializer.is_valid(raise_exception=True): serializer.save() return res(serializer.data)
def test_edit(self): item2 = TestHelpers.user_seeding(2, True, False) # Update not exist resp = self.client.put( "/api/v1/customer/{}".format(0), item2, format='json' ) self.assertEqual(resp.status_code, 404) # Update duplicate resp = self.client.put( "/api/v1/customer/{}".format(self.items[0].pk), item2, format='json' ) self.assertEqual(resp.status_code, 400) # Update success user = UserSr(self.items[1].user) data = {'phone': '000'} data.update(user.data) resp = self.client.put( "/api/v1/customer/{}".format(self.items[1].pk), data, format='json' ) self.assertEqual(resp.status_code, 200)
def add(self, request): data = Tools.parse_user_related_data(request.data) userSr = UserSr(data=data['user']) if userSr.is_valid(raise_exception=True): userSr.save() remain = data['remain'] remain.update({'user': userSr.data['id']}) if type(remain.get('avatar')) is not InMemoryUploadedFile: remain.pop('avatar', None) serializer = CustomerBaseSr(data=remain) if serializer.is_valid(raise_exception=True): serializer.save() serializer.data.update({'user': userSr.data}) return res(serializer.data)
def post(self, request, format=None): user = self.get_object() obj = user.staff data = Tools.parse_user_related_data(request.data) userSr = UserSr(user, data=data['user']) if userSr.is_valid(raise_exception=True): userSr.save() remain = data['remain'] remain.update({'user': user.pk}) serializer = StaffBaseSr(obj, data=remain) if serializer.is_valid(raise_exception=True): serializer.save() return res(serializer.data) else: return err_res(serializer.errors)
def get_data(i: int) -> dict: data = { "username": "******".format(i), "email": "tbson{}@gmail.com".format(i), "password": "******", "first_name": "first{}".format(i), "last_name": "last{}".format(i) } if save is False: return data try: instance = User.objects.get( Q(username=data['username']) | Q(email=data['email'])) except User.DoesNotExist: instance = UserSr(data=data) instance.is_valid(raise_exception=True) instance = instance.save() return instance
def post(self, request, format=None): user = self.get_object() obj = user.customer data = request.data if (type(request.data) is QueryDict): data = request.data.dict() data = Tools.parse_user_related_data(data) userSr = UserSr(user, data=data['user']) if userSr.is_valid(raise_exception=True): userSr.save() remain = data['remain'] remain.update({'user': user.pk}) if type(remain.get('avatar')) is not InMemoryUploadedFile: remain.pop('avatar', None) serializer = CustomerBaseSr(obj, data=remain) if serializer.is_valid(raise_exception=True): serializer.save() return res(serializer.data) else: return err_res(serializer.errors)
def test_edit(self): item2 = TestHelpers.user_seeding(2, True, False) # Update not exist resp = self.client.put("/api/v1/staff/{}".format(0), item2, format='json') self.assertEqual(resp.status_code, 404) # Update duplicate resp = self.client.put("/api/v1/staff/{}".format(self.items[0].pk), item2, format='json') self.assertEqual(resp.status_code, 400) # Update success data = UserSr(self.items[0].user).data resp = self.client.put("/api/v1/staff/{}".format(self.items[0].pk), data, format='json') self.assertEqual(resp.status_code, 200)
def test_manager_getName(self): self.assertEqual(StaffUtils.getName(self.items[0].pk), UserSr(self.items[0].user).data['fullname']) self.assertEqual(StaffUtils.getName(0), "")