Exemple #1
0
 def create_group(self, domain):
     group = CommCareCaseGroup(
         name=self.cleaned_data['name'],
         domain=domain
     )
     group.save()
     return group
Exemple #2
0
    def handle(self, *labels, **options):
        existing_samples = CommCareCaseGroup.get_db().view(
            'reminders/sample_by_domain',
            startkey=[],
            endkey=[{}],
            include_docs=True
        ).all()

        print "Found %d SurveySamples to migrate..." % len(existing_samples)
        print "Migrating"

        for sample in existing_samples:
            try:
                sample_doc = sample["doc"]
                sample_doc["timezone"] = sample_doc.get("time_zone")
                del sample_doc["time_zone"]
                sample_doc["cases"] = sample_doc.get("contacts", [])
                del sample_doc["contacts"]
                sample_doc["doc_type"] = CommCareCaseGroup.__name__

                case_group = CommCareCaseGroup.wrap(sample_doc)
                case_group.save()
                sys.stdout.write('.')
            except Exception:
                sys.stdout.write('!')
            sys.stdout.flush()

        print "\nMigration complete."
Exemple #3
0
 def create_group(self, domain):
     group = CommCareCaseGroup(
         name=self.cleaned_data['name'],
         domain=domain
     )
     group.save()
     return group
Exemple #4
0
def add_cases_to_case_group(domain, case_group_id, uploaded_data):
    response = {
        'errors': [],
        'success': [],
    }
    try:
        case_group = CommCareCaseGroup.get(case_group_id)
    except ResourceNotFound:
        response['errors'].append(_("The case group was not found."))
        return response

    for row in uploaded_data:
        identifier = row.get('case_identifier')
        case = None
        if identifier is not None:
            case = get_case_by_identifier(domain, str(identifier))
        if not case:
            response['errors'].append(_("Could not find case with identifier '%s'." % identifier))
        elif case.doc_type != 'CommCareCase':
            response['errors'].append(_("It looks like the case with identifier '%s' is deleted" % identifier))
        elif case._id in case_group.cases:
            response['errors'].append(_("A case with identifier %s already exists in this group." % identifier))
        else:
            case_group.cases.append(case._id)
            response['success'].append(_("Case with identifier '%s' has been added to this group." % identifier))

    if response['success']:
        case_group.save()

    return response
Exemple #5
0
 def get_deleted_item_data(self, item_id):
     case_group = CommCareCaseGroup.get(item_id)
     item_data = self._get_item_data(case_group)
     case_group.delete()
     return {
         'itemData': item_data,
         'template': 'deleted-group-template',
     }
Exemple #6
0
def get_sample_list(domain):
    #Circular import
    from casexml.apps.case.models import CommCareCaseGroup
    
    sample_list = []
    for sample in CommCareCaseGroup.get_all(domain):
        sample_list.append({"code" : sample._id, "name" : sample.name})
    return sample_list
Exemple #7
0
 def get_deleted_item_data(self, item_id):
     case_group = CommCareCaseGroup.get(item_id)
     item_data = self._get_item_data(case_group)
     case_group.delete()
     return {
         'itemData': item_data,
         'template': 'deleted-group-template',
     }
Exemple #8
0
def get_sample_list(domain):
    #Circular import
    from casexml.apps.case.models import CommCareCaseGroup
    
    sample_list = []
    for sample in CommCareCaseGroup.get_all(domain):
        sample_list.append({"code" : sample._id, "name" : sample.name})
    return sample_list
Exemple #9
0
 def clean(self):
     cleaned_data = super(UpdateCaseGroupForm, self).clean()
     try:
         self.current_group = CommCareCaseGroup.get(self.cleaned_data.get('item_id'))
     except AttributeError:
         raise forms.ValidationError("You're not passing in the group's id!")
     except ResourceNotFound:
         raise forms.ValidationError("This case group was not found in our database!")
     return cleaned_data
Exemple #10
0
 def clean(self):
     cleaned_data = super(UpdateCaseGroupForm, self).clean()
     try:
         self.current_group = CommCareCaseGroup.get(self.cleaned_data.get('item_id'))
     except AttributeError:
         raise forms.ValidationError("You're not passing in the group's id!")
     except ResourceNotFound:
         raise forms.ValidationError("This case group was not found in our database!")
     return cleaned_data
Exemple #11
0
 def paginated_list(self):
     for group in CommCareCaseGroup.get_all(self.domain,
                                            limit=self.limit,
                                            skip=self.skip):
         item_data = self._get_item_data(group)
         item_data['updateForm'] = self.get_update_form_response(
             self.get_update_form(initial_data={
                 'item_id': group._id,
                 'name': group.name,
             }))
         yield {
             'itemData': item_data,
             'template': 'existing-group-template',
         }
Exemple #12
0
 def paginated_list(self):
     for group in CommCareCaseGroup.get_all(
             self.domain,
             limit=self.limit,
             skip=self.skip
         ):
         item_data = self._get_item_data(group)
         item_data['updateForm'] = self.get_update_form_response(
             self.get_update_form(initial_data={
                 'item_id': group._id,
                 'name': group.name,
             })
         )
         yield {
             'itemData': item_data,
             'template': 'existing-group-template',
         }
Exemple #13
0
def add_cases_to_case_group(domain, case_group_id, uploaded_data):
    response = {
        'errors': [],
        'success': [],
    }
    try:
        case_group = CommCareCaseGroup.get(case_group_id)
    except ResourceNotFound:
        response['errors'].append(_("The case group was not found."))
        return response

    for row in uploaded_data:
        identifier = row.get('case_identifier')
        case = None
        if identifier is not None:
            case = get_case_by_identifier(domain, str(identifier))
        if not case:
            response['errors'].append(
                _("Could not find case with identifier '%s'." % identifier))
        elif case.doc_type != 'CommCareCase':
            response['errors'].append(
                _("It looks like the case with identifier '%s' is deleted" %
                  identifier))
Exemple #14
0
 def total(self):
     return CommCareCaseGroup.get_total(self.domain)
Exemple #15
0
 def case_group(self):
     try:
         return CommCareCaseGroup.get(self.group_id)
     except ResourceNotFound:
         raise Http404()
Exemple #16
0
 def case_groups(self):
     return [CommCareCaseGroup.get(g) for g in self.case_group_ids]
Exemple #17
0
 def total(self):
     return CommCareCaseGroup.get_total(self.domain)
Exemple #18
0
 def case_group(self):
     try:
         return CommCareCaseGroup.get(self.group_id)
     except ResourceNotFound:
         raise Http404()
Exemple #19
0
 def options(self):
     return [(g["id"], g["key"][1]) for g in CommCareCaseGroup.get_by_domain(self.domain, include_docs=False)]
Exemple #20
0
 def case_groups(self):
     return [CommCareCaseGroup.get(g) for g in self.case_group_ids]
Exemple #21
0
 def options(self):
     return [(g["id"], g["key"][1])
             for g in CommCareCaseGroup.get_by_domain(self.domain,
                                                      include_docs=False)]