def put(self, request): admin = JWTAuthentication.validate_token(request) serializer = UserStatusUpdateSerializer(data=request.data) if serializer.is_valid(): data = serializer.validated_data user_status = data.get('user_status') if JWTAuthentication.isAdmin(admin.id): try: criterion1 = Q(deleted_at__isnull=True) criterion2 = Q(id=data['id']) user_info = User.objects.get(criterion1 & criterion2) user_info.user_status_id = user_status user_info.save() user_info = User.objects.get(id=data['id']) name = user_info.first_name if user_info.user_status.name == "approved": body = "Hi {},\n Congratulations, we have approved your registration.\ '\n To Login, please use email id and password provided at the time of registration.".format(name) Util.send_email('Home Finder account update', body, '*****@*****.**', [user_info.email_id]) else: body = "Hi {},\n Sorry, as provide information does not qaulify our standards,\ your registration can not be approved.\ '\n Please contact support for more information".format(name) Util.send_email('Home Finder account update',body, '*****@*****.**', [user_info.email_id]) return JsonResponse({'message': 'Update successful'}, status=status.HTTP_201_CREATED) except Exception as ex: return JsonResponse({'message': str(ex)}, status=status.HTTP_404_NOT_FOUND) else: return JsonResponse({'message': 'Admin authentication failed'}, status=status.HTTP_412_PRECONDITION_FAILED) else: return JsonResponse(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def favorites_search_delete(request, favorite_search_id): user = JWTAuthentication.validate_token(request) FavoriteSearch.objects.filter( user=user, id=favorite_search_id).update(deleted_at=datetime.now()) return Response({"status": "favorite deleted"}, status=status.HTTP_204_NO_CONTENT)
def favorites_listing_delete(request, favorite_listing_id): user = JWTAuthentication.validate_token(request) FavoriteListing.objects.filter(id=favorite_listing_id, user=user).update(deleted_at=datetime.now()) return Response({"status": "favorite deleted"}, status=status.HTTP_204_NO_CONTENT)
def listings(request): user = JWTAuthentication.validate_token(request) if request.method == "POST": images_serializer = CreateImagesSerializer(data=dict(request.data)) listing_serializer = CreateListingSerializer(data=request.data) if listing_serializer.is_valid() and images_serializer.is_valid(): with transaction.atomic(): new_listing = listing_serializer.save(listed_by=user) images_serializer.save(listing=new_listing) create_open_house_schedules(request.data, new_listing) new_listing.save() return Response(ListingSerializer(new_listing).data, status=status.HTTP_201_CREATED) return Response( { "listing_errors": listing_serializer.errors, "image_errors": images_serializer.errors }, status=status.HTTP_400_BAD_REQUEST) else: # GET /listings user_listings = Listing.objects.filter(listed_by=user, deleted_at__isnull=True) return Response(ListingSerializer(user_listings, many=True).data, status=status.HTTP_200_OK)
def post(self, request): user = JWTAuthentication.validate_token(request) application_data = request.data['application_details'] home_listing_id = application_data['home_listing'] additional_info_serializer = CreateUserAdditionalInfoSerializer( data=request.data['user_details']) application_serializer = CreateApplicationSerializer( data=request.data['application_details']) if additional_info_serializer.is_valid(): if application_serializer.is_valid(): application = application_serializer.save(user=user) user_application = ApplicationSerializer(application[0]) user_additional_info = additional_info_serializer.save( user=user) user_info = UserAdditionalInfoSerializer( user_additional_info[0]) to_email = Listing.objects.get( id=home_listing_id).listed_by.email_id name = Listing.objects.get( id=home_listing_id).listed_by.first_name body = "Hi {},\n Congratukations, there is application update received to one of your posted listings." \ "\n Please navigate to your listings to review application".format(name) Util.send_email('Recieved New Application', body, '*****@*****.**', [to_email]) return JsonResponse( {'message': 'Application submitted successfully'}, status=status.HTTP_201_CREATED) else: return JsonResponse(application_serializer.errors, status=status.HTTP_400_BAD_REQUEST) else: return JsonResponse(additional_info_serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def get(self, request): admin = JWTAuthentication.validate_token(request) if JWTAuthentication.isAdmin(admin.id): try: criterion1 = ~Q(role__name ='admin') pending_users = User.objects.filter(criterion1) if pending_users: serializer = RetriveUsersSerializer(pending_users,many=True) response_data = {'message':'Users retrival succesful','users_list': serializer.data} return JsonResponse(response_data,status=status.HTTP_200_OK,safe=False) else: return JsonResponse({'message': 'No user found with pending status'}, status=status.HTTP_404_NOT_FOUND) except Exception as ex: return JsonResponse({'message': str(ex)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) else: return JsonResponse({'message': 'Admin authentication failed'}, status=status.HTTP_412_PRECONDITION_FAILED)
def post(self,request): data = request.data email_id = data.get('email_id') password = data.get('password') if email_id and password: try: criterion1 = Q(deleted_at__isnull=True) criterion2 = Q(email_id=email_id) user = User.objects.get(criterion1 & criterion2) if user.user_status.name == 'approved': encrypted_password = user.password res = check_password(password,encrypted_password) if res: access_token = JWTAuthentication.generate_access_token(user.id) user_serializer = UserLoginSerializer(user) response_data = {'message':'Login successful','user_details': user_serializer.data, 'token': access_token} return JsonResponse(response_data, status=status.HTTP_200_OK) else: return JsonResponse({'message': 'Invalid password'}, status=status.HTTP_412_PRECONDITION_FAILED) else: return JsonResponse({'message': 'Cannot login, User is not approved'}, status=status.HTTP_412_PRECONDITION_FAILED) except Exception as ex: return JsonResponse({'message': str(ex)}, status=status.HTTP_404_NOT_FOUND) else: return JsonResponse({'message': 'Provide login details'}, status=status.HTTP_400_BAD_REQUEST)
def delete_listing(request, listing_id): user = JWTAuthentication.validate_token(request) queryset = Listing.objects.filter(listed_by=user) listing = get_object_or_404(queryset, pk=listing_id) listing.deleted_at = datetime.now() listing.deleted_why = request.data.get("deleted_why", "user deleted") listing.save() return Response({"status": "Successfully deleted"}, status=status.HTTP_200_OK)
def get(self,request): user = JWTAuthentication.validate_token(request) try: user_account = UserAdditionalInfo.objects.get(user=user.id) serializer = UserAdditionalInfoSerializer(user_account) response_data = {'message':'User details retrival succesful','user_deatils': serializer.data} return JsonResponse(response_data,status=status.HTTP_200_OK) except: serializer = UserRegistrationSerializer(user) response_data = {'message':'User details retrival succesful','user_deatils': serializer.data} return JsonResponse(response_data,status=status.HTTP_200_OK)
def post(self, request): admin = JWTAuthentication.validate_token(request) serializer = UserStatusUpdateSerializer(data=request.data) if serializer.is_valid(): data = serializer.validated_data user_id = data.get('id') user_status = data.get('user_status') reason = data.get('deleted_why') if JWTAuthentication.isAdmin(admin.id): try: user = User.objects.get(id=user_id) user.user_status_id= user_status user.deleted_at = timezone.now() user.deleted_why = reason user.is_active = False user.save() return JsonResponse({'message': 'User deleted'}, status=status.HTTP_201_CREATED) except Exception as ex: return JsonResponse({'message': str(ex)}, status=status.HTTP_404_NOT_FOUND) else: return JsonResponse({'message': 'Admin authentication failed'}, status=status.HTTP_412_PRECONDITION_FAILED) else: return JsonResponse(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def get(self, request): user = JWTAuthentication.validate_token(request) criterion1 = Q(deleted_at__isnull=True) criterion2 = Q(user=user.id) application = Application.objects.filter(criterion1 & criterion2) if not application: return JsonResponse( {'message': 'User has not submitted any applications'}, status=status.HTTP_404_NOT_FOUND) else: serializer = UserApplicationSerializer(application, many=True) return JsonResponse(serializer.data, status=status.HTTP_200_OK, safe=False)
def update_listing(request, listing_id): user = JWTAuthentication.validate_token(request) queryset = Listing.objects.filter(listed_by=user) listing = get_object_or_404(queryset, pk=listing_id) request.data.pop("images") listing_serializer = CreateListingSerializer(listing, data=request.data) if listing_serializer.is_valid(): print("put is valid") updated_listing = listing_serializer.save() return Response(ListingSerializer(updated_listing).data, status=status.HTTP_200_OK) return Response(listing_serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def favorites_search(request): user = JWTAuthentication.validate_token(request) if request.method == 'POST': favorite_search_serializer = CreateFavoriteSearchSerializer( data=request.data) if favorite_search_serializer.is_valid(): favorite_search_serializer.save(user=user) return Response({"status": "favorite search created"}, status=status.HTTP_201_CREATED) else: favorite_search = FavoriteSearch.objects.filter( user=user, deleted_at__isnull=True) data = FavoriteSearchSerializer(favorite_search, many=True).data return Response(data, status=status.HTTP_200_OK)
def get(self, request): user = JWTAuthentication.validate_token(request) criterion1 = Q(deleted_at__isnull=True) listing_id = int(request.GET.get('listing_id', None)) criterion2 = Q(home_listing=listing_id) querylist = Application.objects.filter(criterion1 & criterion2) if not querylist: return JsonResponse( {'message': 'Home listing has no applications'}, status=status.HTTP_404_NOT_FOUND) else: serializer = ListHomeListingApplications(querylist, many=True) result = serializer.data for i, app in enumerate(querylist): user_info = UserAdditionalInfo.objects.get(user=app.user) user_info_serializer = ListUserAddDeatilsSerializer(user_info) result[i]['user_info'] = user_info_serializer.data return JsonResponse(result, status=status.HTTP_200_OK, safe=False)
def favorites_listing(request): user = JWTAuthentication.validate_token(request) if request.method == 'POST': favorite_listing_serializer = CreateFavoriteListingSerializer( data=request.data) if not FavoriteListing.objects.filter( user=user, listing_id=request.data.get("listing_id"), deleted_at__isnull=True).exists(): if favorite_listing_serializer.is_valid(): favorite_listing_serializer.save(user=user) return Response({"status": "listing marked favorite"}, status=status.HTTP_204_NO_CONTENT) favorite_listings = FavoriteListing.objects.filter(user=user, deleted_at__isnull=True) data = FavoriteListingSerializer(favorite_listings, many=True).data return Response(data, status=status.HTTP_200_OK)
def listing_schedule(request, listing_id): user = JWTAuthentication.validate_token(request) schedule_serializer = CreateListingScheduleSerializer(data=request.data) if schedule_serializer.is_valid(): try: if HomeSchedule.objects.filter( listing_id=listing_id, schedule_visits_date=request.data.get( "schedule_visits_date"), schedule_visits_time=request.data.get( "schedule_visits_time")).exists(): return Response({"status": "Schedule already booked"}, status=status.HTTP_400_BAD_REQUEST) schedule_serializer.save(scheduled_by=user, listing_id=listing_id) scheduled_listing = Listing.objects.get(id=listing_id) Util.send_email( 'Home Finder Schedule booking info', 'Congratulations, your schedule is confirmed' + '\n' + '\n' + 'Scheduled Date: {schedule_date}'.format( schedule_date=request.data.get("schedule_visits_date")) + '\n' + 'Scheduled Time: {schedule_time}'.format( schedule_time=request.data.get("schedule_visits_time")) + '\n' + '\n' + 'Scheduled listing address: ' + '\n' + '{street_addr}'.format( street_addr=scheduled_listing.street_address) + '\n' + '{city}'.format(city=scheduled_listing.city) + '\n' + '{state}'.format(state=scheduled_listing.state) + '\n' + '{zip_code}'.format(zip_code=scheduled_listing.zip_code) + '\n' + 'Cost of the listing: {price}'.format( price=scheduled_listing.price) + '\n' + 'Please contact the property owner if you need further details.', '*****@*****.**', [user.email_id]) except ObjectDoesNotExist as e: Response({}, status=status.HTTP_400_BAD_REQUEST) return Response({"status": "Successfully scheduled"}, status=status.HTTP_200_OK)
def put(self, request): owner = JWTAuthentication.validate_token(request) serializer = ApplicationStatusUpdateSerializer(data=request.data) if serializer.is_valid(): data = serializer.validated_data app_status = data.get('status') app_id = data.get('id') try: queryset = Application.objects.filter(deleted_at__isnull=True) application = get_object_or_404(queryset, id=app_id) application.status = app_status application.save() return JsonResponse( {'message': 'Applciation status update successful'}, status=status.HTTP_201_CREATED) except Exception as ex: return JsonResponse( {'message': str(ex)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) else: return JsonResponse(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def post(self, request): user = JWTAuthentication.validate_token(request) serializer = WithdrawApplicationSerializer(data=request.data) if serializer.is_valid(): data = serializer.validated_data app_id = data.get('id') app_status = data.get('status') reason = data.get('deleted_why') try: application = Application.objects.get(id=app_id) application.status = app_status application.deleted_at = timezone.now() application.deleted_why = reason application.save() return JsonResponse({'message': 'Application deleted'}, status=status.HTTP_201_CREATED) except Exception as ex: return JsonResponse({'message': str(ex)}, status=status.HTTP_404_NOT_FOUND) else: return JsonResponse(serializer.errors, status=status.HTTP_400_BAD_REQUEST)