Ejemplo n.º 1
0
 def insert_ft(self):
     """
         Añade la sugerencia a la tabla en fusiontables
         
         En caso de fallo, se guarda el punto en _Do_later_ft,
         para intentar añadirlo luego
     """
     if self._is_public():
         from mapsServices.fusiontable import ftclient, sqlbuilder
         from django.conf import settings
         from georemindme.models_utils import _Do_later_ft
         try:
             ftclient = ftclient.OAuthFTClient()
             import unicodedata
             name = unicodedata.normalize('NFKD', self.name).encode('ascii','ignore')
             ftclient.query(sqlbuilder.SQL().insert(
                                                     settings.FUSIONTABLES['TABLE_SUGGS'],
                                                     {'name': name,
                                                     'location': '%s,%s' % (self.poi.location.lat, self.poi.location.lon),
                                                     'sug_id': self.id,
                                                     'modified': self.modified.isoformat(),
                                                     'created': self.created.isoformat(),
                                                     'relevance': self._calc_relevance(),
                                                      }
                                                    )
                            )
             delete = _Do_later_ft().get_by_key_name('_do_later_%s' % self.id)
             if delete is not None:
                     delete.delete()
         except Exception, e:  # Si falla, se guarda para intentar añadir mas tarde
             import logging
             logging.error('ERROR FUSIONTABLES new suggestion %s: %s' % (self.id, e))
Ejemplo n.º 2
0
 def insert_ft(self):
     """
         Añade la sugerencia a la tabla en fusiontables
         
         En caso de fallo, se guarda el punto en _Do_later_ft,
         para intentar añadirlo luego
     """
     if self._is_public():
         from mapsServices.fusiontable import ftclient, sqlbuilder
         from django.conf import settings
         from georemindme.models_utils import _Do_later_ft
         try:
             ftclient = ftclient.OAuthFTClient()
             import unicodedata
             name = unicodedata.normalize('NFKD', self.name).encode(
                 'ascii', 'ignore')
             ftclient.query(sqlbuilder.SQL().insert(
                 settings.FUSIONTABLES['TABLE_SUGGS'], {
                     'name':
                     name,
                     'location':
                     '%s,%s' %
                     (self.poi.location.lat, self.poi.location.lon),
                     'sug_id':
                     self.id,
                     'modified':
                     self.modified.isoformat(),
                     'created':
                     self.created.isoformat(),
                     'relevance':
                     self._calc_relevance(),
                 }))
             delete = _Do_later_ft().get_by_key_name('_do_later_%s' %
                                                     self.id)
             if delete is not None:
                 delete.delete()
         except Exception, e:  # Si falla, se guarda para intentar añadir mas tarde
             import logging
             logging.error('ERROR FUSIONTABLES new suggestion %s: %s' %
                           (self.id, e))
Ejemplo n.º 3
0
 def update_ft(self):
     if self._is_public():
         from mapsServices.fusiontable import ftclient, sqlbuilder
         from django.conf import settings
         from georemindme.models_utils import _Do_later_ft
         try:
             ftclient = ftclient.OAuthFTClient()
             rowid = ftclient.query(sqlbuilder.SQL().select(
                                                     settings.FUSIONTABLES['TABLE_SUGGS'],
                                                     ['rowid'],
                                                     'sug_id = %d' % self.id
                                                     )
                                                    )
             rowid = rowid.splitlines()
             if len(rowid) == 1:
                 return self.insert_ft()
             del rowid[0]
             for r in rowid:
                 r = int(r)
                 import unicodedata
                 name = unicodedata.normalize('NFKD', self.name).encode('ascii','ignore')
                 ftclient.query(sqlbuilder.SQL().update(
                                                         settings.FUSIONTABLES['TABLE_SUGGS'],
                                                         ['name', 'location', 'modified', 'relevance'],
                                                         [
                                                          name,
                                                          '%s,%s' % (self.poi.location.lat, self.poi.location.lon),
                                                          self.modified.isoformat(),
                                                          str(self._calc_relevance()),
                                                         ],
                                                         r
                                                        )
                                )
                 delete = _Do_later_ft().get_by_key_name('_do_later_%s' % self.id)
                 if delete is not None:
                     delete.delete()
         except Exception, e:  # Si falla, se guarda para intentar añadir mas tarde
             import logging
             logging.error('ERROR FUSIONTABLES update suggestion %s: %s' % (self.id, e))
Ejemplo n.º 4
0
 def update_ft(self):
     if self._is_public():
         from mapsServices.fusiontable import ftclient, sqlbuilder
         from django.conf import settings
         from georemindme.models_utils import _Do_later_ft
         try:
             ftclient = ftclient.OAuthFTClient()
             rowid = ftclient.query(sqlbuilder.SQL().select(
                 settings.FUSIONTABLES['TABLE_SUGGS'], ['rowid'],
                 'sug_id = %d' % self.id))
             rowid = rowid.splitlines()
             if len(rowid) == 1:
                 return self.insert_ft()
             del rowid[0]
             for r in rowid:
                 r = int(r)
                 import unicodedata
                 name = unicodedata.normalize('NFKD', self.name).encode(
                     'ascii', 'ignore')
                 ftclient.query(sqlbuilder.SQL().update(
                     settings.FUSIONTABLES['TABLE_SUGGS'],
                     ['name', 'location', 'modified', 'relevance'], [
                         name,
                         '%s,%s' %
                         (self.poi.location.lat, self.poi.location.lon),
                         self.modified.isoformat(),
                         str(self._calc_relevance()),
                     ], r))
                 delete = _Do_later_ft().get_by_key_name('_do_later_%s' %
                                                         self.id)
                 if delete is not None:
                     delete.delete()
         except Exception, e:  # Si falla, se guarda para intentar añadir mas tarde
             import logging
             logging.error('ERROR FUSIONTABLES update suggestion %s: %s' %
                           (self.id, e))
Ejemplo n.º 5
0
 def get_nearest(self, location, radius = 5000):
     if not isinstance(location, db.GeoPt):
         location = db.GeoPt(location)
     from mapsServices.fusiontable import ftclient, sqlbuilder
     
     ftclient = ftclient.OAuthFTClient()
     from django.conf import settings as __web_settings # parche hasta conseguir que se cachee variable global
     query = ftclient.query(sqlbuilder.SQL().select(__web_settings.FUSIONTABLES['TABLE_PLACES'], cols=['place_id'],
                                            condition = 'ST_INTERSECTS (location, CIRCLE(LATLNG (%s), %s))' % (location, radius)
                                            )
                    )
     results = query.splitlines()
     del results[0]  #  quitar la primera linea con el nombre de la columna
     places = [db.Key.from_path(self._klass.kind(), result) for result in results] # construir todas las keys para consultar en bach
     places = db.get(places)
     from georemindme.funcs import prefetch_refprops
     places = prefetch_refprops(places, self._klass.user)
     return places
Ejemplo n.º 6
0
    def get_nearest(self, location, radius=5000):
        if not isinstance(location, db.GeoPt):
            location = db.GeoPt(location)
        from mapsServices.fusiontable import ftclient, sqlbuilder

        ftclient = ftclient.OAuthFTClient()
        from django.conf import settings as __web_settings  # parche hasta conseguir que se cachee variable global
        query = ftclient.query(sqlbuilder.SQL().select(
            __web_settings.FUSIONTABLES['TABLE_PLACES'],
            cols=['place_id'],
            condition='ST_INTERSECTS (location, CIRCLE(LATLNG (%s), %s))' %
            (location, radius)))
        results = query.splitlines()
        del results[0]  #  quitar la primera linea con el nombre de la columna
        places = [
            db.Key.from_path(self._klass.kind(), result) for result in results
        ]  # construir todas las keys para consultar en bach
        places = db.get(places)
        from georemindme.funcs import prefetch_refprops
        places = prefetch_refprops(places, self._klass.user)
        return places
Ejemplo n.º 7
0
 def get_nearest(self, location, radius=5000, querier=None):
     if not isinstance(location, db.GeoPt):
         location = db.GeoPt(location)
     import memcache
     client = memcache.mem.Client()
     if querier is not None and querier.is_authenticated():
         sugs = client.get('%ssug_nearest%s,%s_%s' % (
             memcache.version,
             location.lat,
             location.lon,
             querier.username,
         ))
     else:
         sugs = client.get('%ssug_nearest%s,%s' %
                           (memcache.version, location.lat, location.lon))
     if sugs is None:
         from mapsServices.fusiontable import ftclient, sqlbuilder
         ftclient = ftclient.OAuthFTClient()
         from django.conf import settings as __web_settings  # parche hasta conseguir que se cachee variable global
         query = ftclient.query(sqlbuilder.SQL().select(
             __web_settings.FUSIONTABLES['TABLE_SUGGS'],
             cols=['sug_id'],
             condition=
             'ST_INTERSECTS (location, CIRCLE(LATLNG (%s), %s)) ORDER BY relevance DESC LIMIT 50'
             % (location, radius)))
         results = query.splitlines()
         if len(results) == 1:
             return []
         del results[
             0]  #  quitar la primera linea con el nombre de la columna
         try:
             sugs = [
                 db.Key.from_path(self._klass.kind(), int(result))
                 for result in results
             ]  # construir todas las keys para consultar en bach
         except:
             return []
         sugs = db.get(sugs)
         sugs = filter(None, sugs)
         if querier is not None and querier.is_authenticated():
             sugs = filter(
                 lambda x: x.__class__.user.get_value_for_datastore(x) !=
                 querier.key(), sugs)
         from georemindme.funcs import prefetch_refprops
         prefetch_refprops(sugs, Suggestion.user, Suggestion.poi)
         sugs = sorted(sugs,
                       key=lambda x: x._calc_relevance(),
                       reverse=True)
         sugs = [{
             'id': sug.key().id(),
             'slug': sug.slug,
             'username': sug.user.username,
             'name': sug.name,
             'address': sug.poi.address,
             'description': sug.description,
             'poi': {
                 'lat': sug.poi.location.lat,
                 'lon': sug.poi.location.lon,
                 'id': sug.poi.id,
             },
             'created': sug.created,
             'modified': sug.modified,
         } for sug in sugs]
         if querier is not None and querier.is_authenticated():
             client.set(
                 '%ssug_nearest%s,%s_%s' % (
                     memcache.version,
                     location.lat,
                     location.lon,
                     querier.username,
                 ), sugs, 112)
         else:
             client.set(
                 '%ssug_nearest%s,%s' %
                 (memcache.version, location.lat, location.lon), sugs, 112)
         # FIXME : DEBERIA USARSE CAS EN VEZ DE SET EN MEMCACHE
     return sugs