Example #1
0
    def test_get_names(self):
        event = Event.objects.get(title='Test event')
        eq_(CuratedGroup.get_names(event), [])

        group1 = CuratedGroup.objects.create(event=event, name='secrets')
        eq_(CuratedGroup.get_names(event), ['secrets'])

        group2 = CuratedGroup.objects.create(event=event, name='ABBA fans')
        eq_(CuratedGroup.get_names(event), ['ABBA fans', 'secrets'])

        group1.delete()
        eq_(CuratedGroup.get_names(event), ['ABBA fans'])

        group2.name = 'ABBA Fans'
        group2.save()
        eq_(CuratedGroup.get_names(event), ['ABBA Fans'])
Example #2
0
    def test_get_names(self):
        event = Event.objects.get(title='Test event')
        eq_(CuratedGroup.get_names(event), [])

        group1 = CuratedGroup.objects.create(
            event=event,
            name='secrets'
        )
        eq_(CuratedGroup.get_names(event), ['secrets'])

        group2 = CuratedGroup.objects.create(
            event=event,
            name='ABBA fans'
        )
        eq_(CuratedGroup.get_names(event), ['ABBA fans', 'secrets'])

        group1.delete()
        eq_(CuratedGroup.get_names(event), ['ABBA fans'])

        group2.name = 'ABBA Fans'
        group2.save()
        eq_(CuratedGroup.get_names(event), ['ABBA Fans'])
Example #3
0
    def get(self, request, slug):
        event = self.get_event(slug, request)
        if isinstance(event, http.HttpResponse):
            return event

        if not self.can_view_event(event, request):
            return self.cant_view_event(event, request)

        tag = request.GET.get('tag')

        warning = None
        ok_statuses = (
            Event.STATUS_SCHEDULED,
            Event.STATUS_PENDING,
            Event.STATUS_PROCESSING,
        )
        if event.status not in ok_statuses:
            if not request.user.is_superuser:
                self.template_name = 'main/event_not_scheduled.html'
            else:
                warning = "Event is not publicly visible - not scheduled."

        if event.approval_set.filter(approved=False).exists():
            if not request.user.is_active:
                return http.HttpResponse('Event not approved')
            else:
                warning = "Event is not publicly visible - not yet approved."

        hits = None

        # assume this to false to start with
        can_edit_chapters = False

        template_tagged = ''
        if event.template and not event.is_upcoming():
            # The only acceptable way to make autoplay be on
            # is to send ?autoplay=true
            # All other attempts will switch it off.
            autoplay = request.GET.get('autoplay', 'false') == 'true'
            try:
                template_tagged = get_video_tagged(
                    event,
                    request,
                    autoplay=autoplay,
                    tag=tag,
                )
            except VidlySubmission.DoesNotExist:
                return http.HttpResponseBadRequest(
                    'Tag %s does not exist for this event' % (tag, ))
            stats_query = (EventHitStats.objects.filter(
                event=event).values_list('total_hits', flat=True))
            for total_hits in stats_query:
                hits = total_hits

            # if the event has a template is not upcoming
            if not event.is_live():
                # ...and is not live, then
                if request.user.is_active:
                    can_edit_chapters = True

        can_manage_edit_event = (request.user.is_active
                                 and request.user.is_staff and
                                 request.user.has_perm('main.change_event'))
        can_edit_event = request.user.is_active
        can_edit_discussion = (
            can_edit_event and
            # This is a little trick to avoid waking up the
            # SimpleLazyObject on the user. If the .is_active is true
            # the ID will have already been set by the session.
            # So doing this comparison instead avoids causing a
            # select query on the auth_user table.
            request.user.pk == event.creator_id
            and Discussion.objects.filter(event=event).exists())

        request.channels = event.channels.all()

        # needed for the open graph stuff
        event.url = reverse('main:event', args=(event.slug, ))

        context = self.get_default_context(event, request)
        context.update({
            'event': event,
            'video': template_tagged,
            'warning': warning,
            'can_manage_edit_event': can_manage_edit_event,
            'can_edit_event': can_edit_event,
            'can_edit_discussion': can_edit_discussion,
            'can_edit_chapters': can_edit_chapters,
            'Event': Event,
            'hits': hits,
            'tags': [t.name for t in event.tags.all()],
            'channels': request.channels,
            # needed for the _event_privacy.html template
            'curated_groups': CuratedGroup.get_names(event),
        })

        context['chapters'] = []
        for chapter in Chapter.objects.filter(event=event, is_active=True):
            context['chapters'].append({
                'timestamp': chapter.timestamp,
                'text': chapter.text,
            })

        vidly_tag, vidly_hd = self.get_vidly_information(event, tag)
        if vidly_tag:
            context['vidly_tag'] = vidly_tag
            context['vidly_hd'] = vidly_hd

        # If the event is in the processing state (or pending), we welcome
        # people to view it but it'll say that the video isn't ready yet.
        # But we'll also try to include an estimate of how long we think
        # it will take until it's ready to be viewed.
        if ((event.is_processing() or event.is_pending()) and event.duration
                and event.template_environment.get('tag')):
            vidly_submissions = (VidlySubmission.objects.filter(
                event=event, tag=event.template_environment.get(
                    'tag')).order_by('-submission_time'))
            for vidly_submission in vidly_submissions:
                context['estimated_time_left'] = (
                    vidly_submission.get_estimated_time_left())

        if event.pin:
            if (not request.user.is_authenticated()
                    or not is_employee(request.user)):
                entered_pins = request.session.get('entered_pins', [])
                if event.pin not in entered_pins:
                    self.template_name = 'main/event_requires_pin.html'
                    context['pin_form'] = forms.PinForm()
        try:
            context['discussion'] = Discussion.objects.get(event=event)
        except Discussion.DoesNotExist:
            context['discussion'] = {'enabled': False}

        if event.recruitmentmessage and event.recruitmentmessage.active:
            context['recruitmentmessage'] = event.recruitmentmessage

        cache_key = 'event_survey_id_%s' % event.id
        context['survey_id'] = cache.get(cache_key, -1)
        if context['survey_id'] == -1:  # not known in cache
            try:
                survey = Survey.objects.get(events=event, active=True)
                cache.set(cache_key, survey.id, 60 * 60 * 24)
                context['survey_id'] = survey.id
            except Survey.DoesNotExist:
                cache.set(cache_key, None, 60 * 60 * 24)
                context['survey_id'] = None

        if settings.LOG_SEARCHES:
            if request.session.get('logged_search'):
                pk, time_ago = request.session.get('logged_search')
                age = time.time() - time_ago
                if age <= 5:
                    # the search was made less than 5 seconds ago
                    try:
                        logged_search = LoggedSearch.objects.get(pk=pk)
                        logged_search.event_clicked = event
                        logged_search.save()
                    except LoggedSearch.DoesNotExist:
                        pass

        return render(request, self.template_name, context)
Example #4
0
    def get(self, request, slug):
        event = self.get_event(slug, request)
        if isinstance(event, http.HttpResponse):
            return event

        if not self.can_view_event(event, request):
            return self.cant_view_event(event, request)

        tag = request.GET.get('tag')

        warning = None
        ok_statuses = (
            Event.STATUS_SCHEDULED,
            Event.STATUS_PENDING,
            Event.STATUS_PROCESSING,
        )
        if event.status not in ok_statuses:
            if not request.user.is_superuser:
                self.template_name = 'main/event_not_scheduled.html'
            else:
                warning = "Event is not publicly visible - not scheduled."

        if event.approval_set.filter(approved=False).exists():
            if not request.user.is_active:
                return http.HttpResponse('Event not approved')
            else:
                warning = "Event is not publicly visible - not yet approved."

        hits = None

        # assume this to false to start with
        can_edit_chapters = False

        template_tagged = ''
        if event.template and not event.is_upcoming():
            # The only acceptable way to make autoplay be on
            # is to send ?autoplay=true
            # All other attempts will switch it off.
            autoplay = request.GET.get('autoplay', 'false') == 'true'
            try:
                template_tagged = get_video_tagged(
                    event,
                    request,
                    autoplay=autoplay,
                    tag=tag,
                )
            except VidlySubmission.DoesNotExist:
                return http.HttpResponseBadRequest(
                    'Tag %s does not exist for this event' % (tag,)
                )
            stats_query = (
                EventHitStats.objects.filter(event=event)
                .values_list('total_hits', flat=True)
            )
            for total_hits in stats_query:
                hits = total_hits

            # if the event has a template is not upcoming
            if not event.is_live() and event.is_scheduled():
                # ...and is not live, then
                if request.user.is_active:
                    can_edit_chapters = True

        can_manage_edit_event = (
            request.user.is_active and
            request.user.is_staff and
            request.user.has_perm('main.change_event')
        )
        can_edit_event = request.user.is_active
        can_edit_discussion = (
            can_edit_event and
            # This is a little trick to avoid waking up the
            # SimpleLazyObject on the user. If the .is_active is true
            # the ID will have already been set by the session.
            # So doing this comparison instead avoids causing a
            # select query on the auth_user table.
            request.user.pk == event.creator_id and
            Discussion.objects.filter(event=event).exists()
        )

        request.channels = event.channels.all()

        # needed for the open graph stuff
        event.url = reverse('main:event', args=(event.slug,))

        context = self.get_default_context(event, request)
        context.update({
            'event': event,
            'video': template_tagged,
            'warning': warning,
            'can_manage_edit_event': can_manage_edit_event,
            'can_edit_event': can_edit_event,
            'can_edit_discussion': can_edit_discussion,
            'can_edit_chapters': can_edit_chapters,
            'Event': Event,
            'hits': hits,
            'tags': [t.name for t in event.tags.all()],
            'channels': request.channels,
            # needed for the _event_privacy.html template
            'curated_groups': CuratedGroup.get_names(event),
        })

        context['chapters'] = Chapter.objects.filter(
            event=event,
            is_active=True,
        )

        # By default, we want to hint in the DOM that this is an HD
        # video.
        context['hd'] = event.is_scheduled() and not event.is_upcoming()

        vidly_tag, vidly_hd = self.get_vidly_information(event, tag)
        if vidly_tag:
            context['vidly_tag'] = vidly_tag
            context['vidly_hd'] = vidly_hd
            if not vidly_hd:
                context['hd'] = False

        # If the event is in the processing state (or pending), we welcome
        # people to view it but it'll say that the video isn't ready yet.
        # But we'll also try to include an estimate of how long we think
        # it will take until it's ready to be viewed.
        context['estimated_time_left'] = None
        context['time_run'] = None
        if (
            (event.is_processing() or event.is_pending()) and
            event.duration and
            event.template_environment.get('tag')
        ):
            vidly_submissions = (
                VidlySubmission.objects
                .filter(event=event, tag=event.template_environment.get('tag'))
                .order_by('-submission_time')
            )
            for vidly_submission in vidly_submissions[:1]:
                context['estimated_time_left'] = (
                    vidly_submission.get_estimated_time_left()
                )
                context['time_run'] = (
                    (
                        timezone.now() - vidly_submission.submission_time
                    ).seconds
                )

        if event.pin:
            if (
                not request.user.is_authenticated() or
                not is_employee(request.user)
            ):
                entered_pins = request.session.get('entered_pins', [])
                if event.pin not in entered_pins:
                    self.template_name = 'main/event_requires_pin.html'
                    context['pin_form'] = forms.PinForm()
        try:
            context['discussion'] = Discussion.objects.get(event=event)
            # The name of the channel we publish to fanout on when there's
            # changes to this events comments.
            context['subscription_channel_comments'] = 'comments-{}'.format(
                event.id
            )
        except Discussion.DoesNotExist:
            context['discussion'] = {'enabled': False}

        context['subscription_channel_status'] = 'event-{}'.format(event.id)

        # amara_videos = AmaraVideo.objects.filter(
        #     event=event,
        #     transcript__isnull=False,
        # )
        # context['amara_video'] = None
        # for amara_video in amara_videos.order_by('-modified')[:1]:
        #     context['amara_video'] = amara_video

        context['closedcaptions'] = None
        for connection in ClosedCaptionsTranscript.objects.filter(event=event):
            assert connection.closedcaptions.transcript
            context['closedcaptions'] = connection.closedcaptions

        cache_key = 'event_survey_id_%s' % event.id
        context['survey_id'] = cache.get(cache_key, -1)
        if context['survey_id'] == -1:  # not known in cache
            try:
                survey = Survey.objects.get(
                    events=event,
                    active=True
                )
                cache.set(cache_key, survey.id, 60 * 60 * 24)
                context['survey_id'] = survey.id
            except Survey.DoesNotExist:
                cache.set(cache_key, None, 60 * 60 * 24)
                context['survey_id'] = None

        if settings.LOG_SEARCHES:
            if request.session.get('logged_search'):
                pk, time_ago = request.session.get('logged_search')
                age = time.time() - time_ago
                if age <= 5:
                    # the search was made less than 5 seconds ago
                    try:
                        logged_search = LoggedSearch.objects.get(pk=pk)
                        logged_search.event_clicked = event
                        logged_search.save()
                    except LoggedSearch.DoesNotExist:
                        pass

        response = render(request, self.template_name, context)
        self._set_csp_update(response, event)
        return response