def update(self, instance, data, user): exclude_fields = [] if not user.is_staff: if instance.status.id > 2: raise ServiceException('Вы не можете редактировать эту запись') if data['status']['id'] > 2: raise ServiceException('Неправильный статус') exclude_fields.extend(self.private_fields) serializer = NotifySerializer(instance=instance, data=data, partial=True, exclude=exclude_fields) if serializer.is_valid(): if user.is_staff: org = upd_foreign_key('organization', data, instance, Organization) else: org = instance.organization if user.is_staff and 'date' in data: date = data['date'] else: date = instance.date bank = upd_foreign_key('bank', data, instance, CreditOrganization) house = House.objects.get_or_create_new('house', data, instance) if 'status' in data and 'id' in data['status']: if instance.status is not None and data['status'][ 'id'] == instance.status.id: status = instance.status else: status = Status.objects.get(id=data['status']['id']) # присваиваем всем другим записям с этим домом статус Исключено if status.text == 'Согласовано': Notify.objects.filter( house_id=house.id, status_id=3).update( status_id=4, date_of_exclusion=datetime.now()) house.organization = org house.save() else: status = instance.status files = upd_many_to_many('files', data, instance, File) serializer.save(organization=org, bank=bank, house=house, files=files, status=status, date=date) return serializer.data else: raise ServiceException(serializer.errors)
def test_updates_field(self): """Функция должна обновить поле field если в data передан id нового объекта""" org_type = mixer.blend(OrganizationType) new_org_type = mixer.blend(OrganizationType) org = mixer.blend(Organization, type=org_type) data = {'type': {'id': new_org_type.id, 'text': 'some text'}} result = upd_foreign_key('type', data, org, OrganizationType) self.assertEqual(result, new_org_type)
def test_raise_service_exception_if_instance_and_field_in_data_is_none( self): """Функция должна возбудить ошибку, если и instance равен None и в словаре data нет ключа field""" org_type = mixer.blend(OrganizationType) new_org_type = mixer.blend(OrganizationType) org = mixer.blend(Organization, type=org_type) data = { 'some_other_field': { 'id': new_org_type.id, 'text': 'some text' } } try: upd_foreign_key('type', data, None, OrganizationType) raise AssertionError except ServiceException: pass
def test_pass_if_id_not_in_field_data_dict(self): """ Функция должна возратить текущее значения поля field объекта, если в словаре data не передан id нового значения""" org_type = mixer.blend(OrganizationType) org = mixer.blend(Organization, type=org_type) data = {'type': {'text': 'some text'}} result = upd_foreign_key('type', data, org, OrganizationType) self.assertEqual(result, org_type)
def _get_notify(self, data, instance, user): """Возвращает уведомление для записи""" notify = upd_foreign_key('notify', data, instance, Notify) if notify.organization.id != user.organization.id and not user.is_staff: raise ServiceException( 'Организация, указанная в уведомлениии, не соответствует организации пользователя' ) return notify
def update(self, instance, data, user): serializer = OrganizationSerializer(instance=instance, data=data, partial=True) if serializer.is_valid(): org_type = upd_foreign_key('type', data, instance, OrganizationType) serializer.save(type=org_type) return serializer.data else: raise ServiceException(serializer.errors)
def update(self, instance, data, user): serializer = HouseSerializer(instance=instance, data=data, partial=True) if serializer.is_valid(): addr = upd_foreign_key('address', data, instance, Address) serializer.save(address=addr, number=normalize_number(data['number'])) return serializer.data else: raise ServiceException(serializer.errors)
def test_pass_if_field_not_in_data_dict(self): """Функция должна возвратить текущее значение поля field объекта, если в словаре data нет ключа с названием этого поля""" org_type = mixer.blend(OrganizationType) new_org_type = mixer.blend(OrganizationType) org = mixer.blend(Organization, type=org_type) data = { 'some_other_field': { 'id': new_org_type.id, 'text': 'some text' } } result = upd_foreign_key('type', data, org, OrganizationType) self.assertEqual(result, org_type)
def update(self, instance, data, user): if data['id'] != 1 and user.has_perm('dictionaries.change_user'): serializer = UserSerializer(instance=instance, data=data, partial=True) if serializer.is_valid(): if 'password' in data: if data['password'] != data['re_password']: raise ServiceException('Пароли не совпадают') instance.set_password(data['password']) org = upd_foreign_key('organization', data, instance, Organization) groups = upd_many_to_many('groups', data, instance, Group) is_staff = Group.objects.get(name='Администраторы') in groups serializer.save(organization=org, groups=groups, is_staff=is_staff) self._send_activation_mail(data, serializer) return serializer.data else: raise ServiceException(serializer.errors) raise ServiceException('У вас нет соответствующих прав')