Skip to content

Commit b1483dd

Browse files
committed
Merge branch 'release/1.3.0b1'
2 parents 494f63e + ba74285 commit b1483dd

File tree

12 files changed

+570
-383
lines changed

12 files changed

+570
-383
lines changed

adminlteui/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
version = '1.3.0b0'
1+
version = '1.3.0b1'
22
default_app_config = 'adminlteui.apps.AdminlteUIConfig'

adminlteui/admin.py

Lines changed: 81 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
1+
import json
12
import traceback
23
from django import forms
34
from django.contrib import admin
45
from django.contrib import messages
56
from django.contrib.admin import widgets
6-
from django.urls import path
7+
from django.urls import path, reverse
78
from django.template.response import TemplateResponse
89
from django.utils import timezone
910
from django.utils.translation import gettext_lazy as _
1011
from django.utils.html import format_html
11-
from django.db import models
1212
from django.conf import settings
13-
from adminlteui.widgets import AdminlteSelect, AdminlteSelectMultiple
13+
from django.http.response import HttpResponse
14+
from adminlteui.widgets import AdminlteSelect
1415
from treebeard.admin import TreeAdmin
1516
from treebeard.forms import movenodeform_factory
16-
from .models import Options, Menu
17+
from .models import Options, Menu, ContentType
1718

1819

1920
def get_option(option_name):
@@ -192,17 +193,80 @@ def general_option_view(self, request):
192193

193194
@admin.register(Menu)
194195
class MenuAdmin(TreeAdmin):
195-
list_display = ('name', 'position', 'link_type', 'link',
196-
'content_type', 'display_icon',
196+
list_display = ('name', 'position', 'link_type', 'display_link',
197+
'display_content_type', 'display_icon',
197198
'valid')
198199
list_filter = ('position', 'link_type', 'valid')
199200
list_editable = ('valid',)
200201
form = movenodeform_factory(Menu)
201202
change_list_template = 'adminlte/menu_change_list.html'
202203
change_form_template = 'adminlte/menu_change_form.html'
203-
formfield_overrides = {
204-
models.ForeignKey: {'widget': AdminlteSelect}
205-
}
204+
205+
class ContentTypeModelChoiceField(forms.ModelChoiceField):
206+
def label_from_instance(self, obj):
207+
return "%s:%s" % (obj.app_label, obj.model)
208+
209+
def formfield_for_foreignkey(self, db_field, request, **kwargs):
210+
"""
211+
reset content_type display text
212+
"""
213+
if db_field.name == 'content_type':
214+
return self.ContentTypeModelChoiceField(
215+
queryset=ContentType.objects.all(),
216+
required=False,
217+
label=_('ContentType'),
218+
widget=AdminlteSelect)
219+
220+
return super().formfield_for_foreignkey(db_field, request, **kwargs)
221+
222+
def get_urls(self):
223+
base_urls = super().get_urls()
224+
urls = [
225+
path('exchange_menu/',
226+
self.admin_site.admin_view(
227+
self.exchange_menu_view,
228+
cacheable=True), name='exchange_menu'),
229+
]
230+
return urls + base_urls
231+
232+
def exchange_menu_view(self, request):
233+
if request.is_ajax():
234+
response_data = dict()
235+
response_data['message'] = 'success'
236+
try:
237+
use_custom_menu = Options.objects.get(
238+
option_name='USE_CUSTOM_MENU')
239+
except Options.DoesNotExist:
240+
use_custom_menu = None
241+
242+
if not use_custom_menu or use_custom_menu.option_value == '0':
243+
use_custom_menu.option_value = '1'
244+
use_custom_menu.save()
245+
246+
else:
247+
use_custom_menu.option_value = '0'
248+
use_custom_menu.save()
249+
return HttpResponse(json.dumps(response_data),
250+
content_type="application/json,charset=utf-8")
251+
return HttpResponse('method not allowed.')
252+
253+
def display_link(self, obj):
254+
if obj.link:
255+
if '/' in obj.link:
256+
return format_html(
257+
'<i class="fa fa-check text-green"></i> {}'.format(
258+
obj.link))
259+
try:
260+
reverse(obj.link)
261+
return format_html(
262+
'<i class="fa fa-check text-green"></i> {}'.format(
263+
obj.link))
264+
except Exception as e:
265+
return format_html(
266+
'<i class="fa fa-close text-red"></i> {}'.format(obj.link))
267+
return '-'
268+
269+
display_link.short_description = _('Link')
206270

207271
def display_icon(self, obj):
208272
if obj.icon:
@@ -211,3 +275,11 @@ def display_icon(self, obj):
211275
return obj.icon
212276

213277
display_icon.short_description = _('Icon')
278+
279+
def display_content_type(self, obj):
280+
if obj.content_type:
281+
return '{}:{}'.format(obj.content_type.app_label,
282+
obj.content_type.model)
283+
return obj.content_type_id
284+
285+
display_content_type.short_description = _('ContentType')
102 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)