def post(self, request, format=None): u = User.objects.create( username=request.data['login'], # Passamos o login email=request.data['email'] # Passamos o email ) u.set_password(request.data['senha']) request.data['user'] = u.id usuario = UsuarioSerializer(data=request.data) if usuario.is_valid(): try: usuario.save() except utils.IntegrityError: u.delete() return Response(usuario.errors, status=status.HTTP_400_BAD_REQUEST) #Falhou u.save() return Response(usuario.data, status=status.HTTP_201_CREATED) #Sucedeu u.delete() return Response(usuario.errors, status=status.HTTP_400_BAD_REQUEST) #Falhou
def post(self, request): #/usuario/ username = request.data['username'] email = request.data['email'] password = request.data["password"] nome = request.data["nome"] isAdmin = request.data["isAdmin"] user = Usuario(nome=nome, email=email, username=username, isAdmin=isAdmin) user.set_password(password) validadeUsuario = Usuario.objects.filter(username=user.username) validadeNome = Usuario.objects.filter(nome=user.nome) validadeEmail = Usuario.objects.filter(email=user.email) if not (UsuarioSerializer(validadeUsuario, many=True).data or UsuarioSerializer(validadeNome, many=True).data or UsuarioSerializer(validadeEmail, many=True).data): user.save() data = UsuarioSerializer(user).data return Response(data) else: return Response({'error': 'User already exists'})
def put(self, request, pk, format=None): # Cria uma referência ao usuário escolhido usuario = self.get_object(pk) serializer = UsuarioSerializer(usuario, data=request.data) x = self.has_permission(request, UsuarioEdit) y = self.check_object_permission(request.user, usuario) z = self.has_object_permission(request, UsuarioEdit, usuario) if (x and y and z): # Se o serailizer for valido poderá editar os campos if serializer.is_valid(): if (request.data['login'] != usuario.login or request.data['email'] != usuario.email or request.data['senha'] != usuario.senha): user = User.objects.get(pk=usuario.user.pk) user.username = request.data['login'] user.email = request.data['email'] user.set_password(request.data['senha']) user.save() serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) return Response(status=status.HTTP_204_NO_CONTENT)
def put(self, request, pk, format=None): # Cria uma referência ao usuário escolhido usuario = self.get_object(pk) serializer = UsuarioSerializer(usuario, data=request.data) x = self.has_permission(request, UsuarioEdit) y = self.check_object_permission(request.user, usuario) z = self.has_object_permission(request, UsuarioEdit, usuario) if (x and y and z): # Se o serailizer for valido poderá editar os campos if serializer.is_valid(): if request.data['login'] != usuario.login or request.data[ 'email'] != usuario.email or request.data[ 'senha'] != usuario.senha: user = User.objects.get(pk=usuario.user.pk) user.username = request.data['login'] user.email = request.data['email'] user.set_password(request.data['senha']) user.save() serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) return Response(status=status.HTTP_204_NO_CONTENT)
def delete(self, request, pk): usuario = get_object_or_404(Usuario, pk=pk) usuario.delete() data = UsuarioSerializer(usuario).data return Response(data)
def delete(self, request, pk, format=None): usuario = self.get_object(pk) serializer = UsuarioSerializer(usuario) u = User.objects.get(id=serializer.data['user']) u.delete() usuario.delete() return Response(status=status.HTTP_204_NO_CONTENT)
def post(self, request, format=None): u = User.objects.create( username=request.data['login'], # Passamos o login email=request.data['email'] # Passamos o email ) u.set_password(request.data['senha']) request.GET = request.GET.copy() request.GET['user'] = u.id request.GET['cpf'] = request.data['cpf'] request.GET['rg'] = request.data['rg'] request.GET['matricula'] = request.data['matricula'] request.GET['sexo'] = request.data['sexo'] request.GET['status'] = request.data['status'] request.GET['data_nasc'] = request.data['data_nasc'] request.GET['login'] = request.data['login'] request.GET['email'] = request.data['email'] request.GET['senha'] = request.data['senha'] request.GET['nome'] = request.data['nome'] request.GET['grupo_usuario'] = request.data['grupo_usuario'] usuario = UsuarioSerializer(data=request.GET) if usuario.is_valid(): try: usuario.save() except utils.IntegrityError: u.delete() # Falhou return Response(usuario.errors, status=status.HTTP_400_BAD_REQUEST) u.save() # Sucedeu return Response(usuario.data, status=status.HTTP_201_CREATED) u.delete() # Falhou return Response(usuario.errors, status=status.HTTP_400_BAD_REQUEST)
def get(self, request, pk, format=None): usuario = self.get_object(pk) serializer = UsuarioSerializer(usuario) x = self.has_permission(request, UsuarioEdit) y = self.check_object_permission(request.user, usuario) z = self.has_object_permission(request, UsuarioEdit, usuario) if (x and y and z): return Response(serializer.data) return Response(status=status.HTTP_204_NO_CONTENT)
def post(self, request): serializer = UsuarioSerializer(data=request.data) serializer.is_valid(raise_exception=True) usuario = serializer.create(serializer.validated_data) if usuario: host = 'http://localhost/api/usuario/' + str( usuario.token_verificacion + '/verify') mensaje = "Registro exitoso, para activar la cuenta de usuario debe hacer click en el siguiente enlace: <a href='" + host + "'>Click</a>" send_mail('Registro exitoso', '', '*****@*****.**', [usuario.correo], fail_silently=False, html_message=mensaje) return Response( { 'uuid': usuario.token_verificacion, 'fecha_creacion': usuario.fecha_creacion }, status=status.HTTP_201_CREATED) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def post(self, request): nome = request.data['nome'] nickname = request.data['nickname'] seguindo = request.data['seguindo'] seguidores = request.data['seguidores'] usuario = Usuario(nome= nome, nickname= nickname, seguindo= seguindo, seguidores= seguidores ) usuario.save() data = UsuarioSerializer(usuario).data return Response(data)
def post(self, request): username = request.data[ 'username'] # o que está em laranja é escrito no insomnia email = request.data['email'] password = request.data['password'] nome = request.data['nome'] is_admin = request.data['is_admin'] user = Usuario(nome=nome, email=email, username=username, is_admin=is_admin) user.set_password(password) validate = Usuario.objects.filter(username=username) validate_email = Usuario.objects.filter(email=email) if not (UsuarioSerializer(validate, many=True).data or UsuarioSerializer(validate_email, many=True).data): user.save() data = UsuarioSerializer(user).data return Response(data) else: return Response({'error': 'User already exists'})
def post(self, request, format = None): u = User.objects.create( username = request.data['login'], # Passamos o login email = request.data['email'] # Passamos o email ) u.set_password(request.data['senha']) request.data['user'] = u.id usuario = UsuarioSerializer(data = request.data) if usuario.is_valid(): try: usuario.save() except utils.IntegrityError: u.delete() return Response(usuario.errors, status=status.HTTP_400_BAD_REQUEST) #Falhou u.save() return Response(usuario.data, status=status.HTTP_201_CREATED) #Sucedeu u.delete() return Response(usuario.errors, status=status.HTTP_400_BAD_REQUEST) #Falhou
def get(self, request): usuario = Usuario.objects.all() data = UsuarioSerializer(usuario, many=True).data return Response(data)
def get(self, request, pk, format=None): usuario = self.get_object(pk) serializer = UsuarioSerializer(usuario) return Response(serializer.data)
def get(self, request, format=None): usuario = Usuario.objects.all() serializer = UsuarioSerializer(usuario, many=True) return Response(serializer.data)