File size: 6,558 Bytes
4ee4ce0
54f831b
 
91f1be6
bc1fd1a
e8fd9dc
e033ca1
3e01568
0279e41
4ee4ce0
 
 
0279e41
4ee4ce0
 
e033ca1
3e01568
54f831b
3e01568
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54f831b
3e01568
 
 
 
 
012d61e
3e01568
 
 
 
 
 
012d61e
3e01568
 
 
 
 
 
012d61e
 
3e01568
012d61e
3e01568
012d61e
3e01568
012d61e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3e01568
 
012d61e
3e01568
012d61e
 
 
 
 
 
 
 
 
 
3e01568
 
012d61e
3e01568
 
54f831b
3e01568
 
 
 
 
91f1be6
 
3e01568
012d61e
91f1be6
 
012d61e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3e01568
 
 
 
 
 
91f1be6
012d61e
 
91f1be6
012d61e
 
91f1be6
012d61e
 
 
 
 
 
54f831b
 
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import bcrypt
import gradio as gr
from simple_salesforce import Salesforce

# Salesforce Connection
sf = Salesforce(username='[email protected]', password='Sati@1020', security_token='sSSjyhInIsUohKpG8sHzty2q')


# Function to Hash Password
def hash_password(password):
    return bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()).decode('utf-8')

# Function to Verify Password
def verify_password(plain_password, hashed_password):
    return bcrypt.checkpw(plain_password.encode('utf-8'), hashed_password.encode('utf-8'))

# Signup function
def signup(name, email, phone, password):
    try:
        email = email.strip()

        # Check if the email already exists in Salesforce
        query = f"SELECT Id FROM Customer_Login__c WHERE Email__c = '{email}'"
        result = sf.query(query)

        if len(result['records']) > 0:
            return "Email already exists! Please use a different email."

        # Hash the password
        hashed_password = hash_password(password)

        # Create the new user record
        sf.Customer_Login__c.create({
            'Name': name.strip(),
            'Email__c': email,
            'Phone_Number__c': phone.strip(),
            'Password__c': hashed_password
        })
        return "Signup successful! You can now login."
    except Exception as e:
        return f"Error during signup: {str(e)}"

# Login function
def login(email, password):
    try:
        email = email.strip()
        password = password.strip()

        # Query Salesforce for user details
        query = f"SELECT Name, Password__c FROM Customer_Login__c WHERE Email__c = '{email}'"
        result = sf.query(query)

        if len(result['records']) == 0:
            return "Invalid email or password.", None

        user = result['records'][0]
        if verify_password(password, user['Password__c']):
            return "Login successful!", user['Name']
        else:
            return "Invalid email or password.", None
    except Exception as e:
        return f"Error during login: {str(e)}", None

# Function to load menu items from Salesforce
def load_menu_from_salesforce():
    try:
        query = "SELECT Name, Price__c, Description__c, Image1__c, Ingredients__c FROM Menu_Item__c"
        result = sf.query(query)
        return result['records']
    except Exception as e:
        raise ValueError(f"Error loading menu data from Salesforce: {e}")

# Function to filter menu items based on preference
def filter_menu_from_salesforce(preference):
    menu_data = load_menu_from_salesforce()
    filtered_data = []

    for item in menu_data:
        if preference == "Halal/Non-Veg":
            if any(x in item.get("Ingredients__c", "").lower() for x in ["chicken", "mutton", "fish", "prawns", "goat"]):
                filtered_data.append(item)
        elif preference == "Vegetarian":
            if not any(x in item.get("Ingredients__c", "").lower() for x in ["chicken", "mutton", "fish", "prawns", "goat"]):
                filtered_data.append(item)
        elif preference == "Guilt-Free":
            if "fat:" in item.get("Description__c", "").lower():
                filtered_data.append(item)
        else:
            filtered_data = menu_data

    html_content = ""
    for item in filtered_data:
        html_content += f"""
        <div style="display: flex; align-items: center; border: 1px solid #ddd; border-radius: 8px; padding: 15px; margin-bottom: 10px; box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.1);">
            <div style="flex: 1; margin-right: 15px;">
                <h3 style="margin: 0; font-size: 18px;">{item['Name']}</h3>
                <p style="margin: 5px 0; font-size: 16px; color: #888;">${item['Price__c']}</p>
                <p style="margin: 5px 0; font-size: 14px; color: #555;">{item['Description__c']}</p>
            </div>
            <div style="flex-shrink: 0; text-align: center;">
                <img src="{item['Image1__c']}" alt="{item['Name']}" style="width: 100px; height: 100px; border-radius: 8px; object-fit: cover; margin-bottom: 10px;">
                <button style="background-color: #28a745; color: white; border: none; padding: 8px 15px; font-size: 14px; border-radius: 5px; cursor: pointer;">Add</button>
            </div>
        </div>
        """
    return html_content

# Gradio App
with gr.Blocks() as app:
    login_status = gr.State(value=False)

    # Login Page
    with gr.Row(visible=True) as login_page:
        with gr.Column():
            login_email = gr.Textbox(label="Email")
            login_password = gr.Textbox(label="Password", type="password")
            login_button = gr.Button("Login")
            signup_button = gr.Button("Go to Signup")
            login_output = gr.Textbox(label="Status")

    # Signup Page
    with gr.Row(visible=False) as signup_page:
        with gr.Column():
            signup_name = gr.Textbox(label="Name")
            signup_email = gr.Textbox(label="Email")
            signup_phone = gr.Textbox(label="Phone")
            signup_password = gr.Textbox(label="Password", type="password")
            submit_signup = gr.Button("Signup")
            login_redirect = gr.Button("Go to Login")
            signup_output = gr.Textbox(label="Status")

    # Menu Page
    with gr.Row(visible=False) as menu_page:
        preference = gr.Radio(["All", "Halal/Non-Veg", "Vegetarian", "Guilt-Free"], label="Filter Preference")
        show_menu = gr.Button("Show Menu")
        menu_output = gr.HTML()

    # Functions for page transitions and operations
    def handle_login(email, password):
        status, user = login(email, password)
        if status == "Login successful!":
            return gr.update(visible=False), gr.update(visible=True), status
        else:
            return gr.update(), gr.update(), status

    def handle_signup(name, email, phone, password):
        return signup(name, email, phone, password)

    def handle_menu(preference):
        return filter_menu_from_salesforce(preference)

    # Button Actions
    login_button.click(handle_login, [login_email, login_password], [login_page, menu_page, login_output])
    signup_button.click(lambda: (gr.update(visible=False), gr.update(visible=True)), None, [login_page, signup_page])
    submit_signup.click(handle_signup, [signup_name, signup_email, signup_phone, signup_password], signup_output)
    login_redirect.click(lambda: (gr.update(visible=False), gr.update(visible=True)), None, [signup_page, login_page])
    show_menu.click(handle_menu, [preference], menu_output)

app.launch()