def my_memberships(request):
    if request.user.is_authenticated:
        ms = request.user.gestalt.memberships.order_by(
            functions.Lower('group__name'))
    else:
        ms = []
    return {'my_memberships': ms}
Пример #2
0
    def list(self, request, *args, **kwargs):
        """List the available tilesets

        Args:
            request (django.http.HTTPRequest): The HTTP request containing
                no parameters

        Returns:
            django.http.JsonResponse: A json file containing a 'count' as
                well as 'results' with each tileset as an entry
        """
        # only return tilesets which are accessible by this user
        if request.user.is_anonymous:
            user = gu.get_anonymous_user()
        else:
            user = request.user

        queryset = self.queryset.filter(dbm.Q(owner=user) | dbm.Q(private=False))

        queryset = queryset.filter(dbm.Q(requiresAuthentication=False))

        if "ac" in request.GET:
            # Autocomplete fields
            queryset = queryset.filter(name__contains=request.GET["ac"])
        if "t" in request.GET:
            # Filter by filetype
            queryset = queryset.filter(filetype=request.GET["t"])
        if "dt" in request.GET:
            # Filter by datatype
            queryset = queryset.filter(datatype__in=request.GET.getlist("dt"))

        if "o" in request.GET:
            if "r" in request.GET:
                queryset = queryset.order_by(dbmf.Lower(request.GET["o"]).desc())
            else:
                queryset = queryset.order_by(dbmf.Lower(request.GET["o"]).asc())

        # ts_serializer = tss.UserFacingTilesetSerializer(queryset, many=True)
        page = self.paginate_queryset(queryset)
        if page is not None:
            serializer = self.get_serializer(page, many=True)
            return self.get_paginated_response(serializer.data)

        serializer = ts_serializer(queryset, many=True)
        return JsonResponse(serializer.data)

        """
Пример #3
0
def all_products(request):
    """" View to return a page showing all products
    and also handle searching and sorting queries. """

    products = Product.objects.all()
    category_list = Category.objects.all()
    categories = None
    query = None
    sort = None
    method = None

    if request.GET:
        if 'sort' in request.GET:
            sort_choice = request.GET['sort']
            sort = sort_choice
            if sort_choice == 'name':
                sort_choice = 'name_lower'
                products = products.annotate(
                    name_lower=functions.Lower('name'))
            if sort_choice == 'category':
                sort_choice = 'category__name'
            if 'method' in request.GET:
                method = request.GET['method']
                if method == 'desc':
                    sort_choice = f'-{sort_choice}'
            products = products.order_by(sort_choice)
        if 'category' in request.GET:
            categories = request.GET['category'].split(',')
            # search for the 'name' field from the category class in database
            products = products.filter(category__name__in=categories)
            categories = Category.objects.filter(name__in=categories)

        if 'q' in request.GET:
            query = request.GET['q']
            if not query:
                messages.error(request, "You did not enter a search criteria.")
                return redirect(reverse('shop'))

            # Case-insensitive queries made to search product name/description
            queries = Q(
                name__icontains=query) | Q(
                    description__icontains=query)
            products = products.filter(queries)

    chosen_sort = f'{sort}_{method}'
    context = {
        'shop': products,
        'chosen_category': categories,
        'chosen_sort': chosen_sort,
        'all_categories': category_list,
        'query': query,
        'method': method
    }

    return render(request, 'products/products.html', context)
Пример #4
0
    def to_representation(self, queryset):
        payload = {"ignoredArticles": "", "index": []}
        queryset = queryset.with_albums_count()
        queryset = queryset.order_by(functions.Lower("name"))
        values = queryset.values("id", "_albums_count", "name")

        first_letter_mapping = collections.defaultdict(list)
        for artist in values:
            if artist["name"]:
                first_letter_mapping[artist["name"][0].upper()].append(artist)

        for letter, artists in sorted(first_letter_mapping.items()):
            letter_data = {
                "name": letter,
                "artist": [get_artist_data(v) for v in artists],
            }
            payload["index"].append(letter_data)
        return payload
Пример #5
0
 def lower(self):
     """
     Converts value value to lowercase.
     """
     return functions.Lower(self._name)