Example #1
0
    def test_get_resource_list_filter_date(self):
        hs = HydroShare(hostname=self.url)
        from_date = date(2015, 5, 20)
        res_list = hs.getResourceList(from_date=from_date)
        for (i, r) in enumerate(res_list):
            self.assertTrue(
                datetime.strptime(r['date_created'], '%m-%d-%Y').date() >=
                from_date)

        to_date = date(2015, 5, 21)  # up to and including 5/21/2015
        res_list = hs.getResourceList(to_date=to_date)
        for (i, r) in enumerate(res_list):
            self.assertTrue(
                datetime.strptime(r['date_created'], '%m-%d-%Y').date() <
                to_date)

        from_date = date(2015, 5, 19)
        to_date = date(2015, 5, 22)  # up to and including 5/21/2015
        res_list = hs.getResourceList(from_date=from_date, to_date=to_date)
        for (i, r) in enumerate(res_list):
            self.assertTrue(
                datetime.strptime(r['date_created'], '%m-%d-%Y').date() >=
                from_date)
            self.assertTrue(
                datetime.strptime(r['date_created'], '%m-%d-%Y').date() <
                to_date)
def home(request):
    """
    Controller for the app home page.
    """
    logger = logging.getLogger('django')
    logger.error('Entering controller.py...')
    res_output = ""
   
    client_id = getattr(settings, "SOCIAL_AUTH_HYDROSHARE_KEY", "None") 
    client_secret = getattr(settings, "SOCIAL_AUTH_HYDROSHARE_SECRET", "None")
    token = request.user.social_auth.get(provider='hydroshare').extra_data['token_dict']
    
    token_dict_str = "Token: {0}".format(str(token))
    logger.error(token_dict_str)
    auth = HydroShareAuthOAuth2(client_id, client_secret, token=token)
    try:
        logger.error("Fetching resource list from playground.hydroshare.org")
        hs = HydroShare(auth=auth, hostname='playground.hydroshare.org')
        for resource in hs.getResourceList():
            res_output += str(resource) 
     
        context = {"res_output": res_output, "token_dict_str": token_dict_str}
    
        return render(request, 'oauth_user_demo/home.html', context)
    
    except TokenExpiredError as e:
        # TODO: redirect back to login view, giving this view as the view to return to
        logger.error("TokenExpiredError: TODO: redirect to login view")
        raise e
Example #3
0
 def test_get_resource_list_filter_creator_private(self):
     hs = HydroShare(hostname=self.url)
     res_list = hs.getResourceList(creator=self.creator)
     privateResInPublic = filter(lambda x: x['public'] == False, res_list)
     self.assertTrue(
         len(privateResInPublic) == 0,
         "private resources listed in api call from anonymous")
     # should be a lambda to to this
     res_list = hs.getResourceList(creator=self.creator)
     public_count = sum(1 for x in res_list)
     hs_auth = self.test_auth()
     res_list_auth = hs_auth.getResourceList(creator=self.creator)
     private_count = sum(1 for x in res_list_auth)
     self.assertGreater(
         private_count, public_count, "Private(" + str(private_count) +
         ") not greater than puhlic(" + str(public_count) + ") ")
    def test_get_resource_list(self):
        hs = HydroShare()
        res_list = hs.getResourceList()

        for (i, r) in enumerate(res_list):
            self.assertEquals(r['resource_title'], self.resource_titles[i])
            self.assertEquals(r['resource_id'], self.resource_ids[i])
Example #5
0
    def test_get_resource_list(self):
        hs = HydroShare(prompt_auth=False)
        res_list = hs.getResourceList()

        for (i, r) in enumerate(res_list):
            self.assertEqual(r['resource_title'], self.resource_titles[i])
            self.assertEqual(r['resource_id'], self.resource_ids[i])
Example #6
0
def get_resource_list():
    hs = HydroShare()
    resource_list = []
    for resource in hs.getResourceList(types=['RasterResource']):
        res_name = '{} [creator: {}]'.format(resource['resource_title'], resource['creator'])
        resource_list.append((res_name, resource['resource_id']))

    return resource_list
    def test_get_resource_list_filter_date(self):
        hs = HydroShare()
        from_date = date(2015, 5, 20)
        res_list = hs.getResourceList(from_date=from_date)
        for (i, r) in enumerate(res_list):
            self.assertTrue(datetime.strptime(r['date_created'], '%m-%d-%Y').date() >= from_date)

        to_date = date(2015, 5, 21) # up to and including 5/21/2015
        res_list = hs.getResourceList(to_date=to_date)
        for (i, r) in enumerate(res_list):
            self.assertTrue(datetime.strptime(r['date_created'], '%m-%d-%Y').date() < to_date)

        from_date = date(2015, 5, 19)
        to_date = date(2015, 5, 22) # up to and including 5/21/2015
        res_list = hs.getResourceList(from_date=from_date, to_date=to_date)
        for (i, r) in enumerate(res_list):
            self.assertTrue(datetime.strptime(r['date_created'], '%m-%d-%Y').date() >= from_date)
            self.assertTrue(datetime.strptime(r['date_created'], '%m-%d-%Y').date() < to_date)
Example #8
0
def map(request):
    """
    Controller for the app home page.
    """
    hs = HydroShare()
    res_info_list = []
    i =0

    for resource in hs.getResourceList(types=['MODFLOWModelInstanceResource']):
        xml = hs.getScienceMetadata(resource['resource_id'])
        print "iteration number"
        print i
        dict = parse(xml)
        #takes xml turns into string then json dictionary object ^
        try:
            box = dict['rdf:RDF']['rdf:Description'][0]['dc:coverage'][1]['dcterms:box']['rdf:value']
            #['rdf:Description'][0]['dc:coverage'][0]['dcterms:box']['rdf:value']
            # ^From the dictionary I pull out the values I need or the data from the "coverage" part of the model
            list_points = box.split('; ')
            # ^ splits up box to produce a list containing: northlimit, eastlimit, southlimit, westlimit, units
            #   and porjection.
            temp_dict = {} #creates an empty dictionary
            for l in list_points:
                temp_list = l.split('=') # splits up the list even further into name and value ex: northlimit, 40.5033
                # print temp_list
                temp_dict[temp_list[0]] = temp_list[1]
            res_info = {}
            res_info["resource_id"]=resource['resource_id']
            res_info["box"]=temp_dict
            res_info_list.append(res_info)
            print res_info


        except Exception, e:
            pass
        try:
            box = dict['rdf:RDF']['rdf:Description'][0]['dc:coverage'][0]['dcterms:box']['rdf:value']
            # ['rdf:Description'][0]['dc:coverage'][0]['dcterms:box']['rdf:value']
            # ^From the dictionary I pull out the values I need or the data from the "coverage" part of the model
            list_points = box.split('; ')
            # ^ splits up box to produce a list containing: northlimit, eastlimit, southlimit, westlimit, units
            #   and porjection.
            temp_dict = {}  # creates an empty dictionary
            for l in list_points:
                temp_list = l.split('=')  # splits up the list even further into name and value ex: northlimit, 40.5033
                # print temp_list
                temp_dict[temp_list[0]] = temp_list[1]
            res_info = {}
            res_info["resource_id"] = resource['resource_id']
            res_info["box"] = temp_dict
            res_info_list.append(res_info)
            print res_info

        except Exception, e:
            pass
Example #9
0
    def test_get_resource_list(self):
        success_title = False
        success_id = False
        hs = HydroShare(hostname=self.url)

        res_list = hs.getResourceList()

        for (i, r) in enumerate(res_list):
            if (r['resource_title'] == self.a_Title):
                success_title = True
            if r['resource_id'] == self.a_resource_id:
                success_id = True
            if (success_id and success_title):
                break

        self.assertTrue(success_title, "title not found")
        self.assertTrue(success_id, "id not found")
def Test_All_Resources(request):
    try:
        start = time()
        res_count = 0
        num_success = 0
        num_errors = 0
        error_resource_list = []
        ACCEPTABLE_ERRORS_LIST = [
            'This resource is too large to open in HydroShare GIS',
            'There is no projection information associated with this resource',
            'Resource contains insufficient geospatial information'
        ]
        set_currently_testing(True)

        auth = HydroShareAuthBasic(username='******', password='******')
        hs = HydroShare(auth=auth)

        valid_res_types = [
            'GeographicFeatureResource', 'RasterResource',
            'RefTimeSeriesResource', 'GenericResource', 'ScriptResource'
        ]
        resource_list = hs.getResourceList(types=valid_res_types)
        num_resources = 0
        for res in hs.getResourceList(types=valid_res_types):
            if res['public']:
                num_resources += 1

        for res in resource_list:
            res_id = None
            try:
                if res['public']:
                    res_count += 1
                    res_id = res['resource_id']
                    print "Currently testing resource %s of %s: %s" % (
                        res_count, num_resources, res_id)
                    if res['resource_type'] == 'GenericResource':
                        response = get_res_files_list(hs, res_id)
                        if response['success']:
                            res_files_list = response['results'][
                                'generic_res_files_list']
                            num_files_failed = 0
                            for i, res_file in enumerate(res_files_list):
                                response = process_generic_res_file(
                                    hs, res_id, res_file,
                                    request.user.username, i)
                                if response['success']:
                                    pass
                                else:
                                    error_acceptable = False
                                    for error in ACCEPTABLE_ERRORS_LIST:
                                        if error in response['message']:
                                            error_acceptable = True
                                            break
                                    if error_acceptable:
                                        pass
                                    else:
                                        num_files_failed += 1
                                        error_resource_list.append(
                                            'https://www.hydroshare.org/resource/%s on file %s'
                                            % (res_id, res_file))
                                        print 'ERROR ENCOUNTERED:'
                                        print 'RES_ID: %s' % res_id
                                        print 'MESSAGE: %s' % response[
                                            'message']
                            if num_files_failed == 0:
                                num_success += 1
                            else:
                                num_errors += 1
                    else:
                        response = process_nongeneric_res(hs, res_id)
                        if response['success']:
                            num_success += 1
                        else:
                            error_acceptable = False
                            for error in ACCEPTABLE_ERRORS_LIST:
                                if error in response['message']:
                                    error_acceptable = True
                                    break
                            if error_acceptable:
                                num_success += 1
                            else:
                                num_errors += 1
                                error_resource_list.append(
                                    'https://www.hydroshare.org/resource/%s' %
                                    res_id)
                                print 'ERROR ENCOUNTERED:'
                                print 'RES_ID: %s' % res_id
                                print 'MESSAGE: %s' % response['message']
            except Exception as e:
                num_errors += 1
                error_resource_list.append(
                    'https://www.hydroshare.org/resource/%s' % res_id)
                print 'ERROR ENCOUNTERED:'
                print 'RES_ID: %s' % res_id
                print 'MESSAGE: %s' % str(e)

            print "%d%% complete" % (res_count * 100 / num_resources)

        elapsed = str(timedelta(seconds=time() - start))

        results = '''
        ALL TESTS COMPLETE!

        SUMMARY

            TIME ELAPSED: {0}
            TOTAL RESOURCES TESTED: {1}
            TOTAL FAILED: {2}
            PERCENT FAILED: {3}
            TOTAL SUCCESSFUL: {4}
            PERCENT SUCCESSFUL: {5}

        {6}
        {7}
        '''.format(
            elapsed, res_count, num_errors,
            '%d%%' % (num_errors * 100 / res_count), num_success,
            '%d%%' % (num_success * 100 / res_count),
            'LIST OF FAILED RESOURCES:' if len(error_resource_list) != 0 else
            'ALL TESTS PASSED!', '\n'.join(error_resource_list))

        print results
        email_admin('Test Results', custom_msg=results)
        context = {'results': '<br>'.join(results.split('\n'))}
        set_currently_testing(False)
        return render(request, 'hydroshare_gis/test-results.html', context)
    finally:
        set_currently_testing(False)
Example #11
0
def data(request):
    """
    Controller for the app home page.
    """
    # html = 'base html string'
    table = '<table style="width:100%">' \
           '<tr>' \
           '    <th colspan="3">Title</th>' \
           '</tr>' \
           '<tr>' \
           '    <td>Author</td>' \
           '<tr style="border-bottom:1px solid black"><td colspan="100%"></td>' \
           '<td><br></td>' \
           '</table>' \

    hs = HydroShare()
    temp = []
    for resource in hs.getResourceList(types=['MODFLOWModelInstanceResource']):
        # for each resource, extract values you want and append them to your html

        temp.append(resource['resource_id'])

        table += '<table style = "width:100%">'\
                 '</tr>' \
                 '<tr:nth-child(even) {' \
                 '  background-color: #33ccff;}>' \
                 '  <th colspan="3">'+resource['resource_title']+'</th>' \
                 '</tr>' \
                 '<tr>' \
                 '  <td>'+resource['creator']+'</td>' \
                 '</tr>' \
                 '<tr>' \
                 '  <td>'+resource['resource_id']+'</td>' \
                 '<tr>' \
                 '  <th>Model Data:</th>' \
                 '  <th>Date Last updated:</th>'\
                 '</tr>' \
                 '<tr>' \
                 '  <td><button type="button" onclick="download_hs_res(' + "'" +resource['resource_id'] + "'" + ')">Download Files</button></td>' \
                 '  <td>'+resource['date_last_updated']+'</td>' \
                 '<tr style="border-bottom:1px solid black"><td colspan="100%"></td>' \
                 '<td><br></td>' \
        #html += '<tr>' \
         #       '   <th>creator</th>' \
          #      '   <th>creator'
        # string in the format they need to be
        # html += '<tr><td>' + resource['creator'] + '</td>

        '''
        # Button explained

        Explination for the button
        The button's attribute is onclick, and it's value is download_hs_res(...between " "
        The value in the attribute is a function: download_hs_res
        The parameters for the function are: ' + "'" +resource['resource_id'] + "'" + '
        So when button is clicked on it will go to data.js and use the function.
            This function in particular calls for a function in the download-hs-res url
            This goes to app.py and uses the download_hs_res which is in the
            controllers.py
        '''


        print resource['creator']
        table += '</tr>'
        # print(resource)
    # after your loop, make sure to append to the html string variable the closing html tags
    # i.e. html += '</table>'

    context = {
        #'html': '<div><b>Hello World, It works!</b></div>',
        'html': table,

        #'temp': temp
        #'test': 1,
    }

    return render(request, 'modflow_cloud/data.html', context)
 def test_get_resource_list_filter_creator(self):
     hs = HydroShare()
     res_list = hs.getResourceList(creator='bmiles')
     for (i, r) in enumerate(res_list):
         self.assertEquals(r['creator'], 'bmiles')
Example #13
0
 def test_get_resource_list_filter_creator(self):
     hs = HydroShare(hostname=self.url)
     res_list = hs.getResourceList(creator=self.creator)
     for (i, r) in enumerate(res_list):
         self.assertEquals(r['creator'], self.creator)
 def test_get_resource_list_filter_type(self):
     hs = HydroShare()
     res_list = hs.getResourceList(types=('RasterResource',))
     for (i, r) in enumerate(res_list):
         self.assertEquals(r['resource_type'], 'RasterResource')
def Test_All_Resources(request):
    try:
        start = time()
        res_count = 0
        num_success = 0
        num_errors = 0
        error_resource_list = []
        ACCEPTABLE_ERRORS_LIST = ['This resource is too large to open in HydroShare GIS',
                                  'There is no projection information associated with this resource',
                                  'Resource contains insufficient geospatial information']
        set_currently_testing(True)

        auth = HydroShareAuthBasic(username='******', password='******')
        hs = HydroShare(auth=auth)

        valid_res_types = ['GeographicFeatureResource', 'RasterResource', 'RefTimeSeriesResource',
                           'GenericResource', 'ScriptResource']
        resource_list = hs.getResourceList(types=valid_res_types)
        num_resources = 0
        for res in hs.getResourceList(types=valid_res_types):
            if res['public']:
                num_resources += 1

        for res in resource_list:
            res_id = None
            try:
                if res['public']:
                    res_count += 1
                    res_id = res['resource_id']
                    print "Currently testing resource %s of %s: %s" % (res_count, num_resources, res_id)
                    if res['resource_type'] == 'GenericResource':
                        response = get_res_files_list(hs, res_id)
                        if response['success']:
                            res_files_list = response['results']['generic_res_files_list']
                            num_files_failed = 0
                            for i, res_file in enumerate(res_files_list):
                                response = process_generic_res_file(hs, res_id, res_file,
                                                                    request.user.username, i)
                                if response['success']:
                                    pass
                                else:
                                    error_acceptable = False
                                    for error in ACCEPTABLE_ERRORS_LIST:
                                        if error in response['message']:
                                            error_acceptable = True
                                            break
                                    if error_acceptable:
                                        pass
                                    else:
                                        num_files_failed += 1
                                        error_resource_list.append('https://www.hydroshare.org/resource/%s on file %s'
                                                                   % (res_id, res_file))
                                        print 'ERROR ENCOUNTERED:'
                                        print 'RES_ID: %s' % res_id
                                        print 'MESSAGE: %s' % response['message']
                            if num_files_failed == 0:
                                num_success += 1
                            else:
                                num_errors += 1
                    else:
                        response = process_nongeneric_res(hs, res_id)
                        if response['success']:
                            num_success += 1
                        else:
                            error_acceptable = False
                            for error in ACCEPTABLE_ERRORS_LIST:
                                if error in response['message']:
                                    error_acceptable = True
                                    break
                            if error_acceptable:
                                num_success += 1
                            else:
                                num_errors += 1
                                error_resource_list.append('https://www.hydroshare.org/resource/%s' % res_id)
                                print 'ERROR ENCOUNTERED:'
                                print 'RES_ID: %s' % res_id
                                print 'MESSAGE: %s' % response['message']
            except Exception as e:
                num_errors += 1
                error_resource_list.append('https://www.hydroshare.org/resource/%s' % res_id)
                print 'ERROR ENCOUNTERED:'
                print 'RES_ID: %s' % res_id
                print 'MESSAGE: %s' % str(e)

            print "%d%% complete" % (res_count * 100 / num_resources)

        elapsed = str(timedelta(seconds=time()-start))

        results = '''
        ALL TESTS COMPLETE!

        SUMMARY

            TIME ELAPSED: {0}
            TOTAL RESOURCES TESTED: {1}
            TOTAL FAILED: {2}
            PERCENT FAILED: {3}
            TOTAL SUCCESSFUL: {4}
            PERCENT SUCCESSFUL: {5}

        {6}
        {7}
        '''.format(
            elapsed,
            res_count,
            num_errors,
            '%d%%' % (num_errors * 100 / res_count),
            num_success,
            '%d%%' % (num_success * 100 / res_count),
            'LIST OF FAILED RESOURCES:' if len(error_resource_list) != 0 else 'ALL TESTS PASSED!',
            '\n'.join(error_resource_list)
        )

        print results
        email_admin('Test Results', custom_msg=results)
        context = {'results': '<br>'.join(results.split('\n'))}
        set_currently_testing(False)
        return render(request, 'hydroshare_gis/test-results.html', context)
    finally:
        set_currently_testing(False)
Example #16
0
 def test_get_resource_list_filter_creator(self):
     hs = HydroShare(prompt_auth=False)
     res_list = hs.getResourceList(creator='bmiles')
     for (i, r) in enumerate(res_list):
         self.assertEqual(r['creator'], 'bmiles')
Example #17
0
 def test_get_resource_list_filter_type(self):
     hs = HydroShare(prompt_auth=False)
     res_list = hs.getResourceList(types=('RasterResource', ))
     for (i, r) in enumerate(res_list):
         self.assertEqual(r['resource_type'], 'RasterResource')