示例#1
0
文件: story.py 项目: inmagik/miller
    def list(self, request):
        stories = self._getUserAuthorizations(request)
        g = Glue(request=request, queryset=stories)

        stories = g.queryset

        if 'status' not in g.filters:
            stories = stories.exclude(status=Story.DELETED)

        if g.warnings is not None:
            # this comes from the VerbosePagination class
            self.paginator.set_queryset_warnings(g.warnings)
            self.paginator.set_queryset_verbose(g.get_verbose_info())

        page = self.paginate_queryset(stories)

        if page is not None:
            serializer = LiteStorySerializer(page,
                                             many=True,
                                             context={'request': request})
            return self.get_paginated_response(serializer.data)

        serializer = LiteStorySerializer(page,
                                         many=True,
                                         context={'request': request})
        return Response(serializer.data)
示例#2
0
    def hallOfFame(self, request):
        g = Glue(request=self.request, queryset=Story.objects.all())

        if not request.user.is_staff:
            g.filters.update({'status': Story.PUBLIC})

        # horrible workaround.
        ids = g.queryset.values('pk')
        # print ids
        tag__filters, tf = filters_from_request(request=self.request,
                                                field_name='tag__filters')

        if g.ordering is None:
            g.ordering = ['-num_stories']
        # print filters
        # top n authors, per story filters.
        top_tags = Tag.objects.filter(**tag__filters).filter(
            story__pk__in=[s['pk'] for s in ids]).annotate(
                num_stories=Count('story', distinct=True)).order_by(
                    *g.ordering)

        page = self.paginate_queryset(top_tags)
        serializer = self.serializer_class(page,
                                           many=True,
                                           context={'request': request})
        return self.get_paginated_response(serializer.data)
示例#3
0
文件: story.py 项目: inmagik/miller
    def comments(self, request, pk=None):
        if pk.isdigit():
            sel = Q(story__pk=pk)
        else:
            sel = Q(story__slug=pk)

        coms = Comment.objects.exclude(status=Comment.DELETED).filter(sel)

        if request.user.is_staff:
            pass
        elif request.user.is_authenticated:
            # I amn the author of the ocomment OR I am the
            coms = coms.filter(
                Q(story__owner=request.user) | Q(status=Comment.PUBLIC)
                | Q(story__authors__user=request.user)
                | Q(story__reviews__assignee__username=request.user.username)
            ).distinct()
        else:
            coms = coms.filter(status=Comment.PUBLIC).filter(
                story__status=Story.PUBLIC).distinct()

        g = Glue(request=request, queryset=coms)

        page = self.paginate_queryset(g.queryset)
        serializer = CommentSerializer(page,
                                       many=True,
                                       context={'request': request})
        return self.get_paginated_response(serializer.data)
示例#4
0
  def search(self, request):
    """
    Add matches to the list of matching story.
    /api/document/?q=world
    cfr. utils.Glue class
    """
    from miller.forms import SearchQueryForm
    from miller.helpers import search_whoosh_index_headline
    form = SearchQueryForm(request.query_params)
    if not form.is_valid():
      return Response(form.errors, status=status.HTTP_201_CREATED)
    
    stories = self._getUserAuthorizations(request)
    # get the results
    g = Glue(request=request, queryset=stories)
    
    # queryset = g.queryset.annotate(matches=RawSQL("SELECT 
    page    = self.paginate_queryset(g.queryset.distinct())

    # get hort_url to get results out of whoosh
    highlights = search_whoosh_index_headline(query=form.cleaned_data['q'], paths=map(lambda x:x.short_url, page))

    def mapper(d):
      d.matches = []
      for hit in highlights:
        if d.short_url == hit['short_url']:
          d.matches = hit
          break
      return d

    serializer = MatchingStorySerializer(map(mapper, page), many=True,
      context={'request': request}
    )
    return self.get_paginated_response(serializer.data)
示例#5
0
 def list(self, request):
     g = Glue(request=request, queryset=self.queryset.distinct())
     page = self.paginate_queryset(g.queryset)
     serializer = self.list_serializer_class(page,
                                             many=True,
                                             context={'request': request})
     return self.get_paginated_response(serializer.data)
示例#6
0
    def list(self, request):
        tags = Tag.objects.all()
        if not request.user.is_staff:
            tags = tags.filter(
                category__in=settings.MILLER_NON_STAFF_TAG_CATEGORIES)

        g = Glue(request=request, queryset=tags)
        page = self.paginate_queryset(g.queryset)

        serializer = TagSerializer(page,
                                   many=True,
                                   context={'request': request})
        return self.get_paginated_response(serializer.data)
示例#7
0
    def suggest(self, request):
        """
    quggest querystring based on this model search 
    """
        form = SearchQueryForm(request.query_params)
        if not form.is_valid():
            return Response(form.errors, status=status.HTTP_400_BAD_REQUEST)

        g = Glue(request=request, queryset=self.queryset, perform_q=False)

        ckey = g.get_hash(request=request)
        print ckey
        if cache.has_key(ckey):
            return Response(cache.get(ckey))

        from django.contrib.postgres.search import SearchVector
        from django.contrib.postgres.search import TrigramSimilarity
        # provided a q

        queryset = g.queryset.annotate(similarity=TrigramSimilarity(
            'ngrams__segment', form.cleaned_data['q']), ).filter(
                similarity__gt=0.35).order_by('-similarity').values_list(
                    'ngrams__segment', flat=True).distinct()[:5]
        # SELECT DISTINCT "miller_ngrams"."segment",
        #     SIMILARITY("miller_ngrams"."segment", europeenne)
        #     FROM "miller_document" LEFT OUTER JOIN "miller_ngrams_documents"
        #     ON ("miller_document"."id" = "miller_ngrams_documents"."document_id")
        #     LEFT OUTER JOIN "miller_ngrams"
        #     ON ("miller_ngrams_documents"."ngrams_id" = "miller_ngrams"."id")
        #     WHERE SIMILARITY("miller_ngrams"."segment", europeenne) > 0.25
        # ORDER BY SIMILARITY("miller_ngrams"."segment", europeenne)
        # DESC LIMIT 20
        # print queryset.query
        d = {'results': queryset}

        cache.set(ckey, d)
        d.update({'cached': False})
        return Response(d)
示例#8
0
    def list(self, request, *args, **kwargs):
        reviews = self.queryset.filter(assignee=request.user)
        g = Glue(queryset=reviews, request=request)
        #filters = filtersFromRequest(request=self.request)
        #ordering = orderbyFromRequest(request=self.request)
        """
    This is the list of your reviews. Access to the uncompleted list via todo
    """
        reviews = g.queryset

        page = self.paginate_queryset(reviews)
        serializer = LiteReviewSerializer(page,
                                          many=True,
                                          context={'request': request})
        return self.get_paginated_response(serializer.data)
示例#9
0
  def pending(self, request):
    """
    Return a list of stories marked for reviews without assigned reviews.
    This is also accessible by reviewers.
    """
    if not request.user.is_authenticated or not request.user.groups.filter(name='chief-reviewers').exists():
      # check 
      raise PermissionDenied()

    qs = self.queryset.filter(status__in=[Story.PENDING, Story.REVIEW, Story.REVIEW_DONE, Story.EDITING]).exclude(authors__user=request.user).distinct()
    g = Glue(request=request, queryset=qs)

    # cannot get your own stories...
    page    = self.paginate_queryset(g.queryset)
    serializer = PendingStorySerializer(page, many=True, context={'request': request})
    return self.get_paginated_response(serializer.data)
示例#10
0
    def list(self, request):
        # authors = self.queryset.annotate(num_stories=Count(Case(
        #     When(stories__status=Story.PUBLIC, then=1),
        #     output_field=CharField(),
        # ),distinct=True))

        authors = self.queryset.distinct()

        g = Glue(request=request,
                 queryset=authors,
                 extra_ordering=['num_stories', 'stories__date_last_modified'])

        page = self.paginate_queryset(g.queryset)
        #print g.queryset.query
        serializer = self.list_serializer_class(page,
                                                many=True,
                                                context={'request': request})
        return self.get_paginated_response(serializer.data)
示例#11
0
    def list(self, request):
        coms = self._getUserAuthorizations(request).exclude(
            status=Comment.DELETED)
        g = Glue(request=request, queryset=coms)

        coms = g.queryset

        # add orderby

        # print coms.query
        page = self.paginate_queryset(coms)

        serializer = CommentSerializer(page,
                                       many=True,
                                       context={'request': request})
        return self.get_paginated_response(serializer.data)

    #  pass
示例#12
0
    def hallOfFame(self, request):
        stories = Story.objects.all()
        if not request.user.is_staff:
            stories = stories.filter(status=Story.PUBLIC)

        g = Glue(queryset=stories, request=request)

        # print g.filters, g.filtersWaterfall

        ids = g.queryset.values('pk')

        # top n authors, per story filters.
        top_authors = Author.objects.filter(
            stories__pk__in=[s['pk'] for s in ids]).annotate(num_stories=Count(
                'stories', distinct=True)).order_by('-num_stories')

        page = self.paginate_queryset(top_authors)
        serializer = LiteAuthorSerializer(page,
                                          many=True,
                                          context={'request': request})
        return self.get_paginated_response(serializer.data)