Exemple #1
0
    def test_2012ct_bad_location(self):
        with open('test_data/usfirst_html/usfirst_event_details_2012ct.html', 'r') as f:
            event, _ = UsfirstEventDetailsParser.parse(f.read())

        event['location'] = "somewhere on mars"
        self.assertEqual(LocationHelper.get_lat_lng(event['location']), None)
        self.assertEqual(LocationHelper.get_timezone_id(event['location']), None)
    def test_2013flbr(self):
        with open('test_data/usfirst_html/usfirst_event_details_2013flbr.html',
                  'r') as f:
            event, _ = UsfirstEventDetailsParser.parse(f.read())

        self.assertEqual(LocationHelper.get_lat_lng(event['location']),
                         (26.1224386, -80.13731740000001))
        self.assertEqual(LocationHelper.get_timezone_id(event['location']),
                         'America/New_York')
    def test_2005is(self):
        with open('test_data/usfirst_html/usfirst_event_details_2005is.html',
                  'r') as f:
            event, _ = UsfirstEventDetailsParser.parse(f.read())

        self.assertEqual(LocationHelper.get_lat_lng(event['location']),
                         (32.7940463, 34.989571))
        self.assertEqual(LocationHelper.get_timezone_id(event['location']),
                         'Asia/Jerusalem')
    def test_2005or(self):
        with open('test_data/usfirst_html/usfirst_event_details_2005or.html',
                  'r') as f:
            event, _ = UsfirstEventDetailsParser.parse(f.read())

        self.assertEqual(LocationHelper.get_lat_lng(event['location']),
                         (45.5230622, -122.6764816))
        self.assertEqual(LocationHelper.get_timezone_id(event['location']),
                         'America/Los_Angeles')
    def test_1997il(self):
        with open('test_data/usfirst_html/usfirst_event_details_1997il.html',
                  'r') as f:
            event, _ = UsfirstEventDetailsParser.parse(f.read())

        self.assertEqual(LocationHelper.get_lat_lng(event['location']),
                         (41.8781136, -87.6297982))
        self.assertEqual(LocationHelper.get_timezone_id(event['location']),
                         'America/Chicago')
    def test_2012ct(self):
        with open('test_data/usfirst_html/usfirst_event_details_2012ct.html',
                  'r') as f:
            event, _ = UsfirstEventDetailsParser.parse(f.read())

        self.assertEqual(LocationHelper.get_lat_lng(event['location']),
                         (41.76371109999999, -72.6850932))
        self.assertEqual(LocationHelper.get_timezone_id(event['location']),
                         'America/New_York')
    def test_2001sj(self):
        with open('test_data/usfirst_html/usfirst_event_details_2001ca2.html',
                  'r') as f:
            event, _ = UsfirstEventDetailsParser.parse(f.read())

        self.assertEqual(LocationHelper.get_lat_lng(event['location']),
                         (37.3382082, -121.8863286))
        self.assertEqual(LocationHelper.get_timezone_id(event['location']),
                         'America/Los_Angeles')
    def test_2013casj(self):
        with open('test_data/usfirst_html/usfirst_event_details_2013casj.html',
                  'r') as f:
            event, _ = UsfirstEventDetailsParser.parse(f.read())

        location = LocationHelper.get_lat_lng(event['location'])
        self.assertAlmostEqual(location[0], 37.3382082, places=6)
        self.assertAlmostEqual(location[1], -121.8863286, places=6)
        self.assertEqual(LocationHelper.get_timezone_id(event['location']),
                         'America/Los_Angeles')
Exemple #9
0
    def test_2005is(self):
        with open('test_data/usfirst_html/usfirst_event_details_2005is.html', 'r') as f:
            event, _ = UsfirstEventDetailsParser.parse(f.read())

        location = None
        for _ in xrange(5):
            location = LocationHelper.get_lat_lng(event['location'])
            if location:
                break
        self.assertAlmostEqual(location[0], 32.7940463, places=6)
        self.assertAlmostEqual(location[1], 34.989571, places=6)
        self.assertEqual(LocationHelper.get_timezone_id(event['location']), 'Asia/Jerusalem')
Exemple #10
0
    def test_2005or(self):
        with open('test_data/usfirst_html/usfirst_event_details_2005or.html', 'r') as f:
            event, _ = UsfirstEventDetailsParser.parse(f.read())

        location = None
        for _ in xrange(5):
            location = LocationHelper.get_lat_lng(event['location'])
            if location:
                break
        self.assertAlmostEqual(location[0], 45.5230622, places=6)
        self.assertAlmostEqual(location[1], -122.6764816, places=6)
        self.assertEqual(LocationHelper.get_timezone_id(event['location']), 'America/Los_Angeles')
Exemple #11
0
    def test_2012ct(self):
        with open('test_data/usfirst_html/usfirst_event_details_2012ct.html', 'r') as f:
            event, _ = UsfirstEventDetailsParser.parse(f.read())

        location = None
        for _ in xrange(5):
            location = LocationHelper.get_lat_lng(event['location'])
            if location:
                break
        self.assertAlmostEqual(location[0], 41.76371109999999, places=6)
        self.assertAlmostEqual(location[1], -72.6850932, places=6)
        self.assertEqual(LocationHelper.get_timezone_id(event['location']), 'America/New_York')
Exemple #12
0
    def test_2013flbr(self):
        with open('test_data/usfirst_html/usfirst_event_details_2013flbr.html', 'r') as f:
            event, _ = UsfirstEventDetailsParser.parse(f.read())

        location = None
        for _ in xrange(5):
            location = LocationHelper.get_lat_lng(event['location'])
            if location:
                break
        self.assertAlmostEqual(location[0], 26.1224386, places=6)
        self.assertAlmostEqual(location[1], -80.13731740000001, places=6)
        self.assertEqual(LocationHelper.get_timezone_id(event['location']), 'America/New_York')
Exemple #13
0
    def test_1997il(self):
        with open('test_data/usfirst_html/usfirst_event_details_1997il.html', 'r') as f:
            event, _ = UsfirstEventDetailsParser.parse(f.read())

        location = None
        for _ in xrange(5):
            location = LocationHelper.get_lat_lng(event['location'])
            if location:
                break
        self.assertAlmostEqual(location[0], 41.8781136, places=6)
        self.assertAlmostEqual(location[1], -87.6297982, places=6)
        self.assertEqual(LocationHelper.get_timezone_id(event['location']), 'America/Chicago')
    def test_2013flbr(self):
        with open('test_data/usfirst_html/usfirst_event_details_2013flbr.html', 'r') as f:
            event, _ = UsfirstEventDetailsParser.parse(f.read())

        self.assertEqual(LocationHelper.get_lat_lng(event['location']), (26.1224386, -80.13731740000001))
        self.assertEqual(LocationHelper.get_timezone_id(event['location']), 'America/New_York')
    def test_2005is(self):
        with open('test_data/usfirst_html/usfirst_event_details_2005is.html', 'r') as f:
            event, _ = UsfirstEventDetailsParser.parse(f.read())

        self.assertEqual(LocationHelper.get_lat_lng(event['location']), (32.7940463, 34.989571))
        self.assertEqual(LocationHelper.get_timezone_id(event['location']), 'Asia/Jerusalem')
Exemple #16
0
    def _render(self):
        year, location, range_limit, search_type, page = self._get_params()

        num_results = 0
        results = []
        distances = []
        if location:
            lat_lon = LocationHelper.get_lat_lng(location)
            if lat_lon:
                lat, lon = lat_lon

                dist_expr = 'distance(location, geopoint({}, {}))'.format(
                    lat, lon)
                query_string = '{} < {} AND year={}'.format(
                    dist_expr, range_limit * self.METERS_PER_MILE, year)

                offset = self.PAGE_SIZE * page

                query = search.Query(
                    query_string=query_string,
                    options=search.QueryOptions(
                        limit=self.PAGE_SIZE,
                        offset=offset,
                        sort_options=search.SortOptions(expressions=[
                            search.SortExpression(
                                expression=dist_expr,
                                direction=search.SortExpression.ASCENDING)
                        ]),
                        returned_expressions=[
                            search.FieldExpression(name='distance',
                                                   expression=dist_expr)
                        ],
                    ))
                if search_type == 'teams':
                    search_index = search.Index(name="teamLocation")
                else:
                    search_index = search.Index(name="eventLocation")

                docs = search_index.search(query)
                num_results = docs.number_found
                distances = {}
                keys = []
                for result in docs.results:
                    model_key = result.doc_id
                    if '_' in model_key:
                        model_key = model_key.split('_')[0]

                    distances[model_key] = result.expressions[
                        0].value / self.METERS_PER_MILE
                    if search_type == 'teams':
                        keys.append(ndb.Key('Team', model_key))
                    else:
                        keys.append(ndb.Key('Event', model_key))

                result_futures = ndb.get_multi_async(keys)
                results = [
                    result_future.get_result()
                    for result_future in result_futures
                ]

        self.template_values.update({
            'valid_years': self.VALID_YEARS,
            'valid_ranges': self.VALID_RANGES,
            'page_size': self.PAGE_SIZE,
            'page': page,
            'year': year,
            'location': location,
            'range_limit': range_limit,
            'search_type': search_type,
            'num_results': num_results,
            'results': results,
            'distances': distances,
        })

        return jinja2_engine.render('nearby.html', self.template_values)
    def test_2005or(self):
        with open('test_data/usfirst_html/usfirst_event_details_2005or.html', 'r') as f:
            event, _ = UsfirstEventDetailsParser.parse(f.read())

        self.assertEqual(LocationHelper.get_lat_lng(event['location']), (45.5230622, -122.6764816))
        self.assertEqual(LocationHelper.get_timezone_id(event['location']), 'America/Los_Angeles')
    def test_1997il(self):
        with open('test_data/usfirst_html/usfirst_event_details_1997il.html', 'r') as f:
            event, _ = UsfirstEventDetailsParser.parse(f.read())

        self.assertEqual(LocationHelper.get_lat_lng(event['location']), (41.8781136, -87.6297982))
        self.assertEqual(LocationHelper.get_timezone_id(event['location']), 'America/Chicago')
    def test_2012ct(self):
        with open('test_data/usfirst_html/usfirst_event_details_2012ct.html', 'r') as f:
            event, _ = UsfirstEventDetailsParser.parse(f.read())

        self.assertEqual(LocationHelper.get_lat_lng(event['location']), (41.76371109999999, -72.6850932))
        self.assertEqual(LocationHelper.get_timezone_id(event['location']), 'America/New_York')
    def test_2002sj(self):
        with open('test_data/usfirst_html/usfirst_event_details_2002sj.html', 'r') as f:
            event, _ = UsfirstEventDetailsParser.parse(f.read())

        self.assertEqual(LocationHelper.get_lat_lng(event['location']), (37.3382082, -121.8863286))
        self.assertEqual(LocationHelper.get_timezone_id(event['location']), 'America/Los_Angeles')
Exemple #21
0
    def _render(self):
        year, location, range_limit, search_type, page = self._get_params()

        num_results = 0
        results = []
        distances = []
        if location:
            lat_lon = LocationHelper.get_lat_lng(location)
            if lat_lon:
                lat, lon = lat_lon

                dist_expr = 'distance(location, geopoint({}, {}))'.format(lat, lon)
                if search_type == 'teams':
                    query_string = '{} < {}'.format(dist_expr, range_limit * self.METERS_PER_MILE)
                else:
                    query_string = '{} < {} AND year={}'.format(dist_expr, range_limit * self.METERS_PER_MILE, year)

                offset = self.PAGE_SIZE * page

                query = search.Query(
                    query_string=query_string,
                    options=search.QueryOptions(
                        limit=self.PAGE_SIZE,
                        offset=offset,
                        sort_options=search.SortOptions(
                            expressions=[
                                search.SortExpression(
                                    expression=dist_expr,
                                    direction=search.SortExpression.ASCENDING
                                )
                            ]
                        ),
                        returned_expressions=[
                            search.FieldExpression(
                                name='distance',
                                expression=dist_expr
                            )
                        ],
                    )
                )
                if search_type == 'teams':
                    search_index = search.Index(name="teamLocation")
                else:
                    search_index = search.Index(name="eventLocation")

                docs = search_index.search(query)
                num_results = docs.number_found
                distances = {}
                keys = []
                event_team_count_futures = {}
                for result in docs.results:
                    distances[result.doc_id] = result.expressions[0].value / self.METERS_PER_MILE
                    if search_type == 'teams':
                        event_team_count_futures[result.doc_id] = EventTeam.query(
                            EventTeam.team == ndb.Key('Team', result.doc_id),
                            EventTeam.year == year).count_async(limit=1, keys_only=True)
                        keys.append(ndb.Key('Team', result.doc_id))
                    else:
                        keys.append(ndb.Key('Event', result.doc_id))

                result_futures = ndb.get_multi_async(keys)

                if search_type == 'teams':
                    results = []
                    for result_future, team_key in zip(result_futures, keys):
                        if event_team_count_futures[team_key.id()].get_result() != 0:
                            results.append(result_future.get_result())

                else:
                    results = [result_future.get_result() for result_future in result_futures]

        self.template_values.update({
            'valid_years': self.VALID_YEARS,
            'valid_ranges': self.VALID_RANGES,
            'page_size': self.PAGE_SIZE,
            'page': page,
            'year': year,
            'location': location,
            'range_limit': range_limit,
            'search_type': search_type,
            'num_results': num_results,
            'results': results,
            'distances': distances,
        })

        return jinja2_engine.render('nearby.html', self.template_values)