コード例 #1
0
ファイル: views.py プロジェクト: ybekdemir/TolaTables
def file(request, id):
    if request.method == 'POST':
        form = UploadForm(request.POST)
        if form.is_valid():
            read_obj = Read.objects.get(pk=id)
            user = User.objects.get(username__exact=request.user)

            if request.POST.get("new_silo", None):
                silo = Silo(name=request.POST['new_silo'], owner=user, public=False,
                            create_date=timezone.now())
                silo.save()
            else:
                silo = Silo.objects.get(id = request.POST["silo_id"])
            # try:
            silo.reads.add(read_obj)
            silo_id = silo.id
            data = json.load(read_obj.file_data)
            saveDataToSilo(silo, data, read_obj)
            return HttpResponseRedirect('/silo_detail/%s/' % silo_id)
            # except Exception as e:
            #     messages.error(request, "Your JSON file was formatted incorrectly")
            #     return HttpResponseRedirect('/fileuploadjson')
        else:
            messages.error(request, "There was a problem with reading the contents of your file" + form.errors)

    user = User.objects.get(username__exact=request.user)
    # get all of the silo info to pass to the form
    get_silo = Silo.objects.filter(owner=user)

    # display the form for user to choose a table or ener a new table name to import data into
    return render(request, 'read/file.html', {
        'read_id': id, 'form_action': reverse_lazy("fileupload", kwargs={"id": id}), 'get_silo': get_silo,
    })
コード例 #2
0
ファイル: util.py プロジェクト: AthmanZiri/TolaTables
def importJSON(read_obj,
               user,
               remote_user=None,
               password=None,
               silo_id=None,
               silo_name=None,
               return_data=False):
    try:
        request2 = urllib2.Request(read_obj.read_url)
        # If the read_obj has token then use it; otherwise, check for login info
        if read_obj.token:
            request2.add_header("Authorization", "Basic %s" % read_obj.token)
        elif remote_user and password:
            base64string = base64.encodestring('%s:%s' %
                                               (remote_user, password))[:-1]
            request2.add_header("Authorization", "Basic %s" % base64string)
        else:
            pass
        # retrieve JSON data from formhub via auth info
        json_file = urllib2.urlopen(request2)

        if silo_name:
            silo = Silo(name=silo_name,
                        owner=user,
                        public=False,
                        create_date=timezone.now())
            silo.save()
        else:
            silo = Silo.objects.get(id=silo_id)

        silo.reads.add(read_obj)
        silo_id = silo.id

        # create object from JSON String
        data = json.load(json_file)
        json_file.close()

        # if the caller of this function does not want to the data to go into
        #  the silo yet
        if return_data:
            return data

        save_data_to_silo(silo, data, read_obj)
        return messages.SUCCESS, "Data imported successfully.", str(silo_id)
    except Exception as e:
        if return_data:
            return None
        return messages.ERROR, "An error has occured: %s" % e, str(silo_id)
コード例 #3
0
ファイル: util.py プロジェクト: dborchers/TolaTables
def importJSON(read_obj,
               user,
               remote_user=None,
               password=None,
               silo_id=None,
               silo_name=None):
    # set date time stamp
    today = datetime.date.today()
    today.strftime('%Y-%m-%d')
    today = str(today)
    try:
        request2 = urllib2.Request(read_obj.read_url)
        # If the read_obj has token then use it; otherwise, check for login info.
        if read_obj.token:
            request2.add_header("Authorization", "Basic %s" % read_obj.token)
        elif remote_user and password:
            base64string = base64.encodestring('%s:%s' %
                                               (remote_user, password))[:-1]
            request2.add_header("Authorization", "Basic %s" % base64string)
        else:
            pass
        #retrieve JSON data from formhub via auth info
        json_file = urllib2.urlopen(request2)
        silo = None

        if silo_name:
            silo = Silo(name=silo_name,
                        owner=user,
                        public=False,
                        create_date=today)
            silo.save()
        else:
            silo = Silo.objects.get(id=silo_id)

        silo.reads.add(read_obj)
        silo_id = silo.id

        #create object from JSON String
        data = json.load(json_file)
        json_file.close()

        skipped_rows = saveDataToSilo(silo, data)
        return (messages.SUCCESS, "Data imported successfully.", str(silo_id))
    except Exception as e:
        return (messages.ERROR, "An error has occured: %s" % e, str(silo_id))
コード例 #4
0
ファイル: views.py プロジェクト: marclaporte/asana
def getJSON(request):
    # retrieve submitted Feed info from database
    read_obj = Read.objects.latest('id')
    # set date time stamp
    today = datetime.date.today()
    today.strftime('%Y-%m-%d')
    today = str(today)
    #New silo or existing
    if request.POST['new_silo']:
        print "NEW"
        new_silo = Silo(name=request.POST['new_silo'], source=read_obj, owner=read_obj.owner, create_date=today)
        new_silo.save()
        silo_id = new_silo.id
    else:
        print "EXISTING"
        silo_id = request.POST['silo_id']

    #get auth info from form post then encode and add to the request header
    username = request.POST['user_name']
    password = request.POST['password']
    base64string = base64.encodestring('%s:%s' % (username, password))[:-1]
    request = urllib2.Request(read_obj.read_url)
    request.add_header("Authorization", "Basic %s" % base64string)
    #retrieve JSON data from formhub via auth info
    json_file = urllib2.urlopen(request)

    #create object from JSON String
    data = json.load(json_file)
    json_file.close()
    #loop over data and insert create and edit dates and append to dict
    for row in data:
        for new_label, new_value in row.iteritems():
            if new_value is not "" and new_label is not None:
                #save to DB
                saveJSON(new_value, new_label, silo_id)

    #get fields to display back to user for verification
    getFields = DataField.objects.filter(silo_id=silo_id)

    #send the keys and vars from the json data to the template along with submitted feed info and silos for new form
    return render_to_response("read/show-columns.html", {'getFields': getFields, 'silo_id': silo_id})
コード例 #5
0
ファイル: util.py プロジェクト: mercycorps/TolaTables
def importJSON(read_obj, user, remote_user=None, password=None, silo_id=None, silo_name=None):
    # set date time stamp
    today = datetime.date.today()
    today.strftime("%Y-%m-%d")
    today = str(today)
    try:
        request2 = urllib2.Request(read_obj.read_url)
        # If the read_obj has token then use it; otherwise, check for login info.
        if read_obj.token:
            request2.add_header("Authorization", "Basic %s" % read_obj.token)
        elif remote_user and password:
            base64string = base64.encodestring("%s:%s" % (remote_user, password))[:-1]
            request2.add_header("Authorization", "Basic %s" % base64string)
        else:
            pass
        # retrieve JSON data from formhub via auth info
        json_file = urllib2.urlopen(request2)
        silo = None

        if silo_name:
            silo = Silo(name=silo_name, owner=user, public=False, create_date=today)
            silo.save()
        else:
            silo = Silo.objects.get(id=silo_id)

        silo.reads.add(read_obj)
        silo_id = silo.id

        # create object from JSON String
        data = json.load(json_file)
        json_file.close()

        skipped_rows = saveDataToSilo(silo, data)
        return (messages.SUCCESS, "Data imported successfully.", str(silo_id))
    except Exception as e:
        return (messages.ERROR, "An error has occured: %s" % e, str(silo_id))