Ejemplo n.º 1
0
def add_timeline_to_follower(user_key, follower_key, limit=10):
    from models_acc import UserTimeline, UserTimelineFollowersIndex
    from google.appengine.ext import db
    timeline = UserTimeline.all(keys_only=True).filter('user ='******'_vis =', 'public').order('-modified').fetch(limit)
    indexes_to_save = []
    for t in timeline:
        if UserTimelineFollowersIndex.all(keys_only=True).ancestor(t).filter('followers =', follower_key).get() is not None:
            continue # ya esta el usuario como seguidor del timeline
        index = UserTimelineFollowersIndex.all().ancestor(t).order('-created').get()
        if index is None:  # no existen indices o hemos alcanzado el maximo
            index = UserTimelineFollowersIndex(parent=t)
        index.followers.append(follower_key)
        indexes_to_save.append(index)
    db.put(indexes_to_save)
    return True
Ejemplo n.º 2
0
def add_timeline_to_follower(user_key, follower_key, limit=10):
    from models_acc import UserTimeline, UserTimelineFollowersIndex
    from google.appengine.ext import db
    timeline = UserTimeline.all(keys_only=True).filter(
        'user ='******'_vis =',
                                   'public').order('-modified').fetch(limit)
    indexes_to_save = []
    for t in timeline:
        if UserTimelineFollowersIndex.all(keys_only=True).ancestor(t).filter(
                'followers =', follower_key).get() is not None:
            continue  # ya esta el usuario como seguidor del timeline
        index = UserTimelineFollowersIndex.all().ancestor(t).order(
            '-created').get()
        if index is None:  # no existen indices o hemos alcanzado el maximo
            index = UserTimelineFollowersIndex(parent=t)
        index.followers.append(follower_key)
        indexes_to_save.append(index)
    db.put(indexes_to_save)
    return True
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