Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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,
'error': error,
'search_results': query,
'introspections': json.dumps(UserQLSchema(query.model).as_dict()),
})
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')
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
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,
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
"""
model = User
name = 'age'
def get_lookup_name(self):
"""
We'll be doing comparisons vs. this model field
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')
list_display = ('username', 'first_name', 'last_name', 'is_staff', 'group')
def group(self, obj):
return ', '.join([g.name for g in obj.groups.all()])
group.short_description = 'Groups'
def get_queryset(self, request):
qs = super(CustomUserAdmin, self).get_queryset(request)
return qs.\
annotate(groups_count=Count('groups')).\
prefetch_related('groups')
def get_fields(self, model):
fields = super(UserQLSchema, self).get_fields(model)
if model == User:
fields = [UserAgeField(), IntField(name='groups_count')] + fields
return fields
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
"""
model = User
name = 'age'
def get_lookup_name(self):
"""
We'll be doing comparisons vs. this model field
"""
return 'date_joined'
def get_lookup(self, path, operator, value):
if operator == 'in':
result = None
for year in value:
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,
'error': error,
'search_results': query,
'introspections': json.dumps(UserQLSchema(query.model).as_dict()),
})
value_types_description = 'floating point numbers'
class StrField(DjangoQLField):
type = 'str'
value_types = [text_type]
value_types_description = 'strings'
class BoolField(DjangoQLField):
type = 'bool'
value_types = [bool]
value_types_description = 'True or False'
class DateField(DjangoQLField):
type = 'date'
value_types = [text_type]
value_types_description = 'dates in "YYYY-MM-DD" format'
def validate(self, value):
super(DateField, self).validate(value)
try:
self.get_lookup_value(value)
except ValueError:
raise DjangoQLSchemaError(
'Field "%s" can be compared to dates in '
'"YYYY-MM-DD" format, but not to %s' % (
self.name,
repr(value),
)
)