Ejemplo n.º 1
0
 def prefetch_timeline(entities):
     # from http://blog.notdot.net/2010/01/ReferenceProperty-prefetching-in-App-Engine
     """
         Carga todos los timelines apuntados por _Notifications
         de una sola vez
     """
     from models_acc import UserTimelineSuggest, UserTimeline
     from geovote.models import Comment, Vote
     from geolist.models import List, ListSuggestion
     from geoalert.models import Event, Suggestion
     ref_keys = [x['timeline'] for x in entities]
     timelines = model_plus.get(set(ref_keys))
     timelines = filter(None, timelines)
     # precargar las referencias
     timelines = model_plus.prefetch(timelines, UserTimeline.instance, UserTimeline.user)
     from helpers_acc import _load_ref_instances
     return timelines, _load_ref_instances(timelines)
Ejemplo n.º 2
0
 def prefetch_timeline(entities):
     # from http://blog.notdot.net/2010/01/ReferenceProperty-prefetching-in-App-Engine
     """
         Carga todos los timelines apuntados por _Notifications
         de una sola vez
     """
     from models_acc import UserTimelineSuggest, UserTimeline
     from geovote.models import Comment, Vote
     from geolist.models import List, ListSuggestion
     from geoalert.models import Event, Suggestion
     ref_keys = [x['timeline'] for x in entities]
     timelines = model_plus.get(set(ref_keys))
     timelines = filter(None, timelines)
     # precargar las referencias
     timelines = model_plus.prefetch(timelines, UserTimeline.instance,
                                     UserTimeline.user)
     from helpers_acc import _load_ref_instances
     return timelines, _load_ref_instances(timelines)
Ejemplo n.º 3
0
 def get_activity_timeline(self, query_id=None):
     from models_acc import UserTimelineSystem, UserTimeline, UserTimelineFollowersIndex, UserTimelineSuggest
     from geovote.models import Comment, Vote
     from geoalert.models import Event
     from geolist.models import List
     # definir las consultas
     query_chrono = UserTimelineFollowersIndex.all().filter(
         'followers =', self.key()).order('-created')
     query_activity = UserTimelineSystem.all().filter(
         'user ='******'visible =', True).order('-modified')
     # recuperar cursores
     if query_id is not None and len(query_id) >= 2:
         cursor_chronology = query_id[0]
         cursor_activity = query_id[1]
         query_chrono = query_chrono.with_cursor(
             start_cursor=cursor_chronology)
         query_activity = query_activity.with_cursor(
             start_cursor=cursor_activity)
     else:
         cursor_activity = None
         cursor_chronology = None
     # let's go!
     timeline = []
     timeline_chrono = []
     activity_async = query_activity.run(
         config=datastore_query.QueryOptions(limit=TIMELINE_PAGE_SIZE))
     chrono_async = query_chrono.run(config=datastore_query.QueryOptions(
         limit=TIMELINE_PAGE_SIZE))
     _go_chrono = True
     chrono = None
     for activity_timeline in activity_async:
         while _go_chrono:
             if len(timeline) + len(timeline_chrono) >= TIMELINE_PAGE_SIZE:
                 _go_chrono = False
                 break
             if chrono is None:
                 try:
                     chrono = chrono_async.next()
                 except:
                     _go_chrono = False
                     break
             if chrono is not None and chrono.created > activity_timeline.modified:
                 timeline_chrono.append(chrono)
                 chrono = None
             else:
                 break
         timeline.append(activity_timeline)
         if len(timeline) + len(timeline_chrono) >= TIMELINE_PAGE_SIZE:
             break
     # generar timeline
     timeline_chrono = fetch_parents(timeline_chrono)
     timeline = prefetch_refprops(timeline, UserTimeline.user)
     timeline_chrono = prefetch_refprops(timeline_chrono,
                                         UserTimeline.instance,
                                         UserTimeline.user)
     timeline.extend(timeline_chrono)
     #seguimos cargando por lotes todas las referencias
     from helpers_acc import _load_ref_instances
     instances = _load_ref_instances(timeline)
     timeline = [{
         'id':
         int(activity_timeline.id),
         'created':
         activity_timeline.created,
         'modified':
         activity_timeline.modified,
         'msg':
         activity_timeline.msg,
         'username':
         activity_timeline.user.username,
         'msg_id':
         activity_timeline.msg_id,
         'instance':
         instances.get(
             UserTimeline.instance.get_value_for_datastore(
                 activity_timeline), activity_timeline.instance),
         'has_voted':
         Vote.objects.user_has_voted(self, activity_timeline.instance.key())
         if activity_timeline.instance is not None else None,
         'vote_counter':
         Vote.objects.get_vote_counter(activity_timeline.instance.key())
         if activity_timeline.instance is not None else None,
         'comments':
         Comment.objects.get_by_instance(activity_timeline.instance,
                                         querier=self),
         'list':
         instances.get(
             UserTimelineSuggest.list_id.get_value_for_datastore(
                 activity_timeline), activity_timeline.list_id)
         if isinstance(activity_timeline, UserTimelineSuggest) else None,
         'status':
         activity_timeline.status
         if hasattr(activity_timeline, 'status') else None,
         'is_private':
         True,
         'user_follower':
         instances.get(
             UserTimeline.instance.get_value_for_datastore(
                 activity_timeline),
             activity_timeline.instance).has_follower(self) if hasattr(
                 instances.get(
                     UserTimeline.instance.get_value_for_datastore(
                         activity_timeline), activity_timeline.instance),
                 'has_follower') else None,
     } for activity_timeline in timeline]
     from operator import itemgetter
     timeline_sorted = sorted(timeline,
                              key=itemgetter('modified'),
                              reverse=True)
     chronology = [[query_chrono.cursor(),
                    query_activity.cursor()], timeline_sorted]
     return chronology
Ejemplo n.º 4
0
 def get_activity_timeline(self, query_id=None):
     from models_acc import UserTimelineSystem, UserTimeline, UserTimelineFollowersIndex, UserTimelineSuggest
     from geovote.models import Comment, Vote
     from geoalert.models import Event
     from geolist.models import List
     # definir las consultas
     query_chrono = UserTimelineFollowersIndex.all().filter('followers =', self.key()).order('-created')
     query_activity = UserTimelineSystem.all().filter('user ='******'visible =', True).order('-modified')
     # recuperar cursores
     if query_id is not None and len(query_id)>=2:
         cursor_chronology = query_id[0]
         cursor_activity = query_id[1]
         query_chrono = query_chrono.with_cursor(start_cursor=cursor_chronology)
         query_activity = query_activity.with_cursor(start_cursor=cursor_activity)
     else:
         cursor_activity = None
         cursor_chronology = None
     # let's go!
     timeline = []
     timeline_chrono = []
     activity_async = query_activity.run(config=datastore_query.QueryOptions(limit=TIMELINE_PAGE_SIZE))
     chrono_async = query_chrono.run(config=datastore_query.QueryOptions(limit=TIMELINE_PAGE_SIZE))
     _go_chrono = True
     chrono = None
     for activity_timeline in activity_async:
         while _go_chrono:
             if len(timeline) + len(timeline_chrono) >= TIMELINE_PAGE_SIZE:
                 _go_chrono = False
                 break
             if chrono is None:
                 try:
                     chrono = chrono_async.next()
                 except:
                     _go_chrono = False
                     break
             if chrono is not None and chrono.created > activity_timeline.modified:
                 timeline_chrono.append(chrono)
                 chrono = None
             else:
                 break
         timeline.append(activity_timeline)
         if len(timeline) + len(timeline_chrono) >= TIMELINE_PAGE_SIZE:
             break
     # generar timeline
     timeline_chrono = fetch_parents(timeline_chrono)
     timeline = prefetch_refprops(timeline, UserTimeline.user)
     timeline_chrono = prefetch_refprops(timeline_chrono, UserTimeline.instance, UserTimeline.user)
     timeline.extend(timeline_chrono)
     #seguimos cargando por lotes todas las referencias
     from helpers_acc import _load_ref_instances
     instances = _load_ref_instances(timeline)
     timeline = [{
                 'id': int(activity_timeline.id),
                 'created': activity_timeline.created,
                 'modified': activity_timeline.modified,
                 'msg': activity_timeline.msg,
                 'username': activity_timeline.user.username,
                 'msg_id': activity_timeline.msg_id,
                 'instance': instances.get(UserTimeline.instance.get_value_for_datastore(activity_timeline), activity_timeline.instance),
                 'has_voted': Vote.objects.user_has_voted(self, activity_timeline.instance.key()) if activity_timeline.instance is not None else None,
                 'vote_counter': Vote.objects.get_vote_counter(activity_timeline.instance.key()) if activity_timeline.instance is not None else None,
                 'comments': Comment.objects.get_by_instance(activity_timeline.instance, querier=self),
                 'list': instances.get(UserTimelineSuggest.list_id.get_value_for_datastore(activity_timeline), activity_timeline.list_id)
                                 if isinstance(activity_timeline, UserTimelineSuggest) else None,
                 'status': activity_timeline.status if hasattr(activity_timeline, 'status') else None,
                 'is_private': True,
                 'user_follower': instances.get(UserTimeline.instance.get_value_for_datastore(activity_timeline), activity_timeline.instance).has_follower(self)
                     if hasattr(instances.get(UserTimeline.instance.get_value_for_datastore(activity_timeline), activity_timeline.instance), 'has_follower')
                     else None,
                 } for activity_timeline in timeline]
     from operator import itemgetter
     timeline_sorted = sorted(timeline, key=itemgetter('modified'), reverse=True)
     chronology = [[query_chrono.cursor(), query_activity.cursor()], timeline_sorted]
     return chronology