2020-01-21 06:35:58 +00:00
|
|
|
import requests
|
|
|
|
from django.conf import settings
|
|
|
|
from django.contrib.auth.decorators import login_required
|
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
2022-05-14 17:57:27 +00:00
|
|
|
from django.http import (
|
|
|
|
Http404,
|
|
|
|
HttpResponse,
|
|
|
|
HttpResponseBadRequest,
|
|
|
|
HttpResponseForbidden,
|
|
|
|
HttpResponseRedirect,
|
|
|
|
)
|
2020-01-21 06:35:58 +00:00
|
|
|
from django.utils.translation import gettext as _
|
|
|
|
from django.views.generic import View
|
|
|
|
|
|
|
|
from judge.models import Submission
|
|
|
|
|
2022-05-14 17:57:27 +00:00
|
|
|
__all__ = ["rejudge_submission", "DetectTimezone"]
|
2020-01-21 06:35:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
def rejudge_submission(request):
|
2022-05-14 17:57:27 +00:00
|
|
|
if (
|
|
|
|
request.method != "POST"
|
|
|
|
or not request.user.has_perm("judge.rejudge_submission")
|
|
|
|
or not request.user.has_perm("judge.edit_own_problem")
|
|
|
|
):
|
2020-01-21 06:35:58 +00:00
|
|
|
return HttpResponseForbidden()
|
|
|
|
|
2022-05-14 17:57:27 +00:00
|
|
|
if "id" not in request.POST or not request.POST["id"].isdigit():
|
2020-01-21 06:35:58 +00:00
|
|
|
return HttpResponseBadRequest()
|
|
|
|
|
|
|
|
try:
|
2022-05-14 17:57:27 +00:00
|
|
|
submission = Submission.objects.get(id=request.POST["id"])
|
2020-01-21 06:35:58 +00:00
|
|
|
except Submission.DoesNotExist:
|
|
|
|
return HttpResponseBadRequest()
|
|
|
|
|
2022-05-14 17:57:27 +00:00
|
|
|
if not request.user.has_perm(
|
|
|
|
"judge.edit_all_problem"
|
|
|
|
) and not submission.problem.is_editor(request.profile):
|
2020-01-21 06:35:58 +00:00
|
|
|
return HttpResponseForbidden()
|
|
|
|
|
|
|
|
submission.judge(rejudge=True)
|
|
|
|
|
2022-05-14 17:57:27 +00:00
|
|
|
redirect = request.POST.get("path", None)
|
2020-01-21 06:35:58 +00:00
|
|
|
|
2022-05-14 17:57:27 +00:00
|
|
|
return (
|
|
|
|
HttpResponseRedirect(redirect)
|
|
|
|
if redirect
|
|
|
|
else HttpResponse("success", content_type="text/plain")
|
|
|
|
)
|
2020-01-21 06:35:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
class DetectTimezone(View):
|
|
|
|
def askgeo(self, lat, long):
|
2022-05-14 17:57:27 +00:00
|
|
|
if not hasattr(settings, "ASKGEO_ACCOUNT_ID") or not hasattr(
|
|
|
|
settings, "ASKGEO_ACCOUNT_API_KEY"
|
|
|
|
):
|
2020-01-21 06:35:58 +00:00
|
|
|
raise ImproperlyConfigured()
|
2022-05-14 17:57:27 +00:00
|
|
|
data = requests.get(
|
|
|
|
"http://api.askgeo.com/v1/%s/%s/query.json?databases=TimeZone&points=%f,%f"
|
|
|
|
% (settings.ASKGEO_ACCOUNT_ID, settings.ASKGEO_ACCOUNT_API_KEY, lat, long)
|
|
|
|
).json()
|
2020-01-21 06:35:58 +00:00
|
|
|
try:
|
2022-05-14 17:57:27 +00:00
|
|
|
return HttpResponse(
|
|
|
|
data["data"][0]["TimeZone"]["TimeZoneId"], content_type="text/plain"
|
|
|
|
)
|
2020-01-21 06:35:58 +00:00
|
|
|
except (IndexError, KeyError):
|
2022-05-14 17:57:27 +00:00
|
|
|
return HttpResponse(
|
|
|
|
_("Invalid upstream data: %s") % data,
|
|
|
|
content_type="text/plain",
|
|
|
|
status=500,
|
|
|
|
)
|
2020-01-21 06:35:58 +00:00
|
|
|
|
|
|
|
def geonames(self, lat, long):
|
2022-05-14 17:57:27 +00:00
|
|
|
if not hasattr(settings, "GEONAMES_USERNAME"):
|
2020-01-21 06:35:58 +00:00
|
|
|
raise ImproperlyConfigured()
|
2022-05-14 17:57:27 +00:00
|
|
|
data = requests.get(
|
|
|
|
"http://api.geonames.org/timezoneJSON?lat=%f&lng=%f&username=%s"
|
|
|
|
% (lat, long, settings.GEONAMES_USERNAME)
|
|
|
|
).json()
|
2020-01-21 06:35:58 +00:00
|
|
|
try:
|
2022-05-14 17:57:27 +00:00
|
|
|
return HttpResponse(data["timezoneId"], content_type="text/plain")
|
2020-01-21 06:35:58 +00:00
|
|
|
except KeyError:
|
2022-05-14 17:57:27 +00:00
|
|
|
return HttpResponse(
|
|
|
|
_("Invalid upstream data: %s") % data,
|
|
|
|
content_type="text/plain",
|
|
|
|
status=500,
|
|
|
|
)
|
2020-01-21 06:35:58 +00:00
|
|
|
|
|
|
|
def default(self, lat, long):
|
|
|
|
raise Http404()
|
|
|
|
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
backend = settings.TIMEZONE_DETECT_BACKEND
|
|
|
|
try:
|
2022-05-14 17:57:27 +00:00
|
|
|
lat, long = float(request.GET["lat"]), float(request.GET["long"])
|
2020-01-21 06:35:58 +00:00
|
|
|
except (ValueError, KeyError):
|
2022-05-14 17:57:27 +00:00
|
|
|
return HttpResponse(
|
|
|
|
_("Bad latitude or longitude"), content_type="text/plain", status=404
|
|
|
|
)
|
|
|
|
return {"askgeo": self.askgeo, "geonames": self.geonames,}.get(
|
|
|
|
backend, self.default
|
|
|
|
)(lat, long)
|