예제 #1
0
def make_supply_point_product(supply_point_case, product_uuid):
    domain = supply_point_case.domain
    id = uuid.uuid4().hex
    user_id = get_commtrack_user_id(domain)
    username = const.COMMTRACK_USERNAME
    caseblock = CaseBlock(
        case_id=id,
        create=True,
        version=V2,
        user_id=user_id,
        case_type=const.SUPPLY_POINT_PRODUCT_CASE_TYPE,
        update={
            "product": product_uuid
        },
        index={
            const.PARENT_CASE_REF: (const.SUPPLY_POINT_CASE_TYPE,
                                    supply_point_case._id),
        }
    )
    casexml = ElementTree.tostring(caseblock.as_xml())
    submit_case_blocks(casexml, domain, username, user_id)
    pc = CommCareCase.get(id)
    pc.location_ = supply_point_case.location_
    pc.save()
    return pc
예제 #2
0
def make_supply_point_product(supply_point_case, product_uuid, owner_id=None):
    domain = supply_point_case.domain
    id = uuid.uuid4().hex
    user_id = get_commtrack_user_id(domain)
    owner_id = owner_id or get_owner_id(supply_point_case) or user_id
    username = const.COMMTRACK_USERNAME
    product_name = Product.get(product_uuid).name
    caseblock = CaseBlock(
        case_id=id,
        create=True,
        version=V2,
        case_name=product_name,
        user_id=user_id,
        owner_id=owner_id,
        case_type=const.SUPPLY_POINT_PRODUCT_CASE_TYPE,
        update={
            "product": product_uuid
        },
        index={
            const.PARENT_CASE_REF: (const.SUPPLY_POINT_CASE_TYPE,
                                    supply_point_case._id),
        }
    )
    casexml = ElementTree.tostring(caseblock.as_xml())
    submit_case_blocks(casexml, domain, username, user_id,
                       xmlns=const.COMMTRACK_SUPPLY_POINT_PRODUCT_XMLNS)
    return SupplyPointProductCase.get(id)
예제 #3
0
def make_supply_point_product(supply_point_case, product_uuid, owner_id=None):
    domain = supply_point_case.domain
    id = uuid.uuid4().hex
    user_id = get_commtrack_user_id(domain)
    owner_id = owner_id or get_owner_id(supply_point_case) or user_id
    username = const.COMMTRACK_USERNAME
    product_name = Product.get(product_uuid).name
    caseblock = CaseBlock(case_id=id,
                          create=True,
                          version=V2,
                          case_name=product_name,
                          user_id=user_id,
                          owner_id=owner_id,
                          case_type=const.SUPPLY_POINT_PRODUCT_CASE_TYPE,
                          update={"product": product_uuid},
                          index={
                              const.PARENT_CASE_REF:
                              (const.SUPPLY_POINT_CASE_TYPE,
                               supply_point_case._id),
                          })
    casexml = ElementTree.tostring(caseblock.as_xml())
    submit_case_blocks(casexml,
                       domain,
                       username,
                       user_id,
                       xmlns=const.COMMTRACK_SUPPLY_POINT_PRODUCT_XMLNS)
    return SupplyPointProductCase.get(id)
예제 #4
0
 def testBadIndexReference(self):
     CASE_ID = 'test-bad-index-case'
     block = CaseBlock(create=True, case_id=CASE_ID, user_id=USER_ID, version=V2,
                       index={'bad': ('bad-case', 'not-an-existing-id')})
     try:
         post_case_blocks([block.as_xml()])
         self.fail("Submitting against a bad case in an index should fail!")
     except Exception:
         pass
예제 #5
0
def sync_user_cases(commcare_user):
    from casexml.apps.case.tests.util import CaseBlock

    domain = commcare_user.project
    if not (domain and domain.call_center_config.enabled):
        return

    # language or phone_number can be null and will break
    # case submission
    fields = {
        'name': commcare_user.name,
        'email': commcare_user.email,
        'language': commcare_user.language or '',
        'phone_number': commcare_user.phone_number or ''
    }
    # fields comes second to prevent custom user data overriding
    fields = dict(commcare_user.user_data, **fields)

    found = False
    try:
        case = get_case_by_domain_hq_user_id(domain.name, commcare_user._id, include_docs=True)
        found = bool(case)
    except MultipleResultsFound:
        return

    close = commcare_user.to_be_deleted() or not commcare_user.is_active

    if found:
        caseblock = CaseBlock(
            create=False,
            case_id=case._id,
            version=V2,
            owner_id=domain.call_center_config.case_owner_id,
            case_type=domain.call_center_config.case_type,
            close=close,
            update=fields
        )
    else:
        fields['hq_user_id'] = commcare_user._id
        caseblock = CaseBlock(
            create=True,
            case_id=uuid.uuid4().hex,
            owner_id=domain.call_center_config.case_owner_id,
            user_id=commcare_user._id,
            version=V2,
            case_type=domain.call_center_config.case_type,
            update=fields
        )

    casexml = ElementTree.tostring(caseblock.as_xml())
    submit_case_blocks(casexml, domain, commcare_user.username, commcare_user._id)
예제 #6
0
 def to_xml(self):
     caseblock = CaseBlock(
         case_id=self.id,
         create=self.create,
         version=V2,
         user_id=self.user_id,
         case_type=const.REQUISITION_CASE_TYPE,
         update={
             "amount_requested": str(self.amount_requested)
         },
         index={
             const.PARENT_CASE_REF: (const.SUPPLY_POINT_PRODUCT_CASE_TYPE,
                                     self.product_stock_case_id),
         }
     )
     return ElementTree.tostring(caseblock.as_xml())
예제 #7
0
def make_supply_point(domain, location):
    # a supply point is currently just a case with a special type
    id = uuid.uuid4().hex
    user_id = get_commtrack_user_id(domain)
    username = const.COMMTRACK_USERNAME
    caseblock = CaseBlock(
        case_id=id,
        create=True,
        version=V2,
        user_id=user_id,
        case_type=const.SUPPLY_POINT_CASE_TYPE,
    )
    casexml = ElementTree.tostring(caseblock.as_xml())
    submit_case_blocks(casexml, domain, username, user_id)
    c = CommCareCase.get(id)
    c.bind_to_location(location)
    c.save()
    return c
예제 #8
0
def make_supply_point(domain, location, owner_id=None):
    # a supply point is currently just a case with a special type
    id = uuid.uuid4().hex
    user_id = get_commtrack_user_id(domain)
    owner_id = owner_id or user_id
    username = const.COMMTRACK_USERNAME
    caseblock = CaseBlock(
        case_id=id,
        create=True,
        version=V2,
        case_name=location.name,
        user_id=user_id,
        owner_id=owner_id,
        case_type=const.SUPPLY_POINT_CASE_TYPE,
        update={"location_id": location._id},
    )
    casexml = ElementTree.tostring(caseblock.as_xml())
    submit_case_blocks(casexml, domain, username, user_id, xmlns=const.COMMTRACK_SUPPLY_POINT_XMLNS)
    return SupplyPointCase.get(id)
예제 #9
0
 def to_xml(self):
     extras = {}
     if self.owner_id:
         extras["owner_id"] = self.owner_id
     if self.create:
         extras["case_name"] = self.product_stock_case.name
         extras["index"] = {
             const.PARENT_CASE_REF: (const.SUPPLY_POINT_PRODUCT_CASE_TYPE, self.product_stock_case._id)
         }
     caseblock = CaseBlock(
         case_id=self.id,
         create=self.create,
         version=V2,
         user_id=self.user_id,
         case_type=const.REQUISITION_CASE_TYPE,
         update=copy(self.custom_fields),
         close=self.close,
         **extras
     )
     return ElementTree.tostring(caseblock.as_xml(format_datetime=json_format_datetime))
예제 #10
0
 def to_xml(self):
     extras = {}
     if self.owner_id:
         extras['owner_id'] = self.owner_id
     if self.create:
         extras['case_name'] = self.product_stock_case.name
         extras['index'] = {
             const.PARENT_CASE_REF: (const.SUPPLY_POINT_PRODUCT_CASE_TYPE,
                                     self.product_stock_case._id),
         }
     caseblock = CaseBlock(case_id=self.id,
                           create=self.create,
                           version=V2,
                           user_id=self.user_id,
                           case_type=const.REQUISITION_CASE_TYPE,
                           update=copy(self.custom_fields),
                           close=self.close,
                           **extras)
     return ElementTree.tostring(
         caseblock.as_xml(format_datetime=json_format_datetime))
예제 #11
0
def make_supply_point(domain, location, owner_id=None):
    # a supply point is currently just a case with a special type
    id = uuid.uuid4().hex
    user_id = get_commtrack_user_id(domain)
    owner_id = owner_id or user_id
    username = const.COMMTRACK_USERNAME
    caseblock = CaseBlock(case_id=id,
                          create=True,
                          version=V2,
                          case_name=location.name,
                          user_id=user_id,
                          owner_id=owner_id,
                          case_type=const.SUPPLY_POINT_CASE_TYPE,
                          update={
                              'location_id': location._id,
                          })
    casexml = ElementTree.tostring(caseblock.as_xml())
    submit_case_blocks(casexml,
                       domain,
                       username,
                       user_id,
                       xmlns=const.COMMTRACK_SUPPLY_POINT_XMLNS)
    return SupplyPointCase.get(id)