def create(self, data, user): exclude_fields = [] if not user.is_staff: exclude_fields.extend(self.private_fields) serializer = NotifySerializer(data=data, exclude=exclude_fields) serializer.is_valid() if serializer.is_valid(): if data['status']['id'] > 2: raise ServiceException('Неправильный статус') status = Status.objects.get(id=data['status']['id']) bank = CreditOrganization.objects.get(id=data['bank']['id']) house, created = House.objects.get_or_create( address_id=data['house']['address']['id'], number=str(data['house']['number']).strip().lower()) if user.is_staff: org = Organization.objects.get(id=data['organization']['id']) else: org = user.organization files = upd_many_to_many('files', data, None, File) serializer.save(organization=org, date=datetime.today().date(), status=status, bank=bank, files=files, house=house) return serializer.data else: raise ServiceException(serializer.errors)
def create(self, data, user): """Создание пользователя администратором""" serializer = UserSerializer(data=data) if serializer.is_valid(): if 'organization' not in data or 'id' not in data['organization']: raise ServiceException('Не выбрана организация') if 'password' not in data or 're_password' not in data: raise ServiceException('Не указан пароль') if data['password'] != data['re_password']: raise ServiceException('Пароли не совпадают') org = Organization.objects.get(id=data['organization']['id']) groups = upd_many_to_many('groups', data, None, Group) is_staff = Group.objects.get(name='Администраторы') in groups if 'is_active' not in data: is_active = False else: is_active = data['is_active'] if len(groups) == 0: groups.append( Group.objects.get(name='Управляющие организации')) serializer.save(organization=org, groups=groups, is_active=is_active, is_staff=is_staff) user = User.objects.get(id=serializer.instance.id) user.set_password(data['password']) user.save() return serializer.data else: raise ServiceException(serializer.errors)
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 update(self, instance, data, user): exclude_fields = [] data = data if not user.is_staff: if data['status']['id'] > 2: raise ServiceException('Неправильный статус') if instance.status.id > 2: raise ServiceException('Вы не можете редактировать эту запись') exclude_fields.append('comment2') serializer = ContributionsInformationSerializer(instance=instance, data=data, partial=True, exclude=exclude_fields) serializer.is_valid() if serializer.is_valid(): 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']) else: status = instance.status if user.is_staff and 'date' in data: date = data['date'] else: date = instance.date notify = self._get_notify(data, instance, user) files = upd_many_to_many('files', data, instance, File) mistakes = self._get_mistakes(data, instance, user) if status.id == 3: self._set_old_last_notify_to_none(notify) last_notify = notify notify.latest_contrib_date = date notify.save() else: last_notify = None serializer.save(files=files, status=status, notify=notify, last_notify=last_notify, mistakes=mistakes, date=date) return serializer.data else: raise ServiceException(serializer.errors)
def create(self, data, user): serializer = OrganizationSerializer(data=data) if serializer.is_valid(): org_type = OrganizationType.objects.get(id=data['type']['id']) serializer.save(type=org_type, name=replace_quotes(data['name'])) return serializer.data else: raise ServiceException(serializer.errors)
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 register(self, data): """Регистрация пользователем""" item = UserSerializer(data=data) if item.is_valid(): if data['password'] != data['re_password']: raise ServiceException('Пароли не совпадают') if 'organization' not in data: raise ServiceException('Не указана организация') if 'name' not in data['organization']: raise ServiceException('Не указано наименование организации') if 'inn' not in data['organization']: raise ServiceException('Не указан ИНН организации') if 'ogrn' not in data['organization']: raise ServiceException('Не указан ОГРН организации') if 'type' not in data['organization'] or 'id' not in data[ 'organization']['type']: raise ServiceException('Не указан тип организации') org, created = Organization.objects.get_or_create( inn=data['organization']['inn']) if created: org.name = data['organization']['name'] org.ogrn = data['organization']['ogrn'] org.type = OrganizationType.objects.get( id=data['organization']['type']['id']) org.save() User.objects.create_user(username=data['username'], organization=org, password=data['password'], email=data['email']) \ .groups.add(Group.objects.get(name='Управляющие организации')) return item.data else: raise ServiceException(item.errors)
def create(self, data, user): serializer = AddressSerializer(data=data) serializer.is_valid() if serializer.is_valid(): serializer.save(street=normalize_street(data['street']), city=normalize_city(data['city'])) return serializer.data else: raise ServiceException(serializer.errors)
def update(self, instance, data, user): serializer = AddressSerializer(instance=instance, data=data, partial=True) if serializer.is_valid(): serializer.save(street=normalize_street(data['street']), city=normalize_city(data['city'])) return serializer.data else: raise ServiceException(serializer.errors)
def create(self, data, user): serializer = HouseSerializer(data=data) serializer.is_valid() if serializer.is_valid(): addr = Address.objects.get(id=data['address']['id']) serializer.save(address=addr, number=normalize_number(data['number'])) return serializer.data else: raise ServiceException(serializer.errors)
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 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('У вас нет соответствующих прав')
def create(self, data, user): exclude_fields = [] if not user.is_staff: exclude_fields.extend(self.private_fields) serializer = ContributionsInformationSerializer(data=data, exclude=exclude_fields) serializer.is_valid() if serializer.is_valid(): if data['status']['id'] > 2: raise ServiceException('Неправильный статус') status = Status.objects.get(id=data['status']['id']) notify = self._get_notify(data, None, user) files = upd_many_to_many('files', data, None, File) mistakes = self._get_mistakes(data, None, user) serializer.save(date=datetime.today().date(), status=status, files=files, notify=notify, mistakes=mistakes) return serializer.data else: raise ServiceException(serializer.errors)