Cloned DMOJ
This commit is contained in:
parent
f623974b58
commit
49dc9ff10c
513 changed files with 132349 additions and 39 deletions
2
judge/views/api/__init__.py
Normal file
2
judge/views/api/__init__.py
Normal file
|
@ -0,0 +1,2 @@
|
|||
from .api_v1 import *
|
||||
from .api_v2 import *
|
173
judge/views/api/api_v1.py
Normal file
173
judge/views/api/api_v1.py
Normal file
|
@ -0,0 +1,173 @@
|
|||
from operator import attrgetter
|
||||
|
||||
from django.db.models import F, Prefetch
|
||||
from django.http import Http404, JsonResponse
|
||||
from django.shortcuts import get_object_or_404
|
||||
|
||||
from dmoj import settings
|
||||
from judge.models import Contest, ContestParticipation, ContestTag, Problem, Profile, Submission
|
||||
|
||||
|
||||
def sane_time_repr(delta):
|
||||
days = delta.days
|
||||
hours = delta.seconds / 3600
|
||||
minutes = (delta.seconds % 3600) / 60
|
||||
return '%02d:%02d:%02d' % (days, hours, minutes)
|
||||
|
||||
|
||||
def api_v1_contest_list(request):
|
||||
queryset = Contest.objects.filter(is_visible=True, is_private=False,
|
||||
is_organization_private=False).prefetch_related(
|
||||
Prefetch('tags', queryset=ContestTag.objects.only('name'), to_attr='tag_list')).defer('description')
|
||||
|
||||
return JsonResponse({c.key: {
|
||||
'name': c.name,
|
||||
'start_time': c.start_time.isoformat(),
|
||||
'end_time': c.end_time.isoformat(),
|
||||
'time_limit': c.time_limit and sane_time_repr(c.time_limit),
|
||||
'labels': list(map(attrgetter('name'), c.tag_list)),
|
||||
} for c in queryset})
|
||||
|
||||
|
||||
def api_v1_contest_detail(request, contest):
|
||||
contest = get_object_or_404(Contest, key=contest)
|
||||
|
||||
in_contest = contest.is_in_contest(request.user)
|
||||
can_see_rankings = contest.can_see_scoreboard(request.user)
|
||||
if contest.hide_scoreboard and in_contest:
|
||||
can_see_rankings = False
|
||||
|
||||
problems = list(contest.contest_problems.select_related('problem')
|
||||
.defer('problem__description').order_by('order'))
|
||||
participations = (contest.users.filter(virtual=0, user__is_unlisted=False)
|
||||
.prefetch_related('user__organizations')
|
||||
.annotate(username=F('user__user__username'))
|
||||
.order_by('-score', 'cumtime') if can_see_rankings else [])
|
||||
|
||||
can_see_problems = (in_contest or contest.ended or contest.is_editable_by(request.user))
|
||||
|
||||
return JsonResponse({
|
||||
'time_limit': contest.time_limit and contest.time_limit.total_seconds(),
|
||||
'start_time': contest.start_time.isoformat(),
|
||||
'end_time': contest.end_time.isoformat(),
|
||||
'tags': list(contest.tags.values_list('name', flat=True)),
|
||||
'is_rated': contest.is_rated,
|
||||
'rate_all': contest.is_rated and contest.rate_all,
|
||||
'has_rating': contest.ratings.exists(),
|
||||
'rating_floor': contest.rating_floor,
|
||||
'rating_ceiling': contest.rating_ceiling,
|
||||
'format': {
|
||||
'name': contest.format_name,
|
||||
'config': contest.format_config,
|
||||
},
|
||||
'problems': [
|
||||
{
|
||||
'points': int(problem.points),
|
||||
'partial': problem.partial,
|
||||
'name': problem.problem.name,
|
||||
'code': problem.problem.code,
|
||||
} for problem in problems] if can_see_problems else [],
|
||||
'rankings': [
|
||||
{
|
||||
'user': participation.username,
|
||||
'points': participation.score,
|
||||
'cumtime': participation.cumtime,
|
||||
'is_disqualified': participation.is_disqualified,
|
||||
'solutions': contest.format.get_problem_breakdown(participation, problems),
|
||||
} for participation in participations],
|
||||
})
|
||||
|
||||
|
||||
def api_v1_problem_list(request):
|
||||
queryset = Problem.objects.filter(is_public=True, is_organization_private=False)
|
||||
if settings.ENABLE_FTS and 'search' in request.GET:
|
||||
query = ' '.join(request.GET.getlist('search')).strip()
|
||||
if query:
|
||||
queryset = queryset.search(query)
|
||||
queryset = queryset.values_list('code', 'points', 'partial', 'name', 'group__full_name')
|
||||
|
||||
return JsonResponse({code: {
|
||||
'points': points,
|
||||
'partial': partial,
|
||||
'name': name,
|
||||
'group': group,
|
||||
} for code, points, partial, name, group in queryset})
|
||||
|
||||
|
||||
def api_v1_problem_info(request, problem):
|
||||
p = get_object_or_404(Problem, code=problem)
|
||||
if not p.is_accessible_by(request.user):
|
||||
raise Http404()
|
||||
|
||||
return JsonResponse({
|
||||
'name': p.name,
|
||||
'authors': list(p.authors.values_list('user__username', flat=True)),
|
||||
'types': list(p.types.values_list('full_name', flat=True)),
|
||||
'group': p.group.full_name,
|
||||
'time_limit': p.time_limit,
|
||||
'memory_limit': p.memory_limit,
|
||||
'points': p.points,
|
||||
'partial': p.partial,
|
||||
'languages': list(p.allowed_languages.values_list('key', flat=True)),
|
||||
})
|
||||
|
||||
|
||||
def api_v1_user_list(request):
|
||||
queryset = Profile.objects.filter(is_unlisted=False).values_list('user__username', 'points', 'performance_points',
|
||||
'display_rank')
|
||||
return JsonResponse({username: {
|
||||
'points': points,
|
||||
'performance_points': performance_points,
|
||||
'rank': rank,
|
||||
} for username, points, performance_points, rank in queryset})
|
||||
|
||||
|
||||
def api_v1_user_info(request, user):
|
||||
profile = get_object_or_404(Profile, user__username=user)
|
||||
submissions = list(Submission.objects.filter(case_points=F('case_total'), user=profile, problem__is_public=True,
|
||||
problem__is_organization_private=False)
|
||||
.values('problem').distinct().values_list('problem__code', flat=True))
|
||||
resp = {
|
||||
'points': profile.points,
|
||||
'performance_points': profile.performance_points,
|
||||
'rank': profile.display_rank,
|
||||
'solved_problems': submissions,
|
||||
'organizations': list(profile.organizations.values_list('id', flat=True)),
|
||||
}
|
||||
|
||||
last_rating = profile.ratings.last()
|
||||
|
||||
contest_history = {}
|
||||
if not profile.is_unlisted:
|
||||
participations = ContestParticipation.objects.filter(user=profile, virtual=0, contest__is_visible=True,
|
||||
contest__is_private=False,
|
||||
contest__is_organization_private=False)
|
||||
for contest_key, rating, volatility in participations.values_list('contest__key', 'rating__rating',
|
||||
'rating__volatility'):
|
||||
contest_history[contest_key] = {
|
||||
'rating': rating,
|
||||
'volatility': volatility,
|
||||
}
|
||||
|
||||
resp['contests'] = {
|
||||
'current_rating': last_rating.rating if last_rating else None,
|
||||
'volatility': last_rating.volatility if last_rating else None,
|
||||
'history': contest_history,
|
||||
}
|
||||
|
||||
return JsonResponse(resp)
|
||||
|
||||
|
||||
def api_v1_user_submissions(request, user):
|
||||
profile = get_object_or_404(Profile, user__username=user)
|
||||
subs = Submission.objects.filter(user=profile, problem__is_public=True, problem__is_organization_private=False)
|
||||
|
||||
return JsonResponse({sub['id']: {
|
||||
'problem': sub['problem__code'],
|
||||
'time': sub['time'],
|
||||
'memory': sub['memory'],
|
||||
'points': sub['points'],
|
||||
'language': sub['language__key'],
|
||||
'status': sub['status'],
|
||||
'result': sub['result'],
|
||||
} for sub in subs.values('id', 'problem__code', 'time', 'memory', 'points', 'language__key', 'status', 'result')})
|
122
judge/views/api/api_v2.py
Normal file
122
judge/views/api/api_v2.py
Normal file
|
@ -0,0 +1,122 @@
|
|||
from operator import attrgetter
|
||||
|
||||
from django.db.models import Max
|
||||
from django.http import JsonResponse
|
||||
|
||||
from judge.models import ContestParticipation, Problem, Profile, Submission
|
||||
from judge.utils.ranker import ranker
|
||||
from judge.views.contests import contest_ranking_list
|
||||
|
||||
|
||||
def error(message):
|
||||
return JsonResponse({'error': message}, status=422)
|
||||
|
||||
|
||||
def api_v2_user_info(request):
|
||||
"""
|
||||
{
|
||||
"points": 100.0,
|
||||
"rating": 2452,
|
||||
"rank": "user",
|
||||
"organizations": [],
|
||||
"solved_problems": ["ccc14s4", ...],
|
||||
"attempted_problems": [
|
||||
{
|
||||
"code": "Hello, World!",
|
||||
"points": 1.0,
|
||||
"max_points": 2.0
|
||||
}
|
||||
],
|
||||
"authored_problems": ["dmpg16s4"],
|
||||
"contest_history": [
|
||||
{
|
||||
"contest": {
|
||||
"code": "halloween14",
|
||||
"name": "Kemonomimi Party",
|
||||
"tags": ["seasonal"],
|
||||
"time_limit": null,
|
||||
"start_time": "2014-10-31T04:00:00+00:00",
|
||||
"end_time": "2014-11-10T05:00:00+00:00"
|
||||
},
|
||||
"rank": 1,
|
||||
"rating:": 1800
|
||||
},
|
||||
// ...
|
||||
]
|
||||
}
|
||||
"""
|
||||
try:
|
||||
username = request.GET['username']
|
||||
except KeyError:
|
||||
return error("no username passed")
|
||||
if not username:
|
||||
return error("username argument not provided")
|
||||
try:
|
||||
profile = Profile.objects.get(user__username=username)
|
||||
except Profile.DoesNotExist:
|
||||
return error("no such user")
|
||||
|
||||
last_rating = list(profile.ratings.order_by('-contest__end_time'))
|
||||
|
||||
resp = {
|
||||
"rank": profile.display_rank,
|
||||
"organizations": list(profile.organizations.values_list('key', flat=True)),
|
||||
}
|
||||
|
||||
contest_history = []
|
||||
for participation in (ContestParticipation.objects.filter(user=profile, virtual=0, contest__is_visible=True)
|
||||
.order_by('-contest__end_time')):
|
||||
contest = participation.contest
|
||||
|
||||
problems = list(contest.contest_problems.select_related('problem').defer('problem__description')
|
||||
.order_by('order'))
|
||||
rank, result = next(filter(lambda data: data[1].user == profile.user,
|
||||
ranker(contest_ranking_list(contest, problems),
|
||||
key=attrgetter('points', 'cumtime'))))
|
||||
|
||||
contest_history.append({
|
||||
'contest': {
|
||||
'code': contest.key,
|
||||
'name': contest.name,
|
||||
'tags': list(contest.tags.values_list('name', flat=True)),
|
||||
'time_limit': contest.time_limit and contest.time_limit.total_seconds(),
|
||||
'start_time': contest.start_time.isoformat(),
|
||||
'end_time': contest.end_time.isoformat(),
|
||||
},
|
||||
'rank': rank,
|
||||
'rating': result.participation_rating,
|
||||
})
|
||||
|
||||
resp['contests'] = {
|
||||
"current_rating": last_rating[0].rating if last_rating else None,
|
||||
"volatility": last_rating[0].volatility if last_rating else None,
|
||||
'history': contest_history,
|
||||
}
|
||||
|
||||
solved_problems = []
|
||||
attempted_problems = []
|
||||
|
||||
problem_data = (Submission.objects.filter(points__gt=0, user=profile, problem__is_public=True,
|
||||
problem__is_organization_private=False)
|
||||
.annotate(max_pts=Max('points'))
|
||||
.values_list('max_pts', 'problem__points', 'problem__code')
|
||||
.distinct())
|
||||
for awarded_pts, max_pts, problem in problem_data:
|
||||
if awarded_pts == max_pts:
|
||||
solved_problems.append(problem)
|
||||
else:
|
||||
attempted_problems.append({
|
||||
'awarded': awarded_pts,
|
||||
'max': max_pts,
|
||||
'problem': problem,
|
||||
})
|
||||
|
||||
resp['problems'] = {
|
||||
'points': profile.points,
|
||||
'solved': solved_problems,
|
||||
'attempted': attempted_problems,
|
||||
'authored': list(Problem.objects.filter(is_public=True, is_organization_private=False, authors=profile)
|
||||
.values_list('code', flat=True)),
|
||||
}
|
||||
|
||||
return JsonResponse(resp)
|
Loading…
Add table
Add a link
Reference in a new issue