示例#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)
    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)
示例#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)
    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)
示例#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)
示例#4
0
 def test_first_claim_one(self):
     """
     get_first_claim should return one claim
     """
     claim_case(DOMAIN,
                self.restore_user,
                self.host_case_id,
                host_type=self.host_case_type,
                host_name=self.host_case_name)
     claim = get_first_claims(DOMAIN, self.user.user_id,
                              [self.host_case_id])
     self.assertEqual(len(claim), 1)
示例#5
0
 def test_claim_case(self):
     """
     claim_case should create an extension case
     """
     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.assert_claim(claim_id=claim_id)
示例#6
0
 def test_claim_case(self):
     """
     claim_case should create an extension case
     """
     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.assert_claim(claim_id=claim_id)
示例#7
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)
示例#8
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)
示例#9
0
 def test_claim_case_other_domain(self):
     malicious_domain = 'malicious_domain'
     domain_obj = create_domain(malicious_domain)
     self.addCleanup(domain_obj.delete)
     claim_id = claim_case(malicious_domain, self.user.user_id, self.host_case_id,
                           host_type=self.host_case_type, host_name=self.host_case_name)
     with self.assertRaises(CaseNotFound):
         CaseAccessors(malicious_domain).get_case(claim_id)
示例#10
0
def claim(request, domain):
    """
    Allows a user to claim a case that they don't own.
    """
    as_user = unquote(request.POST.get('commcare_login_as', ''))
    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_ids = request.POST.getlist('case_id')

    if not case_ids:
        return HttpResponse('A case_id is required', status=400)

    try:
        case_ids_already_claimed = get_first_claims(domain,
                                                    restore_user.user_id,
                                                    case_ids)
        case_ids_to_claim = {
            case
            for case in case_ids if case not in case_ids_already_claimed
        }
    except CaseNotFound as err:
        return HttpResponse(f'No cases claimed. Case IDs "{err}" not found',
                            status=410)

    for case_id in case_ids_to_claim:
        claim_case(domain,
                   restore_user,
                   case_id,
                   host_type=unquote(request.POST.get('case_type', '')),
                   host_name=unquote(request.POST.get('case_name', '')),
                   device_id=__name__ + ".claim")

    def phone_holds_all_cases(request):
        try:
            synclog = get_properly_wrapped_sync_log(request.last_sync_token)
            missing_case_ids_on_phone = set(
                case_ids) - synclog.case_ids_on_phone
            return not missing_case_ids_on_phone
        except MissingSyncLog:
            return False

    if not case_ids_to_claim and phone_holds_all_cases(request):
        return HttpResponse(status=204)

    return HttpResponse(status=201)
示例#11
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)
示例#12
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)
示例#13
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)
示例#14
0
 def test_claim_case_other_domain(self):
     malicious_domain = 'malicious_domain'
     domain_obj = create_domain(malicious_domain)
     self.addCleanup(domain_obj.delete)
     claim_id = claim_case(malicious_domain,
                           self.restore_user,
                           self.host_case_id,
                           host_type=self.host_case_type,
                           host_name=self.host_case_name)
     with self.assertRaises(CaseNotFound):
         CommCareCase.objects.get_case(claim_id, malicious_domain)
示例#15
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)
示例#16
0
 def test_closed_claim(self):
     """
     get_first_claim should return None if claim case is closed
     """
     claim_id = claim_case(DOMAIN,
                           self.restore_user,
                           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_claims(DOMAIN, self.user.user_id,
                                    [self.host_case_id])
     self.assertEqual(len(first_claim), 0)
示例#17
0
    def test_claim_index_deleted(self):
        """
        get_first_claim should return None if claim case is closed
        """
        claim_id = claim_case(DOMAIN,
                              self.restore_user,
                              self.host_case_id,
                              host_type=self.host_case_type,
                              host_name=self.host_case_name)

        # delete the case index
        case_block = CaseBlock(create=False,
                               case_id=claim_id,
                               index={
                                   "host": (self.host_case_type, "")
                               }).as_xml()
        post_case_blocks([case_block], {'domain': DOMAIN})

        first_claim = get_first_claims(DOMAIN, self.user.user_id,
                                       [self.host_case_id])
        self.assertEqual(len(first_claim), 0)
示例#18
0
 def test_claim_case_id_only(self):
     """
     claim_case should look up host case details if only ID is passed
     """
     claim_id = claim_case(DOMAIN, self.restore_user, self.host_case_id)
     self.assert_claim(claim_id=claim_id)
示例#19
0
 def test_claim_case_id_only(self):
     """
     claim_case should look up host case details if only ID is passed
     """
     claim_id = claim_case(DOMAIN, self.user.user_id, self.host_case_id)
     self.assert_claim(claim_id=claim_id)