From 47374e5f3b293897a4d1fa2357f24a42564758f4 Mon Sep 17 00:00:00 2001
From: jonasled <jonas@jonasled.de>
Date: Sun, 23 Feb 2020 21:21:16 +0100
Subject: [PATCH] added message, if file is called directly, not via import.

if (__name__ == "__main__"):
    print("This file is not made fore direct call, please run the main.py")
    exit()
---
 api.py                | 6 +++++-
 deletelink.py         | 6 +++++-
 githubcallback.py     | 6 +++++-
 googlecallback.py     | 6 +++++-
 grecaptcha_verify.py  | 6 +++++-
 home.py               | 6 +++++-
 login.py              | 6 +++++-
 main.py               | 2 +-
 makeqr.py             | 6 +++++-
 newurl.py             | 4 ++++
 redirectShortenURL.py | 4 ++++
 table_check.py        | 6 +++++-
 userprofile.py        | 4 ++++
 13 files changed, 58 insertions(+), 10 deletions(-)

diff --git a/api.py b/api.py
index 96648ca..bf535ec 100644
--- a/api.py
+++ b/api.py
@@ -75,4 +75,8 @@ def apiPost(request, url_scheme, domain, sAPI):
             return jsonify(
                 status="4",
                 message="short url already in use"
-            )
\ No newline at end of file
+            )
+            
+if (__name__ == "__main__"):
+    print("This file is not made fore direct call, please run the main.py")
+    exit()
\ No newline at end of file
diff --git a/deletelink.py b/deletelink.py
index 454a4ff..2d8a04b 100644
--- a/deletelink.py
+++ b/deletelink.py
@@ -16,4 +16,8 @@ def deleteLink(request, s):
             cursor.execute('DELETE FROM WEB_URL WHERE SHORT_URL=? AND USERNAME=?', [linkToDelete, userID]) #Delete the entrie
             return redirect('/user/links') #redirect the user back to the table.
         except:
-            abort(500)
\ No newline at end of file
+            abort(500)
+            
+if (__name__ == "__main__"):
+    print("This file is not made fore direct call, please run the main.py")
+    exit()
\ No newline at end of file
diff --git a/githubcallback.py b/githubcallback.py
index 8c18ade..d23162f 100644
--- a/githubcallback.py
+++ b/githubcallback.py
@@ -19,4 +19,8 @@ def githubCallback(request, GITHUB_CLIENT_SECRET, GITHUB_CLIENT_ID, s):
         resp.set_cookie('username', s.dumps(username))
         return resp
     except:
-        print("Authentication failed")
\ No newline at end of file
+        print("Authentication failed")
+        
+if (__name__ == "__main__"):
+    print("This file is not made fore direct call, please run the main.py")
+    exit()
\ No newline at end of file
diff --git a/googlecallback.py b/googlecallback.py
index 0af7947..f100d48 100644
--- a/googlecallback.py
+++ b/googlecallback.py
@@ -17,4 +17,8 @@ def googleCallback(request, GOOGLE_CLIENT_ID, GOOGLE_CLIENT_SECRET, url_scheme,
         resp.set_cookie('username', s.dumps(name))
         return resp
     except:
-        print("Authentication failed")
\ No newline at end of file
+        print("Authentication failed")
+        
+if (__name__ == "__main__"):
+    print("This file is not made fore direct call, please run the main.py")
+    exit()
\ No newline at end of file
diff --git a/grecaptcha_verify.py b/grecaptcha_verify.py
index 55acc15..4895296 100644
--- a/grecaptcha_verify.py
+++ b/grecaptcha_verify.py
@@ -9,4 +9,8 @@ def grecaptcha_verify(request, skipCaptcha, recaptchaPrivateKey): #This function
     verify_rs = post(url,params, headers=headers) #Send a post request with the parameters from before to googlde
     verify_rs = verify_rs.json()
     response = verify_rs.get("success", False) #Verify that the response includes the success, if so return True, if not return False
-    return response 
\ No newline at end of file
+    return response 
+    
+if (__name__ == "__main__"):
+    print("This file is not made fore direct call, please run the main.py")
+    exit()
\ No newline at end of file
diff --git a/home.py b/home.py
index 3749ec1..2e815ca 100644
--- a/home.py
+++ b/home.py
@@ -8,4 +8,8 @@ def home(request, builddate, version, domain_prepared, recaptchaPublicKey, showD
     try:
         return render_template('home.html', builddate=builddate, version=version, domain=domain_prepared, recaptchaPublicKey=recaptchaPublicKey, showDomainSelect=showDomainSelect, loginbar=loginbar, cookieNotice=cookieNotice ,domain_prefilled=domain_to_index[request.headers["host"]], loginEnabled=loginEnabled) #return the default site to create a new shorten link
     except:
-        abort(500)
\ No newline at end of file
+        abort(500)
+
+if (__name__ == "__main__"):
+    print("This file is not made fore direct call, please run the main.py")
+    exit()
\ No newline at end of file
diff --git a/login.py b/login.py
index 299dbe6..30cd7e6 100644
--- a/login.py
+++ b/login.py
@@ -4,4 +4,8 @@ def login(request, GITHUB_CLIENT_ID, cookieNotice, GOOGLE_CLIENT_ID, url_scheme,
     service = request.args.get("service")
     if(service == "github"): return redirect("https://github.com/login/oauth/authorize/?client_id=" + GITHUB_CLIENT_ID + "&scope=user") #redirect the user to the github login page and ask for access to user data (name, email, ...)
     if(service == "google"): return redirect("https://accounts.google.com/o/oauth2/v2/auth?client_id=" + GOOGLE_CLIENT_ID + "&scope=profile%20email%20openid&response_type=code&access_type=offline&include_granted_scopes=true&redirect_uri=" + url_scheme + "://" + domain[0] + "/user/google-callback")
-    return render_template("login.html", cookieNotice=cookieNotice)
\ No newline at end of file
+    return render_template("login.html", cookieNotice=cookieNotice)
+    
+if (__name__ == "__main__"):
+    print("This file is not made fore direct call, please run the main.py")
+    exit()
\ No newline at end of file
diff --git a/main.py b/main.py
index 80a84e6..8f0849b 100644
--- a/main.py
+++ b/main.py
@@ -202,4 +202,4 @@ def startup():
     else:
         app.run(host=host, port=5000, debug=True) #Start the Webserver in Debug mode. This means, if the script runs in an error, it will show the error message in Browser.
 
-if (__name__ == "__main__"): startup()
\ No newline at end of file
+if (__name__ == "__main__"): startup() #Only run the startup script, if this file is directly called.
\ No newline at end of file
diff --git a/makeqr.py b/makeqr.py
index 058324c..f3c0d22 100644
--- a/makeqr.py
+++ b/makeqr.py
@@ -16,4 +16,8 @@ def makeQR(text): #This function is used to create a QR code and encode it base6
     img = qr.make_image(fill_color="black", back_color="white") #Encode the WR as base 64
     with BytesIO() as buffer:
         img.save(buffer, 'jpeg')
-        return b64encode(buffer.getvalue()).decode()
\ No newline at end of file
+        return b64encode(buffer.getvalue()).decode()
+
+if (__name__ == "__main__"):
+    print("This file is not made fore direct call, please run the main.py")
+    exit()
\ No newline at end of file
diff --git a/newurl.py b/newurl.py
index 8ae8195..dd8ebdb 100644
--- a/newurl.py
+++ b/newurl.py
@@ -39,3 +39,7 @@ def newurl(request, skipCaptcha, recaptchaPrivateKey, recaptchaPublicKey, buildd
             return render_template('home.html', short_url=shorturl, recaptchaPublicKey=recaptchaPublicKey, builddate=builddate, version=version, domain=domain_prepared, qrcode=makeQR(url_scheme + "://" + shorturl), loginbar=loginbar, cookieNotice=cookieNotice) #return the shorten link to the user
         else:
             return render_template('home.html', builddate=builddate, version=version, recaptchaPublicKey=recaptchaPublicKey, domain=domain_prepared, snackbar="URL already used, please try another one", long_url_prefilled=request.form.get('url'), short_url_prefilled=request.form.get('short').lower(), domain_prefilled=domain_to_index[request.form.get('domain')], showDomainSelect=showDomainSelect, loginbar=loginbar, cookieNotice=cookieNotice) #return the user the prefilled form with an error message, because the url was already used
+
+if (__name__ == "__main__"):
+    print("This file is not made fore direct call, please run the main.py")
+    exit()
\ No newline at end of file
diff --git a/redirectShortenURL.py b/redirectShortenURL.py
index ddf99da..bc72395 100644
--- a/redirectShortenURL.py
+++ b/redirectShortenURL.py
@@ -29,3 +29,7 @@ def redirectShortenURL(request, short_url):
             return redirect(url) #I use temp redirect here, because the owner of a link can delete it. If then the link is reused, the user will maybe redirected to the wrong page
         else:
             abort(404) #Serve de default not found page.
+
+if (__name__ == "__main__"):
+    print("This file is not made fore direct call, please run the main.py")
+    exit()
\ No newline at end of file
diff --git a/table_check.py b/table_check.py
index 329f34f..0f97a72 100644
--- a/table_check.py
+++ b/table_check.py
@@ -23,4 +23,8 @@ def table_check(): #This function is used on start to make a new Database if not
         try: #Try making the database structure, if fails Database was already created.
             cursor.execute(create_table_analytics)
         except OperationalError:
-            pass
\ No newline at end of file
+            pass
+
+if (__name__ == "__main__"):
+    print("This file is not made fore direct call, please run the main.py")
+    exit()
\ No newline at end of file
diff --git a/userprofile.py b/userprofile.py
index bb50b99..1ca308b 100644
--- a/userprofile.py
+++ b/userprofile.py
@@ -41,3 +41,7 @@ def userProfile(request, cookieNotice, s, pageNumber, url_scheme):
             print(Exception)
             abort(500) #Shouldn't happen, 500 means internal server error
         return render_template('editEntries.html', content=response, loginbar=loginbar, cookieNotice=cookieNotice, backButton=backButton, nextButton=nextButton) #Put the table and the login div inside the template and server it to the user
+
+if (__name__ == "__main__"):
+    print("This file is not made fore direct call, please run the main.py")
+    exit()
\ No newline at end of file
-- 
GitLab