示例#1
0
def unfollow_handler(user_id, content_type_id, object_id):
    target_content_type = ContentType.objects.get_for_id(content_type_id)
    target_object = target_content_type.get_object_for_this_type(id=object_id)
    if target_content_type.model_class() == User:
        logger.debug("unfollowing target user's feed...")
        user_timeline = stream_client.feed(FeedNames.timeline, user_id)
        user_aggregated_timeline = stream_client.feed(
            FeedNames.timeline_aggregated, user_id)
        # notification_feed = stream_client.feed(FeedNames.notification, user_id)
        unfollow_timeline = user_timeline.unfollow(FeedNames.user, object_id)
        unfollow_aggr = user_aggregated_timeline.unfollow(
            FeedNames.user, object_id)
        # unfollow_not = notification_feed.unfollow(FeedNames.user, object_id)
        delete_follow_activity(user_id, object_id)
        logger.debug("unfollow %s", unfollow_timeline)
        upd_basic_stats_unfollow(user_id, target_object)
        bet_tagging.models.NotificationSubscription.user_unfollow_handle(
            user_id, target_object)
    elif target_content_type.model_class() == bet_tagging.models.BetTag:
        logger.debug("unfollowing bet group %s", target_object)
        bet_tagging.models.NotificationSubscription.bet_group_unfollow_handle(
            user_id, target_object)
        delete_bet_group_follow_activity(user_id, object_id)
    else:
        logger.error("Unfollowing object class is not User")
    return target_object
示例#2
0
def user_notification_timeline(request, user_id):
    # todo high don't allow others to see the notifications of another user by changing the url (same with tips)
    # TODO high more button to load the next activities
    # I use the low level client (stream_client) instead of the high level feedmanager of the stream django app
    # without specific reason so I can use the feedmanager instead
    get_activities_num = 80
    per_page = 12
    user_notification_feed = stream_client.feed(FeedNames.notification,
                                                user_id)
    # it seems that first the activities are collected and then they are marked as seen. This way I can
    # check if an activity is currently seen or not
    activities = user_notification_feed.get(limit=get_activities_num,
                                            mark_seen=True,
                                            mark_read=True)['results']
    # next_activities = user_notification_feed.get(limit=get_activities_num, id_lt=activities[-1]['id'])['results']
    enricher = Enrich(
        fields=['actor', 'object', 'bet_event', 'total_bet', 'event'])
    enriched_activities = enricher.enrich_aggregated_activities(activities)
    paginator = Paginator(enriched_activities, per_page)
    enriched_activities = zakanda.utils.paginate(request, paginator,
                                                 enriched_activities)
    context = {
        'enriched_activities': enriched_activities,
        'not_started': games.models.Event.not_started
    }
    return TemplateResponse(request, 'feeds/timeline.html', context)
示例#3
0
def user_timeline(request, user_pk):
    get_activities_num = 80
    per_page = 15
    enricher = Enrich(
        fields=['actor', 'object', 'bet_event', 'total_bet', 'event'])
    logger.debug("getting timeline feed...")
    user_timeline = stream_client.feed(FeedNames.timeline, user_pk)
    # we collect a number of activities and then we can filter or paginate them as we want
    activities = user_timeline.get(limit=get_activities_num)['results']
    # next_activities = user_timeline.get(limit=50, id_lt=activities[-1]['id'])['results']
    # enriched activities is a list of stream_django.enrich.EnrichedActivity instances (they have __getitem__, __iter__)
    enriched_activities = enricher.enrich_activities(activities)
    # # add_isopen_to_tb_enriched_activities(enriched_activities)
    paginator = Paginator(enriched_activities, per_page)
    enriched_activities = zakanda.utils.paginate(request, paginator,
                                                 enriched_activities)
    # enriched_activities_2 = enricher.enrich_activities(next_activities)
    enriched_activities_2 = None
    # # add_isopen_to_tb_enriched_activities(enriched_activities_2)

    context = {
        'enriched_activities': enriched_activities,
        'enriched_activities_2': enriched_activities_2,
        'not_started': games.models.Event.not_started
    }
    return TemplateResponse(request, 'feeds/timeline.html', context)
示例#4
0
 def process_request(self, request):
     if request.user.is_authenticated():
         stream_token = request.session.get(
             SessionKeys.stream_notification_token, None)
         if not stream_token:
             user_notification_feed = stream_client.feed(
                 FeedNames.notification, request.user.id)
             token = user_notification_feed.token
             request.session[SessionKeys.stream_notification_token] = token
示例#5
0
def notifications_unfollow_all():
    follows = Follow.objects.all()
    for obj in follows:
        user_id = obj.user.id
        content_type = obj.content_type
        target_id = obj.object_id
        user_content_type = ContentType.objects.get_for_model(User)
        if content_type == user_content_type:
            notification_feed = stream_client.feed(FeedNames.notification,
                                                   user_id)
            unfollow = notification_feed.unfollow(FeedNames.user, target_id)
示例#6
0
def follow_handler(user_id, content_type_id, object_id, follow_obj):
    """
    follow and unfollow actions can't be worker jobs since they need to be executed in the right order. If you
    follow a user, the job is enqueue then you unfollow, it is also enqueue before the follow job is executed.
    In this case the unfollow job might be executed first
    :param user_id: the id of the user that did the follow action
    :param object_id: the id of the object that the user chose to follow
    """
    target_content_type = ContentType.objects.get_for_id(content_type_id)
    target_object = target_content_type.get_object_for_this_type(id=object_id)
    if target_content_type.model_class() == User:
        logger.debug("following target user's feed...")
        # user's timeline and aggregated_timeline feeds follow target user's user_feed (stream_django abstraction)
        # follow = feed_manager.follow_user(user_id, target_object)
        user_timeline = stream_client.feed(FeedNames.timeline, user_id)
        user_aggregated_timeline = stream_client.feed(
            FeedNames.timeline_aggregated, user_id)
        # notification_feed = stream_client.feed(FeedNames.notification, user_id)
        follow_timeline = user_timeline.follow(FeedNames.user, object_id)
        follow_aggr = user_aggregated_timeline.follow(FeedNames.user,
                                                      object_id)
        # if your notification feed follows the target user, then every activity of the target user
        # will be collected and aggregated by the notification feed. Such activities might be:
        # submit_bet, follow user, make a comment etc. If you do not want that then you must
        # not follow him with your notification feed, but manually add to the notification feed
        # the activities you want, using either the "to" field or the activity_notify method
        # follow_not = notification_feed.follow(FeedNames.user, object_id)
        create_follow_activity(user_id, object_id, follow_obj.id)
        logger.debug("follow %s", follow_timeline)
        upd_basic_stats_follow(user_id, target_object)
        bet_tagging.models.NotificationSubscription.user_follow_handle(
            user_id, target_object)
    elif target_content_type.model_class() == bet_tagging.models.BetTag:
        logger.debug("following bet group %s", target_object)
        bet_tagging.models.NotificationSubscription.bet_group_follow_handle(
            user_id, target_object)
        create_bet_group_follow_activity(user_id, object_id, follow_obj.id)
    else:
        logger.error("Following object class is not User")
    return target_object
示例#7
0
def user_aggregated_timeline(request, user_pk):
    get_activities_num = 80
    per_page = 15
    user_aggr_timeline = stream_client.feed(FeedNames.timeline_aggregated,
                                            user_pk)
    activities = user_aggr_timeline.get(limit=get_activities_num)['results']
    # logger.debug("activities: %s", activities['activities'])
    enricher = Enrich(
        fields=['actor', 'object', 'bet_event', 'total_bet', 'event'])
    enriched_activities = enricher.enrich_aggregated_activities(activities)
    paginator = Paginator(enriched_activities, per_page)
    enriched_activities = zakanda.utils.paginate(request, paginator,
                                                 enriched_activities)
    feeds.utils.print_enriched_aggr_activities(enriched_activities)
    context = {
        'enriched_activities': enriched_activities,
        'not_started': games.models.Event.not_started
    }
    return TemplateResponse(request, 'feeds/timeline.html', context)
示例#8
0
    def get_feed(self):
        """
        Each user has 2 feeds:
        * The default public user feed: Activities of free bet groups are added to this feed.
        * A private user feed: This feed is for all activities that are strictly private,
          activities that you don't want the followers of the target user to know about.
          Activities of private bet groups are added to this feed

        There is also a bet_group flat feed. Each premium bet_group has a feed similar with how each user
        has a public feed. When you subscribe to a premium bet group your timeline and aggregated timeline feeds
        follow this bet group's feed. All the activities of the public bet groups are added to the public user feed.
        When you follow a user, your feeds follow his public user feed. When you subscribe to a premium bet_group
        your feeds follow that bet_group's feed.
        :return:
        """
        bet_group_type = self.type
        if bet_group_type == self.free:
            feed_name = FeedNames.user
            actor_id = self.owner.id
        elif bet_group_type == self.premium:
            feed_name = FeedNames.bet_group
            actor_id = self.id
        elif bet_group_type == self.private:
            feed_name = FeedNames.user_private
            actor_id = self.owner.id
        elif bet_group_type == self.archived:
            logger.warning(
                'There is no feed for archived Bet Groups (invoked from %s)',
                self)
            return
        else:
            logger.error('Bet Group %s has invalid type %s!', self,
                         bet_group_type)
            return
        feed = stream_client.feed(feed_name, actor_id)
        return feed
示例#9
0
 def get_feed(self, feed, user_id):
     return stream_client.feed(feed, user_id)
示例#10
0
 def get_notification_feed(self, user_id):
     return stream_client.feed(self.notification_feed, user_id)
示例#11
0
 def get_user_feed(self, user_id, feed_type=None):
     if feed_type is None:
         feed_type = self.user_feed
     feed = stream_client.feed(feed_type, user_id)
     return feed
示例#12
0
 def get_group_feed(self, group_id):
     return stream_client.feed('group', group_id)