# Buggy code
def __init__(self, environ, populate_request=True, shallow=False):
    super(ReceiveModeRequest, self).__init__(environ, populate_request, shallow)
    self.web = environ["web"]

    # Is this a valid upload request?
    self.upload_request = False
    if self.method == "POST":
        if self.path == "/{}/upload".format(self.web.slug):
            self.upload_request = True
        else:
            if self.web.common.settings.get("public_mode"):
                if self.path == "/upload":
                    self.upload_request = True

    if self.upload_request:
        # A dictionary that maps filenames to the bytes uploaded so far
        self.progress = {}

        # Create an upload_id, attach it to the request
        self.upload_id = self.upload_count
        self.upload_count += 1

        # Figure out the content length
        try:
            self.content_length = int(self.headers["Content-Length"])
        except:
            self.content_length = 0

        print(
            "{}: {}".format(
                datetime.now().strftime("%b %d, %I:%M%p"),
                strings._("receive_mode_upload_starting").format(
                    self.web.common.human_readable_filesize(self.content_length)
                ),
            )
        )

        # Tell the GUI
        self.web.add_request(
            self.web.REQUEST_STARTED,
            self.path,
            {"id": self.upload_id, "content_length": self.content_length},
        )

        self.previous_file = None


# Traceback (AttributeError)
[Sep 22 2018 10:19:11 AM] Web.check_slug_candidate: slug_candidate=leggings-cornea
127.0.0.1 - - [22/Sep/2018 10:19:11] "GET /leggings-cornea HTTP/1.1" 200 -
127.0.0.1 - - [22/Sep/2018 10:19:15] "GET /static/css/style.css HTTP/1.1" 200 -
127.0.0.1 - - [22/Sep/2018 10:19:16] "GET /static/img/logo.png HTTP/1.1" 200 -
127.0.0.1 - - [22/Sep/2018 10:19:16] "GET /static/img/logo_large.png HTTP/1.1" 200 -
127.0.0.1 - - [22/Sep/2018 10:19:17] "GET /static/img/favicon.ico HTTP/1.1" 200 -
127.0.0.1 - - [22/Sep/2018 10:19:33] "POST /leggings-cornea/upload HTTP/1.1" 500 -
Error on request:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/werkzeug/serving.py", line 205, in run_wsgi
execute(self.server.app)
File "/usr/lib/python3/dist-packages/werkzeug/serving.py", line 193, in execute
application_iter = app(environ, start_response)
File "/usr/lib/python3/dist-packages/flask/app.py", line 1997, in __call__
return self.wsgi_app(environ, start_response)
File "/home/user/git/onionshare/onionshare/web/receive_mode.py", line 188, in __call__
return self.app(environ, start_response)
File "/usr/lib/python3/dist-packages/flask/app.py", line 1977, in wsgi_app
ctx = self.request_context(environ)
File "/usr/lib/python3/dist-packages/flask/app.py", line 1938, in request_context
return RequestContext(self, environ)
File "/usr/lib/python3/dist-packages/flask/ctx.py", line 240, in __init__
request = app.request_class(environ)
File "/home/user/git/onionshare/onionshare/web/receive_mode.py", line 252, in __init__
self.upload_id = self.upload_count
AttributeError: 'ReceiveModeRequest' object has no attribute 'upload_count'

# Fixed code
def __init__(self, environ, populate_request=True, shallow=False):
    super(ReceiveModeRequest, self).__init__(environ, populate_request, shallow)
    self.web = environ["web"]

    # Is this a valid upload request?
    self.upload_request = False
    if self.method == "POST":
        if self.path == "/{}/upload".format(self.web.slug):
            self.upload_request = True
        else:
            if self.web.common.settings.get("public_mode"):
                if self.path == "/upload":
                    self.upload_request = True

    if self.upload_request:
        # A dictionary that maps filenames to the bytes uploaded so far
        self.progress = {}

        # Create an upload_id, attach it to the request
        self.upload_id = self.web.receive_mode.upload_count
        self.web.receive_mode.upload_count += 1

        # Figure out the content length
        try:
            self.content_length = int(self.headers["Content-Length"])
        except:
            self.content_length = 0

        print(
            "{}: {}".format(
                datetime.now().strftime("%b %d, %I:%M%p"),
                strings._("receive_mode_upload_starting").format(
                    self.web.common.human_readable_filesize(self.content_length)
                ),
            )
        )

        # Tell the GUI
        self.web.add_request(
            self.web.REQUEST_STARTED,
            self.path,
            {"id": self.upload_id, "content_length": self.content_length},
        )

        self.previous_file = None