示例#1
0
文件: views.py 项目: tremolo/formhub
def delete_data(request, username=None, id_string=None):
    xform, owner = check_and_set_user_and_form(username, id_string, request)
    response_text = u''
    if not xform:
        return HttpResponseForbidden(_(u'Not shared.'))

    data_id = request.POST.get('id')
    if not data_id:
        return HttpResponseBadRequest(_(u"id must be specified"))

    Instance.set_deleted_at(data_id)
    audit = {
        'xform': xform.id_string
    }
    audit_log(
        Actions.SUBMISSION_DELETED, request.user, owner,
        _("Deleted submission with id '%(record_id)s' "
            "on '%(id_string)s'.") %
        {
            'id_string': xform.id_string,
            'record_id': data_id
        }, audit, request)
    response_text = json.dumps({"success": "Deleted data %s" % data_id})
    if 'callback' in request.GET and request.GET.get('callback') != '':
        callback = request.GET.get('callback')
        response_text = ("%s(%s)" % (callback, response_text))
    return HttpResponse(response_text, mimetype='application/json')
示例#2
0
def delete_data(request, username=None, id_string=None):
    xform, owner = check_and_set_user_and_form(username, id_string, request)
    response_text = u""
    if not xform:
        return HttpResponseForbidden(_(u"Not shared."))

    data_id = request.POST.get("id")
    if not data_id:
        return HttpResponseBadRequest(_(u"id must be specified"))

    Instance.set_deleted_at(data_id)
    audit = {"xform": xform.id_string}
    audit_log(
        Actions.SUBMISSION_DELETED,
        request.user,
        owner,
        _("Deleted submission with id '%(record_id)s' " "on '%(id_string)s'.")
        % {"id_string": xform.id_string, "record_id": data_id},
        audit,
        request,
    )
    response_text = json.dumps({"success": "Deleted data %s" % data_id})
    if "callback" in request.GET and request.GET.get("callback") != "":
        callback = request.GET.get("callback")
        response_text = "%s(%s)" % (callback, response_text)
    return HttpResponse(response_text, mimetype="application/json")
示例#3
0
def delete_data(request, username=None, id_string=None):
    query = request.POST.get('query', None)
    if query is None:
        return HttpResponseBadRequest(_(u"Invalid query parameter"))

    try:
        simplejson.loads(query)
    except ValueError:
        return HttpResponseBadRequest(_(u"Invalid query parameter"))

    xform, owner = check_and_set_user_and_form(username, id_string, request)
    response_text = u''
    if not xform:
        return HttpResponseForbidden(_(u'Not shared.'))
    try:
        query_args = {
            "username": username, "id_string": id_string,
            "query": query,
            "fields": request.POST.get('fields', None),
            "sort": request.POST.get('sort', None),
            "limit": 1
        }

        if 'limit' in request.GET:
            query_args["limit"] = int(request.GET.get('limit'))
        cursor = ParsedInstance.query_mongo(**query_args)
    except ValueError as e:
        return HttpResponseBadRequest(e)
    else:
        records = list(record for record in cursor)
        if records.__len__():
            for record in records:
                Instance.delete_by_uuid(
                    username, id_string, uuid=record['_uuid'])
                audit = {
                    'xform': xform.id_string
                }
                audit_log(Actions.SUBMISSION_DELETED, request.user, owner,
                    _("Deleted submission with id '%(record_id)s' on '%(id_string)s'.") %\
                    {
                        'id_string': xform.id_string,
                        'record_id': record['_id']
                    }, audit, request)
            response_text = simplejson.dumps(records)
    if 'callback' in request.GET and request.GET.get('callback') != '':
        callback = request.GET.get('callback')
        response_text = ("%s(%s)" % (callback, response_text))
    return HttpResponse(response_text, mimetype='application/json')
示例#4
0
    def get_simple_instance(self, custom_values={}):
        simple_xforms = XForm.objects.filter(title=u"WaterSimple")
        if simple_xforms.count() == 0:
            xf = self.create_simple_xform()
        else:
            xf = simple_xforms[0]

        #these values can be overridden with custom values
        values = {
            u'device_id': u'12345',
            u'start': u'2011-01-01T09:50:06.966',
            u'end': u'2011-01-01T09:53:22.965',
            u'geopoint': u'40.783594633609184 -73.96436698913574 300.0 4.0'
        }

        if u'start' in custom_values:
            st = custom_values[u'start']
            custom_values[u'start'] = st.strftime(XFORM_TIME_FORMAT)

            #if no end_time is specified, defaults to 1 hour
            values[u'end'] = (st + ONE_HOUR).strftime(XFORM_TIME_FORMAT)

        if u'end' in custom_values:
            custom_values[u'end'] = custom_values[u'end'].strftime(
                XFORM_TIME_FORMAT)

        values.update(custom_values)

        water_simple_survey = _load_simple_survey_object()
        simple_survey = water_simple_survey.instantiate()

        for k, v in values.items():
            simple_survey.answer(name=k, value=v)

        #setting the id_string so that it doesn't end up
        #with the timestamp of the new survey object
        simple_survey._id = xf.id_string

        instance_xml = simple_survey.to_xml()

        return Instance(xml=instance_xml)
示例#5
0
    def get_registration_instance(self, custom_values={}):
        """
        1. Checks to see if the registration form has been created alread. If not, it loads it in.
        
        2. Loads a registration instance.
        """
        registration_xforms = XForm.objects.filter(title=u"registration")
        if registration_xforms.count() == 0:
            xf = self.create_registration_xform()
        else:
            xf = registration_xforms[0]

        values = {
            u'device_id': u'12345',
            u'start': u'2011-01-01T09:50:06.966',
            u'end': u'2011-01-01T09:53:22.965'
        }

        if u'start' in custom_values:
            st = custom_values[u'start']
            custom_values[u'start'] = st.strftime(XFORM_TIME_FORMAT)

            #if no end_time is specified, defaults to 1 hour
            values[u'end'] = (st + ONE_HOUR).strftime(XFORM_TIME_FORMAT)

        if u'end' in custom_values:
            custom_values[u'end'] = custom_values[u'end'].strftime(
                XFORM_TIME_FORMAT)

        values.update(custom_values)

        reg_xform = _load_registration_survey_object()
        reg_instance = reg_xform.instantiate()
        reg_instance._id = xf.id_string

        for k, v in values.items():
            reg_instance.answer(name=k, value=v)

        instance_xml = reg_instance.to_xml()

        return Instance(xml=instance_xml)