コード例 #1
0
ファイル: test_archive.py プロジェクト: dimagi/couchforms
    def testArchive(self):
        form = XFormInstance(form={'foo': 'bar'})
        form.save()
        self.assertEqual("XFormInstance", form.doc_type)
        self.assertEqual(0, len(form.history))

        lower_bound = datetime.utcnow() - timedelta(seconds=1)
        form.archive(user='******')
        upper_bound = datetime.utcnow() + timedelta(seconds=1)
        form = fetch_and_wrap_form(form._id)
        self.assertEqual('XFormArchived', form.doc_type)
        self.assertTrue(isinstance(form, XFormArchived))

        [archival] = form.history
        self.assertTrue(lower_bound <= archival.date <= upper_bound)
        self.assertEqual('archive', archival.operation)
        self.assertEqual('mr. librarian', archival.user)

        lower_bound = datetime.utcnow() - timedelta(seconds=1)
        form.unarchive(user='******')
        upper_bound = datetime.utcnow() + timedelta(seconds=1)
        form = fetch_and_wrap_form(form._id)
        self.assertEqual('XFormInstance', form.doc_type)
        self.assertTrue(isinstance(form, XFormInstance))

        [archival, restoration] = form.history
        self.assertTrue(lower_bound <= restoration.date <= upper_bound)
        self.assertEqual('unarchive', restoration.operation)
        self.assertEqual('mr. researcher', restoration.user)
コード例 #2
0
    def testArchive(self):
        form = XFormInstance(form={'foo': 'bar'})
        form.save()
        self.assertEqual("XFormInstance", form.doc_type)
        self.assertEqual(0, len(form.history))

        lower_bound = datetime.utcnow() - timedelta(seconds=1)
        form.archive(user='******')
        upper_bound = datetime.utcnow() + timedelta(seconds=1)
        form = fetch_and_wrap_form(form._id)
        self.assertEqual('XFormArchived', form.doc_type)
        self.assertTrue(isinstance(form, XFormArchived))

        [archival] = form.history
        self.assertTrue(lower_bound <= archival.date <= upper_bound)
        self.assertEqual('archive', archival.operation)
        self.assertEqual('mr. librarian', archival.user)

        lower_bound = datetime.utcnow() - timedelta(seconds=1)
        form.unarchive(user='******')
        upper_bound = datetime.utcnow() + timedelta(seconds=1)
        form = fetch_and_wrap_form(form._id)
        self.assertEqual('XFormInstance', form.doc_type)
        self.assertTrue(isinstance(form, XFormInstance))

        [archival, restoration] = form.history
        self.assertTrue(lower_bound <= restoration.date <= upper_bound)
        self.assertEqual('unarchive', restoration.operation)
        self.assertEqual('mr. researcher', restoration.user)
コード例 #3
0
ファイル: cleanup.py プロジェクト: ekush/commcare-hq
def get_case_forms(case_id):
    """
    Get all forms that have submitted against a case (including archived and deleted forms)
    wrapped by the appropriate form type.
    """
    form_ids = get_case_xform_ids(case_id)
    return [fetch_and_wrap_form(id) for id in form_ids]
コード例 #4
0
ファイル: cleanup.py プロジェクト: LifeCoaching/commcare-hq
def get_case_forms(case_id):
    """
    Get all forms that have submitted against a case (including archived and deleted forms)
    wrapped by the appropriate form type.
    """
    form_ids = get_case_xform_ids(case_id)
    return [fetch_and_wrap_form(id) for id in form_ids]
コード例 #5
0
ファイル: cleanup.py プロジェクト: kamilk161/commcare-hq
def rebuild_case(case_id):
    """
    Given a case ID, rebuild the entire case state based on all existing forms
    referencing it. Useful when things go wrong or when you need to manually
    rebuild a case after archiving / deleting it
    """

    try:
        case = CommCareCase.get(case_id)
        found = True
    except ResourceNotFound:
        case = CommCareCase()
        case._id = case_id
        found = False

    # clear actions, xform_ids, close state, and all dynamic properties
    dynamic_properties = set([k for action in case.actions for k in action.updated_unknown_properties.keys()])
    for k in dynamic_properties:
        try:
            delattr(case, k)
        except KeyError:
            pass

    # already deleted means it was explicitly set to "deleted",
    # as opposed to getting set to that because it has no actions
    already_deleted = case.doc_type == 'CommCareCase-Deleted' and primary_actions(case)
    if not already_deleted:
        case.doc_type = 'CommCareCase'
    case.xform_ids = []
    case.actions = []
    case.closed = False
    case.closed_on = None
    case.closed_by = ''

    form_ids = get_case_xform_ids(case_id)
    forms = [fetch_and_wrap_form(id) for id in form_ids]
    filtered_forms = [f for f in forms if f.doc_type == "XFormInstance"]
    sorted_forms = sorted(filtered_forms, key=lambda f: f.received_on)
    for form in sorted_forms:
        if not found and case.domain is None:
            case.domain = form.domain
        assert form.domain == case.domain

        case_updates = get_case_updates(form)
        filtered_updates = [u for u in case_updates if u.id == case_id]
        for u in filtered_updates:
            case.update_from_case_update(u, form)

    case.xform_ids = [f._id for f in sorted_forms]
    if not case.xform_ids:
        if not found:
            return None
        # there were no more forms. 'delete' the case
        case.doc_type = 'CommCareCase-Deleted'

    # add a "rebuild" action
    case.actions.append(_rebuild_action())
    case.save()
    return case
コード例 #6
0
ファイル: models.py プロジェクト: ekush/commcare-hq
def get(doc_id):
    import warnings
    warnings.warn(
        'couchforms.models.get has been deprecated. '
        'You should use couchforms.fetch_and_wrap_form instead.',
        DeprecationWarning)
    import couchforms
    return couchforms.fetch_and_wrap_form(doc_id)
コード例 #7
0
ファイル: models.py プロジェクト: johan--/commcare-hq
def get(doc_id):
    import warnings
    warnings.warn(
        'couchforms.models.get has been deprecated. '
        'You should use couchforms.fetch_and_wrap_form instead.',
        DeprecationWarning
    )
    import couchforms
    return couchforms.fetch_and_wrap_form(doc_id)
コード例 #8
0
ファイル: v0_3.py プロジェクト: pawelreise/commcare-hq
 def obj_get(self, bundle, **kwargs):
     domain = kwargs['domain']
     doc_id = kwargs['pk']
     doc_type = 'XFormInstance'
     # Logic borrowed from util.get_object_or_not_exist
     try:
         doc = couchforms.fetch_and_wrap_form(doc_id)
         if doc and doc.domain == domain:
             return doc
     except ResourceNotFound:
         pass # covered by the below
     except AttributeError:
         # there's a weird edge case if you reference a form with a case id
         # that explodes on the "version" property. might as well swallow that
         # too.
         pass
     raise object_does_not_exist(doc_type, doc_id)
コード例 #9
0
ファイル: v0_3.py プロジェクト: thedevelopermw/commcare-hq
 def obj_get(self, bundle, **kwargs):
     domain = kwargs['domain']
     doc_id = kwargs['pk']
     doc_type = 'XFormInstance'
     # Logic borrowed from util.get_object_or_not_exist
     try:
         doc = couchforms.fetch_and_wrap_form(doc_id)
         if doc and doc.domain == domain:
             return doc
     except ResourceNotFound:
         pass  # covered by the below
     except AttributeError:
         # there's a weird edge case if you reference a form with a case id
         # that explodes on the "version" property. might as well swallow that
         # too.
         pass
     raise object_does_not_exist(doc_type, doc_id)
コード例 #10
0
    def handle(self, *args, **options):
        if len(args) == 1:
            filename = args[0]
        else:
            raise CommandError('Usage: %s\n%s' % (self.args, self.help))

        doc_id_index = HEADERS.index('doc_id')
        domain_index = HEADERS.index('domain')
        with open(filename, 'r') as f:
            reader = csv.reader(f)
            for row in reader:
                domain = row[domain_index]
                doc_id = row[doc_id_index]
                # don't process the header row
                if doc_id == "doc_id": 
                    continue

                print 'reprocessing form %s in domain %s' % (doc_id, domain)
                form = fetch_and_wrap_form(doc_id)
                try:
                    reprocess_form_cases(form)
                except AssertionError:
                    print 'form %s FAILED' % doc_id
コード例 #11
0
    def handle(self, *args, **options):
        if len(args) == 1:
            filename = args[0]
        else:
            raise CommandError('Usage: %s\n%s' % (self.args, self.help))

        doc_id_index = HEADERS.index('doc_id')
        domain_index = HEADERS.index('domain')
        with open(filename, 'r') as f:
            reader = csv.reader(f)
            for row in reader:
                domain = row[domain_index]
                doc_id = row[doc_id_index]
                # don't process the header row
                if doc_id == "doc_id":
                    continue

                print 'reprocessing form %s in domain %s' % (doc_id, domain)
                form = fetch_and_wrap_form(doc_id)
                try:
                    reprocess_form_cases(form)
                except AssertionError:
                    print 'form %s FAILED' % doc_id
コード例 #12
0
            pass

    # already deleted means it was explicitly set to "deleted",
    # as opposed to getting set to that because it has no actions
    already_deleted = case.doc_type == 'CommCareCase-Deleted' and primary_actions(
        case)
    if not already_deleted:
        case.doc_type = 'CommCareCase'
    case.xform_ids = []
    case.actions = []
    case.closed = False
    case.closed_on = None
    case.closed_by = ''

    form_ids = get_case_xform_ids(case_id)
    forms = [fetch_and_wrap_form(id) for id in form_ids]
    filtered_forms = [f for f in forms if f.doc_type == "XFormInstance"]
    sorted_forms = sorted(filtered_forms, key=lambda f: f.received_on)
    for form in sorted_forms:
        if not found and case.domain is None:
            case.domain = form.domain
        assert form.domain == case.domain

        case_updates = get_case_updates(form)
        filtered_updates = [u for u in case_updates if u.id == case_id]
        for u in filtered_updates:
            case.update_from_case_update(u, form)

    case.xform_ids = [f._id for f in sorted_forms]
    if not case.xform_ids:
        if not found: