Skip to content
Snippets Groups Projects
flask_backend.py 3.04 KiB
Newer Older
  • Learn to ignore specific revisions
  • fcrisafulli-dev's avatar
    fcrisafulli-dev committed
    from flask import Flask, request, abort
    
    from flask_cors import CORS
    
    from database_api import CrisisEventsDatabase
    
    #from database_implementation import sqlite_api
    
    from runtime_import import runtime_import
    
    fcrisafulli-dev's avatar
    fcrisafulli-dev committed
    
    app = Flask(__name__)
    
    #pip install flask-cors
    CORS(app) 
    
    
    database:CrisisEventsDatabase = None
    
    #this function is terrible, oh well!
    def database_debug_view():
        db_html = ""
    
    fcrisafulli-dev's avatar
    fcrisafulli-dev committed
        user_db_html = ""
    
        for collection in database.get_sample_of_collections():
    
    fcrisafulli-dev's avatar
    fcrisafulli-dev committed
            db_html += f"<br/><code>{collection}</code>"
    
    fcrisafulli-dev's avatar
    fcrisafulli-dev committed
    
        for collection in database.get_sample_of_users():
            user_db_html += f"<br/><code>{collection}</code>"
    
    
        return f"""
        <html>
        <head></head>
        <body>
            <p>This service uses {database.get_info()} as its database</p>
            <p>Warning: if there are any '<'  or '>' or '/' or '\\' in the database: this will break! </p>
    
            <code>CollectionID, UserID, CollectionData, CollectionSummary, CollectionName</code>
    
            {db_html}
    
    fcrisafulli-dev's avatar
    fcrisafulli-dev committed
            <br/><br/><br/>
            <code>UserID, UserName, Hash</code>
            {user_db_html}
    
        </body>
        </html>
        """
    
    @app.route('/login', methods = ['POST'])
    def login():
         
        data = request.json  # Retrieve JSON data from the request
        print(f'The login data: {data}')
    
    
        if 'username' not in data or 'password' not in data:
            abort(400)
    
        username = data['username']
        password = data ['password']
    
        user_hash = hashlib.md5(password.encode()).digest().hex()
    
        user_data = database.get_user_by_username(username)
    
        if user_data is None:
            #abort(401)  # Unauthorized if user does not exist
            print(f'Username or password is incorrect: {data}')
            return 'Username or password is incorrect', 401
            
        stored_hash = user_data[2]
    
        if user_hash != stored_hash:
            print(f'Password is incorrect: {data}')
            return 'Incorrect Password', 401
        
    
    
        return 'Login successful', 200  # Return a response to indicate success
    
    
    
    
    @app.route('/database_service', methods=['POST','GET'])
    def database_service():
    
    fcrisafulli-dev's avatar
    fcrisafulli-dev committed
        if request.method == 'POST':
            print(request.json)
    
    fcrisafulli-dev's avatar
    fcrisafulli-dev committed
            result = ''
    
    fcrisafulli-dev's avatar
    fcrisafulli-dev committed
                command = request.json["command"]
                if command == "create_collection":
    
                    database.create_collection(0,request.json["data"]["collection_name"])
    
    fcrisafulli-dev's avatar
    fcrisafulli-dev committed
                elif command == "get_collection":
                    result = str(database.get_collection(0,0))
    
                else:
                    print("Invalid command")
                    abort(400)
            except KeyError:
                print("Got poorly formatted request")
                abort(400)
    
    
    fcrisafulli-dev's avatar
    fcrisafulli-dev committed
            return result, 200
    
        elif request.method == 'GET':
            return database_debug_view(), 200
    
    fcrisafulli-dev's avatar
    fcrisafulli-dev committed
        else:
            abort(400)
    
    
    fcrisafulli-dev's avatar
    fcrisafulli-dev committed
    
    if __name__ == '__main__':
    
        database = runtime_import("database_implementation/")[0].IMPLEMENTATION()
        database.initialize()
    
        #database.create_collection(0,"First Collection")
        #database.create_collection(0,"Second Collection")
        #database.create_collection(0,"Third Collection")
    
    fcrisafulli-dev's avatar
    fcrisafulli-dev committed
        app.run()