def add_navnode(request, tree): """Add a new node""" response = {} # get the type of object object_type = request.POST['object_type'] if object_type: app_label, model_name = object_type.split('.') content_type = ContentType.objects.get(app_label=app_label, model=model_name) model_class = content_type.model_class() object_id = request.POST['object_id'] model_name = get_model_label(model_class) if not object_id: raise ValidationError( _("Please choose an existing {0}").format(model_name.lower())) try: obj = model_class.objects.get(id=object_id) except model_class.DoesNotExist: raise ValidationError( _("{0} {1} not found").format(get_model_label(model_class), object_id)) # objects can not be added twice in the navigation tree if models.NavNode.objects.filter(tree=tree, content_type=content_type, object_id=obj.id).count() > 0: raise ValidationError( _("The {0} is already in navigation").format( get_model_label(model_class))) else: content_type = None obj = None # Create the node parent_id = request.POST.get('parent_id', 0) if parent_id: parent = models.NavNode.objects.get(tree=tree, id=parent_id) else: parent = None node = models.create_navigation_node(content_type, obj, tree, parent) response['label'] = node.label response['id'] = 'node_{0}'.format(node.id) response['message'] = _("'{0}' has added to the navigation tree.").format( node.label) return response
def get_suggest_list(request, tree): """call by auto-complete""" response = {} suggestions = [] term = request.POST["term"] # the 1st chars entered in the autocomplete if tree.types.count() == 0: nav_types = models.NavType.objects.all() else: nav_types = tree.types.all() for nav_type in nav_types: content_type = nav_type.content_type if nav_type.label_rule == models.NavType.LABEL_USE_SEARCH_FIELD: # Get the name of the default field for the current type (eg: Page->title, Url->url ...) lookup = {nav_type.search_field + '__icontains': term} objects = content_type.model_class().objects.filter(**lookup) elif nav_type.label_rule == models.NavType.LABEL_USE_GET_LABEL: objects = [ obj for obj in content_type.model_class().objects.all() if term.lower() in obj.get_label().lower() ] else: objects = [ obj for obj in content_type.model_class().objects.all() if term.lower() in '{0}'.format(obj).lower() ] already_in_navigation = [ node.object_id for node in models.NavNode.objects.filter( tree=tree, content_type=content_type) ] # Get suggestions as a list of {label: object.get_label() or unicode if no get_label, 'value':<object.id>} for obj in objects: if obj.id not in already_in_navigation: # Suggest only articles which are not in navigation yet suggestions.append({ 'label': models.get_object_label(content_type, obj), 'value': obj.id, 'category': get_model_label(content_type.model_class()).capitalize(), 'type': content_type.app_label + '.' + content_type.model, }) # Add suggestion for an empty node suggestions.append({ 'label': _("Node"), 'value': 0, 'category': _("Empty node"), 'type': "", }) response['suggestions'] = suggestions return response
def add_navnode(request, tree): """Add a new node""" response = {} # get the type of object object_type = request.POST['object_type'] if object_type: app_label, model_name = object_type.split('.') content_type = ContentType.objects.get(app_label=app_label, model=model_name) model_class = content_type.model_class() object_id = request.POST['object_id'] model_name = get_model_label(model_class) if not object_id: raise ValidationError(_("Please choose an existing {0}").format(model_name.lower())) try: obj = model_class.objects.get(id=object_id) except model_class.DoesNotExist: raise ValidationError(_("{0} {1} not found").format(get_model_label(model_class), object_id)) # objects can not be added twice in the navigation tree if models.NavNode.objects.filter(tree=tree, content_type=content_type, object_id=obj.id).count() > 0: raise ValidationError(_("The {0} is already in navigation").format(get_model_label(model_class))) else: content_type = None obj = None # Create the node parent_id = request.POST.get('parent_id', 0) if parent_id: parent = models.NavNode.objects.get(tree=tree, id=parent_id) else: parent = None node = models.create_navigation_node(content_type, obj, tree, parent) response['label'] = node.label response['id'] = 'node_{0}'.format(node.id) response['message'] = _("'{0}' has added to the navigation tree.").format(node.label) return response
def newsletter_admin(request, context): """show menu""" newsletter = context.get('newsletter') object_class = newsletter.__class__ view_name = 'admin:{0}_{1}_change'.format(get_model_app(object_class), get_model_name(object_class)) try: return make_link( reverse(view_name, args=[newsletter.id]), _('Admin {0}').format(get_model_label(object_class)), 'fugue/table.png', classes=['icon', 'alert_on_click'] ) except NoReverseMatch: pass
def newsletter_admin(request, context): """show menu""" newsletter = context.get('newsletter') object_class = newsletter.__class__ view_name = 'admin:{0}_{1}_change'.format(get_model_app(object_class), get_model_name(object_class)) try: return make_link(reverse(view_name, args=[newsletter.id]), _('Admin {0}').format(get_model_label(object_class)), 'table', classes=['icon', 'alert_on_click']) except NoReverseMatch: pass
def django_admin_edit_object(request, context): """show edit object in admin""" if request and request.user.is_staff and context.get('object', None): obj = context['object'] object_class = obj.__class__ view_name = 'admin:{0}_{1}_change'.format(get_model_app(object_class), get_model_name(object_class)) try: return make_link( reverse(view_name, args=[obj.id]), _('Admin {0}').format(get_model_label(object_class)), 'fugue/table.png', classes=['icon', 'alert_on_click'] ) except NoReverseMatch: pass
def get_suggest_list(request, tree): """call by auto-complete""" response = {} suggestions = [] term = request.POST["term"] # the 1st chars entered in the autocomplete if tree.types.count() == 0: nav_types = models.NavType.objects.all() else: nav_types = tree.types.all() for nav_type in nav_types: content_type = nav_type.content_type if nav_type.label_rule == models.NavType.LABEL_USE_SEARCH_FIELD: # Get the name of the default field for the current type (eg: Page->title, Url->url ...) lookup = {nav_type.search_field + '__icontains': term} objects = content_type.model_class().objects.filter(**lookup) elif nav_type.label_rule == models.NavType.LABEL_USE_GET_LABEL: objects = [ obj for obj in content_type.model_class().objects.all() if term.lower() in obj.get_label().lower() ] else: objects = [ obj for obj in content_type.model_class().objects.all() if term.lower() in '{0}'.format(obj).lower() ] already_in_navigation = [ node.object_id for node in models.NavNode.objects.filter(tree=tree, content_type=content_type) ] # Get suggestions as a list of {label: object.get_label() or unicode if no get_label, 'value':<object.id>} for obj in objects: if obj.id not in already_in_navigation: # Suggest only articles which are not in navigation yet suggestions.append({ 'label': models.get_object_label(content_type, obj), 'value': obj.id, 'category': get_model_label(content_type.model_class()).capitalize(), 'type': content_type.app_label + '.' + content_type.model, }) # Add suggestion for an empty node suggestions.append({ 'label': _("Node"), 'value': 0, 'category': _("Empty node"), 'type': "", }) response['suggestions'] = suggestions return response
def django_admin_list_objects(request, context): """show menu""" if request and request.user.is_staff and (context.get('object', None) or context.get('model', None)): object_class = context.get('model', None) if not object_class: object_class = context['object'].__class__ try: view_name = 'admin:{0}_{1}_changelist'.format(get_model_app(object_class), get_model_name(object_class)) return make_link( reverse(view_name), _('List {0}').format(get_model_label(object_class)), 'fugue/table.png', classes=['icon', 'alert_on_click'] ) except NoReverseMatch: pass
def django_admin_edit_object(request, context): """show edit object in admin""" if request and request.user.is_staff and context.get('object', None): obj = context['object'] object_class = obj.__class__ view_name = 'admin:{0}_{1}_change'.format(get_model_app(object_class), get_model_name(object_class)) try: return make_link(reverse(view_name, args=[obj.id]), _('Admin {0}').format( get_model_label(object_class)), 'table', classes=['icon', 'alert_on_click']) except NoReverseMatch: pass
def django_admin_list_objects(request, context): """show menu""" if request and request.user.is_staff and (context.get('object', None) or context.get('model', None)): object_class = context.get('model', None) if not object_class: object_class = context['object'].__class__ try: view_name = 'admin:{0}_{1}_changelist'.format( get_model_app(object_class), get_model_name(object_class)) return make_link(reverse(view_name), _('List {0}').format( get_model_label(object_class)), 'table', classes=['icon', 'alert_on_click']) except NoReverseMatch: pass