@@ -28,7 +28,7 @@ def load_installed_challenges():
28
28
return s .get ("/api/v1/challenges?view=admin" , json = True ).json ()["data" ]
29
29
30
30
31
- def sync_challenge (challenge ):
31
+ def sync_challenge (challenge , ignore = [] ):
32
32
data = {
33
33
"name" : challenge ["name" ],
34
34
"category" : challenge ["category" ],
@@ -43,7 +43,7 @@ def sync_challenge(challenge):
43
43
if challenge ["value" ] is None :
44
44
del challenge ["value" ]
45
45
46
- if challenge .get ("attempts" ):
46
+ if challenge .get ("attempts" ) and "attempts" not in ignore :
47
47
data ["max_attempts" ] = challenge .get ("attempts" )
48
48
49
49
data ["state" ] = "hidden"
@@ -65,16 +65,15 @@ def sync_challenge(challenge):
65
65
r = s .patch (f"/api/v1/challenges/{ challenge_id } " , json = data )
66
66
r .raise_for_status ()
67
67
68
- # Delete existing flags
69
- current_flags = s .get (f"/api/v1/flags" , json = data ).json ()["data" ]
70
- for flag in current_flags :
71
- if flag ["challenge_id" ] == challenge_id :
72
- flag_id = flag ["id" ]
73
- r = s .delete (f"/api/v1/flags/{ flag_id } " , json = True )
74
- r .raise_for_status ()
75
-
76
68
# Create new flags
77
- if challenge .get ("flags" ):
69
+ if challenge .get ("flags" ) and "flags" not in ignore :
70
+ # Delete existing flags
71
+ current_flags = s .get (f"/api/v1/flags" , json = data ).json ()["data" ]
72
+ for flag in current_flags :
73
+ if flag ["challenge_id" ] == challenge_id :
74
+ flag_id = flag ["id" ]
75
+ r = s .delete (f"/api/v1/flags/{ flag_id } " , json = True )
76
+ r .raise_for_status ()
78
77
for flag in challenge ["flags" ]:
79
78
if type (flag ) == str :
80
79
data = {"content" : flag , "type" : "static" , "challenge" : challenge_id }
@@ -85,31 +84,31 @@ def sync_challenge(challenge):
85
84
r = s .post (f"/api/v1/flags" , json = flag )
86
85
r .raise_for_status ()
87
86
88
- # Delete existing tags
89
- current_tags = s .get (f"/api/v1/tags" , json = data ).json ()["data" ]
90
- for tag in current_tags :
91
- if tag ["challenge_id" ] == challenge_id :
92
- tag_id = tag ["id" ]
93
- r = s .delete (f"/api/v1/tags/{ tag_id } " , json = True )
94
- r .raise_for_status ()
95
-
96
87
# Update tags
97
- if challenge .get ("tags" ):
88
+ if challenge .get ("tags" ) and "tags" not in ignore :
89
+ # Delete existing tags
90
+ current_tags = s .get (f"/api/v1/tags" , json = data ).json ()["data" ]
91
+ for tag in current_tags :
92
+ if tag ["challenge_id" ] == challenge_id :
93
+ tag_id = tag ["id" ]
94
+ r = s .delete (f"/api/v1/tags/{ tag_id } " , json = True )
95
+ r .raise_for_status ()
98
96
for tag in challenge ["tags" ]:
99
97
r = s .post (f"/api/v1/tags" , json = {"challenge" : challenge_id , "value" : tag })
100
98
r .raise_for_status ()
101
99
102
- # Delete existing files
103
- all_current_files = s .get (f"/api/v1/files?type=challenge" , json = data ).json ()["data" ]
104
- for f in all_current_files :
105
- for used_file in original_challenge ["files" ]:
106
- if f ["location" ] in used_file :
107
- file_id = f ["id" ]
108
- r = s .delete (f"/api/v1/files/{ file_id } " , json = True )
109
- r .raise_for_status ()
110
-
111
100
# Upload files
112
- if challenge .get ("files" ):
101
+ if challenge .get ("files" ) and "files" not in ignore :
102
+ # Delete existing files
103
+ all_current_files = s .get (f"/api/v1/files?type=challenge" , json = data ).json ()[
104
+ "data"
105
+ ]
106
+ for f in all_current_files :
107
+ for used_file in original_challenge ["files" ]:
108
+ if f ["location" ] in used_file :
109
+ file_id = f ["id" ]
110
+ r = s .delete (f"/api/v1/files/{ file_id } " , json = True )
111
+ r .raise_for_status ()
113
112
files = []
114
113
for f in challenge ["files" ]:
115
114
file_path = Path (challenge .directory , f )
@@ -125,16 +124,16 @@ def sync_challenge(challenge):
125
124
r = s .post (f"/api/v1/files" , files = files , data = data )
126
125
r .raise_for_status ()
127
126
128
- # Delete existing hints
129
- current_hints = s .get (f"/api/v1/hints" , json = data ).json ()["data" ]
130
- for hint in current_hints :
131
- if hint ["challenge_id" ] == challenge_id :
132
- hint_id = hint ["id" ]
133
- r = s .delete (f"/api/v1/hints/{ hint_id } " , json = True )
134
- r .raise_for_status ()
135
-
136
127
# Create hints
137
- if challenge .get ("hints" ):
128
+ if challenge .get ("hints" ) and "hints" not in ignore :
129
+ # Delete existing hints
130
+ current_hints = s .get (f"/api/v1/hints" , json = data ).json ()["data" ]
131
+ for hint in current_hints :
132
+ if hint ["challenge_id" ] == challenge_id :
133
+ hint_id = hint ["id" ]
134
+ r = s .delete (f"/api/v1/hints/{ hint_id } " , json = True )
135
+ r .raise_for_status ()
136
+
138
137
for hint in challenge ["hints" ]:
139
138
if type (hint ) == str :
140
139
data = {"content" : hint , "cost" : 0 , "challenge" : challenge_id }
@@ -149,7 +148,7 @@ def sync_challenge(challenge):
149
148
r .raise_for_status ()
150
149
151
150
# Update requirements
152
- if challenge .get ("requirements" ):
151
+ if challenge .get ("requirements" ) and "requirements" not in ignore :
153
152
installed_challenges = load_installed_challenges ()
154
153
required_challenges = []
155
154
for r in challenge ["requirements" ]:
@@ -166,16 +165,17 @@ def sync_challenge(challenge):
166
165
r .raise_for_status ()
167
166
168
167
# Unhide challenge depending upon the value of "state" in spec
169
- data = {"state" : "visible" }
170
- if challenge .get ("state" ):
171
- if challenge ["state" ] in ["hidden" , "visible" ]:
172
- data ["state" ] = challenge ["state" ]
168
+ if "state" not in ignore :
169
+ data = {"state" : "visible" }
170
+ if challenge .get ("state" ):
171
+ if challenge ["state" ] in ["hidden" , "visible" ]:
172
+ data ["state" ] = challenge ["state" ]
173
173
174
- r = s .patch (f"/api/v1/challenges/{ challenge_id } " , json = data )
175
- r .raise_for_status ()
174
+ r = s .patch (f"/api/v1/challenges/{ challenge_id } " , json = data )
175
+ r .raise_for_status ()
176
176
177
177
178
- def create_challenge (challenge ):
178
+ def create_challenge (challenge , ignore = [] ):
179
179
data = {
180
180
"name" : challenge ["name" ],
181
181
"category" : challenge ["category" ],
@@ -190,7 +190,7 @@ def create_challenge(challenge):
190
190
if challenge ["value" ] is None :
191
191
del challenge ["value" ]
192
192
193
- if challenge .get ("attempts" ):
193
+ if challenge .get ("attempts" ) and "attempts" not in ignore :
194
194
data ["max_attempts" ] = challenge .get ("attempts" )
195
195
196
196
s = generate_session ()
@@ -202,7 +202,7 @@ def create_challenge(challenge):
202
202
challenge_id = challenge_data ["data" ]["id" ]
203
203
204
204
# Create flags
205
- if challenge .get ("flags" ):
205
+ if challenge .get ("flags" ) and "flags" not in ignore :
206
206
for flag in challenge ["flags" ]:
207
207
if type (flag ) == str :
208
208
data = {"content" : flag , "type" : "static" , "challenge" : challenge_id }
@@ -214,13 +214,13 @@ def create_challenge(challenge):
214
214
r .raise_for_status ()
215
215
216
216
# Create tags
217
- if challenge .get ("tags" ):
217
+ if challenge .get ("tags" ) and "tags" not in ignore :
218
218
for tag in challenge ["tags" ]:
219
219
r = s .post (f"/api/v1/tags" , json = {"challenge" : challenge_id , "value" : tag })
220
220
r .raise_for_status ()
221
221
222
222
# Upload files
223
- if challenge .get ("files" ):
223
+ if challenge .get ("files" ) and "files" not in ignore :
224
224
files = []
225
225
for f in challenge ["files" ]:
226
226
file_path = Path (challenge .directory , f )
@@ -237,7 +237,7 @@ def create_challenge(challenge):
237
237
r .raise_for_status ()
238
238
239
239
# Add hints
240
- if challenge .get ("hints" ):
240
+ if challenge .get ("hints" ) and "hints" not in ignore :
241
241
for hint in challenge ["hints" ]:
242
242
if type (hint ) == str :
243
243
data = {"content" : hint , "cost" : 0 , "challenge" : challenge_id }
@@ -252,7 +252,7 @@ def create_challenge(challenge):
252
252
r .raise_for_status ()
253
253
254
254
# Add requirements
255
- if challenge .get ("requirements" ):
255
+ if challenge .get ("requirements" ) and "requirements" not in ignore :
256
256
installed_challenges = load_installed_challenges ()
257
257
required_challenges = []
258
258
for r in challenge ["requirements" ]:
@@ -269,7 +269,7 @@ def create_challenge(challenge):
269
269
r .raise_for_status ()
270
270
271
271
# Set challenge state
272
- if challenge .get ("state" ):
272
+ if challenge .get ("state" ) and "state" not in ignore :
273
273
data = {"state" : "hidden" }
274
274
if challenge ["state" ] in ["hidden" , "visible" ]:
275
275
data ["state" ] = challenge ["state" ]
0 commit comments