# -*- coding: utf-8 -*-
"""
Created on Apr 19, 2016

@author: noob
"""

from common.models import UploadToken
from django.http import HttpResponseBadRequest
from api.views.base import ApiJob


class CheckUploadToken(ApiJob):
    def get(self, request, job):
        try:
            upload_token = request.GET.get('upload_token', '')
            assert upload_token
            UploadToken.objects.get(job=job.n, token=upload_token)
            return True
        except AssertionError:
            return HttpResponseBadRequest()
        except UploadToken.DoesNotExist:
            return False

            #
            # def check_upload_token(request):
            #
            #     try:
            #         job = request.GET.get('jobno', '')
            #         upload_token = request.GET.get('upload_token', '')
            #         assert job and job.isdigit() and upload_token
            #         Job.objects.get(n=int(job))
            #         UploadToken.objects.get(job=int(job), token=upload_token)
            #         return HttpResponse(dumps(True), content_type='text/json')
            #     except AssertionError:
            #         return HttpResponseBadRequest()
            #     except Job.DoesNotExist:
            #         return HttpResponseNotFound()
            #     except UploadToken.DoesNotExist:
            #         return HttpResponse(dumps(False), content_type='text/json')
            #
