예제 #1
0
def send_mail(sender, to, subject, body):
    if settings.running_on_test_server():
        print "SEND EMAIL"
        print 'to: ' + str(to)
        print 'Re: ' + subject
        print body
    else:
        mail.send_mail(sender, to, subject, body)
예제 #2
0
파일: places_db.py 프로젝트: wgilpin/shout
    def get_google_db_places(cls, lat, lng, name, radius):
        """
    do a google geo search
    :param lat: float
    :param lng: float
    :param name: string - to look for
    :param radius: int - search radius (m)
    :return: dict - {"item_count": int, "items": []}
    """
        results = {"item_count": 0, "items": []}
        try:
            # remove AND or & from name
            search_words = cls.get_word_list(name)
            search_text_or = "|".join(search_words)
            search_text_and = " ".join(search_words)
            escaped_name = "%s%%7C%s" % (urllib2.quote(search_text_or),
                                         urllib2.quote(search_text_and))
            url = ("https://maps.googleapis.com/maps/api/place/nearbysearch/"
                  "json?rankby=distance&types=%s&location=%f,%f&name=%s&sensor=false&key=%s")\
                  % \
                  (settings.config['place_types'],
                   lat,
                   lng,
                   escaped_name,
                   settings.config['google_api_key'] )
            addresses = []
            response = urllib2.urlopen(url, timeout=15)
            jsonResult = response.read()
            addressResult = json.loads(jsonResult)
            logging.info("get_google_db_places: Url=%s" % url)

        except Exception, e:
            if settings.running_on_test_server():
                # make up a test
                print "get_google_db_places OFFLINE - Making Up Fake Data"
                addressResult = {
                    'status':
                    'OK',
                    'results': [{
                        'formatted_address': '1 Crouch Hill, London',
                        'name': 'A Madeup Place',
                        'place_id': '0',
                        'geometry': {
                            'location': {
                                'lat': 54.0 + random(),
                                'lng': -(1.0 + random())
                            }
                        }
                    }]
                }
            else:
                logging_ext.error('get_google_db_places Exception in Load',
                                  exc_info=True)
                return None
예제 #3
0
파일: places_db.py 프로젝트: wgilpin/shout
 def log_to_console(cls, message):
     if settings.running_on_test_server():
         print datetime.now(), " LOG: ", message
     else:
         logging.info(message)
예제 #4
0
def load_data(wipe=False, section=None, Max=None):
    if not settings.running_on_test_server():
        return "Forbidden"
    result_strings = []
    if geo.geoCodeAddress("1 Crouch Hill, London"):
        # if we can geocode, we will - no fake
        fakeGeoCoder = None
    else:
        # if we cant geocode, use the fake one
        fakeGeoCoder = fakeGeoCode
    if section == "addresses":
        return add_addresses_to_db()
    else:
        if wipe:
            # wipe_table("User")
            wipe_table("Category")
            wipe_table("Item")
            wipe_table("Vote")
            print "wiped"
        if not section or section == 'user':
            for idx, usr in enumerate(Users):
                if Max:
                    if idx >= Max:
                        break
                user_name = usr[0]
                this_user = User.get_by_auth_id(user_name)
                if not this_user:
                    email = usr[1]
                    name = usr[2]
                    last_name = usr[3]
                    password = usr[4]

                    unique_properties = ['email_address']
                    this_user = User.create_user(user_name,
                                                 unique_properties,
                                                 email_address=email,
                                                 name=name,
                                                 password_raw=password,
                                                 last_name=last_name,
                                                 verified=False)
                    if not this_user[0]:  # user_data is a tuple
                        result_strings.append("ERROR - User: "******"User: "******"User exists: " + usr[0])
        a_sample_user = User.get_by_auth_id(
            Users[0][0])  # used for the owner of the records
        print "users ok"
        if not section or section == "category":
            for idx, cat in enumerate(Categories):
                if Max:
                    if idx >= Max:
                        break
                if models.Category.get_by_id(cat):
                    result_strings.append("Category exists: " + cat)
                else:
                    new_cat = models.Category(id=cat)
                    new_cat.title = cat
                    new_cat.put()
                    result_strings.append("Created: " + cat)

        print "category ok"
        if not section or section == "item":
            home = geo.LatLng(lat=51.57, lng=-0.13)
            for idx, item in enumerate(items_data_list):
                if Max:
                    if idx >= Max:
                        break
                it = models.Item.query(models.Item.place_name == item[0]).get()
                if it:
                    result_strings.append("Item exists: " + item[0])
                    it.category = models.Category.get_by_id(item[2]).key
                    it.save()
                else:
                    new_it = models.Item()
                    new_it.category = models.Category.get_by_id(item[2]).key
                    new_it.place_name = item[0]
                    lat_long = fakeGeoCode(
                    ) if fakeGeoCoder else geo.geoCodeAddress(item[1])
                    new_it.lat = lat_long['lat']
                    new_it.lng = lat_long['lng']
                    new_it.address = item[1]
                    new_it.owner = a_sample_user.key.id()
                    # new_it.descr = "blah"
                    new_it.geo_hash = geohash.encode(new_it.lat, new_it.lng)
                    img = models.DBImage()
                    detail = geo.getPlaceDetailFromGoogle(new_it)
                    remoteURL = detail['photo']
                    if remoteURL:
                        main_url = remoteURL % 250
                        data = urllib2.urlopen(main_url)
                        img.picture = db.Blob(data.read())
                        img.remoteURL = None
                        thumb_url = remoteURL % 65
                        thumb_data = urllib2.urlopen(thumb_url)
                        img.thumb = db.Blob(thumb_data.read())
                    img.put()
                    new_it.photo = img.key
                    new_it.telephone = detail['telephone']
                    new_it.put()
                    result_strings.append('Item: ' + item[0])

            print "items"
            # votes
            items = models.Item.query()
            i = 0
            for idx, vote_item in enumerate(items):
                if Max:
                    if idx >= Max:
                        break
                vote = models.Vote()
                vote_score = randint(0, 5)
                if vote_score == 0:
                    vote.stars = 0
                    vote.untried = True
                else:
                    vote.stars = vote_score
                    vote.untried = False
                vote.comment = "blah v" + str(i)
                vote.voter = a_sample_user.key.integer_id()
                vote.item = vote_item.key
                vote.cuisine = vote_item.category
                vote.put()
                i += 1
            result_strings.append("Votes")
            print "votes"

        if not section or section == 'friends':
            for idx, pair in enumerate(FriendsList):
                if Max:
                    if idx >= Max:
                        break
                left = User.get_by_auth_id(pair[0])
                right = User.get_by_auth_id(pair[1])
                left_prof = left.profile()
                models.Friends.addFriends(left.get_id(), right.get_id())
                result_strings.append("Friends %s - %s" % (pair[0], pair[1]))
        print "friends"
        return result_strings
예제 #5
0
 def error(cls, message, exc_info=False):
     if settings.running_on_test_server():
         print message, sys.exc_info()
     else:
         logging.error(message, exc_info=exc_info)
예제 #6
0
 def log_to_console(cls, message):
     if settings.running_on_test_server():
         print message