Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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 search_mode_toggle_enabled(self):
# If search fields were defined on a child ModelAdmin instance,
# we suppose that the developer wants two search modes and therefore
# enable search mode toggle
return self.search_fields != DjangoQLSearchMixin.search_fields
def get_search_results(self, request, queryset, search_term):
if (
self.search_mode_toggle_enabled() and
not self.djangoql_search_enabled(request)
):
return super(DjangoQLSearchMixin, self).get_search_results(
request=request,
queryset=queryset,
search_term=search_term,
)
use_distinct = False
if not search_term:
return queryset, use_distinct
try:
return (
apply_search(queryset, search_term, self.djangoql_schema),
use_distinct,
)
except (DjangoQLError, ValueError, FieldError, ValidationError) as e:
msg = self.djangoql_error_message(e)
messages.add_message(request, messages.WARNING, msg)
return queryset.none(), use_distinct
"UserAdmin",
"XboxLiveAccountAdmin",
"XboxLiveAccountInlineAdmin",
]
User = get_user_model()
# override admin site template
admin.site.login_template = "microsoft/admin_login.html"
# djangoql support
extra_base = []
if apps.is_installed("djangoql"): # pragma: no branch
from djangoql.admin import DjangoQLSearchMixin
extra_base = [DjangoQLSearchMixin]
base_admin = extra_base + [admin.ModelAdmin]
base_user_admin = extra_base + [BaseUserAdmin]
# unregister User mode if it is already registered
if admin.site.is_registered(User): # pragma: no branch
admin.site.unregister(User)
class MicrosoftAccountAdmin(*base_admin):
readonly_fields = ("microsoft_id",)
class MicrosoftAccountInlineAdmin(admin.StackedInline):
model = MicrosoftAccount
readonly_fields = ("microsoft_id",)