def viewer_counter(request):
    temp_dir = get_workspace()
    try:


        if controllers.use_hs_client_helper:
            hs = controllers.get_oauth_hs(request)
        else:
            hs = controllers.getOAuthHS(request)

        user =  hs.getUserInfo()
        user1 = user['username']
    except:
        user1 =""
    if user1 != 'mbayles2':
        temp_dir = temp_dir[:-24]
        file_temp_name = temp_dir + '/view_counter.txt'
        if not os.path.exists(temp_dir+"/view_counter.txt"):
            file_temp = open(file_temp_name, 'a')
            first = '1'
            file_temp.write(first)
            file_temp.close()
        else:
            file_temp = open(file_temp_name, 'r+')
            content = file_temp.read()
            number = int(content)
            number  = number +1
            number  = str(number)
            file_temp.seek(0)
            file_temp.write(number)
            file_temp.close()
    else:
        user1=''
Exemple #2
0
def get_hydroshare_resource(request, res_id, data_for_chart):
    error = False
    is_owner = False
    file_path = get_workspace() + '/id'
    root_dir = file_path + '/' + res_id
    #
    # elif 'hydroshare_generic' in src:
    #     target_url =  'https://www.hydroshare.org/django_irods/download/'+res_id+'/data/contents/HIS_reference_timeseries.txt'
    #     d

    try:
        shutil.rmtree(root_dir)
    except:
        nothing = None
    try:
        hs = controllers.getOAuthHS(request)
        hs.getResource(res_id, destination=file_path, unzip=True)
        data_dir = root_dir + '/' + res_id + '/data/contents/'
        for subdir, dirs, files in os.walk(data_dir):
            for file in files:
                if 'xml' in file:
                    data_file = data_dir + file
                    with open(data_file, 'r') as f:
                        # print f.read()
                        data = f.read()
                        # print data
                        f.close()
                        print data
                        try:
                            data = data.decode('latin-1')
                        except:
                            data = data
                        data_for_chart.update({str(file): data})

        # data_for_chart = {'bjo':'hello'}
        user = hs.getUserInfo()
        user1 = user['username']
        # resource = hs.getResourceList(user ='******')
        resource = hs.getResourceList(owner=user1)
        for res in resource:
            # print res
            id = res["resource_id"]
            # print id
            if (res_id == res["resource_id"]):
                is_owner = True
    except Exception as inst:
        data_for_chart = 'You are not authorized to access this resource'
        owner = False
        error = True
        print 'start'
        print(type(inst))
        print(inst.args)
        try:
            data_for_chart = str(inst)
        except:
            data_for_chart = "There was an error loading data for resource" + res_id
        print "end"
    data_dic = {"data": data_for_chart, "owner": is_owner, "error": error}
    return data_dic
def get_hydroshare_resource(request,res_id,data_for_chart):
    error = False
    is_owner = False
    file_path = get_workspace() + '/id'
    root_dir = file_path + '/' + res_id
      #
    # elif 'hydroshare_generic' in src:
    #     target_url =  'https://www.hydroshare.org/django_irods/download/'+res_id+'/data/contents/HIS_reference_timeseries.txt'
    #     d

    try:
        shutil.rmtree(root_dir)
    except:
        nothing =None
    try:
        hs = controllers.getOAuthHS(request)
        hs.getResource(res_id, destination=file_path, unzip=True)
        data_dir = root_dir + '/' + res_id + '/data/contents/'
        for subdir, dirs, files in os.walk(data_dir):
            for file in files:
                if 'xml' in file:
                    data_file = data_dir + file
                    with open(data_file, 'r') as f:
                        # print f.read()
                        data = f.read()
                        # print data
                        f.close()
                        print data
                        try:
                            data= data.decode('latin-1')
                        except:
                            data = data
                        data_for_chart.update({str(file): data})

        # data_for_chart = {'bjo':'hello'}
        user =  hs.getUserInfo()
        user1 = user['username']
        # resource = hs.getResourceList(user ='******')
        resource = hs.getResourceList(owner = user1)
        for  res in resource:
            # print res
            id = res["resource_id"]
            # print id
            if(res_id ==res["resource_id"]):
                is_owner = True
    except Exception as inst:
        data_for_chart = 'You are not authorized to access this resource'
        owner = False
        error = True
        print 'start'
        print(type(inst))
        print(inst.args)
        try:
            data_for_chart = str(inst)
        except:
            data_for_chart = "There was an error loading data for resource"+res_id
        print "end"
    data_dic = {"data":data_for_chart,"owner":is_owner,"error":error}
    return data_dic
def unzip_waterml(request, res_id,src,res_id2,xml_id):
        print src
        file_number=0
        temp_dir = get_workspace()
        file_data =None
        # get the URL of the remote zipped WaterML resource
        if not os.path.exists(temp_dir+"/id"):
            os.makedirs(temp_dir+"/id")
        if 'cuahsi'in src :
            url_zip = 'http://qa-webclient-solr.azurewebsites.net/CUAHSI/HydroClient/WaterOneFlowArchive/'+res_id+'/zip'
        elif 'hydroshare' in src:
            if controllers.use_hs_client_helper:
                hs = controllers.get_oauth_hs(request)
            else:
                hs = controllers.getOAuthHS(request)
            file_path = get_workspace() + '/id'
            hs.getResource(res_id, destination=file_path, unzip=True)
            root_dir = file_path + '/' + res_id
            data_dir = root_dir + '/' + res_id + '/data/contents/'
            for subdir, dirs, files in os.walk(data_dir):
                for file in files:
                    if  'wml_1_' in file:
                        data_file = data_dir + file
                        with open(data_file, 'r') as f:
                            # print f.read()
                            file_data = f.read()
                            f.close()
                            file_temp_name = temp_dir + '/id/' + res_id + '.xml'
                            file_temp = open(file_temp_name, 'wb')
                            file_temp.write(file_data)
                            file_temp.close()
                    if '.json.refts' in file:
                        data_file = data_dir +file
                        with open(data_file, 'r') as f:
                            file_data = f.read()

                            file_number = parse_ts_layer(file_data)
        elif "xmlrest" in src:

            url_zip = res_id2
            res = urllib.unquote(res_id2).decode()
            r = requests.get(res, verify=False)

            file_data = r.content
            print file_data
            file_temp_name = temp_dir + '/id/'+xml_id+'.xml'
            file_temp = open(file_temp_name, 'wb')
            file_temp.write(file_data)
            file_temp.close()
        else:
            url_zip = 'http://' + request.META['HTTP_HOST'] + '/apps/data-cart/showfile/'+res_id


        if src != 'hydroshare_generic' and src != 'xmlrest' and src !='hydroshare':
            try:
                r = requests.get(url_zip, verify=False)
                z = zipfile.ZipFile(StringIO.StringIO(r.content))
                file_list = z.namelist()
                try:
                    for file in file_list:
                        if 'hydroshare' in src:
                            if 'wml_1_' in file:
                                file_data = z.read(file)
                                file_temp_name = temp_dir + '/id/' + res_id + '.xml'
                                file_temp = open(file_temp_name, 'wb')
                                file_temp.write(file_data)
                                file_temp.close()
                        else:
                            file_data = z.read(file)
                            file_temp_name = temp_dir + '/id/' + res_id + '.xml'
                            file_temp = open(file_temp_name, 'wb')
                            file_temp.write(file_data)
                            file_temp.close()
                # error handling

                # checks to see if data is an xml
                except etree.XMLSyntaxError as e:
                    print "Error:Not XML"
                    error_report("Error:Not XML")
                    return False

                # checks to see if Url is valid
                except ValueError, e:
                    error_report("Error:invalid Url")
                    print "Error:invalid Url"
                    return False

                # checks to see if xml is formatted correctly
                except TypeError, e:
                    error_report("Error:string indices must be integers not str")
                    print "Error:string indices must be integers not str"
                    return False