Search code examples
pythonbottle

trouble retrieving an email from an username from a text document


so I have this website that allows me to get the email from a user that has signed up to the website, so when you sign up it adds your discord username and your email after it in a text file like this

Discorduser#0182 [email protected]
Discordusernumber_one#0182 [email protected]

However, it retrieves the email from the first line when i put in the first option, but if i do the 2nd user, it cant seem to find it, here is the code to the website... the code is part of a bottle.py script but here is the main parts of this page

@get('/get_confirm')
def confirm():
    return CONFIRM_PAGE

@post('/get_confirm')
def confirm():
    name = request.forms.get('name')
    for line in open('Confirmations.txt', 'r').readlines():
        login_info = line.replace('\n', '').split()
        if name == login_info[0]:
            return CONFIRM_PAGE.replace('''<h1 id='emailbox'></h1>''', '<h1>' + login_info[1] + '</h1>')
        else:
            return CONFIRM_PAGE.replace('''<h1 id='emailbox'></h1>''', '<h1>' + 'That user is not valid' + '</h1>')
CONFIRM_PAGE = '''
  <!DOCTYPE html>
  <html>
  <head>
    <link rel="icon" href="/static/icon.ico">
    <title>TylerR - get_confirms</title>
    <style>
        .content {
  background: white;
  padding: 15px;
  border-radius: 3px;
}    

        ul {
            list-style-type: none;
            margin: 0;
            padding: 0;
            overflow: hidden;
            background-color: #333;
        }

        li {
            float: left;
        }

        li a, .dropbtn {
            display: inline-block;
            color: white;
            text-align: center;
            padding: 14px 16px;
            text-decoration: none;
        }

        li a:hover, .dropdown:hover .dropbtn {
            background-color: red;
        }

        li.dropdown {
            display: inline-block;
        }

        .dropdown-content {
            display: none;
            position: absolute;
            background-color: #f9f9f9;
            min-width: 160px;
            box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
            z-index: 1;
        }

        .dropdown-content a {
            color: black;
            padding: 12px 16px;
            text-decoration: none;
            display: block;
            text-align: left;
        }

        .dropdown-content a:hover {background-color: #f1f1f1}

        .dropdown:hover .dropdown-content {
            display: block;
        }
    </style>
  </head>
  <body style='background-size: cover; background-image: url(\"/static/WebisteBackground.png\"); background-attachment: fixed;'>
    <ul>
        <li><a href='/'>Home</a></li>
        <li><a href='/commands'>Commands</a></li>
        <li><a href='/youtube'>Youtube</a></li>
        <li><a href='/downloads'>Downloads</a></li>
        <a href='https://www.youtube.com/channel/UCTHnGh3DpDXMyuL03ove1tQ'>
            <img align='right' src='/static/youtubelogo.png' style="width:40px;height:40px;">
        </a>
        <a href='https://www.instagram.com/drumsnaps/'>
            <img align='right' src='/static/Insta.png' style="width:40px;height:40px;">
        </a>
    </ul>
    </div>
    <center>
    <h1 style="font-family:Cooper Black; font-size: 7em;"><b>Confirmations<b></h1>
    <br>
    <h1>Please enter an username to retrieve email</h1>
    <h3>When entering name, replace any spaces with _</h3>
    <form method='POST'  action='/get_confirm'>
        <h2>Discord Username:</h2>
        <input name='name' type='text' placeholder='User#0000'>
        <br>
        <input type='submit'>
        </form>
        <div class='content'>
            <h1 id='emailbox'></h1>
        </div>
  </center>
  </body>
  </html>
  '''

I am using bottle.py

so if anyone could let me know why this is happening that would be great


Solution

  • In your check it returns immediately - with success (if user is on the first line) or no success (if user is not on first line). You want to return no success, only after you checked all lines.

    @post('/get_confirm')
    def confirm():
        name = request.forms.get('name')
        with open('Confirmations.txt', 'r') as f:
            for line in f:
                user, email = line.strip().split()
                if name == user:
                    return CONFIRM_PAGE.replace('''<h1 id='emailbox'></h1>''', '<h1>' + email + '</h1>')
    
         return CONFIRM_PAGE.replace('''<h1 id='emailbox'></h1>''', '<h1>' + 'That user is not valid' + '</h1>')
    

    Also, better use template engine like jinja2 to construct the HTML page.