File size: 1,240 Bytes
9e22989
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import streamlit as st
from typing import Optional
import hashlib
import os
import pandas as pd
from .config import UPLOAD_FOLDER, DB_FOLDER

def init_folders():
    """Initialize required folders"""
    for folder in [UPLOAD_FOLDER, DB_FOLDER]:
        os.makedirs(folder, exist_ok=True)

def get_file_hash(file_content: bytes) -> str:
    """Generate SHA-256 hash of file content"""
    return hashlib.sha256(file_content).hexdigest()

def save_uploaded_file(uploaded_file) -> Optional[str]:
    """Save uploaded file and return the path"""
    try:
        file_path = os.path.join(UPLOAD_FOLDER, uploaded_file.name)
        os.makedirs(os.path.dirname(file_path), exist_ok=True)
        with open(file_path, "wb") as f:
            f.write(uploaded_file.getvalue())
        return file_path
    except Exception as e:
        st.error(f"Error saving file: {str(e)}")
        return None

def validate_csv_structure(df: pd.DataFrame) -> tuple[bool, list]:
    """Validate CSV file structure"""
    required_columns = ['UnitCode', 'UnitType', 'Floor', 'Developer', 'TotalArea', 'AskingPrice', 'View']
    missing_columns = [col for col in required_columns if col not in df.columns]
    return len(missing_columns) == 0, missing_columns