def getid(request, mvp_site): '''Get request from Commcare check if they have HID and resubmit again ''' try: site = Site.objects.get(slug=mvp_site) except Site.DoesNotExist: return HttpResponse(_(u"Site %s is not configured") % mvp_site) data = request.raw_post_data s = LoggedMessage() s.text = data s.direction = s.DIRECTION_INCOMING s.site = site s.save() if site.slug == 'mvp-mwandama': injectid.apply_async((), {'obj': s})
def advanced_injector(obj): info = {} try: z = LoggedMessage.objects.get(pk=obj.pk) except LoggedMessage.DoesNotExist: z = False if z: pstatus = get_caseid(z.text) if pstatus: site = z.site try: Cases.objects.get(case=pstatus, site__pk=site) m = True except Cases.DoesNotExist: m = False if not m: cc = Cases.objects.create(case=pstatus, site=site) cc.save() p = sanitise_case(z.site, z.text) if not p['status']: # GET HID k = IssuedIdentifier.objects.filter(site=z.site) _all = Identifier.objects.exclude(pk__in=k.values('identifier_id')) hid = _all[0] info['caseid'] = pstatus info['health_id'] = hid.identifier if p['form_type'] == 'child' or p['form_type'] == 'pregnancy' or p['form_type'] == 'household': xml_template = HOUSEHOLD_XFORM if p['household'] else OTHERS_XFORM if check_file(xml_template, "xml"): form = CaseXMLInterface(info, xml_template) COMMCARE_URL = COMMCARE_LINK % z.site print COMMCARE_URL if xml_template_upload(form, COMMCARE_URL): s = LoggedMessage() s.text = form.render() s.direction = s.DIRECTION_OUTGOING s.response_to = z s.site = z.site s.save() LoggedMessage.objects.filter(pk=obj.pk).update(status=LoggedMessage.STATUS_SUCCESS) #z.save() issued_id = IssuedIdentifier() issued_id.status = IssuedIdentifier.STATUS_ISSUED issued_id.identifier = hid issued_id.site = z.site issued_id.save() if p['form_type'] == 'child': case_t = Cases.TYPE_CHILD if p['form_type'] == 'pregnancy': case_t = Cases.TYPE_PREGNANCY if p['form_type'] == 'household': case_t = Cases.TYPE_HOUSEHOLD cc.case_type = case_t cc.identifier = issued_id cc.save() else: cc.delete() LoggedMessage.objects.filter(pk=obj.pk).update(status=LoggedMessage.LoggedMessage.STATUS_ERROR) #z.status = LoggedMessage.STATUS_ERROR #z.save() return "HURRAY WRONG " else: cc.delete() return "Wrong xml " else: return "Already Case exist" else: return "Wrong xml. No case ID"
def injectid_crontab(): site = 'mvp-mwandama' msm = LoggedMessage.objects.filter(site__pk=site, status__isnull=True, ) msgs = msm.exclude(direction=LoggedMessage.DIRECTION_OUTGOING, response_to__isnull=False) for z in msgs: pstatus = get_caseid(z.text) if pstatus: site = z.site try: Cases.objects.get(case=pstatus, site__pk=site) m = True except Cases.DoesNotExist: m = False if not m: cc = Cases.objects.create(case=pstatus, site=site) cc.save() p = sanitise_case(z.site, z.text) if not p['status']: soup = Soup(z.text, 'xml') # GET HID k = IssuedIdentifier.objects.filter(site=z.site) _all = Identifier.objects.exclude(pk__in=k.values('identifier_id')) hid = _all[0] print p case_ = "household_head_health_id" if p['household'] else "health_id" case_type = p['form_type'] c = soup.find(case_) mm = "<%s>%s</%s>" % (case_, hid.identifier, case_) c = str(c) soup = str(soup) soup = soup.replace(c, mm) soup = soup.replace("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n", "") y = "<%s> %s </%s>" % (case_type, soup, case_type) COMMCARE_URL = COMMCARE_LINK % z.site print "HID: %s \n" % hid.identifier print "COMMCARE_URL: %s \n" % COMMCARE_URL print y print "============================================================" form = {'data': y, 'SUBMIT_TO_COMMCARE': SUBMIT_TO_COMMCARE, 'COMMCARE_URL': COMMCARE_URL} if transmit_form(form): s = LoggedMessage() s.text = y s.direction = s.DIRECTION_OUTGOING s.response_to = z s.site = z.site s.save() z.status = s.STATUS_SUCCESS z.save() p = IssuedIdentifier() p.status = IssuedIdentifier.STATUS_ISSUED p.identifier = hid p.site = z.site p.save() cc.identifier = hid cc.save() else: s = LoggedMessage() s.text = y s.direction = s.DIRECTION_OUTGOING s.response_to = z s.site = z.site s.save() z.status = s.STATUS_ERROR z.save() else: cc.delete() return "Wrong xml " else: return "Already Case exist" else: return "Wrong xml. No case ID"
def injectid(obj): z = LoggedMessage.objects.get(pk=obj.pk) p = sanitise_case(z.site, z.text) if not p['status']: soup = Soup(z.text, 'xml') # GET HID k = IssuedIdentifier.objects.filter(site=z.site) _all = Identifier.objects.exclude(pk__in=k.values('identifier_id')) hid = _all[0] print p case_ = "household_head_health_id" if p['household'] else "health_id" case_type = p['form_type'] c = soup.find(case_) mm = "<%s>%s</%s>" % (case_, hid.identifier, case_) c = str(c) soup = str(soup) soup = soup.replace(c, mm) soup = soup.replace("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n", "") y = "<%s> %s </%s>" % (case_type, soup, case_type) COMMCARE_URL = COMMCARE_LINK % z.site print "HID: %s \n" % hid.identifier print "COMMCARE_URL: %s \n" % COMMCARE_URL print y print "==========================================================" form = {'data': y, 'SUBMIT_TO_COMMCARE': SUBMIT_TO_COMMCARE, 'COMMCARE_URL': COMMCARE_URL} if transmit_form(form): s = LoggedMessage() s.text = y s.direction = s.DIRECTION_OUTGOING s.response_to = z s.site = z.site s.save() z.status = s.STATUS_SUCCESS z.save() p = IssuedIdentifier() p.status = IssuedIdentifier.STATUS_ISSUED p.identifier = hid p.site = z.site p.save() else: s = LoggedMessage() s.text = y s.direction = s.DIRECTION_OUTGOING s.response_to = z s.site = z.site s.save() z.status = s.STATUS_ERROR z.save()