1+ import json
12import traceback
23from django import forms
34from django .contrib import admin
45from django .contrib import messages
56from django .contrib .admin import widgets
6- from django .urls import path
7+ from django .urls import path , reverse
78from django .template .response import TemplateResponse
89from django .utils import timezone
910from django .utils .translation import gettext_lazy as _
1011from django .utils .html import format_html
11- from django .db import models
1212from django .conf import settings
13- from adminlteui .widgets import AdminlteSelect , AdminlteSelectMultiple
13+ from django .http .response import HttpResponse
14+ from adminlteui .widgets import AdminlteSelect
1415from treebeard .admin import TreeAdmin
1516from treebeard .forms import movenodeform_factory
16- from .models import Options , Menu
17+ from .models import Options , Menu , ContentType
1718
1819
1920def get_option (option_name ):
@@ -192,17 +193,80 @@ def general_option_view(self, request):
192193
193194@admin .register (Menu )
194195class 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' )
0 commit comments