Skip to content

Commit

Permalink
Add key status in import panel
Browse files Browse the repository at this point in the history
  • Loading branch information
magnified103 committed Oct 14, 2024
1 parent 7955282 commit 1a4fd73
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
24 changes: 17 additions & 7 deletions judge/views/problem_transfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,22 +140,23 @@ def clean_new_code(self):
return new_code

def clean(self):
key_info = requests.get(get_problem_export_url(), timeout=settings.VNOJ_PROBLEM_IMPORT_TIMEOUT).json()
if not key_info:
self.add_error(None, _('Request timed out'))
elif key_info['remaining_uses'] <= 0:
response = requests.get(get_problem_export_url(), timeout=settings.VNOJ_PROBLEM_IMPORT_TIMEOUT)
if not response.ok:
self.add_error(None, _('Bad request'))
elif response.json().get('remaining_uses') <= 0:
self.add_error('secret', _('No remaining uses'))


@shared_task(bind=True)
def import_problem(self, user_id, problem, new_code):
old_code = problem
problem_info = requests.post(get_problem_export_url(),
response = requests.post(get_problem_export_url(),
data={'code': old_code},
timeout=settings.VNOJ_PROBLEM_IMPORT_TIMEOUT).json()
timeout=settings.VNOJ_PROBLEM_IMPORT_TIMEOUT)

if not problem_info:
if not response.ok:
raise Http404()
problem_info = response.json()
problem = Problem()
problem.code = new_code
# Use the exported code
Expand Down Expand Up @@ -200,6 +201,15 @@ def form_valid(self, form):
redirect=reverse('problem_edit', args=(form.cleaned_data['new_code'],)),
)

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['host_url'] = settings.VNOJ_PROBLEM_IMPORT_HOST
response = requests.get(get_problem_export_url(), timeout=settings.VNOJ_PROBLEM_IMPORT_TIMEOUT)
context['status'] = response.ok
if response.ok:
context['remaining_uses'] = response.json().get('remaining_uses')
return context

def dispatch(self, request, *args, **kwargs):
if not settings.VNOJ_PROBLEM_ENABLE_IMPORT:
raise Http404()
Expand Down
13 changes: 13 additions & 0 deletions templates/problem/import.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@
{% endblock %}

{% block body %}
<div class="alert alert-info">
<div>
Host: <a href="{{ host_url }}">{{ host_url }}</a>
{% if status %}
<i style="color: #44AD41" class="fa fa-check-circle"></i>
{% else %}
<i style="color: #DE2121" class="fa fa-minus-circle"></i>
{% endif %}
</div>
{% if status %}
<div>Remaining uses: {{ remaining_uses }}</div>
{% endif %}
</div>
<div>
<form method="post" class="form-area" style="display: flex; justify-content: center; flex-direction: column;">
{% if form.non_field_errors() %}
Expand Down

0 comments on commit 1a4fd73

Please sign in to comment.