Update app.py
Browse files
    	
        app.py
    CHANGED
    
    | @@ -3,40 +3,42 @@ from flask_session import Session  # Import the Session class | |
| 3 | 
             
            from flask.sessions import SecureCookieSessionInterface  # Import the class
         | 
| 4 | 
             
            from salesforce import get_salesforce_connection
         | 
| 5 | 
             
            from datetime import timedelta
         | 
| 6 | 
            -
            import os
         | 
| 7 | 
            -
            import smtplib
         | 
| 8 | 
             
            from email.mime.multipart import MIMEMultipart
         | 
| 9 | 
             
            from email.mime.text import MIMEText
         | 
| 10 | 
             
            from menu import menu_blueprint  # Make sure this import is correct
         | 
| 11 | 
             
            from cart import cart_blueprint  # Same for other blueprints
         | 
| 12 | 
             
            from order import order_blueprint  # Same for user blueprint
         | 
| 13 | 
            -
            from user_details import user_details_blueprint | 
| 14 | 
            -
             | 
| 15 | 
            -
             | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 16 | 
             
            app = Flask(__name__)
         | 
| 17 | 
            -
             | 
| 18 |  | 
| 19 | 
             
            # Add debug logs in Salesforce connection setup
         | 
| 20 | 
             
            sf = get_salesforce_connection()
         | 
| 21 | 
            -
             | 
| 22 |  | 
| 23 | 
             
            # Set the secret key to handle sessions securely
         | 
| 24 | 
             
            app.secret_key = os.getenv("SECRET_KEY", "xEr0cwgsiatzrzaeFewYrVA1O")  # Replace with a secure key
         | 
| 25 | 
            -
             | 
| 26 | 
             
            app.config["SESSION_TYPE"] = "filesystem"  # Storing sessions in filesystem
         | 
| 27 | 
             
            app.config["SESSION_COOKIE_SECURE"] = True  # Enabling secure cookies (ensure your app is served over HTTPS)
         | 
| 28 | 
             
            app.config["SESSION_COOKIE_SAMESITE"] = "None"  # Cross-site cookies allowed
         | 
| 29 |  | 
| 30 | 
             
            # Initialize the session
         | 
| 31 | 
             
            Session(app)  # Correctly initialize the Session object
         | 
| 32 | 
            -
            print("Session interface configured.")
         | 
| 33 | 
            -
            app.register_blueprint(user_details_blueprint, url_prefix='/user')
         | 
| 34 | 
            -
            # Ensure secure session handling for environments like Hugging Face
         | 
| 35 | 
             
            app.session_interface = SecureCookieSessionInterface()
         | 
| 36 | 
            -
             | 
| 37 | 
            -
            import random
         | 
| 38 | 
            -
            import string
         | 
| 39 | 
             
            app.register_blueprint(cart_blueprint, url_prefix='/cart') 
         | 
|  | |
|  | |
|  | |
|  | |
| 40 |  | 
| 41 | 
             
            @app.route("/")
         | 
| 42 | 
             
            def home():
         | 
| @@ -57,11 +59,6 @@ def home(): | |
| 57 |  | 
| 58 | 
             
                return render_template("index.html")
         | 
| 59 |  | 
| 60 | 
            -
             | 
| 61 | 
            -
             | 
| 62 | 
            -
            from datetime import datetime
         | 
| 63 | 
            -
             | 
| 64 | 
            -
             | 
| 65 | 
             
            @app.route("/generate_custom_dish", methods=["POST"])
         | 
| 66 | 
             
            def generate_custom_dish():
         | 
| 67 | 
             
                try:
         | 
| @@ -158,12 +155,6 @@ def generate_custom_dish(): | |
| 158 | 
             
                except Exception as e:
         | 
| 159 | 
             
                    return jsonify({"success": False, "error": str(e)}), 500
         | 
| 160 |  | 
| 161 | 
            -
             | 
| 162 | 
            -
             
         | 
| 163 | 
            -
             | 
| 164 | 
            -
            from datetime import datetime
         | 
| 165 | 
            -
            import pytz  # Library to handle timezone conversions
         | 
| 166 | 
            -
             | 
| 167 | 
             
            @app.route("/order-history", methods=["GET"])
         | 
| 168 | 
             
            def order_history():
         | 
| 169 | 
             
                email = session.get('user_email')  # Get logged-in user's email
         | 
| @@ -262,68 +253,10 @@ def logout(): | |
| 262 | 
             
                return render_template("redirect_page.html", table_number=table_number)
         | 
| 263 |  | 
| 264 |  | 
| 265 | 
            -
            @app.route("/login", methods=["GET", "POST"])
         | 
| 266 | 
            -
            def login():
         | 
| 267 | 
            -
                if request.method == "POST":
         | 
| 268 | 
            -
                    email = request.form.get("email")
         | 
| 269 | 
            -
                    password = request.form.get("password")
         | 
| 270 | 
            -
                    print(f"Login attempt with email: {email}")  # Debug log
         | 
| 271 | 
            -
             | 
| 272 | 
            -
                    try:
         | 
| 273 | 
            -
                        # Fetch user details from Salesforce
         | 
| 274 | 
            -
                        query = f"SELECT Id, Name, Email__c, Reward_Points__c FROM Customer_Login__c WHERE Email__c='{email}' AND Password__c='{password}'"
         | 
| 275 | 
            -
                        result = sf.query(query)
         | 
| 276 | 
            -
             | 
| 277 | 
            -
                        if result["records"]:
         | 
| 278 | 
            -
                            user = result["records"][0]
         | 
| 279 | 
            -
                            session['user_id'] = user['Id']
         | 
| 280 | 
            -
             | 
| 281 | 
            -
                            # ✅ Always store or update session email
         | 
| 282 | 
            -
                            if 'user_email' not in session or session['user_email'] != email:
         | 
| 283 | 
            -
                                session['user_email'] = email
         | 
| 284 | 
            -
                                session['user_name'] = user.get("Name", "")
         | 
| 285 | 
            -
                                print(f"✅ Session email updated: {session['user_email']}")
         | 
| 286 | 
            -
             | 
| 287 | 
            -
                            reward_points = user.get("Reward_Points__c") or 0
         | 
| 288 | 
            -
             | 
| 289 | 
            -
                            # Coupon generation logic (if reward points >= 500)
         | 
| 290 | 
            -
                            if reward_points >= 500:
         | 
| 291 | 
            -
                                new_coupon_code = generate_coupon_code()
         | 
| 292 | 
            -
                                coupon_query = sf.query(f"SELECT Id, Coupon_Code__c FROM Referral_Coupon__c WHERE Referral_Email__c = '{email}'")
         | 
| 293 | 
            -
             | 
| 294 | 
            -
                                if coupon_query["records"]:
         | 
| 295 | 
            -
                                    coupon_record = coupon_query["records"][0]
         | 
| 296 | 
            -
                                    referral_coupon_id = coupon_record["Id"]
         | 
| 297 | 
            -
                                    existing_coupons = coupon_record.get("Coupon_Code__c", "")
         | 
| 298 | 
            -
             | 
| 299 | 
            -
                                    updated_coupons = f"{existing_coupons}\n{new_coupon_code}".strip()
         | 
| 300 | 
            -
                                    sf.Referral_Coupon__c.update(referral_coupon_id, {"Coupon_Code__c": updated_coupons})
         | 
| 301 | 
            -
                                else:
         | 
| 302 | 
            -
                                    sf.Referral_Coupon__c.create({
         | 
| 303 | 
            -
                                        "Referral_Email__c": email,
         | 
| 304 | 
            -
                                        "Name": user.get("Name", ""),
         | 
| 305 | 
            -
                                        "Coupon_Code__c": new_coupon_code
         | 
| 306 | 
            -
                                    })
         | 
| 307 | 
            -
             | 
| 308 | 
            -
                                new_reward_points = reward_points - 500
         | 
| 309 | 
            -
                                sf.Customer_Login__c.update(user['Id'], {"Reward_Points__c": new_reward_points})
         | 
| 310 | 
            -
             | 
| 311 | 
            -
                            return redirect(url_for("menu.menu"))
         | 
| 312 | 
            -
             | 
| 313 | 
            -
                        else:
         | 
| 314 | 
            -
                            print("Invalid credentials!")
         | 
| 315 | 
            -
                            return render_template("login.html", error="Invalid credentials!")
         | 
| 316 | 
            -
             | 
| 317 | 
            -
                    except Exception as e:
         | 
| 318 | 
            -
                        print(f"Error during login: {str(e)}")
         | 
| 319 | 
            -
                        return render_template("login.html", error=f"Error: {str(e)}")
         | 
| 320 | 
            -
             | 
| 321 | 
            -
                return render_template("login.html")
         | 
| 322 | 
            -
            # Register Blueprints for each functionality
         | 
| 323 | 
            -
            app.register_blueprint(menu_blueprint)
         | 
| 324 | 
            -
             # Register the cart blueprint with URL prefix
         | 
| 325 |  | 
| 326 | 
            -
             | 
|  | |
|  | |
| 327 |  | 
| 328 |  | 
| 329 | 
             
            if __name__ == "__main__":
         | 
|  | |
| 3 | 
             
            from flask.sessions import SecureCookieSessionInterface  # Import the class
         | 
| 4 | 
             
            from salesforce import get_salesforce_connection
         | 
| 5 | 
             
            from datetime import timedelta
         | 
|  | |
|  | |
| 6 | 
             
            from email.mime.multipart import MIMEMultipart
         | 
| 7 | 
             
            from email.mime.text import MIMEText
         | 
| 8 | 
             
            from menu import menu_blueprint  # Make sure this import is correct
         | 
| 9 | 
             
            from cart import cart_blueprint  # Same for other blueprints
         | 
| 10 | 
             
            from order import order_blueprint  # Same for user blueprint
         | 
| 11 | 
            +
            from user_details import user_details_blueprint
         | 
| 12 | 
            +
            from datetime import datetime
         | 
| 13 | 
            +
            from datetime import datetime
         | 
| 14 | 
            +
            import pytz  # Library to handle timezone conversions
         | 
| 15 | 
            +
            import os
         | 
| 16 | 
            +
            import smtplib
         | 
| 17 | 
            +
            import random
         | 
| 18 | 
            +
            import string
         | 
| 19 | 
            +
             | 
| 20 | 
             
            app = Flask(__name__)
         | 
| 21 | 
            +
             | 
| 22 |  | 
| 23 | 
             
            # Add debug logs in Salesforce connection setup
         | 
| 24 | 
             
            sf = get_salesforce_connection()
         | 
| 25 | 
            +
             | 
| 26 |  | 
| 27 | 
             
            # Set the secret key to handle sessions securely
         | 
| 28 | 
             
            app.secret_key = os.getenv("SECRET_KEY", "xEr0cwgsiatzrzaeFewYrVA1O")  # Replace with a secure key
         | 
|  | |
| 29 | 
             
            app.config["SESSION_TYPE"] = "filesystem"  # Storing sessions in filesystem
         | 
| 30 | 
             
            app.config["SESSION_COOKIE_SECURE"] = True  # Enabling secure cookies (ensure your app is served over HTTPS)
         | 
| 31 | 
             
            app.config["SESSION_COOKIE_SAMESITE"] = "None"  # Cross-site cookies allowed
         | 
| 32 |  | 
| 33 | 
             
            # Initialize the session
         | 
| 34 | 
             
            Session(app)  # Correctly initialize the Session object
         | 
|  | |
|  | |
|  | |
| 35 | 
             
            app.session_interface = SecureCookieSessionInterface()
         | 
| 36 | 
            +
             | 
|  | |
|  | |
| 37 | 
             
            app.register_blueprint(cart_blueprint, url_prefix='/cart') 
         | 
| 38 | 
            +
            app.register_blueprint(user_details_blueprint, url_prefix='/user')
         | 
| 39 | 
            +
            app.register_blueprint(menu_blueprint)
         | 
| 40 | 
            +
            app.register_blueprint(order_blueprint)
         | 
| 41 | 
            +
             | 
| 42 |  | 
| 43 | 
             
            @app.route("/")
         | 
| 44 | 
             
            def home():
         | 
|  | |
| 59 |  | 
| 60 | 
             
                return render_template("index.html")
         | 
| 61 |  | 
|  | |
|  | |
|  | |
|  | |
|  | |
| 62 | 
             
            @app.route("/generate_custom_dish", methods=["POST"])
         | 
| 63 | 
             
            def generate_custom_dish():
         | 
| 64 | 
             
                try:
         | 
|  | |
| 155 | 
             
                except Exception as e:
         | 
| 156 | 
             
                    return jsonify({"success": False, "error": str(e)}), 500
         | 
| 157 |  | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 158 | 
             
            @app.route("/order-history", methods=["GET"])
         | 
| 159 | 
             
            def order_history():
         | 
| 160 | 
             
                email = session.get('user_email')  # Get logged-in user's email
         | 
|  | |
| 253 | 
             
                return render_template("redirect_page.html", table_number=table_number)
         | 
| 254 |  | 
| 255 |  | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 256 |  | 
| 257 | 
            +
             | 
| 258 | 
            +
             | 
| 259 | 
            +
             | 
| 260 |  | 
| 261 |  | 
| 262 | 
             
            if __name__ == "__main__":
         |