Esempio n. 1
0
def club_class_day_page(request, clubname, day, classid=None):
    club = _find_club(clubname)
    if club is None:
        raise Http404("Could not find the club")

    if classid:
        try:
            class_ = ClubClass.objects.get(pk=classid)
        except ClubClass.DoesNotExist:
            # probably a deleted class
            raise Http404("Class by that ID does not exist")
        classes = ClubClass.objects.filter(
            club=class_.club, day=class_.day, address1=class_.address1, address2=class_.address2
        )

    else:
        # The desperate way of doing it
        classes = ClubClass.objects.filter(club=club, day__iexact=day).order_by("start_time")

    try:
        first_class = classes[0]
        instructor = Instructor.objects.get(pk=first_class.club.head_instructor.id)
        address1 = first_class.address1
        address2 = first_class.address2
        address3 = first_class.address3
        address4 = first_class.address4
        address5 = first_class.address5

        if valid_uk_postcode(address5.strip()):
            post_code = format_uk_postcode(address5.strip())
        elif address4 == "Sweden":
            # Gabor
            post_code = "%s, %s" % (address3, address4)
        elif address5 == "Ireland" or address3.startswith("Dublin"):
            post_code = "%s, %s, Ireland" % (address2, address3)
        elif valid_uk_postcode(address4.strip()):  # eg. FWC Kensington & Chelsea
            post_code = format_uk_postcode(address4.strip())
        else:
            post_code = None
            print repr(address1)
            print repr(address2)
            print repr(address3)
            print repr(address4)
            print repr(address5)

        if post_code:
            map_link = "%smap/?" % request.build_absolute_uri()
            # print repr(post_code)
            map_link += urllib.urlencode(dict(q=post_code))
        else:
            map_link = first_class.map_link.strip()
            if not map_link.startswith("http"):
                map_link = "http://%s" % map_link

    except IndexError:
        # if this happens, there are no classes on this day and that can happen
        # if you go to a day that no longer exists (stale URL).
        first_class = None

    return _render("club_class_day.html", locals(), request)
Esempio n. 2
0
def _address_list_to_geopoint(address_bits):
    address_bits = [x.strip() for x in address_bits if x.strip()]
    cache_key = '|'.join([x.replace(' ','') for x in address_bits])
    in_cache = cache.get(cache_key)

    if in_cache is None:
        # first find out if one of the items is a UK postcode
        address_search = None
        for bit in address_bits:
            if valid_uk_postcode(bit):
                address_search = format_uk_postcode(bit)

        if not address_search:
            if FIND_ONLY_UK_POSTCODES:
                raise AddressNotFound, ','.join(address_bits)
            else:
                address_search = ', '.join(address_bits[2:])

        try:
            place, (lat, lng) = geopy_geocode(address_search,
                                              settings.GOOGLEMAPS_API_KEY)
        except ValueError, msg:
            raise AddressNotFound, address_search

        #in_cache = (lat, lng)
        in_cache = (lng, lat)

        # save it in cache
        cache.set(cache_key, in_cache, 3600*24) # 1 day, make it a month?