# Buggy code
def deleteHost(ip):
    delForm = forms.DeleteForm()

    if delForm.validate_on_submit():
        deleted = current_app.elastic.delete_host(ip)
        if deleted > 0:
            flash("Successfully deleted host %s" % ip, "success")
            return redirect(url_for("main.search"))
        else:
            flash("Couldn't delete host: %s" % ip, "danger")
    else:
        flash("Couldn't validate form!")
        return redirect(request.referrer)


# Traceback (TypeError)
Nov 05 17:22:50 natlas-server bash[10026]: [2019-11-05 17:22:50,681] ERROR in app: Exception on /api/getwork [GET]
Nov 05 17:22:50 natlas-server bash[10026]: Traceback (most recent call last):
Nov 05 17:22:50 natlas-server bash[10026]:   File "/opt/natlas/natlas-server/venv/lib/python3.6/site-packages/flask/app.py", line 2292, in wsgi_app
Nov 05 17:22:50 natlas-server bash[10026]:     response = self.full_dispatch_request()
Nov 05 17:22:50 natlas-server bash[10026]:   File "/opt/natlas/natlas-server/venv/lib/python3.6/site-packages/flask/app.py", line 1815, in full_dispatch_request
Nov 05 17:22:50 natlas-server bash[10026]:     rv = self.handle_user_exception(e)
Nov 05 17:22:50 natlas-server bash[10026]:   File "/opt/natlas/natlas-server/venv/lib/python3.6/site-packages/flask/app.py", line 1718, in handle_user_exception
Nov 05 17:22:50 natlas-server bash[10026]:     reraise(exc_type, exc_value, tb)
Nov 05 17:22:50 natlas-server bash[10026]:   File "/opt/natlas/natlas-server/venv/lib/python3.6/site-packages/flask/_compat.py", line 35, in reraise
Nov 05 17:22:50 natlas-server bash[10026]:     raise value
Nov 05 17:22:50 natlas-server bash[10026]:   File "/opt/natlas/natlas-server/venv/lib/python3.6/site-packages/flask/app.py", line 1813, in full_dispatch_request
Nov 05 17:22:50 natlas-server bash[10026]:     rv = self.dispatch_request()
Nov 05 17:22:50 natlas-server bash[10026]:   File "/opt/natlas/natlas-server/venv/lib/python3.6/site-packages/flask/app.py", line 1799, in dispatch_request
Nov 05 17:22:50 natlas-server bash[10026]:     return self.view_functions[rule.endpoint](**req.view_args)
Nov 05 17:22:50 natlas-server bash[10026]:   File "/opt/natlas/natlas-server/app/auth/wrappers.py", line 41, in decorated_function
Nov 05 17:22:50 natlas-server bash[10026]:     return f(*args, **kwargs)
Nov 05 17:22:50 natlas-server bash[10026]:   File "/opt/natlas/natlas-server/app/api/routes.py", line 70, in getwork
Nov 05 17:22:50 natlas-server bash[10026]:     count, context = current_app.elastic.gethost_scan_id(rand)
Nov 05 17:22:50 natlas-server bash[10026]:   File "/opt/natlas/natlas-server/app/elastic.py", line 238, in gethost_scan_id
Nov 05 17:22:50 natlas-server bash[10026]:     if not self.checkStatus():
Nov 05 17:22:50 natlas-server bash[10026]:   File "/opt/natlas/natlas-server/app/elastic.py", line 60, in checkStatus
Nov 05 17:22:50 natlas-server bash[10026]:     if not self.attemptReconnect():
Nov 05 17:22:50 natlas-server bash[10026]:   File "/opt/natlas/natlas-server/app/elastic.py", line 43, in attemptReconnect
Nov 05 17:22:50 natlas-server bash[10026]:     delta = now - self.lastReconnectAttempt
Nov 05 17:22:50 natlas-server bash[10026]: TypeError: unsupported operand type(s) for -: 'datetime.datetime' and 'NoneType

# Fixed code
def deleteHost(ip):
    delForm = forms.DeleteForm()

    if delForm.validate_on_submit():
        deleted = current_app.elastic.delete_host(ip)
        if deleted > 0:
            flash(
                f"Successfully deleted {deleted - 1 if deleted > 1 else deleted} scans for {ip}",
                "success",
            )
            return redirect(url_for("main.browse"))
        else:
            flash(f"Couldn't delete host: {ip}", "danger")
    else:
        flash("Couldn't validate form!")
        return redirect(request.referrer)