Skip to content

Commit

Permalink
Add use_web_docker_manager in Challenge model field
Browse files Browse the repository at this point in the history
  • Loading branch information
taoky committed Oct 24, 2024
1 parent 285ac2c commit 35a4945
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 4 deletions.
19 changes: 19 additions & 0 deletions frontend/management/commands/fake_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,22 @@ def a():
'flag': 'flag{hackergame}',
}],
)

Challenge.create(
Context(root),
name='难题 (WDM)',
category='hard',
detail='难题描述',
url_orig='https://example.com/{token}',
check_url_clicked=False,
use_web_docker_manager=True,
prompt='flag{hackergame}',
index=100,
enabled=True,
flags=[{
'name': '',
'score': 100,
'type': 'text',
'flag': 'flag{hackergame}',
}],
)
1 change: 1 addition & 0 deletions frontend/management/commands/import_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ def parse_challenge(self, path):
'category': None,
'url_orig': None,
'check_url_clicked': False,
'use_web_docker_manager': False,
'prompt': 'flag{...}',
'index': 0,
'flags': [],
Expand Down
9 changes: 9 additions & 0 deletions frontend/templates/admin_challenge.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ <h2 v-else>正在创建</h2>
<strong>请确保对应的题目必须点击链接才可完成。</strong>
</div>
</div>
<div class="form-row">
<input type="checkbox" id="form-use-web-docker-manager" name="use-web-docker-manager" v-model="opened.use_web_docker_manager">
<label class="vCheckboxLabel" for="form-use-web-docker-manager">使用 Web 容器管理方案</label>
<div class="help">
在题目中显示启动/关闭环境的提示。
<strong>需要对应题目兼容 Web Docker Manager。</strong>
</div>
</div>
<div class="form-row">
<label for="form-prompt">提示:</label>
<input class="vTextField" type="text" id="form-prompt" name="prompt" v-model="opened.prompt">
Expand Down Expand Up @@ -163,6 +171,7 @@ <h2>Flag <a href="#" @click.prevent="opened.flags.push({name: '', score: 100, ty
prompt: 'flag{...}',
index: 0,
check_url_clicked: false,
use_web_docker_manager: false,
flags: [{
name: '',
score: 100,
Expand Down
28 changes: 27 additions & 1 deletion frontend/tests/command_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,31 @@
```
"""

FILE4 = """---
enabled: true
name: 示例题目 4
category: web
url: http://example.com/?token={token}
prompt: flag{...}
index: 2
check_url_clicked: true
use_web_docker_manager: true
flags:
- name: ''
score: 0
type: text
flag: flag{example4}
---
一段描述 4。
```python
print("Hello, world!")
```
"""


FILES = (FILE1, FILE2, FILE3)
FILES = (FILE1, FILE2, FILE3, FILE4)


class ImportDataTest(TestCase):
Expand Down Expand Up @@ -101,10 +124,13 @@ def test_import(self):
self.assert_("示例题目 3" in keys)
c2 = challenges["示例题目 2"]
c3 = challenges["示例题目 3"]
c4 = challenges["示例题目 4"]
self.assert_(c2.url_orig.endswith("/example.txt"))
self.assertEqual(c3.url_orig, "http://example.com/?token={token}")
self.assertEqual(c2.check_url_clicked, False)
self.assertEqual(c3.check_url_clicked, True)
self.assertEqual(c3.use_web_docker_manager, False)
self.assertEqual(c4.use_web_docker_manager, True)
self.assert_("codehilite" in c3.detail)
flag1 = c2.flags[0]
self.assertEqual(flag1["type"], "expr")
Expand Down
11 changes: 8 additions & 3 deletions server/challenge/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
class Challenge:
json_fields = ('pk', 'score', 'enabled', 'name', 'category',
'detail', 'url', 'url_orig', 'prompt', 'index', 'flags',
'check_url_clicked')
'check_url_clicked', 'use_web_docker_manager')
update_fields = ('enabled', 'name', 'category', 'detail', 'url_orig',
'prompt', 'index', 'flags', 'check_url_clicked')
'prompt', 'index', 'flags', 'check_url_clicked',
'use_web_docker_manager')
subscribers = []

def __init__(self, context, obj: models.Challenge):
Expand Down Expand Up @@ -145,7 +146,7 @@ def _update(self, **kwargs):
if k in {'name', 'category', 'url_orig', 'prompt'}:
v = v or None
setattr(self._obj, k, v)
elif k in {'enabled', 'detail', 'index', 'check_url_clicked'}:
elif k in {'enabled', 'detail', 'index', 'check_url_clicked', 'use_web_docker_manager'}:
setattr(self._obj, k, v)
elif k == 'flags':
flags = [{
Expand Down Expand Up @@ -239,6 +240,10 @@ def check_url_clicked(self):
return self._obj.check_url_clicked
except PermissionRequired:
return None

@property
def use_web_docker_manager(self):
return self._obj.use_web_docker_manager

@property
def prompt(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.2.16 on 2024-10-24 12:22

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("challenge", "0003_rename_url_challenge_url_orig_and_more"),
]

operations = [
migrations.AddField(
model_name="challenge",
name="use_web_docker_manager",
field=models.BooleanField(default=False),
),
]
1 change: 1 addition & 0 deletions server/challenge/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class Challenge(models.Model):
index = models.IntegerField(db_index=True)
flags = models.TextField()
check_url_clicked = models.BooleanField(default=False)
use_web_docker_manager = models.BooleanField(default=False)

class Meta:
default_permissions = ()
Expand Down

0 comments on commit 35a4945

Please sign in to comment.