def change_address_status(self, request: Request, pk: int): user = self.get_object() address_id = get_param_or_400(request.data, "address_id", int) active = get_param_or_400(request.data, "active", bool) address = user.addresses.get(id=address_id) address.active = active address.save() return Response()
def update_address(self, request: Request, pk: int): user = self.get_object() address_id = get_param_or_400(request.data, "user_address_id", int) neighborhood_id = get_param_or_400(request.data, "neighborhood_id", int) address = get_param_or_400(request.data, "address", str) zip_code = get_param_or_400(request.data, "zip", str) user_address = user.addresses.get(id=address_id) user_address.neighborhood_id = neighborhood_id user_address.zip_code = zip_code user_address.address = address user_address.save() return Response()
def reset_password(self, request: Request): user = User.objects.all() email = get_param_or_400(request.data, "email", str) try: user_reset_pass = user.get(email=email) if user_reset_pass: new_pass = generate_new_pass(user_reset_pass) user_reset_pass.set_password(new_pass) user_reset_pass.save() email_render_data = { 'user': user_reset_pass.first_name, 'new_password': new_pass } user_reset_pass.send_email( 'emails/user_reset_password.html', email_render_data, 'Conta Comigo APP - Recuperação de Senha') return Response( data=UserNewPasswordSerializer(user_reset_pass).data) # return Response(data={"password": new_pass}) except Exception as ex: raise ValueError(f"There is no register for {email}: {ex}") return Response()
def find_address_by_zip(self, request: Request): zip_code = get_param_or_400(request.query_params, "zip", str) if len(zip_code) != 8: raise ValueError(_("Zip code length should have exactly 8")) result_dict = { "city": {"id": None, "description": None}, "zip": None, "neighborhood": {"id": None, "description": None}, "state": {"id": None, "description": None, "initials": None}, "address": None, } try: address = Address.get_by_zip(zip_code) except Address.DoesNotExist: address = None if address: result_dict["city"]["id"] = address.neighborhood.city.id result_dict["city"]["description"] = address.neighborhood.city.description result_dict["zip"] = address.zip_code result_dict["neighborhood"]["id"] = address.neighborhood.id result_dict["neighborhood"][ "description" ] = address.neighborhood.description result_dict["state"]["id"] = address.neighborhood.city.state.id result_dict["state"][ "description" ] = address.neighborhood.city.state.description result_dict["state"]["initials"] = address.neighborhood.city.state.initials result_dict["address"] = address.description return Response(data=result_dict)
def add_address(self, request: Request, pk: int): user = self.get_object() neighborhood_id = get_param_or_400(request.data, "neighborhood_id", int) address = get_param_or_400(request.data, "address", str) zip_code = get_param_or_400(request.data, "zip", str) addresses_queryset = UserAddress.objects.all().select_related( "neighborhood", "neighborhood__city", "neighborhood__city__state") new_address = addresses_queryset.create( neighborhood_id=neighborhood_id, address=address, zip_code=zip_code) user.addresses.add(new_address) user.save() return Response(data=UserAddressSerializer(new_address).data)
def post(cls, request: Request, *args, **kwargs): """ Authenticate an user. """ username = get_param_or_400(request.data, "username", str) password = get_param_or_400(request.data, "password", str) print(f"Password for login method: {password}") ## Decrypt the password password = decrypt_pass(password) user = authenticate(username=username, password=password) if user: login(request, user) data = UserSerializer(user).data return Response(data=data) else: raise exceptions.AuthenticationFailed( _("Invalid username/password."))
def post(self, request: Request, *args, **kwargs): """ Authenticate an user returning a token. """ password = get_param_or_400(request.data, "password", str) # Decrypt the password password = decrypt_pass(password) request.data["password"] = password result = super().post(request, *args, **kwargs) return result
def cancel_request(self, request: Request, pk): # Lock database row to control concurrency in status update queryset = self.get_queryset() try: help_request = queryset.select_for_update().get(id=pk) except HelpRequest.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) reason_id = get_param_or_400(request.data, "reasonId", int) help_request.status_id = HelpRequestStatus.AllStatus.Canceled help_request.cancel_reason_id = reason_id help_request.save() return Response()