Ejemplo n.º 1
0
    def get_context_data(self, **kwargs):
        context = \
            super(PersonQCStatsView, self).get_context_data(**kwargs)

        person = get_person(self.request)
        language = get_current_language(self.request)

        qcs = RecordingQualityControl.objects\
            .filter(person=person)\
            .filter(recording__language=language)

        now = timezone.now()

        today_begining = \
            datetime.datetime.combine(now, datetime.time())

        todays_qcs = qcs\
            .filter(updated__gte=today_begining).order_by('updated')

        stats = {
            'qcs': build_qualitycontrol_stat_dict(qcs),
            'qcs_today': build_qualitycontrol_stat_dict(todays_qcs)
        }

        context['person'] = person
        context['stats'] = stats

        return context
Ejemplo n.º 2
0
    def create(self, validated_data):
        request = self.context['request']
        validated_data['uploaded_by'] = get_person(request)

        method = request.GET.get('method', '')
        if 'stream' in method:
            logger.debug(type(validated_data['audio_file']))
            result = transcribe_audio_quick(validated_data['audio_file'])
            text = result['transcription'].strip()
            validated_data['transcription'] = text

            # For streaming, let's not create an AFT
            # Instead we should just have a log of a transcription
            # I meand we could keep the audio as well...
            aft = AudioFileTranscription()
            for key in validated_data.keys():
                setattr(aft, key, validated_data[key])

            try:
                aft.metadata = result['metadata']
            except KeyError:
                pass

            return aft

        if 'name' not in validated_data.keys():
            fname = validated_data['audio_file'].name
            validated_data['name'] = ''.join(fname.split('.')[:-1])

        return super(AudioFileTranscriptionSerializer,
                     self).create(validated_data)
Ejemplo n.º 3
0
    def get_context_data(self, **kwargs):
        context = \
            super(PersonQCStatsView, self).get_context_data(**kwargs)

        person = get_person(self.request)
        language = get_current_language(self.request)

        qcs = QualityControl.objects.filter(person=person)

        pks = []
        for qc in qcs:
            if qc.content_type.name.lower() in 'recording' \
                    and qc.content_object.sentence.language == language:
                pks.append(qc.pk)

        qcs = qcs.filter(pk__in=pks)

        now = timezone.now()

        today_begining = \
            datetime.datetime.combine(now, datetime.time())

        todays_qcs = qcs\
            .filter(updated__gte=today_begining).order_by('updated')

        stats = {
            'qcs': build_qualitycontrol_stat_dict(qcs),
            'qcs_today': build_qualitycontrol_stat_dict(todays_qcs)
        }

        context['person'] = person
        context['stats'] = stats

        return context
Ejemplo n.º 4
0
    def get_queryset(self):
        person = get_person(self.request)
        queryset = TranscriptionSegment.objects\
            .filter(parent__uploaded_by=person)\
            .order_by('start')\
            .order_by('-parent__created')

        return queryset
Ejemplo n.º 5
0
 def get_queryset(self):
     person = get_person(self.request)
     qs = AudioFileTranscription.objects\
         .filter(uploaded_by=person)\
         .annotate(num_segments=Count('transcriptionsegment'))\
         .filter(num_segments__gte=1)\
         .order_by('-updated')
     return qs
Ejemplo n.º 6
0
    def has_object_permission(self, request, view, obj):

        person = get_person(request)
        if person == obj.person:
            # Only a person can view/edit his/her own data
            return True
        else:
            self.message = _("You're not allowed to view this information.")
            return False
Ejemplo n.º 7
0
 def to_representation(self, value):
     serializer = QualityControlSerializer(value,
                                           context=self.parent.context)
     person = get_person(self.parent.context['request'])
     data = serializer.data
     return data
     if person is data['person']:
         return serializer.data
     else:
         return None
Ejemplo n.º 8
0
 def has_object_permission(self, request, view, obj):
     if request.method in permissions.SAFE_METHODS:
         self.message = _("Only staff can read recordings.")
         if request.user.is_staff:
             person = get_person(request)
             cache.set('{0}:{0}:listen'.format(person.uuid, obj.id), True,
                       15)
             return True
     else:
         if request.method in ['PUT']:
             if request.user.is_staff:
                 return True
             person = get_person(request)
             if person is not None:
                 self.message = _(
                     "You're not allowed to edit this recording.")
                 return obj.person == person
     self.message = _("Reading recording is not allowed.")
     return False
Ejemplo n.º 9
0
 def get_queryset(self):
     person = get_person(self.request)
     qs = AudioFileTranscription.objects\
         .filter(uploaded_by=person)\
         .order_by('-updated')
     # .annotate(num_segments=Count('transcriptionsegment'))\ => takes too
     # long instead maybe we should delete the "quick test recordings" or
     # better yet just store them as anonuymous data if we *really* want to
     # keep it .filter(num_segments__gte=1)\ => prevents us from showing
     # fresh aft
     return qs
Ejemplo n.º 10
0
    def test_func(self):
        person = get_person(self.request)
        path = self.request.get_full_path()
        pk = int(path.split('/')[-1])

        if self.request.user.is_staff:
            return True
        elif person.groups.filter(pk__in=[pk]).exists():
            return True
        else:
            return False
Ejemplo n.º 11
0
 def get_context_data(self, **kwargs):
     context = super(SentenceListView, self).get_context_data(**kwargs)
     user = self.request.user
     person = get_person(self.request)
     user.can_approve = user.is_staff and user.is_authenticated()
     ct = ContentType.objects.get(model='sentence')
     context['content_type'] = ct.id
     context['user'] = user
     context['person'] = person
     context['uuid'] = self.request.get_signed_cookie('uuid', 'none')
     return context
Ejemplo n.º 12
0
    def get_context_data(self, **kwargs):
        context = super(DashboardView, self).get_context_data(**kwargs)
        person = get_person(self.request)

        try:
            token = Token.objects.get(user=person.user)
            context['token'] = token.key
        except ObjectDoesNotExist:
            pass

        return context
Ejemplo n.º 13
0
 def has_permission(self, request, view):
     person = get_person(request)
     if request.method in permissions.SAFE_METHODS:
         if request.user.is_staff:
             return True
         elif person is not None:
             return request.user.is_authenticated
         return False
     else:
         if request.method in ['PUT']:
             return request.user.is_authenticated
     return False
Ejemplo n.º 14
0
 def run_validation(self, data):
     d2 = None
     if 'person' in data.keys():
         if data['person'] == 'self':
             d2 = data.copy()
             person = get_person(self.context['request'])
             if person is not None:
                 d2['person'] = person.id
             else:
                 d2['person'] = None
     if d2 is None:
         d2 = data
     return super(SetPersonFromTokenWhenSelf, self).run_validation(d2)
Ejemplo n.º 15
0
    def test_func(self):
        person = get_person(self.request)
        aft = self.get_object()

        key = self.request.GET.get('key', '')

        if key == '720031ba-4db3-11e8-88f9-8c8590055544':
            return True

        if self.request.user.is_staff:
            return True
        else:
            return person == aft.uploaded_by
Ejemplo n.º 16
0
    def has_object_permission(self, request, view, obj):
        person = get_person(request)

        if request.method in permissions.SAFE_METHODS:
            self.message = _("Only staff can read transcriptions.")
            if request.user.is_staff:
                person = get_person(request)
                cache.set(
                    '{0}:{0}:listen'.format(person.uuid, obj.recording.id), True, 15)
                return True
            elif person is not None:
                # Allow people to get their own transcriptions.
                return person == obj.recording.person
        else:
            if request.method in ['PUT']:
                if request.user.is_staff:
                    return True
                if person is not None:
                    self.message = _(
                        "You're not allowed to edit this transcription.")
                    return person == obj.recording.person
        self.message = _("Reading transcriptions is not allowed.")
        return False
Ejemplo n.º 17
0
 def has_object_permission(self, request, view, obj):
     person = get_person(request)
     if request.method in permissions.SAFE_METHODS:
         if request.user.is_staff:
             return True
         elif person is not None:
             return person == obj.parent.uploaded_by
     else:
         if request.method in ['PUT']:
             if request.user.is_staff:
                 return True
             if person is not None:
                 return person == obj.parent.uploaded_by
     return False
Ejemplo n.º 18
0
    def has_object_permission(self, request, view, obj):
        if request.method in permissions.SAFE_METHODS:

            # We can create a short lived token here to allow someone to access
            # the file URL. We will need to store in the cache framework.
            person = get_person(request)
            key = '{0}:{0}:listen'.format(person.uuid, obj.id)
            cache.set(key, True, 15)
            # logger.debug('  CACHE KEY: {0}'.format(key))
            return True
        else:
            self.message = _("{0} not allowed with this API\
                             endpoint.".format(request.method))
            return False
Ejemplo n.º 19
0
def approve_sentence(request, sentence):
    person = get_person(request)
    try:
        qc = SentenceQualityControl.objects.create(
            person=person,
            approved=True,
            approved_by=request.user,
            notes='Approved in bulk using the admin page.',
            sentence=sentence,
            # object_id=sentence.pk,
            # content_type=ContentType.objects.get_for_model(sentence)
        )
    except:
        return False
    return True
Ejemplo n.º 20
0
    def create(self, validated_data):

        person = get_person(self.context['request'])

        try:
            current_license = License.objects\
                .get(sitelicense__site=settings.SITE_ID)
        except ObjectDoesNotExist:
            return None

        accept_license, created = \
            AcceptLicense.objects.get_or_create(person=person)

        accept_license.license.add(current_license)
        accept_license.save()
        return accept_license
Ejemplo n.º 21
0
    def has_object_permission(self, request, view, obj):
        person = get_person(request)

        if request.method in permissions.SAFE_METHODS:
            if request.user.is_staff:
                return True
            elif person is not None:
                # Allow people to get their own TS objects.
                return person == obj.uploaded_by
        else:
            if request.method in ['PUT', 'DELETE']:
                if request.user.is_staff:
                    return True
                if person is not None:
                    return person == obj.uploaded_by
        return False
Ejemplo n.º 22
0
    def has_permission(self, request, view):
        person = get_person(request)
        if request.method in permissions.SAFE_METHODS:

            if request.user.is_staff:
                return True
            elif person is not None:
                return request.user.is_authenticated
            return False
            # return request.user.is_staff
        else:
            # Anyone can post a transcription
            if request.method in ['POST', 'PUT', 'DELETE']:
                return request.user.is_authenticated

        return False
Ejemplo n.º 23
0
    def get_queryset(self):

        person = get_person(self.request)
        language = get_current_language(self.request)

        people = Person.objects.filter(user__is_staff=True)

        people = people\
            .annotate(
                num_approved=models.Sum(
                    Case(
                        When(
                            recordingqualitycontrol__isnull=True,
                            then=Value(0)),
                        When(
                            recordingqualitycontrol__approved=True,
                            then=Value(1)),
                        When(
                            recordingqualitycontrol__approved=False,
                            then=Value(0)),
                        default=Value(0),
                        output_field=IntegerField())))\
            .annotate(
                num_good=models.Sum(
                    Case(
                        When(
                            recordingqualitycontrol__isnull=True,
                            then=Value(0)),
                        When(
                            recordingqualitycontrol__good__gte=1,
                            then=Value(1)),
                        default=Value(0),
                        output_field=IntegerField())))\
            .annotate(
                num_bad=models.Sum(
                    Case(
                        When(
                            recordingqualitycontrol__isnull=True,
                            then=Value(0)),
                        When(
                            recordingqualitycontrol__bad__gte=1,
                            then=Value(1)),
                        default=Value(0),
                        output_field=IntegerField())))\
            .order_by('-num_reviews')

        return people
Ejemplo n.º 24
0
    def create(self, validated_data):
        request = self.context['request']
        validated_data['uploaded_by'] = get_person(request)

        method = request.GET.get('method', '')
        if 'stream' in method:
            logger.debug(type(validated_data['audio_file']))
            result = transcribe_audio_quick(validated_data['audio_file'])
            text = result['transcription'].strip()
            validated_data['transcription'] = text

        if 'name' not in validated_data.keys():
            fname = validated_data['audio_file'].name
            validated_data['name'] = ''.join(fname.split('.')[:-1])

        return super(AudioFileTranscriptionSerializer,
                     self).create(validated_data)
Ejemplo n.º 25
0
    def create(self, validated_data):
        # This sets a person even when you don't say person=self
        try:
            person = validated_data['person']
            if person is None:
                raise KeyError
        except KeyError:
            person = get_person(self.context['request'])
            if person is not None:
                validated_data['person'] = person

        recording = \
            super(RecordingSerializerPost, self).create(validated_data)

        # result = transcribe_audio(recording, validated_data['audio_file'])

        return recording
Ejemplo n.º 26
0
    def get_queryset(self):
        person = get_person(self.request)

        # Don't listen to one's own recording
        queryset = Recording.objects\
            .exclude(person=person)

        # Exclude all approved recordings
        queryset = queryset\
            .annotate(num_approved=Sum(
                Case(
                    When(
                        quality_control__isnull=True,
                        then=Value(0)),
                    When(
                        quality_control__approved=True,
                        then=Value(1)),
                    When(
                        quality_control__approved=False,
                        then=Value(0)),
                    default=Value(0),
                    output_field=IntegerField())))
        queryset = queryset.exclude(num_approved__gte=1)

        # Exclude items you already listened to
        queryset = queryset.exclude(quality_control__person=person)

        sort_by = self.request.query_params.get('sort_by', '')

        # Let's just get a random recording.
        '''
        queryset = queryset\
            .annotate(num_qc=Count('quality_control'))\
            .order_by('num_qc')
        '''

        if 'random' in sort_by.lower():
            count = queryset.count()
            if count > 1:
                i = random.randint(0, count - 1)
                return [queryset[i]]

        return queryset
Ejemplo n.º 27
0
    def get_context_data(self, **kwargs):
        context = \
            super(PersonRecordingStatsView, self).get_context_data(**kwargs)

        person = get_person(self.request)
        language = get_current_language(self.request)

        recordings = Recording.objects.filter(sentence__language=language,
                                              person=person)

        # Assume for now user is in NZ timezone = UTC + 12 hours
        time_offset = 0
        now = timezone.now() + datetime.timedelta(hours=time_offset)

        # Find the day for NZ, then take us back to utc time.
        today_begining = \
            datetime.datetime.combine(now, datetime.time()) - \
            datetime.timedelta(hours=time_offset)

        # This logical is compared against utc time - the default timezone for our data
        # I presume django deals with this timezoen stuff anyway?
        todays_recordings = recordings.filter(
            created__gte=today_begining).order_by('created')

        stats = {
            'recordings': build_recordings_stat_dict(recordings),
            'recordings_today': build_recordings_stat_dict(todays_recordings)
        }

        stats['recordings_today']['start_time'] = today_begining
        stats['recordings_today']['end_time'] = timezone.now()
        if todays_recordings:
            stats['recordings_today']['earliest_time'] = todays_recordings[
                0].created
            stats['recordings_today']['latest_time'] = todays_recordings[
                todays_recordings.count() - 1].created

        context['person'] = person
        context['stats'] = stats

        return context
Ejemplo n.º 28
0
    def get(self, request, *args, **kwargs):
        m = get_object_or_404(Recording, pk=kwargs['pk'])
        u = request.user
        p = get_person(request)

        audio_file = m.audio_file
        if m.audio_file_aac:
            audio_file = m.audio_file_aac

        key = '{0}:{0}:listen'.format(p.uuid, m.id)
        access = cache.get(key)
        # logger.debug('CAN VIEW:  {0} {1}'.format(key, access))

        if (u.is_authenticated() and u.is_staff) or (p
                                                     == m.person) or (access):
            try:
                url = audio_file.path
                url = audio_file.url
            except:
                try:
                    url = self.get_redirect_url(filepath=audio_file.name)
                except:
                    url = audio_file.url

            if url:
                if self.permanent:
                    return http.HttpResponsePermanentRedirect(url)
                else:
                    return http.HttpResponseRedirect(url)
            else:
                logger.warning('Gone: %s',
                               self.request.path,
                               extra={
                                   'status_code': 410,
                                   'request': self.request
                               })
                return http.HttpResponseGone()
        else:
            raise http.Http404
Ejemplo n.º 29
0
    def has_object_permission(self, request, view, obj):
        if request.user.is_staff and request.user.is_authenticated:
            # Authenticated staff have full access
            return True
        else:
            person = get_person(request)
            if person == obj:
                # Only a person can view/edit his/her own data
                if request.method in permissions.SAFE_METHODS:
                    # No authentication needed to view data
                    return True
                elif request.method in ['POST', 'PUT']:
                    return True  # No longer requiring authentication

                    # Authentication needed to edit data
                    self.message = _("You must sign in to edit your info.")
                    return request.user.is_authenticated
            else:
                self.message = _("You're not allowed to view this information.")
                return False
        self.message = _("You're not allowed to view this information.")
        return False
Ejemplo n.º 30
0
 def get_changeform_initial_data(self, request):
     person = get_person(request)
     return {'uploaded_by': person.id}