We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Configuration can be auto-filled potentially causing issues


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `CTFd/admin/__init__.py`
Content:
```
1 import hashlib
2 import json
3 import os
4 import datetime
5 
6 from flask import current_app as app, render_template, request, redirect, jsonify, url_for, Blueprint, \
7     abort, render_template_string, send_file
8 from passlib.hash import bcrypt_sha256
9 from sqlalchemy.sql import not_
10 from sqlalchemy.exc import IntegrityError
11 
12 from CTFd.utils import admins_only, is_admin, cache, export_ctf, import_ctf
13 from CTFd.models import db, Teams, Solves, Awards, Challenges, WrongKeys, Keys, Tags, Files, Tracking, Pages, Config, DatabaseError
14 from CTFd.plugins.keys import get_key_class, KEY_CLASSES
15 
16 from CTFd.admin.statistics import admin_statistics
17 from CTFd.admin.challenges import admin_challenges
18 from CTFd.admin.scoreboard import admin_scoreboard
19 from CTFd.admin.pages import admin_pages
20 from CTFd.admin.keys import admin_keys
21 from CTFd.admin.teams import admin_teams
22 
23 from CTFd import utils
24 
25 
26 admin = Blueprint('admin', __name__)
27 
28 
29 @admin.route('/admin', methods=['GET'])
30 def admin_view():
31     if is_admin():
32         return redirect(url_for('admin_statistics.admin_graphs'))
33 
34     return redirect(url_for('auth.login'))
35 
36 
37 @admin.route('/admin/plugins/<plugin>', methods=['GET', 'POST'])
38 @admins_only
39 def admin_plugin_config(plugin):
40     if request.method == 'GET':
41         plugins_path = os.path.join(app.root_path, 'plugins')
42 
43         config_html_plugins = [name for name in os.listdir(plugins_path)
44                                if os.path.isfile(os.path.join(plugins_path, name, 'config.html'))]
45 
46         if plugin in config_html_plugins:
47             config = open(os.path.join(app.root_path, 'plugins', plugin, 'config.html')).read()
48             return render_template_string(config)
49         abort(404)
50     elif request.method == 'POST':
51         for k, v in request.form.items():
52             if k == "nonce":
53                 continue
54             utils.set_config(k, v)
55         with app.app_context():
56             cache.clear()
57         return '1'
58 
59 
60 @admin.route('/admin/import', methods=['GET', 'POST'])
61 @admins_only
62 def admin_import_ctf():
63     backup = request.files['backup']
64     segments = request.form.get('segments')
65     errors = []
66     try:
67         if segments:
68             import_ctf(backup, segments=segments.split(','))
69         else:
70             import_ctf(backup)
71     except Exception as e:
72         print(e)
73         errors.append(type(e).__name__)
74 
75     if errors:
76         return errors[0], 500
77     else:
78         return redirect(url_for('admin.admin_config'))
79 
80 
81 @admin.route('/admin/export', methods=['GET', 'POST'])
82 @admins_only
83 def admin_export_ctf():
84     segments = request.args.get('segments')
85     if segments:
86         backup = export_ctf(segments.split(','))
87     else:
88         backup = export_ctf()
89     ctf_name = utils.ctf_name()
90     day = datetime.datetime.now().strftime("%Y-%m-%d")
91     full_name = "{}.{}.zip".format(ctf_name, day)
92     return send_file(backup, as_attachment=True, attachment_filename=full_name)
93 
94 
95 @admin.route('/admin/config', methods=['GET', 'POST'])
96 @admins_only
97 def admin_config():
98     if request.method == "POST":
99         start = None
100         end = None
101         freeze = None
102         if request.form.get('start'):
103             start = int(request.form['start'])
104         if request.form.get('end'):
105             end = int(request.form['end'])
106         if request.form.get('freeze'):
107             freeze = int(request.form['freeze'])
108 
109         try:
110             view_challenges_unregistered = bool(request.form.get('view_challenges_unregistered', None))
111             view_scoreboard_if_authed = bool(request.form.get('view_scoreboard_if_authed', None))
112             hide_scores = bool(request.form.get('hide_scores', None))
113             prevent_registration = bool(request.form.get('prevent_registration', None))
114             prevent_name_change = bool(request.form.get('prevent_name_change', None))
115             view_after_ctf = bool(request.form.get('view_after_ctf', None))
116             verify_emails = bool(request.form.get('verify_emails', None))
117             mail_tls = bool(request.form.get('mail_tls', None))
118             mail_ssl = bool(request.form.get('mail_ssl', None))
119             mail_useauth = bool(request.form.get('mail_useauth', None))
120         except (ValueError, TypeError):
121             view_challenges_unregistered = None
122             view_scoreboard_if_authed = None
123             hide_scores = None
124             prevent_registration = None
125             prevent_name_change = None
126             view_after_ctf = None
127             verify_emails = None
128             mail_tls = None
129             mail_ssl = None
130             mail_useauth = None
131         finally:
132             view_challenges_unregistered = utils.set_config('view_challenges_unregistered', view_challenges_unregistered)
133             view_scoreboard_if_authed = utils.set_config('view_scoreboard_if_authed', view_scoreboard_if_authed)
134             hide_scores = utils.set_config('hide_scores', hide_scores)
135             prevent_registration = utils.set_config('prevent_registration', prevent_registration)
136             prevent_name_change = utils.set_config('prevent_name_change', prevent_name_change)
137             view_after_ctf = utils.set_config('view_after_ctf', view_after_ctf)
138             verify_emails = utils.set_config('verify_emails', verify_emails)
139             mail_tls = utils.set_config('mail_tls', mail_tls)
140             mail_ssl = utils.set_config('mail_ssl', mail_ssl)
141             mail_useauth = utils.set_config('mail_useauth', mail_useauth)
142 
143         mail_server = utils.set_config("mail_server", request.form.get('mail_server', None))
144         mail_port = utils.set_config("mail_port", request.form.get('mail_port', None))
145 
146         mail_username = utils.set_config("mail_username", request.form.get('mail_username', None))
147         mail_password = utils.set_config("mail_password", request.form.get('mail_password', None))
148 
149         ctf_name = utils.set_config("ctf_name", request.form.get('ctf_name', None))
150         ctf_theme = utils.set_config("ctf_theme", request.form.get('ctf_theme', None))
151 
152         mailfrom_addr = utils.set_config("mailfrom_addr", request.form.get('mailfrom_addr', None))
153         mg_base_url = utils.set_config("mg_base_url", request.form.get('mg_base_url', None))
154         mg_api_key = utils.set_config("mg_api_key", request.form.get('mg_api_key', None))
155 
156         db_freeze = utils.set_config("freeze", freeze)
157 
158         db_start = Config.query.filter_by(key='start').first()
159         db_start.value = start
160 
161         db_end = Config.query.filter_by(key='end').first()
162         db_end.value = end
163 
164         db.session.add(db_start)
165         db.session.add(db_end)
166 
167         db.session.commit()
168         db.session.close()
169         with app.app_context():
170             cache.clear()
171         return redirect(url_for('admin.admin_config'))
172 
173     with app.app_context():
174         cache.clear()
175     ctf_name = utils.get_config('ctf_name')
176     ctf_theme = utils.get_config('ctf_theme')
177     hide_scores = utils.get_config('hide_scores')
178 
179     mail_server = utils.get_config('mail_server')
180     mail_port = utils.get_config('mail_port')
181     mail_username = utils.get_config('mail_username')
182     mail_password = utils.get_config('mail_password')
183 
184     mailfrom_addr = utils.get_config('mailfrom_addr')
185     mg_api_key = utils.get_config('mg_api_key')
186     mg_base_url = utils.get_config('mg_base_url')
187 
188     view_after_ctf = utils.get_config('view_after_ctf')
189     start = utils.get_config('start')
190     end = utils.get_config('end')
191     freeze = utils.get_config('freeze')
192 
193     mail_tls = utils.get_config('mail_tls')
194     mail_ssl = utils.get_config('mail_ssl')
195     mail_useauth = utils.get_config('mail_useauth')
196 
197     view_challenges_unregistered = utils.get_config('view_challenges_unregistered')
198     view_scoreboard_if_authed = utils.get_config('view_scoreboard_if_authed')
199     prevent_registration = utils.get_config('prevent_registration')
200     prevent_name_change = utils.get_config('prevent_name_change')
201     verify_emails = utils.get_config('verify_emails')
202 
203     db.session.commit()
204     db.session.close()
205 
206     themes = utils.get_themes()
207     themes.remove(ctf_theme)
208 
209     return render_template('admin/config.html',
210                            ctf_name=ctf_name,
211                            ctf_theme_config=ctf_theme,
212                            start=start,
213                            end=end,
214                            freeze=freeze,
215                            hide_scores=hide_scores,
216                            mail_server=mail_server,
217                            mail_port=mail_port,
218                            mail_useauth=mail_useauth,
219                            mail_username=mail_username,
220                            mail_password=mail_password,
221                            mail_tls=mail_tls,
222                            mail_ssl=mail_ssl,
223                            view_challenges_unregistered=view_challenges_unregistered,
224                            view_scoreboard_if_authed=view_scoreboard_if_authed,
225                            prevent_registration=prevent_registration,
226                            mailfrom_addr=mailfrom_addr,
227                            mg_base_url=mg_base_url,
228                            mg_api_key=mg_api_key,
229                            prevent_name_change=prevent_name_change,
230                            verify_emails=verify_emails,
231                            view_after_ctf=view_after_ctf,
232                            themes=themes)
233
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/CTFd/admin/__init__.py b/CTFd/admin/__init__.py
--- a/CTFd/admin/__init__.py
+++ b/CTFd/admin/__init__.py
@@ -143,8 +143,15 @@
         mail_server = utils.set_config("mail_server", request.form.get('mail_server', None))
         mail_port = utils.set_config("mail_port", request.form.get('mail_port', None))
 
-        mail_username = utils.set_config("mail_username", request.form.get('mail_username', None))
-        mail_password = utils.set_config("mail_password", request.form.get('mail_password', None))
+        if request.form.get('mail_useauth', None) and (request.form.get('mail_u', None) or request.form.get('mail_p', None)):
+            if len(request.form.get('mail_u')) > 0:
+                mail_username = utils.set_config("mail_username", request.form.get('mail_u', None))
+            if len(request.form.get('mail_p')) > 0:
+                mail_password = utils.set_config("mail_password", request.form.get('mail_p', None))
+
+        elif request.form.get('mail_useauth', None) is None:
+            utils.set_config("mail_username", None)
+            utils.set_config("mail_password", None)
 
         ctf_name = utils.set_config("ctf_name", request.form.get('ctf_name', None))
         ctf_theme = utils.set_config("ctf_theme", request.form.get('ctf_theme', None))

{"golden_diff": "diff --git a/CTFd/admin/__init__.py b/CTFd/admin/__init__.py\n--- a/CTFd/admin/__init__.py\n+++ b/CTFd/admin/__init__.py\n@@ -143,8 +143,15 @@\n         mail_server = utils.set_config(\"mail_server\", request.form.get('mail_server', None))\n         mail_port = utils.set_config(\"mail_port\", request.form.get('mail_port', None))\n \n-        mail_username = utils.set_config(\"mail_username\", request.form.get('mail_username', None))\n-        mail_password = utils.set_config(\"mail_password\", request.form.get('mail_password', None))\n+        if request.form.get('mail_useauth', None) and (request.form.get('mail_u', None) or request.form.get('mail_p', None)):\n+            if len(request.form.get('mail_u')) > 0:\n+                mail_username = utils.set_config(\"mail_username\", request.form.get('mail_u', None))\n+            if len(request.form.get('mail_p')) > 0:\n+                mail_password = utils.set_config(\"mail_password\", request.form.get('mail_p', None))\n+\n+        elif request.form.get('mail_useauth', None) is None:\n+            utils.set_config(\"mail_username\", None)\n+            utils.set_config(\"mail_password\", None)\n \n         ctf_name = utils.set_config(\"ctf_name\", request.form.get('ctf_name', None))\n         ctf_theme = utils.set_config(\"ctf_theme\", request.form.get('ctf_theme', None))\n", "issue": "Configuration can be auto-filled potentially causing issues\n\n", "before_files": [{"content": "import hashlib\nimport json\nimport os\nimport datetime\n\nfrom flask import current_app as app, render_template, request, redirect, jsonify, url_for, Blueprint, \\\n    abort, render_template_string, send_file\nfrom passlib.hash import bcrypt_sha256\nfrom sqlalchemy.sql import not_\nfrom sqlalchemy.exc import IntegrityError\n\nfrom CTFd.utils import admins_only, is_admin, cache, export_ctf, import_ctf\nfrom CTFd.models import db, Teams, Solves, Awards, Challenges, WrongKeys, Keys, Tags, Files, Tracking, Pages, Config, DatabaseError\nfrom CTFd.plugins.keys import get_key_class, KEY_CLASSES\n\nfrom CTFd.admin.statistics import admin_statistics\nfrom CTFd.admin.challenges import admin_challenges\nfrom CTFd.admin.scoreboard import admin_scoreboard\nfrom CTFd.admin.pages import admin_pages\nfrom CTFd.admin.keys import admin_keys\nfrom CTFd.admin.teams import admin_teams\n\nfrom CTFd import utils\n\n\nadmin = Blueprint('admin', __name__)\n\n\n@admin.route('/admin', methods=['GET'])\ndef admin_view():\n    if is_admin():\n        return redirect(url_for('admin_statistics.admin_graphs'))\n\n    return redirect(url_for('auth.login'))\n\n\n@admin.route('/admin/plugins/<plugin>', methods=['GET', 'POST'])\n@admins_only\ndef admin_plugin_config(plugin):\n    if request.method == 'GET':\n        plugins_path = os.path.join(app.root_path, 'plugins')\n\n        config_html_plugins = [name for name in os.listdir(plugins_path)\n                               if os.path.isfile(os.path.join(plugins_path, name, 'config.html'))]\n\n        if plugin in config_html_plugins:\n            config = open(os.path.join(app.root_path, 'plugins', plugin, 'config.html')).read()\n            return render_template_string(config)\n        abort(404)\n    elif request.method == 'POST':\n        for k, v in request.form.items():\n            if k == \"nonce\":\n                continue\n            utils.set_config(k, v)\n        with app.app_context():\n            cache.clear()\n        return '1'\n\n\n@admin.route('/admin/import', methods=['GET', 'POST'])\n@admins_only\ndef admin_import_ctf():\n    backup = request.files['backup']\n    segments = request.form.get('segments')\n    errors = []\n    try:\n        if segments:\n            import_ctf(backup, segments=segments.split(','))\n        else:\n            import_ctf(backup)\n    except Exception as e:\n        print(e)\n        errors.append(type(e).__name__)\n\n    if errors:\n        return errors[0], 500\n    else:\n        return redirect(url_for('admin.admin_config'))\n\n\n@admin.route('/admin/export', methods=['GET', 'POST'])\n@admins_only\ndef admin_export_ctf():\n    segments = request.args.get('segments')\n    if segments:\n        backup = export_ctf(segments.split(','))\n    else:\n        backup = export_ctf()\n    ctf_name = utils.ctf_name()\n    day = datetime.datetime.now().strftime(\"%Y-%m-%d\")\n    full_name = \"{}.{}.zip\".format(ctf_name, day)\n    return send_file(backup, as_attachment=True, attachment_filename=full_name)\n\n\n@admin.route('/admin/config', methods=['GET', 'POST'])\n@admins_only\ndef admin_config():\n    if request.method == \"POST\":\n        start = None\n        end = None\n        freeze = None\n        if request.form.get('start'):\n            start = int(request.form['start'])\n        if request.form.get('end'):\n            end = int(request.form['end'])\n        if request.form.get('freeze'):\n            freeze = int(request.form['freeze'])\n\n        try:\n            view_challenges_unregistered = bool(request.form.get('view_challenges_unregistered', None))\n            view_scoreboard_if_authed = bool(request.form.get('view_scoreboard_if_authed', None))\n            hide_scores = bool(request.form.get('hide_scores', None))\n            prevent_registration = bool(request.form.get('prevent_registration', None))\n            prevent_name_change = bool(request.form.get('prevent_name_change', None))\n            view_after_ctf = bool(request.form.get('view_after_ctf', None))\n            verify_emails = bool(request.form.get('verify_emails', None))\n            mail_tls = bool(request.form.get('mail_tls', None))\n            mail_ssl = bool(request.form.get('mail_ssl', None))\n            mail_useauth = bool(request.form.get('mail_useauth', None))\n        except (ValueError, TypeError):\n            view_challenges_unregistered = None\n            view_scoreboard_if_authed = None\n            hide_scores = None\n            prevent_registration = None\n            prevent_name_change = None\n            view_after_ctf = None\n            verify_emails = None\n            mail_tls = None\n            mail_ssl = None\n            mail_useauth = None\n        finally:\n            view_challenges_unregistered = utils.set_config('view_challenges_unregistered', view_challenges_unregistered)\n            view_scoreboard_if_authed = utils.set_config('view_scoreboard_if_authed', view_scoreboard_if_authed)\n            hide_scores = utils.set_config('hide_scores', hide_scores)\n            prevent_registration = utils.set_config('prevent_registration', prevent_registration)\n            prevent_name_change = utils.set_config('prevent_name_change', prevent_name_change)\n            view_after_ctf = utils.set_config('view_after_ctf', view_after_ctf)\n            verify_emails = utils.set_config('verify_emails', verify_emails)\n            mail_tls = utils.set_config('mail_tls', mail_tls)\n            mail_ssl = utils.set_config('mail_ssl', mail_ssl)\n            mail_useauth = utils.set_config('mail_useauth', mail_useauth)\n\n        mail_server = utils.set_config(\"mail_server\", request.form.get('mail_server', None))\n        mail_port = utils.set_config(\"mail_port\", request.form.get('mail_port', None))\n\n        mail_username = utils.set_config(\"mail_username\", request.form.get('mail_username', None))\n        mail_password = utils.set_config(\"mail_password\", request.form.get('mail_password', None))\n\n        ctf_name = utils.set_config(\"ctf_name\", request.form.get('ctf_name', None))\n        ctf_theme = utils.set_config(\"ctf_theme\", request.form.get('ctf_theme', None))\n\n        mailfrom_addr = utils.set_config(\"mailfrom_addr\", request.form.get('mailfrom_addr', None))\n        mg_base_url = utils.set_config(\"mg_base_url\", request.form.get('mg_base_url', None))\n        mg_api_key = utils.set_config(\"mg_api_key\", request.form.get('mg_api_key', None))\n\n        db_freeze = utils.set_config(\"freeze\", freeze)\n\n        db_start = Config.query.filter_by(key='start').first()\n        db_start.value = start\n\n        db_end = Config.query.filter_by(key='end').first()\n        db_end.value = end\n\n        db.session.add(db_start)\n        db.session.add(db_end)\n\n        db.session.commit()\n        db.session.close()\n        with app.app_context():\n            cache.clear()\n        return redirect(url_for('admin.admin_config'))\n\n    with app.app_context():\n        cache.clear()\n    ctf_name = utils.get_config('ctf_name')\n    ctf_theme = utils.get_config('ctf_theme')\n    hide_scores = utils.get_config('hide_scores')\n\n    mail_server = utils.get_config('mail_server')\n    mail_port = utils.get_config('mail_port')\n    mail_username = utils.get_config('mail_username')\n    mail_password = utils.get_config('mail_password')\n\n    mailfrom_addr = utils.get_config('mailfrom_addr')\n    mg_api_key = utils.get_config('mg_api_key')\n    mg_base_url = utils.get_config('mg_base_url')\n\n    view_after_ctf = utils.get_config('view_after_ctf')\n    start = utils.get_config('start')\n    end = utils.get_config('end')\n    freeze = utils.get_config('freeze')\n\n    mail_tls = utils.get_config('mail_tls')\n    mail_ssl = utils.get_config('mail_ssl')\n    mail_useauth = utils.get_config('mail_useauth')\n\n    view_challenges_unregistered = utils.get_config('view_challenges_unregistered')\n    view_scoreboard_if_authed = utils.get_config('view_scoreboard_if_authed')\n    prevent_registration = utils.get_config('prevent_registration')\n    prevent_name_change = utils.get_config('prevent_name_change')\n    verify_emails = utils.get_config('verify_emails')\n\n    db.session.commit()\n    db.session.close()\n\n    themes = utils.get_themes()\n    themes.remove(ctf_theme)\n\n    return render_template('admin/config.html',\n                           ctf_name=ctf_name,\n                           ctf_theme_config=ctf_theme,\n                           start=start,\n                           end=end,\n                           freeze=freeze,\n                           hide_scores=hide_scores,\n                           mail_server=mail_server,\n                           mail_port=mail_port,\n                           mail_useauth=mail_useauth,\n                           mail_username=mail_username,\n                           mail_password=mail_password,\n                           mail_tls=mail_tls,\n                           mail_ssl=mail_ssl,\n                           view_challenges_unregistered=view_challenges_unregistered,\n                           view_scoreboard_if_authed=view_scoreboard_if_authed,\n                           prevent_registration=prevent_registration,\n                           mailfrom_addr=mailfrom_addr,\n                           mg_base_url=mg_base_url,\n                           mg_api_key=mg_api_key,\n                           prevent_name_change=prevent_name_change,\n                           verify_emails=verify_emails,\n                           view_after_ctf=view_after_ctf,\n                           themes=themes)\n", "path": "CTFd/admin/__init__.py"}], "after_files": [{"content": "import hashlib\nimport json\nimport os\nimport datetime\n\nfrom flask import current_app as app, render_template, request, redirect, jsonify, url_for, Blueprint, \\\n    abort, render_template_string, send_file\nfrom passlib.hash import bcrypt_sha256\nfrom sqlalchemy.sql import not_\nfrom sqlalchemy.exc import IntegrityError\n\nfrom CTFd.utils import admins_only, is_admin, cache, export_ctf, import_ctf\nfrom CTFd.models import db, Teams, Solves, Awards, Challenges, WrongKeys, Keys, Tags, Files, Tracking, Pages, Config, DatabaseError\nfrom CTFd.plugins.keys import get_key_class, KEY_CLASSES\n\nfrom CTFd.admin.statistics import admin_statistics\nfrom CTFd.admin.challenges import admin_challenges\nfrom CTFd.admin.scoreboard import admin_scoreboard\nfrom CTFd.admin.pages import admin_pages\nfrom CTFd.admin.keys import admin_keys\nfrom CTFd.admin.teams import admin_teams\n\nfrom CTFd import utils\n\n\nadmin = Blueprint('admin', __name__)\n\n\n@admin.route('/admin', methods=['GET'])\ndef admin_view():\n    if is_admin():\n        return redirect(url_for('admin_statistics.admin_graphs'))\n\n    return redirect(url_for('auth.login'))\n\n\n@admin.route('/admin/plugins/<plugin>', methods=['GET', 'POST'])\n@admins_only\ndef admin_plugin_config(plugin):\n    if request.method == 'GET':\n        plugins_path = os.path.join(app.root_path, 'plugins')\n\n        config_html_plugins = [name for name in os.listdir(plugins_path)\n                               if os.path.isfile(os.path.join(plugins_path, name, 'config.html'))]\n\n        if plugin in config_html_plugins:\n            config = open(os.path.join(app.root_path, 'plugins', plugin, 'config.html')).read()\n            return render_template_string(config)\n        abort(404)\n    elif request.method == 'POST':\n        for k, v in request.form.items():\n            if k == \"nonce\":\n                continue\n            utils.set_config(k, v)\n        with app.app_context():\n            cache.clear()\n        return '1'\n\n\n@admin.route('/admin/import', methods=['GET', 'POST'])\n@admins_only\ndef admin_import_ctf():\n    backup = request.files['backup']\n    segments = request.form.get('segments')\n    errors = []\n    try:\n        if segments:\n            import_ctf(backup, segments=segments.split(','))\n        else:\n            import_ctf(backup)\n    except Exception as e:\n        print(e)\n        errors.append(type(e).__name__)\n\n    if errors:\n        return errors[0], 500\n    else:\n        return redirect(url_for('admin.admin_config'))\n\n\n@admin.route('/admin/export', methods=['GET', 'POST'])\n@admins_only\ndef admin_export_ctf():\n    segments = request.args.get('segments')\n    if segments:\n        backup = export_ctf(segments.split(','))\n    else:\n        backup = export_ctf()\n    ctf_name = utils.ctf_name()\n    day = datetime.datetime.now().strftime(\"%Y-%m-%d\")\n    full_name = \"{}.{}.zip\".format(ctf_name, day)\n    return send_file(backup, as_attachment=True, attachment_filename=full_name)\n\n\n@admin.route('/admin/config', methods=['GET', 'POST'])\n@admins_only\ndef admin_config():\n    if request.method == \"POST\":\n        start = None\n        end = None\n        freeze = None\n        if request.form.get('start'):\n            start = int(request.form['start'])\n        if request.form.get('end'):\n            end = int(request.form['end'])\n        if request.form.get('freeze'):\n            freeze = int(request.form['freeze'])\n\n        try:\n            view_challenges_unregistered = bool(request.form.get('view_challenges_unregistered', None))\n            view_scoreboard_if_authed = bool(request.form.get('view_scoreboard_if_authed', None))\n            hide_scores = bool(request.form.get('hide_scores', None))\n            prevent_registration = bool(request.form.get('prevent_registration', None))\n            prevent_name_change = bool(request.form.get('prevent_name_change', None))\n            view_after_ctf = bool(request.form.get('view_after_ctf', None))\n            verify_emails = bool(request.form.get('verify_emails', None))\n            mail_tls = bool(request.form.get('mail_tls', None))\n            mail_ssl = bool(request.form.get('mail_ssl', None))\n            mail_useauth = bool(request.form.get('mail_useauth', None))\n        except (ValueError, TypeError):\n            view_challenges_unregistered = None\n            view_scoreboard_if_authed = None\n            hide_scores = None\n            prevent_registration = None\n            prevent_name_change = None\n            view_after_ctf = None\n            verify_emails = None\n            mail_tls = None\n            mail_ssl = None\n            mail_useauth = None\n        finally:\n            view_challenges_unregistered = utils.set_config('view_challenges_unregistered', view_challenges_unregistered)\n            view_scoreboard_if_authed = utils.set_config('view_scoreboard_if_authed', view_scoreboard_if_authed)\n            hide_scores = utils.set_config('hide_scores', hide_scores)\n            prevent_registration = utils.set_config('prevent_registration', prevent_registration)\n            prevent_name_change = utils.set_config('prevent_name_change', prevent_name_change)\n            view_after_ctf = utils.set_config('view_after_ctf', view_after_ctf)\n            verify_emails = utils.set_config('verify_emails', verify_emails)\n            mail_tls = utils.set_config('mail_tls', mail_tls)\n            mail_ssl = utils.set_config('mail_ssl', mail_ssl)\n            mail_useauth = utils.set_config('mail_useauth', mail_useauth)\n\n        mail_server = utils.set_config(\"mail_server\", request.form.get('mail_server', None))\n        mail_port = utils.set_config(\"mail_port\", request.form.get('mail_port', None))\n\n        if request.form.get('mail_useauth', None) and (request.form.get('mail_u', None) or request.form.get('mail_p', None)):\n            if len(request.form.get('mail_u')) > 0:\n                mail_username = utils.set_config(\"mail_username\", request.form.get('mail_u', None))\n            if len(request.form.get('mail_p')) > 0:\n                mail_password = utils.set_config(\"mail_password\", request.form.get('mail_p', None))\n\n        elif request.form.get('mail_useauth', None) is None:\n            utils.set_config(\"mail_username\", None)\n            utils.set_config(\"mail_password\", None)\n\n        ctf_name = utils.set_config(\"ctf_name\", request.form.get('ctf_name', None))\n        ctf_theme = utils.set_config(\"ctf_theme\", request.form.get('ctf_theme', None))\n\n        mailfrom_addr = utils.set_config(\"mailfrom_addr\", request.form.get('mailfrom_addr', None))\n        mg_base_url = utils.set_config(\"mg_base_url\", request.form.get('mg_base_url', None))\n        mg_api_key = utils.set_config(\"mg_api_key\", request.form.get('mg_api_key', None))\n\n        db_freeze = utils.set_config(\"freeze\", freeze)\n\n        db_start = Config.query.filter_by(key='start').first()\n        db_start.value = start\n\n        db_end = Config.query.filter_by(key='end').first()\n        db_end.value = end\n\n        db.session.add(db_start)\n        db.session.add(db_end)\n\n        db.session.commit()\n        db.session.close()\n        with app.app_context():\n            cache.clear()\n        return redirect(url_for('admin.admin_config'))\n\n    with app.app_context():\n        cache.clear()\n    ctf_name = utils.get_config('ctf_name')\n    ctf_theme = utils.get_config('ctf_theme')\n    hide_scores = utils.get_config('hide_scores')\n\n    mail_server = utils.get_config('mail_server')\n    mail_port = utils.get_config('mail_port')\n    mail_username = utils.get_config('mail_username')\n    mail_password = utils.get_config('mail_password')\n\n    mailfrom_addr = utils.get_config('mailfrom_addr')\n    mg_api_key = utils.get_config('mg_api_key')\n    mg_base_url = utils.get_config('mg_base_url')\n\n    view_after_ctf = utils.get_config('view_after_ctf')\n    start = utils.get_config('start')\n    end = utils.get_config('end')\n    freeze = utils.get_config('freeze')\n\n    mail_tls = utils.get_config('mail_tls')\n    mail_ssl = utils.get_config('mail_ssl')\n    mail_useauth = utils.get_config('mail_useauth')\n\n    view_challenges_unregistered = utils.get_config('view_challenges_unregistered')\n    view_scoreboard_if_authed = utils.get_config('view_scoreboard_if_authed')\n    prevent_registration = utils.get_config('prevent_registration')\n    prevent_name_change = utils.get_config('prevent_name_change')\n    verify_emails = utils.get_config('verify_emails')\n\n    db.session.commit()\n    db.session.close()\n\n    themes = utils.get_themes()\n    themes.remove(ctf_theme)\n\n    return render_template('admin/config.html',\n                           ctf_name=ctf_name,\n                           ctf_theme_config=ctf_theme,\n                           start=start,\n                           end=end,\n                           freeze=freeze,\n                           hide_scores=hide_scores,\n                           mail_server=mail_server,\n                           mail_port=mail_port,\n                           mail_useauth=mail_useauth,\n                           mail_username=mail_username,\n                           mail_password=mail_password,\n                           mail_tls=mail_tls,\n                           mail_ssl=mail_ssl,\n                           view_challenges_unregistered=view_challenges_unregistered,\n                           view_scoreboard_if_authed=view_scoreboard_if_authed,\n                           prevent_registration=prevent_registration,\n                           mailfrom_addr=mailfrom_addr,\n                           mg_base_url=mg_base_url,\n                           mg_api_key=mg_api_key,\n                           prevent_name_change=prevent_name_change,\n                           verify_emails=verify_emails,\n                           view_after_ctf=view_after_ctf,\n                           themes=themes)\n", "path": "CTFd/admin/__init__.py"}]}