|
|
|
|
|
from datetime import datetime |
|
from .logging_config import logger |
|
|
|
def analyze_market_value(data): |
|
""" |
|
Analyzes the market value of a property based on its specifications and location |
|
for the Indian real estate market. |
|
""" |
|
specs_verification = { |
|
'is_valid': True, |
|
'bedrooms_reasonable': True, |
|
'bathrooms_reasonable': True, |
|
'total_rooms_reasonable': True, |
|
'parking_reasonable': True, |
|
'sq_ft_reasonable': True, |
|
'market_value_reasonable': True, |
|
'year_built_reasonable': True, |
|
'issues': [] |
|
} |
|
|
|
try: |
|
|
|
valid_property_types = [ |
|
'Apartment', 'House', 'Villa', 'Independent House', 'Independent Villa', |
|
'Studio', 'Commercial', 'Office', 'Shop', 'Warehouse', 'Industrial' |
|
] |
|
|
|
property_type = str(data.get('property_type', '')).strip() |
|
if not property_type or property_type not in valid_property_types: |
|
specs_verification['is_valid'] = False |
|
specs_verification['issues'].append(f"Invalid property type: {property_type if property_type else 'Not specified'}") |
|
|
|
|
|
if 'bedrooms' in data: |
|
try: |
|
bedrooms = int(float(data['bedrooms'])) |
|
if property_type in ['Apartment', 'Studio']: |
|
if bedrooms > 5 or bedrooms < 0: |
|
specs_verification['bedrooms_reasonable'] = False |
|
specs_verification['issues'].append(f"Invalid number of bedrooms for {property_type}: {bedrooms}. Should be between 0 and 5.") |
|
elif property_type in ['House', 'Villa', 'Independent House', 'Independent Villa']: |
|
if bedrooms > 8 or bedrooms < 0: |
|
specs_verification['bedrooms_reasonable'] = False |
|
specs_verification['issues'].append(f"Invalid number of bedrooms for {property_type}: {bedrooms}. Should be between 0 and 8.") |
|
elif property_type in ['Commercial', 'Office', 'Shop', 'Warehouse', 'Industrial']: |
|
if bedrooms > 0: |
|
specs_verification['bedrooms_reasonable'] = False |
|
specs_verification['issues'].append(f"Commercial properties typically don't have bedrooms: {bedrooms}") |
|
except Exception as e: |
|
specs_verification['bedrooms_reasonable'] = False |
|
specs_verification['issues'].append(f"Invalid bedrooms data: must be a number ({str(e)})") |
|
|
|
|
|
if 'bathrooms' in data: |
|
try: |
|
bathrooms = float(data['bathrooms']) |
|
if property_type in ['Apartment', 'Studio']: |
|
if bathrooms > 4 or bathrooms < 0: |
|
specs_verification['bathrooms_reasonable'] = False |
|
specs_verification['issues'].append(f"Invalid number of bathrooms for {property_type}: {bathrooms}. Should be between 0 and 4.") |
|
elif property_type in ['House', 'Villa', 'Independent House', 'Independent Villa']: |
|
if bathrooms > 6 or bathrooms < 0: |
|
specs_verification['bathrooms_reasonable'] = False |
|
specs_verification['issues'].append(f"Invalid number of bathrooms for {property_type}: {bathrooms}. Should be between 0 and 6.") |
|
elif property_type in ['Commercial', 'Office', 'Shop', 'Warehouse', 'Industrial']: |
|
if bathrooms > 0: |
|
specs_verification['bathrooms_reasonable'] = False |
|
specs_verification['issues'].append(f"Commercial properties typically don't have bathrooms: {bathrooms}") |
|
except Exception as e: |
|
specs_verification['bathrooms_reasonable'] = False |
|
specs_verification['issues'].append(f"Invalid bathrooms data: must be a number ({str(e)})") |
|
|
|
|
|
if 'total_rooms' in data: |
|
try: |
|
total_rooms = int(float(data['total_rooms'])) |
|
if total_rooms < 0: |
|
specs_verification['total_rooms_reasonable'] = False |
|
specs_verification['issues'].append(f"Invalid total rooms: {total_rooms}. Cannot be negative.") |
|
elif 'bedrooms' in data and 'bathrooms' in data: |
|
try: |
|
bedrooms = int(float(data['bedrooms'])) |
|
bathrooms = int(float(data['bathrooms'])) |
|
if total_rooms < (bedrooms + bathrooms): |
|
specs_verification['total_rooms_reasonable'] = False |
|
specs_verification['issues'].append(f"Total rooms ({total_rooms}) is less than bedrooms + bathrooms ({bedrooms + bathrooms})") |
|
except Exception: |
|
pass |
|
except Exception as e: |
|
specs_verification['total_rooms_reasonable'] = False |
|
specs_verification['issues'].append(f"Invalid total rooms data: must be a number ({str(e)})") |
|
|
|
|
|
if 'parking' in data: |
|
try: |
|
parking = int(float(data['parking'])) |
|
if property_type in ['Apartment', 'Studio']: |
|
if parking > 2 or parking < 0: |
|
specs_verification['parking_reasonable'] = False |
|
specs_verification['issues'].append(f"Invalid parking spaces for {property_type}: {parking}. Should be between 0 and 2.") |
|
elif property_type in ['House', 'Villa', 'Independent House', 'Independent Villa']: |
|
if parking > 4 or parking < 0: |
|
specs_verification['parking_reasonable'] = False |
|
specs_verification['issues'].append(f"Invalid parking spaces for {property_type}: {parking}. Should be between 0 and 4.") |
|
elif property_type in ['Commercial', 'Office', 'Shop', 'Warehouse', 'Industrial']: |
|
if parking < 0: |
|
specs_verification['parking_reasonable'] = False |
|
specs_verification['issues'].append(f"Invalid parking spaces: {parking}. Cannot be negative.") |
|
except Exception as e: |
|
specs_verification['parking_reasonable'] = False |
|
specs_verification['issues'].append(f"Invalid parking data: must be a number ({str(e)})") |
|
|
|
|
|
if 'sq_ft' in data: |
|
try: |
|
sq_ft = float(str(data['sq_ft']).replace(',', '')) |
|
if sq_ft <= 0: |
|
specs_verification['sq_ft_reasonable'] = False |
|
specs_verification['issues'].append(f"Invalid square footage: {sq_ft}. Must be greater than 0.") |
|
else: |
|
if property_type in ['Apartment', 'Studio']: |
|
if sq_ft > 5000: |
|
specs_verification['sq_ft_reasonable'] = False |
|
specs_verification['issues'].append(f"Square footage ({sq_ft}) seems unreasonably high for {property_type}") |
|
elif sq_ft < 200: |
|
specs_verification['sq_ft_reasonable'] = False |
|
specs_verification['issues'].append(f"Square footage ({sq_ft}) seems unreasonably low for {property_type}") |
|
elif property_type in ['House', 'Villa', 'Independent House', 'Independent Villa']: |
|
if sq_ft > 10000: |
|
specs_verification['sq_ft_reasonable'] = False |
|
specs_verification['issues'].append(f"Square footage ({sq_ft}) seems unreasonably high for {property_type}") |
|
elif sq_ft < 500: |
|
specs_verification['sq_ft_reasonable'] = False |
|
specs_verification['issues'].append(f"Square footage ({sq_ft}) seems unreasonably low for {property_type}") |
|
except Exception as e: |
|
specs_verification['sq_ft_reasonable'] = False |
|
specs_verification['issues'].append(f"Invalid square footage data: must be a number ({str(e)})") |
|
|
|
|
|
if 'market_value' in data: |
|
try: |
|
market_value = float(str(data['market_value']).replace(',', '').replace('₹', '').strip()) |
|
if market_value <= 0: |
|
specs_verification['market_value_reasonable'] = False |
|
specs_verification['issues'].append(f"Invalid market value: {market_value}. Must be greater than 0.") |
|
else: |
|
if property_type in ['Apartment', 'Studio']: |
|
if market_value > 500000000: |
|
specs_verification['market_value_reasonable'] = False |
|
specs_verification['issues'].append(f"Market value (₹{market_value:,.2f}) seems unreasonably high for {property_type}") |
|
elif market_value < 500000: |
|
specs_verification['market_value_reasonable'] = False |
|
specs_verification['issues'].append(f"Market value (₹{market_value:,.2f}) seems unreasonably low for {property_type}") |
|
elif property_type in ['House', 'Villa', 'Independent House', 'Independent Villa']: |
|
if market_value > 2000000000: |
|
specs_verification['market_value_reasonable'] = False |
|
specs_verification['issues'].append(f"Market value (₹{market_value:,.2f}) seems unreasonably high for {property_type}") |
|
elif market_value < 1000000: |
|
specs_verification['market_value_reasonable'] = False |
|
specs_verification['issues'].append(f"Market value (₹{market_value:,.2f}) seems unreasonably low for {property_type}") |
|
elif property_type in ['Commercial', 'Office', 'Shop']: |
|
if market_value < 2000000: |
|
specs_verification['market_value_reasonable'] = False |
|
specs_verification['issues'].append(f"Market value (₹{market_value:,.2f}) seems unreasonably low for {property_type}") |
|
elif property_type in ['Warehouse', 'Industrial']: |
|
if market_value < 5000000: |
|
specs_verification['market_value_reasonable'] = False |
|
specs_verification['issues'].append(f"Market value (₹{market_value:,.2f}) seems unreasonably low for {property_type}") |
|
|
|
|
|
if 'sq_ft' in data and float(str(data['sq_ft']).replace(',', '')) > 0: |
|
try: |
|
sq_ft = float(str(data['sq_ft']).replace(',', '')) |
|
price_per_sqft = market_value / sq_ft |
|
|
|
if property_type in ['Apartment', 'Studio']: |
|
if price_per_sqft < 1000: |
|
specs_verification['market_value_reasonable'] = False |
|
specs_verification['issues'].append(f"Price per sq ft (₹{price_per_sqft:,.2f}) seems unreasonably low for {property_type}") |
|
elif price_per_sqft > 50000: |
|
specs_verification['market_value_reasonable'] = False |
|
specs_verification['issues'].append(f"Price per sq ft (₹{price_per_sqft:,.2f}) seems unreasonably high for {property_type}") |
|
elif property_type in ['House', 'Villa', 'Independent House', 'Independent Villa']: |
|
if price_per_sqft < 500: |
|
specs_verification['market_value_reasonable'] = False |
|
specs_verification['issues'].append(f"Price per sq ft (₹{price_per_sqft:,.2f}) seems unreasonably low for {property_type}") |
|
elif price_per_sqft > 100000: |
|
specs_verification['market_value_reasonable'] = False |
|
specs_verification['issues'].append(f"Price per sq ft (₹{price_per_sqft:,.2f}) seems unreasonably high for {property_type}") |
|
except Exception: |
|
pass |
|
except Exception as e: |
|
specs_verification['market_value_reasonable'] = False |
|
specs_verification['issues'].append(f"Invalid market value data: must be a number ({str(e)})") |
|
|
|
|
|
try: |
|
valid_checks = sum([ |
|
specs_verification['bedrooms_reasonable'], |
|
specs_verification['bathrooms_reasonable'], |
|
specs_verification['total_rooms_reasonable'], |
|
specs_verification['year_built_reasonable'], |
|
specs_verification['parking_reasonable'], |
|
specs_verification['sq_ft_reasonable'], |
|
specs_verification['market_value_reasonable'] |
|
]) |
|
total_checks = 7 |
|
specs_verification['verification_score'] = (valid_checks / total_checks) * 100 |
|
except Exception as e: |
|
logger.error(f"Error calculating verification score: {str(e)}") |
|
specs_verification['verification_score'] = 0.0 |
|
|
|
|
|
try: |
|
specs_verification['is_valid'] = all([ |
|
specs_verification['bedrooms_reasonable'], |
|
specs_verification['bathrooms_reasonable'], |
|
specs_verification['total_rooms_reasonable'], |
|
specs_verification['year_built_reasonable'], |
|
specs_verification['parking_reasonable'], |
|
specs_verification['sq_ft_reasonable'], |
|
specs_verification['market_value_reasonable'] |
|
]) |
|
except Exception as e: |
|
logger.error(f"Error calculating overall validity: {str(e)}") |
|
specs_verification['is_valid'] = False |
|
|
|
except Exception as e: |
|
logger.error(f"Error in property specs verification: {str(e)}") |
|
specs_verification['is_valid'] = False |
|
specs_verification['issues'].append(f"Error in verification: {str(e)}") |
|
|
|
return specs_verification |