From 5bd179c01f442c1072be7d4889e5b94a161ffa92 Mon Sep 17 00:00:00 2001
From: Jonas Leder <jonas@jonasled.de>
Date: Sat, 9 Nov 2019 11:47:57 +0100
Subject: [PATCH] hide domain select, if only one domain is in use

---
 main.py | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)

diff --git a/main.py b/main.py
index dc7d17c..bbab583 100644
--- a/main.py
+++ b/main.py
@@ -14,6 +14,7 @@ domain_to_index = {}
 domain_prepared = ""
 builddate = ""
 version = ""
+showDomainSelect = False
 
 try:
     domain = os.environ["domains"].split(";") #Get the domains from the enviorement variable. If no enviorement variable is set set it to 127.0.0.1:5000 (for testing)
@@ -57,10 +58,13 @@ except:
 
 index = 0
 for domains in domain: #Make from every domnain a entry for the select box later
-    domains = domains
     domain_prepared = domain_prepared + '<option value="' + str(domains) + '">' + str(domains) + '</option>'
     domain_to_index[domains] = str(index)
     index = index + 1
+if(index > 1):
+    showDomainSelect=True #Show only domain select, if there are more than one available
+else:
+    domain_prepared = domain[0]
 
 
 def table_check():
@@ -107,19 +111,19 @@ def grecaptcha_verify(request):
 
 @app.route('/', methods=['GET'])
 def home_get():
-    return render_template('home.html', builddate=builddate, version=version, domain=domain_prepared, recaptchaPublicKey=recaptchaPublicKey) #return the default site to create a new shorten link
+    return render_template('home.html', builddate=builddate, version=version, domain=domain_prepared, recaptchaPublicKey=recaptchaPublicKey, showDomainSelect=showDomainSelect) #return the default site to create a new shorten link
 
 
 @app.route('/', methods=['POST']) #This function is used to create a new url
 def home_post():
     if not grecaptcha_verify(request) and not skipCaptcha:
-        return render_template('home.html', builddate=builddate, version=version, domain=domain_prepared, snackbar="There was an error validating, that you are a human, please try again.", long_url_prefilled=request.form.get('url'), short_url_prefilled=request.form.get('short').lower(), domain_prefilled=domain_to_index[request.form.get('domain')], recaptchaPublicKey=recaptchaPublicKey) #return the user the prefilled form with an error message, because no url to short was provided
+        return render_template('home.html', builddate=builddate, version=version, domain=domain_prepared, snackbar="There was an error validating, that you are a human, please try again.", long_url_prefilled=request.form.get('url'), short_url_prefilled=request.form.get('short').lower(), domain_prefilled=domain_to_index[request.form.get('domain')], recaptchaPublicKey=recaptchaPublicKey, showDomainSelect=showDomainSelect) #return the user the prefilled form with an error message, because no url to short was provided
 
 
     if (request.form.get('url').replace(" ", "") == ""):
-        return render_template('home.html', builddate=builddate, version=version, domain=domain_prepared, snackbar="Please enter a url to short, before submitting this form", long_url_prefilled=request.form.get('url'), short_url_prefilled=request.form.get('short').lower(), domain_prefilled=domain_to_index[request.form.get('domain')], recaptchaPublicKey=recaptchaPublicKey) #return the user the prefilled form with an error message, because no url to short was provided
+        return render_template('home.html', builddate=builddate, version=version, domain=domain_prepared, snackbar="Please enter a url to short, before submitting this form", long_url_prefilled=request.form.get('url'), short_url_prefilled=request.form.get('short').lower(), domain_prefilled=domain_to_index[request.form.get('domain')], recaptchaPublicKey=recaptchaPublicKey, showDomainSelect=showDomainSelect) #return the user the prefilled form with an error message, because no url to short was provided
     if (request.form.get('short').replace(" ", "") == ""):
-        return render_template('home.html', builddate=builddate, version=version, domain=domain_prepared, snackbar="Please enter a short name, before submitting this form", long_url_prefilled=request.form.get('url'), short_url_prefilled=request.form.get('short').lower(), domain_prefilled=domain_to_index[request.form.get('domain')], recaptchaPublicKey=recaptchaPublicKey) #return the user the prefilled form with an error message, because no short link was provided
+        return render_template('home.html', builddate=builddate, version=version, domain=domain_prepared, snackbar="Please enter a short name, before submitting this form", long_url_prefilled=request.form.get('url'), short_url_prefilled=request.form.get('short').lower(), domain_prefilled=domain_to_index[request.form.get('domain')], recaptchaPublicKey=recaptchaPublicKey, showDomainSelect=showDomainSelect) #return the user the prefilled form with an error message, because no short link was provided
     shorturl = (request.form.get('domain') + "/" + request.form.get('short')).lower()
 
     url = request.form.get('url')
@@ -141,7 +145,7 @@ def home_post():
             )
             return render_template('home.html', short_url=shorturl, recaptchaPublicKey=recaptchaPublicKey, builddate=builddate, version=version, domain=domain_prepared, qrcode=makeQR("http://" + shorturl)) #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')]) #return the user the prefilled form with an error message, because the url was already used
+            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) #return the user the prefilled form with an error message, because the url was already used
     
 @app.route('/favicon.ico') #Redirect to the static url of the favicon
 def favicon():
-- 
GitLab