def play_remote(request, session_id=None, playsettings=None): if not session_id: playsettings = playsettings if playsettings is not None else request.POST xform = playsettings.get('xform') try: tmp_file_handle, tmp_file_path = tempfile.mkstemp() tmp_file = os.fdopen(tmp_file_handle, 'w') tmp_file.write(xform.encode('utf-8')) tmp_file.close() new_form = XForm.from_file(tmp_file_path, str(file)) notice = "Created form: %s " % file except Exception, e: logging.error("Problem creating xform from %s: %s" % (file, e)) success = False notice = "Problem creating xform from %s: %s" % (file, e) raise e session = PlaySession( next=playsettings.get('next'), abort=playsettings.get('abort'), input_mode=playsettings.get('input_mode'), preloader_data=json.loads(playsettings.get('data')), xform_id=new_form.id, saved_instance=playsettings.get('instance') ) session.save() params = '&'.join(['%s=%s' % (x[0],x[1]) for x in request.GET.items()]) return HttpResponseRedirect("%s?%s" % (reverse('xform_play_remote', args=[session._id]), params))
def xform_list(request): forms_by_namespace = defaultdict(list) success = True notice = "" if request.method == "POST": if "file" in request.FILES: file = request.FILES["file"] try: tmp_file_handle, tmp_file_path = tempfile.mkstemp() tmp_file = os.fdopen(tmp_file_handle, 'w') tmp_file.write(file.read()) tmp_file.close() new_form = XForm.from_file_with_uuid(tmp_file_path, request.POST.get('uuid')) notice = "Created form: %s" % file if request.POST['format'] == 'json': base_url = request.build_absolute_uri()[:-1] return HttpResponse(json.dumps({ 'uuid': new_form.uuid, 'url': '%s%s' % (base_url, reverse('xform_play_all', kwargs={'xform_uuid': new_form.uuid})) }), content_type='application/json') except Exception, e: logging.error("Problem creating xform from %s: %s" % (file, e)) success = False notice = "Problem creating xform from %s: %s" % (file, e) else: success = False notice = "No uploaded file set." return HttpResponseNotFound()
def bootstrap(): try: # create xform objects for everything in the configured directory, # if we don't already have them if hasattr(settings, "XFORMS_BOOTSTRAP_PATH"): files = os.listdir(settings.XFORMS_BOOTSTRAP_PATH) logging.debug("bootstrapping forms in %s" % settings.XFORMS_BOOTSTRAP_PATH) for filename in files: try: # TODO: is this sneaky lazy loading a reasonable idea? full_name = os.path.join(settings.XFORMS_BOOTSTRAP_PATH, filename) file = open(full_name, "r") checksum = hashlib.sha1(file.read()).hexdigest() file.close() if XForm.objects.filter(checksum=checksum).count() > 0: logging.debug("skipping %s, already loaded" % filename) else: xform = XForm.from_file(full_name) logging.debug("created: %s from %s" % (xform, filename)) except IOError, e: logging.exception("Problem loading file: %s" % filename) except Exception, e: logging.exception( "Unknown problem bootstrapping file: %s" % filename)
def play_remote(request, session_id=None, playsettings=None): if not session_id: playsettings = playsettings if playsettings is not None else request.POST xform = playsettings.get('xform') try: tmp_file_handle, tmp_file_path = tempfile.mkstemp() tmp_file = os.fdopen(tmp_file_handle, 'w') tmp_file.write(xform.encode('utf-8')) tmp_file.close() new_form = XForm.from_file(tmp_file_path, str(file)) notice = "Created form: %s " % file except Exception, e: logging.error("Problem creating xform from %s: %s" % (file, e)) success = False notice = "Problem creating xform from %s: %s" % (file, e) raise e session = PlaySession(next=playsettings.get('next'), abort=playsettings.get('abort'), input_mode=playsettings.get('input_mode'), preloader_data=json.loads( playsettings.get('data')), xform_id=new_form.id, saved_instance=playsettings.get('instance')) session.save() params = '&'.join( ['%s=%s' % (x[0], x[1]) for x in request.GET.items()]) return HttpResponseRedirect( "%s?%s" % (reverse('xform_play_remote', args=[session._id]), params))
def create_xform(data, name="No Name", xform_id=None): logging.debug('Creating or updating an xform object. Name:%s, xform_id: %s.' % (name, xform_id)) new_form = None try: tmp_file_handle, tmp_file_path = tempfile.mkstemp() logging.debug('Creating tempfile on disk') tmp_file = os.fdopen(tmp_file_handle, 'w') tmp_file.write(data) tmp_file.close() if xform_id: file = File(open(tmp_file_path, 'r')) logging.debug('Opened Temp File. Attempting to retrieve XForm object from DB. XFORM_ID IS %s' % xform_id) xform = get_object_or_404(XForm, id=xform_id) xform.file = file xform.save() success = True notice = "Updated form %s" % name else: logging.debug("Creating new XForm object with id: %s" % xform_id) new_form = XForm.from_file(tmp_file_path, name) notice = "Created form: %s " % name success = True except Exception, e: self.error("Problem creating xform from %s: %s" % (name, e)) success = False notice = "Problem creating xform from %s: %s" % (name, e)
def new_progress_note(request, patient_id): #patient_id """ Fill out a NEW progress note """ patient = Patient.objects.get(id=patient_id) pact_id = patient.couchdoc.pact_id case_id = patient.couchdoc.case_id def callback(xform, doc): reverse_back = reverse('view_patient', kwargs={'patient_id': patient_id}) return HttpResponseRedirect(reverse_back) url_resp = urllib2.urlopen('http://build.dimagi.com/commcare/pact/pact_progress_note.xml') #url_resp = urllib2.urlopen('http://xforms.dimagi.com/download/48') xform_str = url_resp.read() try: tmp_file_handle, tmp_file_path = tempfile.mkstemp() tmp_file = os.fdopen(tmp_file_handle, 'w') tmp_file.write(xform_str.decode('utf-8').encode('utf-8')) tmp_file.close() new_form = XForm.from_file(tmp_file_path, str(file)) notice = "Created form: %s " % file except Exception, e: logging.error("Problem creating xform from %s: %s" % (file, e)) success = False notice = "Problem creating xform from %s: %s" % (file, e) raise e
def testFromFile(self): """ This test is only run if you have a bootstrap path set. If that is the case it will sync all your forms and make sure there are no errors. """ if hasattr(settings, "XFORMS_BOOTSTRAP_PATH"): file_path = settings.XFORMS_BOOTSTRAP_PATH for file in os.listdir(file_path): model = XForm.from_file(os.path.join(file_path, file))
def xform_list(request): forms_by_namespace = defaultdict(list) success = True notice = "" if request.method == "POST": if "file" in request.FILES: file = request.FILES["file"] try: tmp_file_handle, tmp_file_path = tempfile.mkstemp() tmp_file = os.fdopen(tmp_file_handle, 'w') tmp_file.write(file.read()) tmp_file.close() XForm.from_file(tmp_file_path, str(file)) notice = "Created form: %s " % file except Exception, e: logging.error("Problem creating xform from %s: %s" % (file, e)) success = False notice = "Problem creating xform from %s: %s" % (file, e) else: success = False notice = "No uploaded file set."
def create_xform(data, name="No Name", xform_id=None): new_form = None try: tmp_file_handle, tmp_file_path = tempfile.mkstemp() tmp_file = os.fdopen(tmp_file_handle, 'w') tmp_file.write(data) tmp_file.close() if xform_id: file = File(open(tmp_file_path, 'r')) print 'GETTING FILE! XFORM_ID IS %s' % xform_id xform = get_object_or_404(XForm, id=xform_id) xform.file = file xform.save() success = True notice = "Updated form %s" % name else: new_form = XForm.from_file(tmp_file_path, name) notice = "Created form: %s " % name success = True except Exception, e: logging.error("Problem creating xform from %s: %s" % (name, e)) success = False notice = "Problem creating xform from %s: %s" % (name, e)
def bootstrap(): try: # create xform objects for everything in the configured directory, # if we don't already have them if hasattr(settings, "XFORMS_BOOTSTRAP_PATH"): files = os.listdir(settings.XFORMS_BOOTSTRAP_PATH) logging.debug("bootstrapping forms in %s" % settings.XFORMS_BOOTSTRAP_PATH) for filename in files: try: # TODO: is this sneaky lazy loading a reasonable idea? full_name = os.path.join(settings.XFORMS_BOOTSTRAP_PATH, filename) file = open(full_name, "r") checksum = hashlib.sha1(file.read()).hexdigest() file.close() if XForm.objects.filter(checksum=checksum).count() > 0: logging.debug("skipping %s, already loaded" % filename) else: xform = XForm.from_file(full_name) logging.debug("created: %s from %s" % (xform, filename)) except IOError, e: logging.exception("Problem loading file: %s" % filename) except Exception, e: logging.exception("Unknown problem bootstrapping file: %s" % filename)
def testFromFile(self): for file_name in os.listdir(DEMO_FORMS): if not file_name.startswith('.'): model = XForm.from_file(os.path.join(DEMO_FORMS, file_name)) self.assertEqual(model.name, file_name)