Exemplo n.º 1
0
def profile_feed(request, username=None):
    enricher = Enrich()
    user = User.objects.get(username=username)
    feed = feed_manager.get_user_feed(user.id)
    activities = feed.get(limit=25)['results']
    enricher.enrich_activities(activities)
    context = {'activities': activities}
    return render(request, 'tweets.html', context)
Exemplo n.º 2
0
def hashtag(request, hashtag):
    enricher = Enrich()
    feed = feed_manager.get_feed('hashtag', hashtag)
    activities = feed.get(limit=25)['results']
    enricher.enrich_activities(activities)
    context = {
        'activities': activities
    }
    return render(request, 'hashtag.html', context)
Exemplo n.º 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)
Exemplo n.º 4
0
    def get(self, request, *args, **kwargs):
        if request.GET.get('partial') == 'activities':
            # activities
            try:
                project = self.get_object()
                enricher = Enrich((
                    'actor',
                    'target',
                ))
                feed = feed_manager.get_challenge_feed(challenge_id=project.pk)
                activities = feed.get(limit=6)['results']
                enriched = enricher.enrich_activities(activities)
                return render(request, 'projects/_activities.html',
                              {'activities': enriched})
            except Exception:
                return HttpResponse()

        if request.GET.get('partial') == 'completed':
            # current user status
            data = {'completed': False}
            try:
                user = request.user
                if not user.is_authenticated():
                    raise Exception('not authenticated')

                project = self.get_object()
                user_project = UserProject.objects.get(project=project,
                                                       user=request.user)
                data['complete'] = user_project.is_complete()
            except Exception:
                pass
            return JsonResponse(data)

        return super().get(request, *args, **kwargs)
Exemplo n.º 5
0
def Home(request):
    enricher = Enrich()
    feeds = feed_manager.get_news_feeds(request.user.id)
    activities = feeds.get('flat').get()['results']
    activities = enricher.enrich_activities(activities)
    hashtags = Hashtag.objects.order_by('-occurrences')
    SelectedAthlete = get_object_or_404(Athlete, id=request.user.athlete.id)
    TotalAthletes = Athlete.objects.count()
    contextdata = {
        'activities': activities,
        'login_user': request.user,
        'hashtags': hashtags,
        'TotalAthletes': TotalAthletes
    }
    return render(request, 'index.html', contextdata)
Exemplo n.º 6
0
 def get(self, request, cityslug, format=None):
     '''
     enricher = Enrich()
     flat_feed = feed_manager.get_news_feed(cityslug)['flat']
     activities = feed.get(limit=25)['results']
     enriched_activities = enricher.enrich_activities(activities)
     '''
     '''
     UserEventsQuerySet = UserEvent.objects.filter(city__slug = cityslug).order_by('-created_at')[:3600]
     serialized = UserEventSerializer(UserEventsQuerySet, many=True)
     return Response(serialized.data)
     '''
     enricher = Enrich()
     feed = feed_manager.get_feed('cityevents', cityslug)
     activities = feed.get(limit=25)['results']
     enriched_activities = enricher.enrich_activities(activities)
     serialized = UserEventSerializer(enriched_activities, many=True)
     return Response(serialized.data)
class PinTest(TestCase):

    def setUp(self):
        feed_manager.enable_model_tracking()
        self.User = get_user_model()
        self.bogus = self.User.objects.create()
        self.enricher = Enrich()

    def register_post_api(self):
        httpretty.register_uri(httpretty.POST, api_url,
              body='{}', status=200,
              content_type='application/json')

    def register_delete_api(self):
        httpretty.register_uri(httpretty.DELETE, api_url,
              body='{}', status=200,
              content_type='application/json')

    @httpretty.activate
    def test_create(self):
        self.register_post_api()
        pin = models.Pin.objects.create(author=self.bogus)
        last_req = httpretty.last_request()
        req_body = last_req.parsed_body
        self.assertEqual(req_body['foreign_id'], 'test_app.Pin:%s' % pin.id)
        self.assertEqual(req_body['object'], 'test_app.Pin:%s' % pin.id)
        self.assertEqual(req_body['verb'], 'pin')
        self.assertEqual(req_body['actor'], 'auth.User:%s' % self.bogus.id)

    @httpretty.activate
    def test_delete(self):
        self.register_post_api()
        self.register_delete_api()
        pin = models.Pin.objects.create(author=self.bogus)
        pin.delete()
        last_req = httpretty.last_request()
        self.assertEqual(last_req.method, httpretty.DELETE)

    @httpretty.activate
    def test_enrich_instance(self):
        self.register_post_api()
        pin = models.Pin.objects.create(author=self.bogus)
        pin.save()
        activity = pin.create_activity()

        enriched_data = self.enricher.enrich_activities([activity])
        # check object field
        self.assertEqual(enriched_data[0]['object'].id, pin.id)
        self.assertIsInstance(enriched_data[0]['object'], models.Pin)
        # check actor field
        self.assertEqual(enriched_data[0]['actor'].id, pin.author_id)
        self.assertIsInstance(enriched_data[0]['actor'], self.User)

    @httpretty.activate
    def test_enrich_data_with_missing_reference(self):
        self.register_post_api()
        self.register_delete_api()
        pin = models.Pin.objects.create(author=self.bogus)
        pin.save()
        activity = pin.create_activity()
        pin.delete()
        # make sure missing data is correctly tracked
        enriched_data = self.enricher.enrich_activities([activity])
        self.assertFalse(enriched_data[0].enriched)
        self.assertIn('object', enriched_data[0].not_enriched_data)
        self.assertDictContainsSubset(dict(object=activity['object']), enriched_data[0].not_enriched_data)
        self.assertEqual(activity['object'], enriched_data[0]['object'])

    def test_disabled_tracking(self):
        feed_manager.disable_model_tracking()
        pin = models.Pin.objects.create(author=self.bogus)
        pin.save()
        pin.delete()
class PinTest(TestCase):
    def setUp(self):
        feed_manager.enable_model_tracking()
        self.User = get_user_model()
        self.bogus = self.User.objects.create()
        self.enricher = Enrich()

    def register_post_api(self):
        httpretty.register_uri(httpretty.POST,
                               api_url,
                               body='{}',
                               status=200,
                               content_type='application/json')

    def register_delete_api(self):
        httpretty.register_uri(httpretty.DELETE,
                               api_url,
                               body='{}',
                               status=200,
                               content_type='application/json')

    @httpretty.activate
    def test_create(self):
        self.register_post_api()
        pin = models.Pin.objects.create(author=self.bogus)
        last_req = httpretty.last_request()
        req_body = last_req.parsed_body
        self.assertEqual(req_body['foreign_id'], 'test_app.Pin:%s' % pin.id)
        self.assertEqual(req_body['object'], 'test_app.Pin:%s' % pin.id)
        self.assertEqual(req_body['verb'], 'pin')
        self.assertEqual(req_body['actor'], 'auth.User:%s' % self.bogus.id)

    @httpretty.activate
    def test_delete(self):
        self.register_post_api()
        self.register_delete_api()
        pin = models.Pin.objects.create(author=self.bogus)
        pin.delete()
        last_req = httpretty.last_request()
        self.assertEqual(last_req.method, httpretty.DELETE)

    @httpretty.activate
    def test_enrich_instance(self):
        self.register_post_api()
        pin = models.Pin.objects.create(author=self.bogus)
        pin.save()
        activity = pin.create_activity()

        enriched_data = self.enricher.enrich_activities([activity])
        # check object field
        self.assertEqual(enriched_data[0]['object'].id, pin.id)
        self.assertIsInstance(enriched_data[0]['object'], models.Pin)
        # check actor field
        self.assertEqual(enriched_data[0]['actor'].id, pin.author_id)
        self.assertIsInstance(enriched_data[0]['actor'], self.User)

    @httpretty.activate
    def test_enrich_data_with_missing_reference(self):
        self.register_post_api()
        self.register_delete_api()
        pin = models.Pin.objects.create(author=self.bogus)
        pin.save()
        activity = pin.create_activity()
        pin.delete()
        # make sure missing data is correctly tracked
        enriched_data = self.enricher.enrich_activities([activity])
        self.assertFalse(enriched_data[0].enriched)
        self.assertIn('object', enriched_data[0].not_enriched_data)
        self.assertDictContainsSubset(dict(object=activity['object']),
                                      enriched_data[0].not_enriched_data)
        self.assertEqual(activity['object'], enriched_data[0]['object'])

    def test_disabled_tracking(self):
        feed_manager.disable_model_tracking()
        pin = models.Pin.objects.create(author=self.bogus)
        pin.save()
        pin.delete()
class PinTest(TestCase):
    def setUp(self):
        feed_manager.enable_model_tracking()
        self.User = get_user_model()
        self.bogus = self.User.objects.create()
        self.enricher = Enrich()

    def register_post_api(self):
        httpretty.register_uri(
            httpretty.POST,
            api_url,
            body="{}",
            status=200,
            content_type="application/json",
        )

    def register_delete_api(self):
        httpretty.register_uri(
            httpretty.DELETE,
            api_url,
            body="{}",
            status=200,
            content_type="application/json",
        )

    @httpretty.activate
    def test_create(self):
        self.register_post_api()
        pin = models.Pin.objects.create(author=self.bogus)
        last_req = httpretty.last_request()
        req_body = last_req.parsed_body
        self.assertEqual(req_body["foreign_id"], "test_app.Pin:%s" % pin.id)
        self.assertEqual(req_body["object"], "test_app.Pin:%s" % pin.id)
        self.assertEqual(req_body["verb"], "pin")
        self.assertEqual(req_body["actor"], "auth.User:%s" % self.bogus.id)

    @httpretty.activate
    def test_delete(self):
        self.register_post_api()
        self.register_delete_api()
        pin = models.Pin.objects.create(author=self.bogus)
        pin.delete()
        last_req = httpretty.last_request()
        self.assertEqual(last_req.method, httpretty.DELETE)

    @httpretty.activate
    def test_enrich_instance(self):
        self.register_post_api()
        pin = models.Pin.objects.create(author=self.bogus)
        pin.save()
        activity = pin.create_activity()

        enriched_data = self.enricher.enrich_activities([activity])
        # check object field
        self.assertEqual(enriched_data[0]["object"].id, pin.id)
        self.assertIsInstance(enriched_data[0]["object"], models.Pin)
        # check actor field
        self.assertEqual(enriched_data[0]["actor"].id, pin.author_id)
        self.assertIsInstance(enriched_data[0]["actor"], self.User)

    @httpretty.activate
    def test_enrich_data_with_missing_reference(self):
        self.register_post_api()
        self.register_delete_api()
        pin = models.Pin.objects.create(author=self.bogus)
        pin.save()
        activity = pin.create_activity()
        pin.delete()
        # make sure missing data is correctly tracked
        enriched_data = self.enricher.enrich_activities([activity])
        self.assertFalse(enriched_data[0].enriched)
        self.assertIn("object", enriched_data[0].not_enriched_data)

        activity_obj = set(dict(object=activity["object"]).items())
        enriched_obj = set(enriched_data[0].not_enriched_data.items())
        assert activity_obj.issubset(enriched_obj)

        self.assertEqual(activity["object"], enriched_data[0]["object"])

    def test_disabled_tracking(self):
        feed_manager.disable_model_tracking()
        pin = models.Pin.objects.create(author=self.bogus)
        pin.save()
        pin.delete()
Exemplo n.º 10
0
def CityCommunityPostPage(request, cityslug):
    contextdata = {}
    SelectedCity = get_object_or_404(City, slug = cityslug)
    SelectedAthlete = get_object_or_404(Athlete, id=request.user.athlete.id)
    if request.method == 'POST' and request.is_ajax():
        form = CommunityPostForm(request.POST)
        if form.is_valid():
            user = request.user
            body = form.cleaned_data['body']
            communitypost = CommunityPost(user=user, body=body, city=SelectedCity)
            communitypost.save()
            contextdata = {
                'CommunityPost': communitypost
            }
            html = render_to_string('communitypost.html', contextdata, context_instance = RequestContext(request))
            return JsonResponse({'html': html})
        else:
            msg="AJAX post invalid"
            contextdata = {'form': form}
            return render(request, 'communitylocation/citypostpage.html', contextdata)
    if request.method == 'GET' and request.is_ajax():
        if request.GET.get('postid'):
            if request.GET.get('verifiedathleteslug'):
                postid = request.GET['postid']
                verifiedathleteslug = request.GET['verifiedathleteslug']
                SelectedAthlete = get_object_or_404(Athlete, slug=verifiedathleteslug)
                Comments = Comment.objects.filter(user__username = SelectedAthlete.user.username).filter(object_pk = postid).order_by('submit_date')
                contextdata = {
                    'comment_list': Comments,
                    'postid': postid
                }
                commentshtml = render_to_string("comments/list.html", contextdata, context_instance = RequestContext(request))
                return JsonResponse({'commentshtml': commentshtml})
            else:
                postid = request.GET['postid']
                Comments = Comment.objects.filter(object_pk = postid).order_by('submit_date')
                contextdata = {
                    'comment_list': Comments,
                    'postid': postid
                }
                commentshtml = render_to_string("comments/list.html", contextdata, context_instance = RequestContext(request))
                return JsonResponse({'commentshtml': commentshtml})
        if request.GET.get('postidreq'):
            postidreq = request.GET['postidreq']
            SelectedPost = CommunityPost.objects.get(id = postidreq)
            SelectedPost.delete()
            return JsonResponse({'postID': postidreq})
        if request.GET.get('commentidreq'):
            commentidreq = request.GET['commentidreq']
            comment = get_object_or_404(comments.get_model(), pk=commentidreq, site__pk=settings.SITE_ID)
            perform_delete(request, comment)
            return JsonResponse({'commentID': commentidreq})
    else:
        form = CommunityPostForm()
        enricher = Enrich()
        feed = feed_manager.get_feed('citypost', SelectedCity.name.replace(" ", "_"))
        activities = feed.get(limit=25)['results']
        enricher.enrich_activities(activities)
        contextdata = {
            'City': SelectedCity,
            'form': form,
            'activities': activities
        }
        return render(request, 'communitylocation/citypostpage.html', contextdata)