Exemple #1
0
def claim(request, domain):
    """
    Allows a user to claim a case that they don't own.
    """
    as_user = request.POST.get('commcare_login_as', None)
    as_user_obj = CouchUser.get_by_username(as_user) if as_user else None
    restore_user = get_restore_user(domain, request.couch_user, as_user_obj)

    case_id = request.POST.get('case_id', None)
    if case_id is None:
        return HttpResponse('A case_id is required', status=400)

    try:
        if get_first_claim(domain, restore_user.user_id, case_id):
            return HttpResponse('You have already claimed that {}'.format(
                request.POST.get('case_type', 'case')),
                                status=409)

        claim_case(domain,
                   restore_user.user_id,
                   case_id,
                   host_type=request.POST.get('case_type'),
                   host_name=request.POST.get('case_name'),
                   device_id=__name__ + ".claim")
    except CaseNotFound:
        return HttpResponse(
            'The case "{}" you are trying to claim was not found'.format(
                case_id),
            status=410)
    return HttpResponse(status=200)
Exemple #2
0
def claim(request, domain):
    """
    Allows a user to claim a case that they don't own.
    """
    as_user = request.POST.get('commcare_login_as', None)
    restore_user = get_restore_user(domain, request.couch_user, as_user)
    cache = get_redis_default_cache()

    case_id = request.POST.get('case_id', None)
    if case_id is None:
        return HttpResponse('A case_id is required', status=400)

    try:
        if (cache.get(_claim_key(restore_user.user_id)) == case_id
                or get_first_claim(domain, restore_user.user_id, case_id)):
            return HttpResponse('You have already claimed that {}'.format(
                request.POST.get('case_type', 'case')),
                                status=409)

        claim_case(domain,
                   restore_user.user_id,
                   case_id,
                   host_type=request.POST.get('case_type'),
                   host_name=request.POST.get('case_name'))
    except CaseNotFound:
        return HttpResponse(
            'The case "{}" you are trying to claim was not found'.format(
                case_id),
            status=410)
    cache.set(_claim_key(restore_user.user_id), case_id)
    return HttpResponse(status=200)
Exemple #3
0
def claim(request, domain):
    """
    Allows a user to claim a case that they don't own.
    """
    as_user = request.POST.get('commcare_login_as', None)
    as_user_obj = CouchUser.get_by_username(as_user) if as_user else None
    restore_user = get_restore_user(domain, request.couch_user, as_user_obj)

    case_id = request.POST.get('case_id', None)
    if case_id is None:
        return HttpResponse('A case_id is required', status=400)

    try:
        if get_first_claim(domain, restore_user.user_id, case_id):
            return HttpResponse('You have already claimed that {}'.format(request.POST.get('case_type', 'case')),
                                status=409)

        claim_case(domain, restore_user.user_id, case_id,
                   host_type=request.POST.get('case_type'),
                   host_name=request.POST.get('case_name'),
                   device_id=__name__ + ".claim")
    except CaseNotFound:
        return HttpResponse('The case "{}" you are trying to claim was not found'.format(case_id),
                            status=410)
    return HttpResponse(status=200)
Exemple #4
0
 def test_first_claim_one(self):
     """
     get_first_claim should return one claim
     """
     claim_id = claim_case(DOMAIN, self.user.user_id, self.host_case_id,
                           host_type=self.host_case_type, host_name=self.host_case_name)
     claim = get_first_claim(DOMAIN, self.user.user_id, self.host_case_id)
     self.assert_claim(claim, claim_id)
Exemple #5
0
 def test_first_claim_one(self):
     """
     get_first_claim should return one claim
     """
     claim_id = claim_case(DOMAIN, self.user.user_id, self.host_case_id,
                           host_type=self.host_case_type, host_name=self.host_case_name)
     claim = get_first_claim(DOMAIN, self.user.user_id, self.host_case_id)
     self.assert_claim(claim, claim_id)
Exemple #6
0
 def test_closed_claim(self):
     """
     get_first_claim should return None if claim case is closed
     """
     claim_id = claim_case(DOMAIN, self.user.user_id, self.host_case_id,
                           host_type=self.host_case_type, host_name=self.host_case_name)
     self._close_case(claim_id)
     first_claim = get_first_claim(DOMAIN, self.user.user_id, self.host_case_id)
     self.assertIsNone(first_claim)
Exemple #7
0
 def test_closed_claim(self):
     """
     get_first_claim should return None if claim case is closed
     """
     claim_id = claim_case(DOMAIN, self.user.user_id, self.host_case_id,
                           host_type=self.host_case_type, host_name=self.host_case_name)
     self._close_case(claim_id)
     first_claim = get_first_claim(DOMAIN, self.user.user_id, self.host_case_id)
     self.assertIsNone(first_claim)
Exemple #8
0
def claim(request, domain):
    """
    Allows a user to claim a case that they don't own.
    """
    couch_user = CouchUser.from_django_user(request.user)
    case_id = request.POST['case_id']
    if (
        request.session.get('last_claimed_case_id') == case_id or
        get_first_claim(domain, couch_user.user_id, case_id)
    ):
        return HttpResponse('You have already claimed that {}'.format(request.POST.get('case_type', 'case')),
                            status=409)
    try:
        claim_case(domain, couch_user.user_id, case_id,
                   host_type=request.POST.get('case_type'), host_name=request.POST.get('case_name'))
    except CaseNotFound:
        return HttpResponse('The case "{}" you are trying to claim was not found'.format(case_id),
                            status=410)
    request.session['last_claimed_case_id'] = case_id
    return HttpResponse(status=200)
Exemple #9
0
def claim(request, domain):
    """
    Allows a user to claim a case that they don't own.
    """
    couch_user = CouchUser.from_django_user(request.user)
    case_id = request.POST['case_id']
    if (
        request.session.get('last_claimed_case_id') == case_id or
        get_first_claim(domain, couch_user.user_id, case_id)
    ):
        return HttpResponse('You have already claimed that {}'.format(request.POST.get('case_type', 'case')),
                            status=409)
    try:
        claim_case(domain, couch_user.user_id, case_id,
                   host_type=request.POST.get('case_type'), host_name=request.POST.get('case_name'))
    except CaseNotFound:
        return HttpResponse('The case "{}" you are trying to claim was not found'.format(case_id),
                            status=410)
    request.session['last_claimed_case_id'] = case_id
    return HttpResponse(status=200)
Exemple #10
0
 def test_first_claim_none(self):
     """
     get_first_claim should return None if not found
     """
     claim = get_first_claim(DOMAIN, self.user.user_id, self.host_case_id)
     self.assertIsNone(claim)
Exemple #11
0
 def test_first_claim_none(self):
     """
     get_first_claim should return None if not found
     """
     claim = get_first_claim(DOMAIN, self.user.user_id, self.host_case_id)
     self.assertIsNone(claim)