We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Add CSV examples for CSV Import
Add CSV examples for CSV Import

--- 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/utils/csv/__init__.py`
Content:
```
1 import csv
2 import json
3 from io import BytesIO, StringIO
4 
5 from CTFd.models import (
6     Flags,
7     Hints,
8     Tags,
9     TeamFields,
10     Teams,
11     UserFields,
12     Users,
13     db,
14     get_class_by_tablename,
15 )
16 from CTFd.plugins.challenges import get_chal_class
17 from CTFd.utils.config import is_teams_mode, is_users_mode
18 from CTFd.utils.scores import get_standings
19 
20 
21 def get_dumpable_tables():
22     csv_keys = list(CSV_KEYS.keys())
23     db_keys = list(db.metadata.tables.keys())
24     tables = csv_keys + db_keys
25     table_keys = list(zip(tables, tables))
26     return table_keys
27 
28 
29 def dump_csv(name):
30     dump_func = CSV_KEYS.get(name)
31     if dump_func:
32         return dump_func()
33     elif get_class_by_tablename(name):
34         return dump_database_table(tablename=name)
35     else:
36         raise KeyError
37 
38 
39 def dump_scoreboard_csv():
40     # TODO: Add fields to scoreboard data
41     temp = StringIO()
42     writer = csv.writer(temp)
43 
44     standings = get_standings()
45 
46     # Get all user fields in a specific order
47     user_fields = UserFields.query.all()
48     user_field_ids = [f.id for f in user_fields]
49     user_field_names = [f.name for f in user_fields]
50 
51     if is_teams_mode():
52         team_fields = TeamFields.query.all()
53         team_field_ids = [f.id for f in team_fields]
54         team_field_names = [f.name for f in team_fields]
55 
56         header = (
57             [
58                 "place",
59                 "team",
60                 "team id",
61                 "score",
62                 "member name",
63                 "member id",
64                 "member email",
65                 "member score",
66             ]
67             + user_field_names
68             + team_field_names
69         )
70         writer.writerow(header)
71 
72         for i, standing in enumerate(standings):
73             team = Teams.query.filter_by(id=standing.account_id).first()
74 
75             # Build field entries using the order of the field values
76             team_field_entries = {f.field_id: f.value for f in team.field_entries}
77             team_field_values = [
78                 team_field_entries.get(f_id, "") for f_id in team_field_ids
79             ]
80             team_row = [
81                 i + 1,
82                 team.name,
83                 team.id,
84                 standing.score,
85                 "",
86                 "",
87             ] + team_field_values
88 
89             writer.writerow(team_row)
90 
91             for member in team.members:
92                 user_field_entries = {f.field_id: f.value for f in member.field_entries}
93                 user_field_values = [
94                     user_field_entries.get(f_id, "") for f_id in user_field_ids
95                 ]
96                 user_row = [
97                     "",
98                     "",
99                     "",
100                     "",
101                     member.name,
102                     member.id,
103                     member.email,
104                     member.score,
105                 ] + user_field_values
106                 writer.writerow(user_row)
107     elif is_users_mode():
108         header = ["place", "user", "score"] + user_field_names
109         writer.writerow(header)
110 
111         for i, standing in enumerate(standings):
112             user = Users.query.filter_by(id=standing.account_id).first()
113 
114             # Build field entries using the order of the field values
115             user_field_entries = {f.field_id: f.value for f in user.field_entries}
116             user_field_values = [
117                 user_field_entries.get(f_id, "") for f_id in user_field_ids
118             ]
119             user_row = [i + 1, user.name, standing.score] + user_field_values
120             writer.writerow(user_row)
121 
122     # In Python 3 send_file requires bytes
123     output = BytesIO()
124     output.write(temp.getvalue().encode("utf-8"))
125     output.seek(0)
126     temp.close()
127 
128     return output
129 
130 
131 def dump_users_with_fields_csv():
132     temp = StringIO()
133     writer = csv.writer(temp)
134 
135     user_fields = UserFields.query.all()
136     user_field_ids = [f.id for f in user_fields]
137     user_field_names = [f.name for f in user_fields]
138 
139     header = [column.name for column in Users.__mapper__.columns] + user_field_names
140     writer.writerow(header)
141 
142     responses = Users.query.all()
143 
144     for curr in responses:
145         user_field_entries = {f.field_id: f.value for f in curr.field_entries}
146         user_field_values = [
147             user_field_entries.get(f_id, "") for f_id in user_field_ids
148         ]
149         user_row = [
150             getattr(curr, column.name) for column in Users.__mapper__.columns
151         ] + user_field_values
152         writer.writerow(user_row)
153 
154     temp.seek(0)
155 
156     # In Python 3 send_file requires bytes
157     output = BytesIO()
158     output.write(temp.getvalue().encode("utf-8"))
159     output.seek(0)
160     temp.close()
161 
162     return output
163 
164 
165 def dump_teams_with_fields_csv():
166     temp = StringIO()
167     writer = csv.writer(temp)
168 
169     team_fields = TeamFields.query.all()
170     team_field_ids = [f.id for f in team_fields]
171     team_field_names = [f.name for f in team_fields]
172 
173     header = [column.name for column in Teams.__mapper__.columns] + team_field_names
174     writer.writerow(header)
175 
176     responses = Teams.query.all()
177 
178     for curr in responses:
179         team_field_entries = {f.field_id: f.value for f in curr.field_entries}
180         team_field_values = [
181             team_field_entries.get(f_id, "") for f_id in team_field_ids
182         ]
183 
184         team_row = [
185             getattr(curr, column.name) for column in Teams.__mapper__.columns
186         ] + team_field_values
187 
188         writer.writerow(team_row)
189 
190     temp.seek(0)
191 
192     # In Python 3 send_file requires bytes
193     output = BytesIO()
194     output.write(temp.getvalue().encode("utf-8"))
195     output.seek(0)
196     temp.close()
197 
198     return output
199 
200 
201 def dump_database_table(tablename):
202     # TODO: It might make sense to limit dumpable tables. Config could potentially leak sensitive information.
203     model = get_class_by_tablename(tablename)
204 
205     if model is None:
206         raise KeyError("Unknown database table")
207 
208     temp = StringIO()
209     writer = csv.writer(temp)
210 
211     header = [column.name for column in model.__mapper__.columns]
212     writer.writerow(header)
213 
214     responses = model.query.all()
215 
216     for curr in responses:
217         writer.writerow(
218             [getattr(curr, column.name) for column in model.__mapper__.columns]
219         )
220 
221     temp.seek(0)
222 
223     # In Python 3 send_file requires bytes
224     output = BytesIO()
225     output.write(temp.getvalue().encode("utf-8"))
226     output.seek(0)
227     temp.close()
228 
229     return output
230 
231 
232 def load_users_csv(dict_reader):
233     for line in dict_reader:
234         result = Users(**line)
235         db.session.add(result)
236         db.session.commit()
237     return True
238 
239 
240 def load_teams_csv(dict_reader):
241     for line in dict_reader:
242         result = Teams(**line)
243         db.session.add(result)
244         db.session.commit()
245     return True
246 
247 
248 def load_challenges_csv(dict_reader):
249     for line in dict_reader:
250         flags = line.pop("flags", None)
251         tags = line.pop("tags", None)
252         hints = line.pop("hints", None)
253         challenge_type = line.pop("type", "standard")
254 
255         # Load in custome type_data
256         type_data = json.loads(line.pop("type_data", "{}"))
257         line.update(type_data)
258 
259         ChallengeClass = get_chal_class(challenge_type)
260         challenge = ChallengeClass.challenge_model(**line)
261         db.session.add(challenge)
262         db.session.commit()
263 
264         if flags:
265             flags = [flag.strip() for flag in flags.split(",")]
266             for flag in flags:
267                 f = Flags(type="static", challenge_id=challenge.id, content=flag,)
268                 db.session.add(f)
269                 db.session.commit()
270 
271         if tags:
272             tags = [tag.strip() for tag in tags.split(",")]
273             for tag in tags:
274                 t = Tags(challenge_id=challenge.id, value=tag,)
275                 db.session.add(t)
276                 db.session.commit()
277 
278         if hints:
279             hints = [hint.strip() for hint in hints.split(",")]
280             for hint in hints:
281                 h = Hints(challenge_id=challenge.id, content=hint,)
282                 db.session.add(h)
283                 db.session.commit()
284     return True
285 
286 
287 CSV_KEYS = {
288     "scoreboard": dump_scoreboard_csv,
289     "users+fields": dump_users_with_fields_csv,
290     "teams+fields": dump_teams_with_fields_csv,
291 }
292
```
--- 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/utils/csv/__init__.py b/CTFd/utils/csv/__init__.py
--- a/CTFd/utils/csv/__init__.py
+++ b/CTFd/utils/csv/__init__.py
@@ -252,8 +252,8 @@
         hints = line.pop("hints", None)
         challenge_type = line.pop("type", "standard")
 
-        # Load in custome type_data
-        type_data = json.loads(line.pop("type_data", "{}"))
+        # Load in custom type_data
+        type_data = json.loads(line.pop("type_data", "{}") or "{}")
         line.update(type_data)
 
         ChallengeClass = get_chal_class(challenge_type)

{"golden_diff": "diff --git a/CTFd/utils/csv/__init__.py b/CTFd/utils/csv/__init__.py\n--- a/CTFd/utils/csv/__init__.py\n+++ b/CTFd/utils/csv/__init__.py\n@@ -252,8 +252,8 @@\n         hints = line.pop(\"hints\", None)\n         challenge_type = line.pop(\"type\", \"standard\")\n \n-        # Load in custome type_data\n-        type_data = json.loads(line.pop(\"type_data\", \"{}\"))\n+        # Load in custom type_data\n+        type_data = json.loads(line.pop(\"type_data\", \"{}\") or \"{}\")\n         line.update(type_data)\n \n         ChallengeClass = get_chal_class(challenge_type)\n", "issue": "Add CSV examples for CSV Import\nAdd CSV examples for CSV Import\n", "before_files": [{"content": "import csv\nimport json\nfrom io import BytesIO, StringIO\n\nfrom CTFd.models import (\n    Flags,\n    Hints,\n    Tags,\n    TeamFields,\n    Teams,\n    UserFields,\n    Users,\n    db,\n    get_class_by_tablename,\n)\nfrom CTFd.plugins.challenges import get_chal_class\nfrom CTFd.utils.config import is_teams_mode, is_users_mode\nfrom CTFd.utils.scores import get_standings\n\n\ndef get_dumpable_tables():\n    csv_keys = list(CSV_KEYS.keys())\n    db_keys = list(db.metadata.tables.keys())\n    tables = csv_keys + db_keys\n    table_keys = list(zip(tables, tables))\n    return table_keys\n\n\ndef dump_csv(name):\n    dump_func = CSV_KEYS.get(name)\n    if dump_func:\n        return dump_func()\n    elif get_class_by_tablename(name):\n        return dump_database_table(tablename=name)\n    else:\n        raise KeyError\n\n\ndef dump_scoreboard_csv():\n    # TODO: Add fields to scoreboard data\n    temp = StringIO()\n    writer = csv.writer(temp)\n\n    standings = get_standings()\n\n    # Get all user fields in a specific order\n    user_fields = UserFields.query.all()\n    user_field_ids = [f.id for f in user_fields]\n    user_field_names = [f.name for f in user_fields]\n\n    if is_teams_mode():\n        team_fields = TeamFields.query.all()\n        team_field_ids = [f.id for f in team_fields]\n        team_field_names = [f.name for f in team_fields]\n\n        header = (\n            [\n                \"place\",\n                \"team\",\n                \"team id\",\n                \"score\",\n                \"member name\",\n                \"member id\",\n                \"member email\",\n                \"member score\",\n            ]\n            + user_field_names\n            + team_field_names\n        )\n        writer.writerow(header)\n\n        for i, standing in enumerate(standings):\n            team = Teams.query.filter_by(id=standing.account_id).first()\n\n            # Build field entries using the order of the field values\n            team_field_entries = {f.field_id: f.value for f in team.field_entries}\n            team_field_values = [\n                team_field_entries.get(f_id, \"\") for f_id in team_field_ids\n            ]\n            team_row = [\n                i + 1,\n                team.name,\n                team.id,\n                standing.score,\n                \"\",\n                \"\",\n            ] + team_field_values\n\n            writer.writerow(team_row)\n\n            for member in team.members:\n                user_field_entries = {f.field_id: f.value for f in member.field_entries}\n                user_field_values = [\n                    user_field_entries.get(f_id, \"\") for f_id in user_field_ids\n                ]\n                user_row = [\n                    \"\",\n                    \"\",\n                    \"\",\n                    \"\",\n                    member.name,\n                    member.id,\n                    member.email,\n                    member.score,\n                ] + user_field_values\n                writer.writerow(user_row)\n    elif is_users_mode():\n        header = [\"place\", \"user\", \"score\"] + user_field_names\n        writer.writerow(header)\n\n        for i, standing in enumerate(standings):\n            user = Users.query.filter_by(id=standing.account_id).first()\n\n            # Build field entries using the order of the field values\n            user_field_entries = {f.field_id: f.value for f in user.field_entries}\n            user_field_values = [\n                user_field_entries.get(f_id, \"\") for f_id in user_field_ids\n            ]\n            user_row = [i + 1, user.name, standing.score] + user_field_values\n            writer.writerow(user_row)\n\n    # In Python 3 send_file requires bytes\n    output = BytesIO()\n    output.write(temp.getvalue().encode(\"utf-8\"))\n    output.seek(0)\n    temp.close()\n\n    return output\n\n\ndef dump_users_with_fields_csv():\n    temp = StringIO()\n    writer = csv.writer(temp)\n\n    user_fields = UserFields.query.all()\n    user_field_ids = [f.id for f in user_fields]\n    user_field_names = [f.name for f in user_fields]\n\n    header = [column.name for column in Users.__mapper__.columns] + user_field_names\n    writer.writerow(header)\n\n    responses = Users.query.all()\n\n    for curr in responses:\n        user_field_entries = {f.field_id: f.value for f in curr.field_entries}\n        user_field_values = [\n            user_field_entries.get(f_id, \"\") for f_id in user_field_ids\n        ]\n        user_row = [\n            getattr(curr, column.name) for column in Users.__mapper__.columns\n        ] + user_field_values\n        writer.writerow(user_row)\n\n    temp.seek(0)\n\n    # In Python 3 send_file requires bytes\n    output = BytesIO()\n    output.write(temp.getvalue().encode(\"utf-8\"))\n    output.seek(0)\n    temp.close()\n\n    return output\n\n\ndef dump_teams_with_fields_csv():\n    temp = StringIO()\n    writer = csv.writer(temp)\n\n    team_fields = TeamFields.query.all()\n    team_field_ids = [f.id for f in team_fields]\n    team_field_names = [f.name for f in team_fields]\n\n    header = [column.name for column in Teams.__mapper__.columns] + team_field_names\n    writer.writerow(header)\n\n    responses = Teams.query.all()\n\n    for curr in responses:\n        team_field_entries = {f.field_id: f.value for f in curr.field_entries}\n        team_field_values = [\n            team_field_entries.get(f_id, \"\") for f_id in team_field_ids\n        ]\n\n        team_row = [\n            getattr(curr, column.name) for column in Teams.__mapper__.columns\n        ] + team_field_values\n\n        writer.writerow(team_row)\n\n    temp.seek(0)\n\n    # In Python 3 send_file requires bytes\n    output = BytesIO()\n    output.write(temp.getvalue().encode(\"utf-8\"))\n    output.seek(0)\n    temp.close()\n\n    return output\n\n\ndef dump_database_table(tablename):\n    # TODO: It might make sense to limit dumpable tables. Config could potentially leak sensitive information.\n    model = get_class_by_tablename(tablename)\n\n    if model is None:\n        raise KeyError(\"Unknown database table\")\n\n    temp = StringIO()\n    writer = csv.writer(temp)\n\n    header = [column.name for column in model.__mapper__.columns]\n    writer.writerow(header)\n\n    responses = model.query.all()\n\n    for curr in responses:\n        writer.writerow(\n            [getattr(curr, column.name) for column in model.__mapper__.columns]\n        )\n\n    temp.seek(0)\n\n    # In Python 3 send_file requires bytes\n    output = BytesIO()\n    output.write(temp.getvalue().encode(\"utf-8\"))\n    output.seek(0)\n    temp.close()\n\n    return output\n\n\ndef load_users_csv(dict_reader):\n    for line in dict_reader:\n        result = Users(**line)\n        db.session.add(result)\n        db.session.commit()\n    return True\n\n\ndef load_teams_csv(dict_reader):\n    for line in dict_reader:\n        result = Teams(**line)\n        db.session.add(result)\n        db.session.commit()\n    return True\n\n\ndef load_challenges_csv(dict_reader):\n    for line in dict_reader:\n        flags = line.pop(\"flags\", None)\n        tags = line.pop(\"tags\", None)\n        hints = line.pop(\"hints\", None)\n        challenge_type = line.pop(\"type\", \"standard\")\n\n        # Load in custome type_data\n        type_data = json.loads(line.pop(\"type_data\", \"{}\"))\n        line.update(type_data)\n\n        ChallengeClass = get_chal_class(challenge_type)\n        challenge = ChallengeClass.challenge_model(**line)\n        db.session.add(challenge)\n        db.session.commit()\n\n        if flags:\n            flags = [flag.strip() for flag in flags.split(\",\")]\n            for flag in flags:\n                f = Flags(type=\"static\", challenge_id=challenge.id, content=flag,)\n                db.session.add(f)\n                db.session.commit()\n\n        if tags:\n            tags = [tag.strip() for tag in tags.split(\",\")]\n            for tag in tags:\n                t = Tags(challenge_id=challenge.id, value=tag,)\n                db.session.add(t)\n                db.session.commit()\n\n        if hints:\n            hints = [hint.strip() for hint in hints.split(\",\")]\n            for hint in hints:\n                h = Hints(challenge_id=challenge.id, content=hint,)\n                db.session.add(h)\n                db.session.commit()\n    return True\n\n\nCSV_KEYS = {\n    \"scoreboard\": dump_scoreboard_csv,\n    \"users+fields\": dump_users_with_fields_csv,\n    \"teams+fields\": dump_teams_with_fields_csv,\n}\n", "path": "CTFd/utils/csv/__init__.py"}], "after_files": [{"content": "import csv\nimport json\nfrom io import BytesIO, StringIO\n\nfrom CTFd.models import (\n    Flags,\n    Hints,\n    Tags,\n    TeamFields,\n    Teams,\n    UserFields,\n    Users,\n    db,\n    get_class_by_tablename,\n)\nfrom CTFd.plugins.challenges import get_chal_class\nfrom CTFd.utils.config import is_teams_mode, is_users_mode\nfrom CTFd.utils.scores import get_standings\n\n\ndef get_dumpable_tables():\n    csv_keys = list(CSV_KEYS.keys())\n    db_keys = list(db.metadata.tables.keys())\n    tables = csv_keys + db_keys\n    table_keys = list(zip(tables, tables))\n    return table_keys\n\n\ndef dump_csv(name):\n    dump_func = CSV_KEYS.get(name)\n    if dump_func:\n        return dump_func()\n    elif get_class_by_tablename(name):\n        return dump_database_table(tablename=name)\n    else:\n        raise KeyError\n\n\ndef dump_scoreboard_csv():\n    # TODO: Add fields to scoreboard data\n    temp = StringIO()\n    writer = csv.writer(temp)\n\n    standings = get_standings()\n\n    # Get all user fields in a specific order\n    user_fields = UserFields.query.all()\n    user_field_ids = [f.id for f in user_fields]\n    user_field_names = [f.name for f in user_fields]\n\n    if is_teams_mode():\n        team_fields = TeamFields.query.all()\n        team_field_ids = [f.id for f in team_fields]\n        team_field_names = [f.name for f in team_fields]\n\n        header = (\n            [\n                \"place\",\n                \"team\",\n                \"team id\",\n                \"score\",\n                \"member name\",\n                \"member id\",\n                \"member email\",\n                \"member score\",\n            ]\n            + user_field_names\n            + team_field_names\n        )\n        writer.writerow(header)\n\n        for i, standing in enumerate(standings):\n            team = Teams.query.filter_by(id=standing.account_id).first()\n\n            # Build field entries using the order of the field values\n            team_field_entries = {f.field_id: f.value for f in team.field_entries}\n            team_field_values = [\n                team_field_entries.get(f_id, \"\") for f_id in team_field_ids\n            ]\n            team_row = [\n                i + 1,\n                team.name,\n                team.id,\n                standing.score,\n                \"\",\n                \"\",\n            ] + team_field_values\n\n            writer.writerow(team_row)\n\n            for member in team.members:\n                user_field_entries = {f.field_id: f.value for f in member.field_entries}\n                user_field_values = [\n                    user_field_entries.get(f_id, \"\") for f_id in user_field_ids\n                ]\n                user_row = [\n                    \"\",\n                    \"\",\n                    \"\",\n                    \"\",\n                    member.name,\n                    member.id,\n                    member.email,\n                    member.score,\n                ] + user_field_values\n                writer.writerow(user_row)\n    elif is_users_mode():\n        header = [\"place\", \"user\", \"score\"] + user_field_names\n        writer.writerow(header)\n\n        for i, standing in enumerate(standings):\n            user = Users.query.filter_by(id=standing.account_id).first()\n\n            # Build field entries using the order of the field values\n            user_field_entries = {f.field_id: f.value for f in user.field_entries}\n            user_field_values = [\n                user_field_entries.get(f_id, \"\") for f_id in user_field_ids\n            ]\n            user_row = [i + 1, user.name, standing.score] + user_field_values\n            writer.writerow(user_row)\n\n    # In Python 3 send_file requires bytes\n    output = BytesIO()\n    output.write(temp.getvalue().encode(\"utf-8\"))\n    output.seek(0)\n    temp.close()\n\n    return output\n\n\ndef dump_users_with_fields_csv():\n    temp = StringIO()\n    writer = csv.writer(temp)\n\n    user_fields = UserFields.query.all()\n    user_field_ids = [f.id for f in user_fields]\n    user_field_names = [f.name for f in user_fields]\n\n    header = [column.name for column in Users.__mapper__.columns] + user_field_names\n    writer.writerow(header)\n\n    responses = Users.query.all()\n\n    for curr in responses:\n        user_field_entries = {f.field_id: f.value for f in curr.field_entries}\n        user_field_values = [\n            user_field_entries.get(f_id, \"\") for f_id in user_field_ids\n        ]\n        user_row = [\n            getattr(curr, column.name) for column in Users.__mapper__.columns\n        ] + user_field_values\n        writer.writerow(user_row)\n\n    temp.seek(0)\n\n    # In Python 3 send_file requires bytes\n    output = BytesIO()\n    output.write(temp.getvalue().encode(\"utf-8\"))\n    output.seek(0)\n    temp.close()\n\n    return output\n\n\ndef dump_teams_with_fields_csv():\n    temp = StringIO()\n    writer = csv.writer(temp)\n\n    team_fields = TeamFields.query.all()\n    team_field_ids = [f.id for f in team_fields]\n    team_field_names = [f.name for f in team_fields]\n\n    header = [column.name for column in Teams.__mapper__.columns] + team_field_names\n    writer.writerow(header)\n\n    responses = Teams.query.all()\n\n    for curr in responses:\n        team_field_entries = {f.field_id: f.value for f in curr.field_entries}\n        team_field_values = [\n            team_field_entries.get(f_id, \"\") for f_id in team_field_ids\n        ]\n\n        team_row = [\n            getattr(curr, column.name) for column in Teams.__mapper__.columns\n        ] + team_field_values\n\n        writer.writerow(team_row)\n\n    temp.seek(0)\n\n    # In Python 3 send_file requires bytes\n    output = BytesIO()\n    output.write(temp.getvalue().encode(\"utf-8\"))\n    output.seek(0)\n    temp.close()\n\n    return output\n\n\ndef dump_database_table(tablename):\n    # TODO: It might make sense to limit dumpable tables. Config could potentially leak sensitive information.\n    model = get_class_by_tablename(tablename)\n\n    if model is None:\n        raise KeyError(\"Unknown database table\")\n\n    temp = StringIO()\n    writer = csv.writer(temp)\n\n    header = [column.name for column in model.__mapper__.columns]\n    writer.writerow(header)\n\n    responses = model.query.all()\n\n    for curr in responses:\n        writer.writerow(\n            [getattr(curr, column.name) for column in model.__mapper__.columns]\n        )\n\n    temp.seek(0)\n\n    # In Python 3 send_file requires bytes\n    output = BytesIO()\n    output.write(temp.getvalue().encode(\"utf-8\"))\n    output.seek(0)\n    temp.close()\n\n    return output\n\n\ndef load_users_csv(dict_reader):\n    for line in dict_reader:\n        result = Users(**line)\n        db.session.add(result)\n        db.session.commit()\n    return True\n\n\ndef load_teams_csv(dict_reader):\n    for line in dict_reader:\n        result = Teams(**line)\n        db.session.add(result)\n        db.session.commit()\n    return True\n\n\ndef load_challenges_csv(dict_reader):\n    for line in dict_reader:\n        flags = line.pop(\"flags\", None)\n        tags = line.pop(\"tags\", None)\n        hints = line.pop(\"hints\", None)\n        challenge_type = line.pop(\"type\", \"standard\")\n\n        # Load in custom type_data\n        type_data = json.loads(line.pop(\"type_data\", \"{}\") or \"{}\")\n        line.update(type_data)\n\n        ChallengeClass = get_chal_class(challenge_type)\n        challenge = ChallengeClass.challenge_model(**line)\n        db.session.add(challenge)\n        db.session.commit()\n\n        if flags:\n            flags = [flag.strip() for flag in flags.split(\",\")]\n            for flag in flags:\n                f = Flags(type=\"static\", challenge_id=challenge.id, content=flag,)\n                db.session.add(f)\n                db.session.commit()\n\n        if tags:\n            tags = [tag.strip() for tag in tags.split(\",\")]\n            for tag in tags:\n                t = Tags(challenge_id=challenge.id, value=tag,)\n                db.session.add(t)\n                db.session.commit()\n\n        if hints:\n            hints = [hint.strip() for hint in hints.split(\",\")]\n            for hint in hints:\n                h = Hints(challenge_id=challenge.id, content=hint,)\n                db.session.add(h)\n                db.session.commit()\n    return True\n\n\nCSV_KEYS = {\n    \"scoreboard\": dump_scoreboard_csv,\n    \"users+fields\": dump_users_with_fields_csv,\n    \"teams+fields\": dump_teams_with_fields_csv,\n}\n", "path": "CTFd/utils/csv/__init__.py"}]}