def enter_data(request, username, id_string): owner = get_object_or_404(User, username=username) xform = get_object_or_404(XForm, user__username=username, id_string=id_string) if not has_edit_permission(xform, owner, request, xform.shared): return HttpResponseForbidden(_(u'Not shared.')) try: formhub_url = "http://%s/" % request.META['HTTP_HOST'] except: formhub_url = "http://formhub.org/" form_url = formhub_url + username if settings.TESTING_MODE: form_url = "https://testserver.com/bob" try: url = enketo_url(form_url, xform.id_string) if not url: return HttpResponseRedirect(reverse('main.views.show', kwargs={'username': username, 'id_string': id_string})) return HttpResponseRedirect(url) except Exception, e: context = RequestContext(request) owner = User.objects.get(username=username) context.profile, created = \ UserProfile.objects.get_or_create(user=owner) context.xform = xform context.content_user = owner context.form_view = True context.message = { 'type': 'alert-error', 'text': u"Enketo error, reason: %s" % e} messages.add_message( request, messages.WARNING, _("Enketo error: enketo replied %s") % e, fail_silently=True) return render_to_response("profile.html", context_instance=context)
def edit_data(request, username, id_string, data_id): context = RequestContext(request) owner = User.objects.get(username=username) xform = get_object_or_404(XForm, user__username=username, id_string=id_string) instance = get_object_or_404(Instance, pk=data_id, xform=xform) if not has_edit_permission(xform, owner, request, xform.shared): return HttpResponseForbidden(_(u'Not shared.')) if not hasattr(settings, 'ENKETO_URL'): return HttpResponseRedirect( reverse('main.views.show', kwargs={ 'username': username, 'id_string': id_string })) url = '%sdata/edit_url' % settings.ENKETO_URL # see commit 220f2dad0e for tmp file creation try: formhub_url = "http://%s/" % request.META['HTTP_HOST'] except: formhub_url = "http://formhub.org/" injected_xml = inject_instanceid(instance.xml, instance.uuid) return_url = request.build_absolute_uri( reverse('odk_viewer.views.instance', kwargs={ 'username': username, 'id_string': id_string }) + "#/" + str(instance.id)) form_url = formhub_url + username if settings.TESTING_MODE: form_url = "https://testserver.com/bob" try: url = enketo_url(form_url, xform.id_string, instance_xml=injected_xml, instance_id=instance.uuid, return_url=return_url) except Exception, e: context.message = { 'type': 'alert-error', 'text': u"Enketo error, reason: %s" % e } messages.add_message(request, messages.WARNING, _("Enketo error: enketo replied %s") % e, fail_silently=True)
def edit_data(request, username, id_string, data_id): context = RequestContext(request) owner = User.objects.get(username=username) xform = get_object_or_404( XForm, user__username=username, id_string=id_string) instance = get_object_or_404( Instance, pk=data_id, xform=xform) if not has_edit_permission(xform, owner, request, xform.shared): return HttpResponseForbidden(_(u'Not shared.')) if not hasattr(settings, 'ENKETO_URL'): return HttpResponseRedirect( reverse( 'main.views.show', kwargs={'username': username, 'id_string': id_string} ) ) url = '%sdata/edit_url' % settings.ENKETO_URL # see commit 220f2dad0e for tmp file creation try: formhub_url = "http://%s/" % request.META['HTTP_HOST'] except: formhub_url = "http://formhub.org/" injected_xml = inject_instanceid(instance.xml, instance.uuid) return_url = request.build_absolute_uri( reverse( 'odk_viewer.views.instance', kwargs={ 'username': username, 'id_string': id_string} ) + "#/" + str(instance.id)) form_url = formhub_url + username if hasattr(settings, "TESTING_MODE") and settings.TESTING_MODE: form_url = "https://testserver.com/bob" try: url = enketo_url( form_url, xform.id_string, instance_xml=injected_xml, instance_id=instance.uuid, return_url=return_url ) except Exception, e: context.message = { 'type': 'alert-error', 'text': u"Enketo error, reason: %s" % e} messages.add_message( request, messages.WARNING, _("Enketo error: enketo replied %s") % e, fail_silently=True)
def enter_data(request, username, id_string): owner = User.objects.get(username=username) xform = get_object_or_404(XForm, user__username=username, id_string=id_string) if not has_edit_permission(xform, owner, request, xform.shared): return HttpResponseForbidden(_(u'Not shared.')) if not hasattr(settings, 'TOUCHFORMS_URL'): return HttpResponseRedirect(reverse('main.views.show', kwargs={'username': username, 'id_string': id_string})) url = settings.TOUCHFORMS_URL register_openers() response = None with tempfile.TemporaryFile() as tmp: tmp.write(xform.xml.encode('utf-8')) tmp.seek(0) values = { 'file': tmp, 'format': 'json', 'uuid': xform.uuid } data, headers = multipart_encode(values) headers['User-Agent'] = 'formhub' req = urllib2.Request(url, data, headers) try: response = urllib2.urlopen(req) response = json.loads(response.read()) context = RequestContext(request) owner = User.objects.get(username=username) context.profile, created = \ UserProfile.objects.get_or_create(user=owner) context.xform = xform context.content_user = owner context.form_view = True context.touchforms = response['url'] return render_to_response("form_entry.html", context_instance=context) #return HttpResponseRedirect(response['url']) except urllib2.URLError: pass # this will happen if we could not connect to touchforms return HttpResponseRedirect(reverse('main.views.show', kwargs={'username': username, 'id_string': id_string}))
def edit_data(request, username, id_string, data_id): owner = User.objects.get(username=username) xform = get_object_or_404( XForm, user__username=username, id_string=id_string) instance = get_object_or_404( Instance, pk=data_id, xform=xform) if not has_edit_permission(xform, owner, request, xform.shared): return HttpResponseForbidden(_(u'Not shared.')) if not hasattr(settings, 'ENKETO_URL'): return HttpResponseRedirect( reverse( 'main.views.show', kwargs={'username': username, 'id_string': id_string} ) ) url = '%sdata/edit_url' % settings.ENKETO_URL register_openers() response = None # see commit 220f2dad0e for tmp file creation try: formhub_url = "http://%s/" % request.META['HTTP_HOST'] except: formhub_url = "http://formhub.org/" injected_xml = inject_instanceid(instance.xml, instance.uuid) values = { 'format': 'json', 'form_id': xform.id_string, 'server_url': formhub_url + username, 'instance': injected_xml, 'instance_id': instance.uuid, 'return_url': request.build_absolute_uri( reverse( 'odk_viewer.views.instance', kwargs={ 'username': username, 'id_string': id_string} ) + "#/" + str(instance.id)) } data, headers = multipart_encode(values) headers['User-Agent'] = 'formhub' req = urllib2.Request(url, data, headers) try: response = urllib2.urlopen(req) response = json.loads(response.read()) context = RequestContext(request) owner = User.objects.get(username=username) context.profile, created = \ UserProfile.objects.get_or_create(user=owner) context.xform = xform context.content_user = owner context.form_view = True if 'edit_url' in response: audit = { "xform": xform.id_string, "data_id": data_id } audit_log(Actions.SUBMISSION_EDIT_REQUESTED, request.user, owner, _("Requested to edit data with id '%(data_id)s' on '%(id_string)s'.") %\ { 'id_string': xform.id_string, 'data_id': data_id }, audit, request) context.enketo = response['edit_url'] return HttpResponseRedirect(response['edit_url']) else: json_msg = response['reason'] """ return HttpResponse("<script>$('body')</script>") """ context.message = { 'type': 'alert-error', 'text': "Enketo error, reason: " + ( response['reason'] and "Server not found.") } messages.add_message(request, messages.WARNING, json_msg) return render_to_response("profile.html", context_instance=context) except urllib2.URLError: # this will happen if we could not connect to enketo messages.add_message( request, messages.WARNING, _("Enketo error: Unable to open webform url.")) except ValueError, e: messages.add_message( request, messages.WARNING, _("Enketo error: enketo replied %s") % e)
def enter_data(request, username, id_string): owner = get_object_or_404(User, username=username) xform = get_object_or_404(XForm, user__username=username, id_string=id_string) if not has_edit_permission(xform, owner, request, xform.shared): return HttpResponseForbidden(_(u'Not shared.')) if not hasattr(settings, 'ENKETO_URL'): return HttpResponseRedirect(reverse('main.views.show', kwargs={'username': username, 'id_string': id_string})) url = '%slaunch/launchSurvey' % settings.ENKETO_URL register_openers() response = None # see commit 220f2dad0e for tmp file creation try: formhub_url = "http://%s/" % request.META['HTTP_HOST'] except: formhub_url = "http://formhub.org/" values = { 'format': 'json', 'form_id': xform.id_string, 'server_url': formhub_url + username } data, headers = multipart_encode(values) headers['User-Agent'] = 'formhub' req = urllib2.Request(url, data, headers) try: response = urllib2.urlopen(req) response = json.loads(response.read()) context = RequestContext(request) owner = User.objects.get(username=username) context.profile, created = \ UserProfile.objects.get_or_create(user=owner) context.xform = xform context.content_user = owner context.form_view = True if 'url' in response: audit = { "xform": xform.id_string } audit_log(Actions.FORM_ENTER_DATA_REQUESTED, request.user, owner, _("Requested enter data url for '%(id_string)s'.") %\ { 'id_string': xform.id_string, }, audit, request) context.enketo = response['url'] #return render_to_response("form_entry.html", # context_instance=context) return HttpResponseRedirect(response['url']) else: json_msg = response['reason'] """ return HttpResponse("<script>$('body')</script>") """ context.message = { 'type': 'alert-error', 'text': "Enketo error, reason: " + ( response['reason'] and "Server not found.")} messages.add_message(request, messages.WARNING, json_msg) return render_to_response("profile.html", context_instance=context) except urllib2.URLError: # this will happen if we could not connect to enketo messages.add_message( request, messages.WARNING, _("Enketo error: Unable to open webform url.")) except ValueError, e: messages.add_message( request, messages.WARNING, _("Enketo error: enketo replied %s") % e)
def edit_data(request, username, id_string, data_id): owner = User.objects.get(username=username) xform = get_object_or_404(XForm, user__username=username, id_string=id_string) instance = get_object_or_404(Instance, pk=data_id, xform=xform) if not has_edit_permission(xform, owner, request, xform.shared): return HttpResponseForbidden(_(u'Not shared.')) if not hasattr(settings, 'ENKETO_URL'): return HttpResponseRedirect( reverse('main.views.show', kwargs={ 'username': username, 'id_string': id_string })) url = '%sdata/edit_url' % settings.ENKETO_URL register_openers() response = None # see commit 220f2dad0e for tmp file creation try: formhub_url = "http://%s/" % request.META['HTTP_HOST'] except: formhub_url = "http://formhub.org/" injected_xml = inject_instanceid(instance.xml, instance.uuid) values = { 'format': 'json', 'form_id': xform.id_string, 'server_url': formhub_url + username, 'instance': injected_xml, 'instance_id': instance.uuid, 'return_url': request.build_absolute_uri( reverse('odk_viewer.views.instance', kwargs={ 'username': username, 'id_string': id_string }) + "#/" + str(instance.id)) } data, headers = multipart_encode(values) headers['User-Agent'] = 'formhub' req = urllib2.Request(url, data, headers) try: response = urllib2.urlopen(req) response = json.loads(response.read()) context = RequestContext(request) owner = User.objects.get(username=username) context.profile, created = \ UserProfile.objects.get_or_create(user=owner) context.xform = xform context.content_user = owner context.form_view = True if 'edit_url' in response: audit = {"xform": xform.id_string, "data_id": data_id} audit_log( Actions.SUBMISSION_EDIT_REQUESTED, request.user, owner, _("Requested to edit data with id " "'%(data_id)s' on '%(id_string)s'.") % { 'id_string': xform.id_string, 'data_id': data_id }, audit, request) context.enketo = response['edit_url'] return HttpResponseRedirect(response['edit_url']) else: json_msg = response['reason'] """ return HttpResponse("<script>$('body')</script>") """ context.message = { 'type': 'alert-error', 'text': "Enketo error, reason: " + (response['reason'] and "Server not found.") } messages.add_message(request, messages.WARNING, json_msg) return render_to_response("profile.html", context_instance=context) except urllib2.URLError: # this will happen if we could not connect to enketo messages.add_message(request, messages.WARNING, _("Enketo error: Unable to open webform url.")) except ValueError, e: messages.add_message(request, messages.WARNING, _("Enketo error: enketo replied %s") % e)
def enter_data(request, username, id_string): owner = get_object_or_404(User, username=username) xform = get_object_or_404(XForm, user__username=username, id_string=id_string) if not has_edit_permission(xform, owner, request, xform.shared): return HttpResponseForbidden(_(u'Not shared.')) if not hasattr(settings, 'ENKETO_URL'): return HttpResponseRedirect( reverse('main.views.show', kwargs={ 'username': username, 'id_string': id_string })) url = '%slaunch/launchSurvey' % settings.ENKETO_URL register_openers() response = None # see commit 220f2dad0e for tmp file creation try: formhub_url = "http://%s/" % request.META['HTTP_HOST'] except: formhub_url = "http://formhub.org/" values = { 'format': 'json', 'form_id': xform.id_string, 'server_url': formhub_url + username } data, headers = multipart_encode(values) headers['User-Agent'] = 'formhub' req = urllib2.Request(url, data, headers) try: response = urllib2.urlopen(req) response = json.loads(response.read()) context = RequestContext(request) owner = User.objects.get(username=username) context.profile, created = \ UserProfile.objects.get_or_create(user=owner) context.xform = xform context.content_user = owner context.form_view = True if 'url' in response: audit = {"xform": xform.id_string} audit_log( Actions.FORM_ENTER_DATA_REQUESTED, request.user, owner, _("Requested enter data url for '%(id_string)s'.") % { 'id_string': xform.id_string, }, audit, request) context.enketo = response['url'] #return render_to_response("form_entry.html", # context_instance=context) return HttpResponseRedirect(response['url']) else: json_msg = response['reason'] """ return HttpResponse("<script>$('body')</script>") """ context.message = { 'type': 'alert-error', 'text': "Enketo error, reason: " + (response['reason'] and "Server not found.") } messages.add_message(request, messages.WARNING, json_msg) return render_to_response("profile.html", context_instance=context) except urllib2.URLError: # this will happen if we could not connect to enketo messages.add_message(request, messages.WARNING, _("Enketo error: Unable to open webform url.")) except ValueError, e: messages.add_message(request, messages.WARNING, _("Enketo error: enketo replied %s") % e)