def get_alerts(request_data):
	if request_data:
		request_data = json.loads(request_data)
		email = putil.validate_for_user_id_exists(request_data.get("user_id"))
		alert = frappe.db.sql("select * from `tabAlerts` where user_id='{0}' order by creation desc limit 1".format(request_data.get("user_id")),as_dict=1)
		try:
			if alert:
				property_search_query = putil.generate_search_query(alert[0])
				new_query = property_search_query.get("query").get("bool").get("must")
				new_query.append({
								    "range" : {
								        "posted_datetime" : {
								            "gte": alert[0].get("creation").strftime("%Y-%m-%d %H:%M:%S"),
								        }
								    }
								})
				property_search_query["query"]["bool"]["must"] = new_query
			else:
				search_query = {	
									"sort": [{ "posted_datetime": { "order": "desc" }}],
									"query":{ "bool":{ "must":[ {"match":{ "user_id":request_data.get("user_id")  } } ] }    } 
								}						
				es = ElasticSearchController()
				response_data, total_records = es.search_document(["request"], search_query, 1, 1)
				if response_data:
					last_month_date = add_months(datetime.datetime.now() ,-1).strftime("%Y-%m-%d %H:%M:%S")
					property_search_query = response_data[0].get("search_query")
					property_search_query = ast.literal_eval(property_search_query.encode("utf8"))
					new_query = property_search_query.get("query").get("bool").get("must")
					new_query.append({
										    "range" : {
										        "posted_datetime" : {
										            "gte":last_month_date,
										        }
										    }
										})
					property_search_query["query"]["bool"]["must"] = new_query
				else:
					raise OperationFailed("No Alerts and Request Id found against User {0}".format(email))
			es = ElasticSearchController()
			response_data, total_records = es.search_document(["property"], property_search_query, request_data.get("page_number",1), request_data.get("records_per_page",40))
			from_record =  ((request_data.get("page_number",1) - 1) * cint(request_data.get("records_per_page",40)) + 1 )
			response_msg = "Property Found" if response_data else "Property not found"
			no_of_pages = math.ceil(flt(total_records)/request_data.get("records_per_page",40))
			return {"operation":"Search", "message":response_msg ,"total_records":total_records,"records_per_page":request_data.get("records_per_page",40),"from_record":from_record ,"to_record": from_record +  len(response_data) - 1 if response_data else from_record + request_data.get("records_per_page",40) - 1 ,"data":response_data, "user_id":request_data.get("user_id"), "no_of_pages":no_of_pages}
		except elasticsearch.ElasticsearchException,e:
			raise ElasticSearchException(e.error)
		except Exception,e:
			raise e
def get_alerts_based_on_last_request(request_data, email):
	
	"""
		Generate search query from last search request of that user &
		check for properties which are posted from last month only. 
	"""

	try:
		search_query = {	
							"sort": [{ "posted_datetime": { "order": "desc" }}],
							"query":{ "bool":{ "must":[ {"match":{ "user_id":request_data.get("user_id")  } } ] }    } 
						}						
		es = ElasticSearchController()
		response_data, total_records = es.search_document(["request"], search_query, 1, 1)
		if response_data:
			last_month_date = add_months(datetime.datetime.now() ,-1).strftime("%Y-%m-%d %H:%M:%S")
			property_search_query = response_data[0].get("search_query")
			property_search_query = ast.literal_eval(property_search_query.encode("utf8"))
			new_query = property_search_query.get("query").get("bool").get("must")
			new_query.append({
								    "range" : {
								        "modified_datetime" : {
								            "gte":last_month_date,
								        }
								    }
								})
			property_search_query["query"]["bool"]["must"] = new_query
			uom = response_data[0].get("unit_of_area")
			uom = "Sq.Ft." if uom not in ["Sq.Ft.", "Acres", "Hectares"] else uom
			return property_search_query,  uom
		else:
			raise OperationFailed("No Alerts and Request Id found against User {0}".format(email))
	except elasticsearch.ElasticsearchException,e:
		raise ElasticSearchException(e.error)
def get_similar_properties(request_data):
	if request_data:
		request_data = json.loads(request_data)
		email = putil.validate_for_user_id_exists(request_data.get("user_id"))
		putil.validate_property_data(request_data, ["request_type", "id"])
		search_dict = {"property_id":get_search_query_of_property_id ,"request_id":get_search_query_of_request_id}
		if request_data.get("request_type") not in ["property_id", "request_id"]:
			raise InvalidDataError("Request type contains Invalid Data")
		search_query, uom = search_dict.get(request_data.get("request_type"))(request_data)
		try:
			
			sp_include_fields = ["property_photo", "property_id", "location", "address",
			                      "city", "carpet_area", "price","property_title"]
			es = ElasticSearchController()
			response_data, total_records = es.search_document(["property"], search_query, request_data.get("page_number",1), request_data.get("records_per_page",4), [], sp_include_fields)
			uom = "Sq.Ft." if uom not in ["Sq.Ft.", "Acres", "Hectares"] else uom
			putil.convert_area_according_to_uom(response_data, uom)
			response_msg = "Similar Property Found" if response_data else "Similar property not found"
			return {
						"operation":"Search", 
						"message":response_msg ,
						"total_records":len(response_data),
						"data":response_data, 
						"user_id":request_data.get("user_id")
					}
		except elasticsearch.ElasticsearchException,e:
			raise ElasticSearchException(e.error)
		except Exception,e:
			raise OperationFailed("Get Similar property Operation Failed")	 
def get_user_properties(request_data):
	if request_data:
		request_data = json.loads(request_data)
		email = putil.validate_for_user_id_exists(request_data.get("user_id"))
		search_query =  { "query": { "match":{ "posted_by":request_data.get("user_id") } } }
		try:

			# fields_to_be_excluded from response and resultset generation 

			include_list = ["property_photo", "city", "location", "carpet_area", "amenities", "no_of_floors", "price", "status",
					"floor_no", "price_per_sq_ft", "property_id", "property_title", "tag", "possession_status", "property_subtype_option"]
			
			es = ElasticSearchController()
			response_data, total_records  = es.search_document(["property"], search_query, request_data.get("page_number",1), request_data.get("records_per_page",40), [], include_list)
			response_data = check_for_shortlisted_property(response_data, request_data.get("user_id"))
			putil.show_amenities_with_yes_status(response_data)

			# response data & pagination logic

			msg = "User Property Found" if len(response_data) else "User Property not found"
			return putil.init_pagination_and_response_generatrion(request_data, response_data, msg, total_records)

		except elasticsearch.ElasticsearchException,e:
			raise ElasticSearchException(e.error)
		except Exception,e:
			raise OperationFailed("Get User Properties Operation Failed")
def share_property(request_data):
	if request_data:
		request_data = json.loads(request_data)
		email = putil.validate_for_user_id_exists(request_data.get("user_id"))
		user_name = frappe.db.get_value("User", {"user_id":request_data.get("user_id")}, ["first_name", "last_name"],as_dict=True)
		putil.validate_property_data(request_data, ["comments", "email_id"])		
		try:
			property_ids_list = {  comment.get("property_id"):comment.get("comment","")  for comment in request_data.get("comments") if comment.get("property_id")}
			search_query = { "query":{ "ids":{ "values":property_ids_list.keys() } }} 
			es = ElasticSearchController()
			response_data, total_records = es.search_document(["property"], search_query, request_data.get("page_number",1), request_data.get("records_per_page",40))				
			if response_data:
				for response in response_data:
					response["comments"] = property_ids_list.get(response.get("property_id"),"")
				args = { "title":"Property Shared by  {0}".format(email) , "property_data":response_data ,"first_name":user_name.get("first_name"), "last_name":user_name.get("last_name")}
				send_email(request_data.get("email_id"), "Propshikari properties shared with you", "/templates/share_property_template.html", args)
				return { "operation":"Share", "message":"Property Shared"}
			else:
				raise DoesNotExistError("Property Id does not exists in elastic search")
		except frappe.OutgoingEmailError:
			raise OutgoingEmailError("Email can not be sent,Outgoing email error")
		except elasticsearch.TransportError:
			raise DoesNotExistError("Property Id does not exists")
		except elasticsearch.ElasticsearchException,e:
			raise ElasticSearchException(e.error)
		except Exception,e:
			raise OperationFailed("Share Property Operation Failed")
def get_shortlisted_property(request_data):
	if request_data:
		request_data = json.loads(request_data)
		
		# Check if shortlisted property existed against user 

		email = putil.validate_for_user_id_exists(request_data.get("user_id"))
		property_ids_list = frappe.db.get_values("Shortlisted Property", {"user_id":request_data.get("user_id"), "status":"Active"}, "property_id")
		if not property_ids_list:
			return {"operation":"Search", "message":"No Single Shortlisted property found", "user_id":request_data.get("user_id")}
		property_ids_list = [ property_id[0] for property_id in property_ids_list if property_id]
		try:
			# generate search_query and resultset & fields to be included in response 

			sp_include_fields= ["property_photo", "city", "location", "carpet_area", "amenities", "no_of_floors", 
								"price", "status","floor_no", "price_per_sq_ft", "property_id", "property_title", 
								"tag", "possession_status", "property_subtype_option", "unit_of_area", "property_age"]

			search_query = { "query":{ "ids":{ "values":property_ids_list } }  } 
			es = ElasticSearchController()
			response_data, total_records = es.search_document(["property"], search_query, request_data.get("page_number",1), 
																request_data.get("records_per_page",40), [], sp_include_fields)	
			
			# response data & pagination logic 

			append_shortlisted_tag(response_data)
			msg = "Shortlisted Property Found" if len(response_data) else "Shortlsited Property not found"
			return putil.init_pagination_and_response_generatrion(request_data, response_data, msg, total_records)

		except elasticsearch.ElasticsearchException,e:
			raise ElasticSearchException(e.error)
		except Exception,e:
		 	raise OperationFailed("Get Shortlisted Property Operation Failed")
def get_property_of_particular_tag(request_data):
	if request_data:
		request_data = json.loads(request_data)
		if not request_data.get("tag"):
			raise MandatoryError("Mandatory Field Tag missing")
		try:
			
			exclude_list = ["agent_name", "agent_no", "contact_no", "contact_person", "created_by", 
				"modified_by", "creation_date", "modified_date", "posted_datetime", "modified_datetime"]

			must_clause_list = [{ "match":{ "tag":request_data.get("tag") } }, { "match":{ "status":"Active" } } ] 	
			search_query = { "query":{ "bool":{ "must":must_clause_list } } } 
			es = ElasticSearchController()
			response_data, total_records = es.search_document(["property"], search_query, request_data.get("page_number",1), request_data.get("records_per_page",40),exclude_list)	
			putil.show_amenities_with_yes_status(response_data)

			msg = "Property found for specfied criteria" if len(response_data) else "Property not found"
			response_dict = putil.init_pagination_and_response_generatrion(request_data, response_data, msg, total_records)
			response_dict["tag"] = request_data.get("tag")
			return response_dict

		except elasticsearch.ElasticsearchException,e:
			raise ElasticSearchException(e.error)
		except Exception,e:
			raise OperationFailed("Get Tagged Property Operation Failed")
def search_unpublished_property(data):
	
	property_data = json.loads(data)
	
	try:	
		# generate search query & result generation & list of fields which should be excluded.

		exclude_list = putil.get_exclude_list_for_search(property_data.get("request_source", ""))

		#must_clause_list.append([{"match":{ "published_status": "Unpublished" } }, { "match": { "status": "Deactivated" }}])
		must_clause_list= [{"match":{ "published_status": "Unpublished" } }]
		search_query = { "query":{ "bool":{ "must":must_clause_list } }, "sort": [{ "posted_datetime": { "order": "desc" }}] }
		
		es = ElasticSearchController()
		response_data, total_records = es.search_document(["property"], search_query, property_data.get("page_number",1), 
										property_data.get("records_per_page",40), exclude_list)
		
		if property_data.get("user_id") != "Guest":				
			response_data = check_for_shortlisted_property(response_data,property_data.get("user_id"))
		response_data = putil.get_date_diff_and_count_from_posting(response_data)
		putil.convert_area_according_to_uom(response_data, property_data.get("unit_of_area", "Sq.Ft."))
		putil.show_amenities_with_yes_status(response_data)
		
		# response data & pagination logic

		msg = "Property found for specfied criteria" if len(response_data) else "Property not found"
		response_dict = putil.init_pagination_and_response_generatrion(property_data, response_data, msg, total_records)
		return response_dict

	except elasticsearch.RequestError,e:
		raise ElasticInvalidInputFormatError(e.error)
def get_location_details_for_map(request_data):
	request_data = json.loads(request_data)
	email = putil.validate_for_user_id_exists(request_data.get("user_id"))
	putil.validate_property_data(request_data, ["request_id"])
	try:
		es = ElasticSearchController()
		response = es.search_document_for_given_id("request",request_data.get("request_id"),[],
														["search_query", "unit_of_area", "adv_search_query"])
		search_query = ast.literal_eval(response.get("adv_search_query").encode("utf8"))
		uom = response.get("unit_of_area")

		include_fields = ["property_title", "property_id", "location", "property_subtype_option",
			                      "operation", "carpet_area", "price", "geo_location_lat", "geo_location_lon" ]

		size = get_count_of_property_records(es)            
		response_data, total_records = es.search_document(["property"], search_query, 1, size, [], include_fields)
		uom = "Sq.Ft." if uom not in ["Sq.Ft.", "Acres", "Hectares"] else uom
		putil.convert_area_according_to_uom(response_data, uom)
		response_msg = "User Property Found" if response_data else "User property not found"
		return {
					"operation":"Search",
					"user_id":request_data.get("user_id"),
					"message":response_msg,
					"data":response_data
				}
	except elasticsearch.TransportError:
		raise DoesNotExistError("Request Id does not exists")
	except elasticsearch.ElasticsearchException,e:
		raise ElasticSearchException(e.error)
Beispiel #10
0
def get_builder_projects(request_data):
    if request_data:
        request_data = json.loads(request_data)
        email = putil.validate_for_user_id_exists(request_data.get("user_id"))
        search_query = {
            "query": {
                "match": {
                    "posted_by": request_data.get("user_id")
                }
            }
        }
        try:

            es = ElasticSearchController()
            size = get_count_of_project_records(es)
            response_data, total_records = es.search_document(
                ["project"], search_query, request_data.get("page_number",
                                                            1), size)

            # response data & pagination logic

            msg = "User Project Found" if len(
                response_data) else "User Project not found"
            return putil.init_pagination_and_response_generatrion(
                request_data, response_data, msg, total_records)

        except elasticsearch.ElasticsearchException, e:
            raise ElasticSearchException(e.error)
        except Exception, e:
            raise OperationFailed("Get Builder Properties Operation Failed")
def get_location_details_for_map(request_data):
	request_data = json.loads(request_data)
	email = putil.validate_for_user_id_exists(request_data.get("user_id"))
	putil.validate_property_data(request_data, ["request_id"])
	try:
		es = ElasticSearchController()
		response = es.search_document_for_given_id("request",request_data.get("request_id"),[],
														["search_query", "unit_of_area", "adv_search_query"])
		search_query = ast.literal_eval(response.get("adv_search_query").encode("utf8"))
		uom = response.get("unit_of_area")

		include_fields = ["property_title", "property_id", "location", "property_subtype_option",
			                      "operation", "carpet_area", "price", "geo_location_lat", "geo_location_lon" ]

		size = get_count_of_property_records(es)            
		response_data, total_records = es.search_document(["property"], search_query, 1, size, [], include_fields)
		uom = "Sq.Ft." if uom not in ["Sq.Ft.", "Acres", "Hectares"] else uom
		putil.convert_area_according_to_uom(response_data, uom)
		response_msg = "User Property Found" if response_data else "User property not found"
		return {
					"operation":"Search",
					"user_id":request_data.get("user_id"),
					"message":response_msg,
					"data":response_data
				}
	except elasticsearch.TransportError:
		raise DoesNotExistError("Request Id does not exists")
	except elasticsearch.ElasticsearchException,e:
		raise ElasticSearchException(e.error)
def get_shortlisted_property(request_data):
	if request_data:
		request_data = json.loads(request_data)
		
		# Check if shortlisted property existed against user 

		email = putil.validate_for_user_id_exists(request_data.get("user_id"))
		property_ids_list = frappe.db.get_values("Shortlisted Property", {"user_id":request_data.get("user_id"), "status":"Active"}, "property_id")
		if not property_ids_list:
			return {"operation":"Search", "message":"No Single Shortlisted property found", "user_id":request_data.get("user_id")}
		property_ids_list = [ property_id[0] for property_id in property_ids_list if property_id]
		try:
			# generate search_query and resultset & fields to be included in response 

			sp_include_fields= ["property_photo", "city", "location", "carpet_area", "amenities", "no_of_floors", 
								"price", "status","floor_no", "price_per_sq_ft", "property_id", "property_title", 
								"tag", "possession_status", "property_subtype_option", "unit_of_area", "property_age"]

			search_query = { "query":{ "ids":{ "values":property_ids_list } }  } 
			es = ElasticSearchController()
			response_data, total_records = es.search_document(["property"], search_query, request_data.get("page_number",1), 
																request_data.get("records_per_page",40), [], sp_include_fields)	
			
			# response data & pagination logic 

			append_shortlisted_tag(response_data)
			msg = "Shortlisted Property Found" if len(response_data) else "Shortlsited Property not found"
			return putil.init_pagination_and_response_generatrion(request_data, response_data, msg, total_records)

		except elasticsearch.ElasticsearchException,e:
			raise ElasticSearchException(e.error)
		except Exception,e:
		 	raise OperationFailed("Get Shortlisted Property Operation Failed")
Beispiel #13
0
def search_project(request_data):
	if request_data:
		project_data = json.loads(request_data)
		project_data = putil.validate_property_posting_data(project_data,"property_json/project_search.json")
		putil.isolate_city_from_location(project_data)
		search_query = putil.generate_project_search_query(project_data)
		try:
			es = ElasticSearchController()
			response_data, total_records = es.search_document(["project"], search_query, project_data.get("page_number",1), project_data.get("records_per_page",40))
			request_id = store_request_in_elastic_search(project_data, search_query, "Project Search")
			response_data = putil.get_date_diff_from_posting(response_data)
			response_msg = "Project found for specfied criteria" if len(response_data) else "Project not found"
			from_record = (project_data.get("page_number",1) - 1) * cint(project_data.get("records_per_page",40)) + 1
			no_of_pages = math.ceil(flt(total_records)/project_data.get("records_per_page",40))
			return {	
						"operation":"Search",
						 "message":response_msg ,
						 "total_records":total_records, 
						 "request_id":request_id, 
						 "records_per_page":project_data.get("records_per_page",40),
						 "from_record":from_record ,
						 "to_record":from_record +  len(response_data) - 1 if response_data else from_record + project_data.get("records_per_page",40) - 1,
						 "data":response_data, 
						 "user_id":project_data.get("user_id"), 
						 "no_of_pages":no_of_pages
					 }
		except elasticsearch.RequestError,e:
			raise ElasticInvalidInputFormatError(e.error)
		except elasticsearch.ElasticsearchException,e:
			raise ElasticSearchException(e.error)
def search_unpublished_property(data):
	
	property_data = json.loads(data)
	
	try:	
		# generate search query & result generation & list of fields which should be excluded.

		exclude_list = putil.get_exclude_list_for_search(property_data.get("request_source", ""))

		#must_clause_list.append([{"match":{ "published_status": "Unpublished" } }, { "match": { "status": "Deactivated" }}])
		must_clause_list= [{"match":{ "published_status": "Unpublished" } }]
		search_query = { "query":{ "bool":{ "must":must_clause_list } }, "sort": [{ "posted_datetime": { "order": "desc" }}] }
		
		es = ElasticSearchController()
		response_data, total_records = es.search_document(["property"], search_query, property_data.get("page_number",1), 
										property_data.get("records_per_page",40), exclude_list)
		
		if property_data.get("user_id") != "Guest":				
			response_data = check_for_shortlisted_property(response_data,property_data.get("user_id"))
		response_data = putil.get_date_diff_and_count_from_posting(response_data)
		putil.convert_area_according_to_uom(response_data, property_data.get("unit_of_area", "Sq.Ft."))
		putil.show_amenities_with_yes_status(response_data)
		
		# response data & pagination logic

		msg = "Property found for specfied criteria" if len(response_data) else "Property not found"
		response_dict = putil.init_pagination_and_response_generatrion(property_data, response_data, msg, total_records)
		return response_dict

	except elasticsearch.RequestError,e:
		raise ElasticInvalidInputFormatError(e.error)
def get_user_properties(request_data):
	if request_data:
		request_data = json.loads(request_data)
		email = putil.validate_for_user_id_exists(request_data.get("user_id"))
		search_query =  { "query": { "match":{ "posted_by":request_data.get("user_id") } } }
		try:

			# fields_to_be_excluded from response and resultset generation 

			include_list = ["property_photo", "city", "location", "carpet_area", "amenities", "no_of_floors", "price", "status",
					"floor_no", "price_per_sq_ft", "property_id", "property_title", "tag", "possession_status", "property_subtype_option"]
			
			es = ElasticSearchController()
			response_data, total_records  = es.search_document(["property"], search_query, request_data.get("page_number",1), request_data.get("records_per_page",40), [], include_list)
			response_data = check_for_shortlisted_property(response_data, request_data.get("user_id"))
			putil.show_amenities_with_yes_status(response_data)

			# response data & pagination logic

			msg = "User Property Found" if len(response_data) else "User Property not found"
			return putil.init_pagination_and_response_generatrion(request_data, response_data, msg, total_records)

		except elasticsearch.ElasticsearchException,e:
			raise ElasticSearchException(e.error)
		except Exception,e:
			raise OperationFailed("Get User Properties Operation Failed")
def get_similar_properties(request_data):
	if request_data:
		request_data = json.loads(request_data)
		email = putil.validate_for_user_id_exists(request_data.get("user_id"))
		putil.validate_property_data(request_data, ["request_type", "id"])
		search_dict = {"property_id":get_search_query_of_property_id ,"request_id":get_search_query_of_request_id}
		if request_data.get("request_type") not in ["property_id", "request_id"]:
			raise InvalidDataError("Request type contains Invalid Data")
		search_query, uom = search_dict.get(request_data.get("request_type"))(request_data)
		try:
			
			sp_include_fields = ["property_photo", "property_id", "location", "address",
			                      "city", "carpet_area", "price","property_title"]
			es = ElasticSearchController()
			response_data, total_records = es.search_document(["property"], search_query, request_data.get("page_number",1), request_data.get("records_per_page",4), [], sp_include_fields)
			uom = "Sq.Ft." if uom not in ["Sq.Ft.", "Acres", "Hectares"] else uom
			putil.convert_area_according_to_uom(response_data, uom)
			response_msg = "Similar Property Found" if response_data else "Similar property not found"
			return {
						"operation":"Search", 
						"message":response_msg ,
						"total_records":len(response_data),
						"data":response_data, 
						"user_id":request_data.get("user_id")
					}
		except elasticsearch.ElasticsearchException,e:
			raise ElasticSearchException(e.error)
		except Exception,e:
			raise OperationFailed("Get Similar property Operation Failed")	 
def share_property(request_data):
	if request_data:
		request_data = json.loads(request_data)
		email = putil.validate_for_user_id_exists(request_data.get("user_id"))
		user_name = frappe.db.get_value("User", {"user_id":request_data.get("user_id")}, ["first_name", "last_name"],as_dict=True)
		putil.validate_property_data(request_data, ["comments", "email_id"])		
		try:
			property_ids_list = {  comment.get("property_id"):comment.get("comment","")  for comment in request_data.get("comments") if comment.get("property_id")}
			search_query = { "query":{ "ids":{ "values":property_ids_list.keys() } }} 
			es = ElasticSearchController()
			response_data, total_records = es.search_document(["property"], search_query, request_data.get("page_number",1), request_data.get("records_per_page",40))				
			if response_data:
				for response in response_data:
					response["comments"] = property_ids_list.get(response.get("property_id"),"")
				args = { "title":"Property Shared by  {0}".format(email) , "property_data":response_data ,"first_name":user_name.get("first_name"), "last_name":user_name.get("last_name")}
				send_email(request_data.get("email_id"), "Propshikari properties shared with you", "/templates/share_property_template.html", args)
				return { "operation":"Share", "message":"Property Shared"}
			else:
				raise DoesNotExistError("Property Id does not exists in elastic search")
		except frappe.OutgoingEmailError:
			raise OutgoingEmailError("Email can not be sent,Outgoing email error")
		except elasticsearch.TransportError:
			raise DoesNotExistError("Property Id does not exists")
		except elasticsearch.ElasticsearchException,e:
			raise ElasticSearchException(e.error)
		except Exception,e:
			raise OperationFailed("Share Property Operation Failed")
def get_alerts_based_on_last_request(request_data, email):
	
	"""
		Generate search query from last search request of that user &
		check for properties which are posted from last month only. 
	"""

	try:
		search_query = {	
							"sort": [{ "posted_datetime": { "order": "desc" }}],
							"query":{ "bool":{ "must":[ {"match":{ "user_id":request_data.get("user_id")  } } ] }    } 
						}						
		es = ElasticSearchController()
		response_data, total_records = es.search_document(["request"], search_query, 1, 1)
		if response_data:
			last_month_date = add_months(datetime.datetime.now() ,-1).strftime("%Y-%m-%d %H:%M:%S")
			property_search_query = response_data[0].get("search_query")
			property_search_query = ast.literal_eval(property_search_query.encode("utf8"))
			new_query = property_search_query.get("query").get("bool").get("must")
			new_query.append({
								    "range" : {
								        "modified_datetime" : {
								            "gte":last_month_date,
								        }
								    }
								})
			property_search_query["query"]["bool"]["must"] = new_query
			uom = response_data[0].get("unit_of_area")
			uom = "Sq.Ft." if uom not in ["Sq.Ft.", "Acres", "Hectares"] else uom
			return property_search_query,  uom
		else:
			raise OperationFailed("No Alerts and Request Id found against User {0}".format(email))
	except elasticsearch.ElasticsearchException,e:
		raise ElasticSearchException(e.error)
def get_property_of_particular_tag(request_data):
	if request_data:
		request_data = json.loads(request_data)
		if not request_data.get("tag"):
			raise MandatoryError("Mandatory Field Tag missing")
		try:
			
			exclude_list = ["agent_name", "agent_no", "contact_no", "contact_person", "created_by", 
				"modified_by", "creation_date", "modified_date", "posted_datetime", "modified_datetime"]

			must_clause_list = [{ "match":{ "tag":request_data.get("tag") } }, { "match":{ "status":"Active" } } ] 	
			search_query = { "query":{ "bool":{ "must":must_clause_list } } } 
			es = ElasticSearchController()
			response_data, total_records = es.search_document(["property"], search_query, request_data.get("page_number",1), request_data.get("records_per_page",40),exclude_list)	
			putil.show_amenities_with_yes_status(response_data)

			msg = "Property found for specfied criteria" if len(response_data) else "Property not found"
			response_dict = putil.init_pagination_and_response_generatrion(request_data, response_data, msg, total_records)
			response_dict["tag"] = request_data.get("tag")
			return response_dict

		except elasticsearch.ElasticsearchException,e:
			raise ElasticSearchException(e.error)
		except Exception,e:
			raise OperationFailed("Get Tagged Property Operation Failed")
def get_user_properties(request_data):
	if request_data:
		request_data = json.loads(request_data)
		email = putil.validate_for_user_id_exists(request_data.get("user_id"))
		search_query =  { "query": { "match":{ "posted_by":request_data.get("user_id") } } }
		try:
			es = ElasticSearchController()
			response_data, total_records  = es.search_document(["property"], search_query, request_data.get("page_number",1), request_data.get("records_per_page",40))
			response_data = check_for_shortlisted_property(response_data, request_data.get("user_id"))
			response_msg = "User Property Found" if len(response_data) else "User Property not found"
			from_record = (request_data.get("page_number",1) - 1) * cint(request_data.get("records_per_page",40)) + 1 
			return {"operation":"Search", "message":response_msg ,"total_records":total_records, "records_per_page":request_data.get("records_per_page",40),"from_record":from_record ,"to_record": from_record +  len(response_data) - 1 if response_data else from_record + request_data.get("records_per_page",40) - 1,"data":response_data, "user_id":request_data.get("user_id")}
		except elasticsearch.ElasticsearchException,e:
			raise ElasticSearchException(e.error)
		except Exception,e:
			raise OperationFailed("Get User Properties Operation Failed")
def get_property_of_particular_tag(request_data):
	if request_data:
		request_data = json.loads(request_data)
		if not request_data.get("tag"):
			raise MandatoryError("Mandatory Field Tag missing")
		try:
			search_query = { "query":{ "match":{ "tag":request_data.get("tag") } }  } 
			es = ElasticSearchController()
			response_data, total_records = es.search_document(["property"], search_query, request_data.get("page_number",1), request_data.get("records_per_page",40))	
			response_msg = "Property found for specfied criteria" if len(response_data) else "Property not found"
			from_record = (request_data.get("page_number",1) - 1) * cint(request_data.get("records_per_page",40)) + 1
			return {"operation":"Search", "message":response_msg ,"total_records":total_records, "records_per_page":request_data.get("records_per_page",40),"from_record":from_record ,"to_record": from_record +  len(response_data) - 1 if response_data else from_record + request_data.get("records_per_page",40) - 1,"data":response_data, "user_id":request_data.get("user_id"), "tag":request_data.get("tag")}
		except elasticsearch.ElasticsearchException,e:
			raise ElasticSearchException(e.error)
		except Exception,e:
			raise OperationFailed("Get Tagged Property Operation Failed")
def get_all_projects(request_data):
	if request_data:
		request_data = json.loads(request_data)
		email = putil.validate_for_user_id_exists(request_data.get("user_id"))
		search_query =  { }
		try:
			include_list = ["project_id", "overview"]
			es = ElasticSearchController()
			size = get_count_of_project_records(es)
			response_data, total_records  = es.search_document(["project"], search_query, request_data.get("page_number",1), size, [], include_list)

			# response data & pagination logic

			msg = "Projects Found" if len(response_data) else "Projects not found"
			return putil.init_pagination_and_response_generatrion(request_data, response_data, msg, total_records)

		except elasticsearch.ElasticsearchException,e:
			raise ElasticSearchException(e.error)
		except Exception,e:
			raise OperationFailed("Get All Projects Operation Failed")
def get_builder_projects(request_data):
	if request_data:
		request_data = json.loads(request_data)
		email = putil.validate_for_user_id_exists(request_data.get("user_id"))
		search_query =  { "query": { "match":{ "posted_by":request_data.get("user_id") } } }
		try:
			
			es = ElasticSearchController()
			size = get_count_of_project_records(es)
			response_data, total_records  = es.search_document(["project"], search_query, request_data.get("page_number",1), size)

			# response data & pagination logic

			msg = "User Project Found" if len(response_data) else "User Project not found"
			return putil.init_pagination_and_response_generatrion(request_data, response_data, msg, total_records)

		except elasticsearch.ElasticsearchException,e:
			raise ElasticSearchException(e.error)
		except Exception,e:
			raise OperationFailed("Get Builder Properties Operation Failed")
def get_similar_properties(request_data):
	if request_data:
		request_data = json.loads(request_data)
		email = putil.validate_for_user_id_exists(request_data.get("user_id"))
		putil.validate_property_data(request_data, ["request_type", "id"])
		search_dict = {"property_id":get_search_query_of_property_id ,"request_id":get_search_query_of_request_id}
		if request_data.get("request_type") not in ["property_id", "request_id"]:
			raise InvalidDataError("Request type contains Invalid Data")
		search_query = search_dict.get(request_data.get("request_type"))(request_data)
		try:
			es = ElasticSearchController()
			response_data, total_records = es.search_document(["property"], search_query, request_data.get("page_number",1), request_data.get("records_per_page",3))
			from_record =  ((request_data.get("page_number",1) - 1) * cint(request_data.get("records_per_page",3)) + 1 )
			response_msg = "Similar Property Found" if response_data else "Similar property not found"
			no_of_pages = math.ceil(flt(total_records)/request_data.get("records_per_page",3))
			return {"operation":"Search", "message":response_msg ,"total_records":total_records,"records_per_page":request_data.get("records_per_page",3),"from_record":from_record ,"to_record": from_record +  len(response_data) - 1 if response_data else from_record + request_data.get("records_per_page",3) - 1 ,"data":response_data, "user_id":request_data.get("user_id"), "no_of_pages":no_of_pages}
		except elasticsearch.ElasticsearchException,e:
			raise ElasticSearchException(e.error)
		except Exception,e:
			raise OperationFailed("Get Similar property Operation Failed")	 
def get_all_projects(request_data):
	if request_data:
		request_data = json.loads(request_data)
		email = putil.validate_for_user_id_exists(request_data.get("user_id"))
		search_query =  { }
		try:
			include_list = ["project_id", "project_name"]
			es = ElasticSearchController()
			size = get_count_of_project_records(es)
			response_data, total_records  = es.search_document(["project"], search_query, request_data.get("page_number",1), size, [], include_list)

			# response data & pagination logic

			msg = "Projects Found" if len(response_data) else "Projects not found"
			return putil.init_pagination_and_response_generatrion(request_data, response_data, msg, total_records)

		except elasticsearch.ElasticsearchException,e:
			raise ElasticSearchException(e.error)
		except Exception,e:
			raise OperationFailed("Get All Projects Operation Failed")
def search_property(data):
	
	property_data = json.loads(data)
	property_data = putil.validate_property_posting_data(property_data, "property_json/property_search.json")
	putil.init_for_location_or_city_creation(property_data)
	putil.convert_area_to_sqft_for_search(property_data)
	try:	
		
		# generate search query & result generation & list of fields which should be excluded.

		exclude_list = putil.get_exclude_list_for_search(property_data.get("request_source", ""))

		search_query = putil.generate_search_query(property_data)
		adv_search_query = search_query
		search_query = cstr(search_query)
		putil.generate_advance_search_query(adv_search_query, property_data)

		es = ElasticSearchController()
		response_data, total_records = es.search_document(["property"], adv_search_query, property_data.get("page_number",1), 
										property_data.get("records_per_page",40), exclude_list)
		
		# post property-search operation

		if not property_data.get("request_id"):	
			request_id = store_request_in_elastic_search(property_data, search_query, "Property Search", adv_search_query)
		if property_data.get("user_id") != "Guest":				
			response_data = check_for_shortlisted_property(response_data,property_data.get("user_id"))
		response_data = putil.get_date_diff_and_count_from_posting(response_data)
		putil.convert_area_according_to_uom(response_data, property_data.get("unit_of_area", "Sq.Ft."))
		putil.show_amenities_with_yes_status(response_data)
		
		# response data & pagination logic

		msg = "Property found for specfied criteria" if len(response_data) else "Property not found"
		response_dict = putil.init_pagination_and_response_generatrion(property_data, response_data, msg, total_records)
		response_dict["request_id"] = property_data.get("request_id") if property_data.get("request_id") else request_id 
		return response_dict

	except elasticsearch.RequestError,e:
		raise ElasticInvalidInputFormatError(e.error)
Beispiel #27
0
def search_project(request_data):
	if request_data:
		project_data = json.loads(request_data)
		project_data = putil.validate_property_posting_data(project_data,"property_json/project_search.json")
		putil.init_for_location_or_city_creation(project_data)
		search_query = putil.generate_project_search_query(project_data)
		try:
			project_fields = get_project_include_fields(project_data.get("request_source", ""))
			exclude_list = putil.get_exclude_list_for_search(project_data.get("request_source", ""))

			es = ElasticSearchController()
			response_data, total_records = es.search_document(["project"], search_query, project_data.get("page_number",1), project_data.get("records_per_page",40), exclude_list, project_fields)
			if not project_data.get("request_id"):	
				request_id = store_request_in_elastic_search(project_data, search_query, "Project Search")
			response_data = putil.get_date_diff_and_count_from_posting(response_data)
			putil.show_amenities_with_yes_status(response_data)
			# property_subtype_option = project_data.get("property_subtype_option","") 
			# get_valid_property_subtype_option(response_data, property_subtype_option) if property_subtype_option else ""


			response_msg = "Project found for specfied criteria" if len(response_data) else "Project not found"
			from_record = (project_data.get("page_number",1) - 1) * cint(project_data.get("records_per_page",40)) + 1
			no_of_pages = math.ceil(flt(total_records)/project_data.get("records_per_page",40))
			to_record = from_record +  len(response_data) - 1 if response_data else from_record + project_data.get("records_per_page",40) - 1
			return {	
						"operation":"Search",
						 "message":response_msg ,
						 "total_records":total_records, 
						 "request_id":project_data.get("request_id") if project_data.get("request_id") else request_id, 
						 "records_per_page":project_data.get("records_per_page",40),
						 "from_record":from_record ,
						 "to_record":to_record,
						 "data":response_data, 
						 "user_id":project_data.get("user_id"), 
						 "no_of_pages":no_of_pages
					 }
		except elasticsearch.RequestError,e:
			raise ElasticInvalidInputFormatError(e.error)
		except elasticsearch.ElasticsearchException,e:
			raise ElasticSearchException(e.error)
def search_property(data):
	
	property_data = json.loads(data)
	property_data = putil.validate_property_posting_data(property_data, "property_json/property_search.json")
	putil.init_for_location_or_city_creation(property_data)
	putil.convert_area_to_sqft_for_search(property_data)
	try:	
		
		# generate search query & result generation & list of fields which should be excluded.

		exclude_list = putil.get_exclude_list_for_search(property_data.get("request_source", ""))

		search_query = putil.generate_search_query(property_data)
		adv_search_query = search_query
		search_query = cstr(search_query)
		putil.generate_advance_search_query(adv_search_query, property_data)

		es = ElasticSearchController()
		response_data, total_records = es.search_document(["property"], adv_search_query, property_data.get("page_number",1), 
										property_data.get("records_per_page",40), exclude_list)
		
		# post property-search operation

		if not property_data.get("request_id"):	
			request_id = store_request_in_elastic_search(property_data, search_query, "Property Search", adv_search_query)
		if property_data.get("user_id") != "Guest":				
			response_data = check_for_shortlisted_property(response_data,property_data.get("user_id"))
		response_data = putil.get_date_diff_and_count_from_posting(response_data)
		putil.convert_area_according_to_uom(response_data, property_data.get("unit_of_area", "Sq.Ft."))
		putil.show_amenities_with_yes_status(response_data)
		
		# response data & pagination logic

		msg = "Property found for specfied criteria" if len(response_data) else "Property not found"
		response_dict = putil.init_pagination_and_response_generatrion(property_data, response_data, msg, total_records)
		response_dict["request_id"] = property_data.get("request_id") if property_data.get("request_id") else request_id 
		return response_dict

	except elasticsearch.RequestError,e:
		raise ElasticInvalidInputFormatError(e.error)
def get_shortlisted_property(request_data):
	if request_data:
		request_data = json.loads(request_data)
		email = putil.validate_for_user_id_exists(request_data.get("user_id"))
		property_ids_list = frappe.db.get_values("Shortlisted Property", {"user_id":request_data.get("user_id")}, "property_id")
		if not property_ids_list:
			return {"operation":"Search", "message":"No Single Shortlisted property found", "user_id":request_data.get("user_id")}
		property_ids_list = [ property_id[0] for property_id in property_ids_list if property_id]
		try:
			search_query = { "query":{ "ids":{ "values":property_ids_list } }  } 
			es = ElasticSearchController()
			response_data, total_records = es.search_document(["property"], search_query, request_data.get("page_number",1), request_data.get("records_per_page",40))	
			for response in response_data:
				new_list = response.get("tag",[])
				new_list.append("Shortlisted")
				response["tag"] = new_list
			response_msg = "Shortlisted Property Found" if len(response_data) else "Shortlsited Property not found"
			from_record = (request_data.get("page_number",1) - 1) * cint(request_data.get("records_per_page",40)) + 1
			return {"operation":"Search", "message":response_msg ,"total_records":total_records, "records_per_page":request_data.get("records_per_page",40),"from_record":from_record ,"to_record": from_record +  len(response_data) - 1 if response_data else from_record + request_data.get("records_per_page",40) - 1,"data":response_data, "user_id":request_data.get("user_id")}
		except elasticsearch.ElasticsearchException,e:
			raise ElasticSearchException(e.error)
		except Exception,e:
		 	raise OperationFailed("Get Shortlisted Property Operation Failed")
def get_alerts(request_data):
	
	"""
		Get property alerts based on two types
		 1. Check if alert is already set against that user else
		 2. Check for last search request & based on that search request
		    show properties to user. 
	"""

	request_data = json.loads(request_data)
	email = putil.validate_for_user_id_exists(request_data.get("user_id"))
	alert = frappe.db.sql("""	select * from `tabAlerts` where user_id='{0}' 
								order by creation desc limit 1""".format(request_data.get("user_id")),as_dict=1)
	try:
		if alert:
			property_search_query, uom = get_alerts_based_on_alert_doctype(alert)
		else:
			property_search_query, uom = get_alerts_based_on_last_request(request_data, email)
		
		# fields to be included in response and resultset generation

		include_fields_list = ["property_id", "property_title", "price", "property_photo", "location", 
								"city", "carpet_area", "unit_of_area", "posted_datetime", "modified_datetime"]		
		es = ElasticSearchController()
		response_data, total_records = es.search_document(["property"], property_search_query, request_data.get("page_number",1), 
										request_data.get("records_per_page",40), [], include_fields_list)
		putil.convert_area_according_to_uom(response_data, uom)
		response_data = modify_alert_resultset(response_data, request_data.get("user_id"))

		#  response generation and pagination logic 

		msg = "Property Found" if response_data else "Property not found"
		return putil.init_pagination_and_response_generatrion(request_data, response_data, msg, total_records)

	except elasticsearch.ElasticsearchException,e:
		raise ElasticSearchException(e.error)
def get_alerts(request_data):
	
	"""
		Get property alerts based on two types
		 1. Check if alert is already set against that user else
		 2. Check for last search request & based on that search request
		    show properties to user. 
	"""

	request_data = json.loads(request_data)
	email = putil.validate_for_user_id_exists(request_data.get("user_id"))
	alert = frappe.db.sql("""	select * from `tabAlerts` where user_id='{0}' 
								order by creation desc limit 1""".format(request_data.get("user_id")),as_dict=1)
	try:
		if alert:
			property_search_query, uom = get_alerts_based_on_alert_doctype(alert)
		else:
			property_search_query, uom = get_alerts_based_on_last_request(request_data, email)
		
		# fields to be included in response and resultset generation

		include_fields_list = ["property_id", "property_title", "price", "property_photo", "location", 
								"city", "carpet_area", "unit_of_area", "posted_datetime", "modified_datetime"]		
		es = ElasticSearchController()
		response_data, total_records = es.search_document(["property"], property_search_query, request_data.get("page_number",1), 
										request_data.get("records_per_page",40), [], include_fields_list)
		putil.convert_area_according_to_uom(response_data, uom)
		response_data = modify_alert_resultset(response_data, request_data.get("user_id"))

		#  response generation and pagination logic 

		msg = "Property Found" if response_data else "Property not found"
		return putil.init_pagination_and_response_generatrion(request_data, response_data, msg, total_records)

	except elasticsearch.ElasticsearchException,e:
		raise ElasticSearchException(e.error)
def get_all_property_data(data):
	es = ElasticSearchController()
	response_data, total_records = es.search_document(["property"], "", 1, 1000)
	return {"total_records":total_records, "data":response_data}
Beispiel #33
0
def get_all_property_data(data):
    es = ElasticSearchController()
    response_data, total_records = es.search_document(["property"], "", 1,
                                                      1000)
    return {"total_records": total_records, "data": response_data}