How to use the djangoql.schema.DjangoQLSchema function in djangoql

To help you get started, we’ve selected a few djangoql examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github ivelum / djangoql / test_project / core / admin.py View on Github external
return Q(**{'%s__lt' % search_field: year_end})
        elif operator == '<':
            return Q(**{'%s__gt' % search_field: year_end})
        elif operator == '<=':
            return Q(**{'%s__gte' % search_field: year_start})

    def years_ago(self, n):
        timestamp = now()
        try:
            return timestamp.replace(year=timestamp.year - n)
        except ValueError:
            # February 29
            return timestamp.replace(month=2, day=28, year=timestamp.year - n)


class UserQLSchema(DjangoQLSchema):
    exclude = (Book,)
    suggest_options = {
        Group: ['name'],
    }

    def get_fields(self, model):
        fields = super(UserQLSchema, self).get_fields(model)
        if model == User:
            fields = [UserAgeField(), IntField(name='groups_count')] + fields
        return fields


@admin.register(User)
class CustomUserAdmin(DjangoQLSearchMixin, UserAdmin):
    djangoql_schema = UserQLSchema
    search_fields = ('username', 'first_name', 'last_name')
github ivelum / djangoql / test_project / core / admin.py View on Github external
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User, Group
from django.db.models import Q, Count
from django.utils.timezone import now

from djangoql.admin import DjangoQLSearchMixin
from djangoql.schema import DjangoQLSchema, IntField

from .models import Book


admin.site.unregister(User)


class BookQLSchema(DjangoQLSchema):
    suggest_options = {
        Book: ['genre'],
    }


@admin.register(Book)
class BookAdmin(DjangoQLSearchMixin, admin.ModelAdmin):
    djangoql_schema = BookQLSchema
    list_display = ('name', 'author', 'genre', 'written', 'is_published')
    list_filter = ('is_published',)
    filter_horizontal = ('similar_books',)


class UserAgeField(IntField):
    """
    Search by given number of full years
github ivelum / djangoql / test_project / core / views.py View on Github external
import json

from django.contrib.auth.models import Group, User
from django.shortcuts import render
from django.views.decorators.http import require_GET

from djangoql.exceptions import DjangoQLError
from djangoql.queryset import apply_search
from djangoql.schema import DjangoQLSchema


class UserQLSchema(DjangoQLSchema):
    include = (User, Group)


@require_GET
def completion_demo(request):
    q = request.GET.get('q', '')
    error = ''
    query = User.objects.all().order_by('username')
    if q:
        try:
            query = apply_search(query, q, schema=UserQLSchema)
        except DjangoQLError as e:
            query = query.none()
            error = str(e)
    return render(request, 'completion_demo.html', context={
        'q': q,
github ivelum / djangoql / djangoql / schema.py View on Github external
def relation(self):
        return DjangoQLSchema.model_label(self.related_model)
github kiwicom / the-zoo / zoo / libraries / models.py View on Github external
return re.sub("[^0-9a-zA-Z]+", "-", attribute)


@receiver(models.signals.pre_save, sender=Library)
def generate_slugs(sender, instance, *args, **kwargs):
    instance.owner_slug = slugify_attribute(instance.owner)
    instance.name_slug = slugify_attribute(instance.name)


@receiver(models.signals.pre_save, sender=Library)
def generate_tags(sender, instance, *args, **kwargs):
    if "general" not in instance.tags:
        instance.tags.append("general")


class LibraryQLSchema(DjangoQLSchema):
    include = (Library,)

    def get_fields(self, model):
        if isinstance(model, Library):
            return ["name", "owner", "status", "impact", "library_url"]
        return super(LibraryQLSchema, self).get_fields(model)
github kiwicom / the-zoo / zoo / services / models.py View on Github external
)


class SentryIssueStats(models.Model):
    class Meta:
        unique_together = ("timestamp", "issue")
        verbose_name_plural = "sentry issue stats"

    timestamp = models.DateTimeField()
    count = models.IntegerField()
    issue = models.ForeignKey(
        "services.SentryIssue", on_delete=models.CASCADE, related_name="stats"
    )


class ServiceQLSchema(DjangoQLSchema):
    include = (Service, Environment)

    def get_fields(self, model):
        if isinstance(model, Service):
            return ["name", "owner", "status", "impact"]
        if isinstance(model, Environment):
            return ["service_urls", "health_check_url"]
        return super(ServiceQLSchema, self).get_fields(model)