def get_queryset(forvar, taggeditem_model, tag_model): through_opts = taggeditem_model._meta count_field = ( "%s_%s_items" % ( through_opts.app_label, through_opts.object_name)).lower() if forvar is None: # get all tags queryset = tag_model.objects.all() else: # extract app label and model name beginning, applabel, model = None, None, None try: beginning, applabel, model = forvar.rsplit('.', 2) except ValueError: try: applabel, model = forvar.rsplit('.', 1) except ValueError: applabel = forvar applabel = applabel.lower() # filter tagged items if model is None: # Get tags for a whole app queryset = taggeditem_model.objects.filter( content_type__app_label=applabel) tag_ids = queryset.values_list('tag_id', flat=True) queryset = tag_model.objects.filter(id__in=tag_ids) else: # Get tags for a model model = model.lower() if ":" in model: model, manager_attr = model.split(":", 1) else: manager_attr = "tags" model_class = get_model(applabel, model) if not model_class: raise Exception( 'Not found such a model "%s" in the application "%s"' % (model, applabel)) manager = getattr(model_class, manager_attr) queryset = manager.all() through_opts = manager.through._meta count_field = ("%s_%s_items" % (through_opts.app_label, through_opts.object_name)).lower() if count_field is None: # Retain compatibility with older versions of Django taggit # a version check (for example taggit.VERSION <= (0,8,0)) does NOT # work because of the version (0,8,0) of the current dev version of # django-taggit try: return queryset.annotate( num_times=Count(settings.TAG_FIELD_RELATED_NAME)) except FieldError: return queryset.annotate( num_times=Count('taggit_taggeditem_items')) else: return queryset.annotate(num_times=Count(count_field))
def get_queryset(forvar, taggeditem_model, tag_model): through_opts = taggeditem_model._meta count_field = ("%s_%s_items" % (through_opts.app_label, through_opts.object_name)).lower() if forvar is None: # get all tags queryset = tag_model.objects.all() else: # extract app label and model name beginning, applabel, model = None, None, None try: beginning, applabel, model = forvar.rsplit('.', 2) except ValueError: try: applabel, model = forvar.rsplit('.', 1) except ValueError: applabel = forvar applabel = applabel.lower() # filter tagged items if model is None: # Get tags for a whole app queryset = taggeditem_model.objects.filter( content_type__app_label=applabel) tag_ids = queryset.values_list('tag_id', flat=True) queryset = tag_model.objects.filter(id__in=tag_ids) else: # Get tags for a model model = model.lower() if ":" in model: model, manager_attr = model.split(":", 1) else: manager_attr = "tags" model_class = get_model(applabel, model) if not model_class: raise Exception( 'Not found such a model "%s" in the application "%s"' % (model, applabel)) manager = getattr(model_class, manager_attr) queryset = manager.all() through_opts = manager.through._meta count_field = ( "%s_%s_items" % (through_opts.app_label, through_opts.object_name)).lower() if count_field is None: # Retain compatibility with older versions of Django taggit # a version check (for example taggit.VERSION <= (0,8,0)) does NOT # work because of the version (0,8,0) of the current dev version of # django-taggit try: return queryset.annotate( num_times=Count(settings.TAG_FIELD_RELATED_NAME)) except FieldError: return queryset.annotate( num_times=Count('taggit_taggeditem_items')) else: return queryset.annotate(num_times=Count(count_field))
from django.conf import settings from taggit_templatetags2.compat import get_model # define the minimal weight of a tag in the tagcloud TAGCLOUD_MIN = getattr(settings, 'TAGGIT_TAGCLOUD_MIN', 1.0) # define the maximum weight of a tag in the tagcloud TAGCLOUD_MAX = getattr(settings, 'TAGGIT_TAGCLOUD_MAX', 6.0) # define the default models for tags and tagged items TAG_MODEL = getattr(settings, 'TAGGIT_TAG_MODEL', ('taggit', 'Tag')) TAG_MODEL = get_model(*TAG_MODEL) TAGGED_ITEM_MODEL = \ getattr(settings, 'TAGGIT_TAGGED_ITEM_MODEL', ('taggit', 'TaggedItem')) TAGGED_ITEM_MODEL = get_model(*TAGGED_ITEM_MODEL) TAG_FIELD_RELATED_NAME = \ getattr(settings, 'TAGGIT_TAG_FIELD_RELATED_NAME', 'taggeditem_items') LIMIT = getattr(settings, 'TAGGIT_LIMIT', 10) TAG_LIST_ORDER_BY = getattr(settings, 'TAGGIT_TAG_LIST_ORDER_BY', '-num_times') TAG_CLOUD_ORDER_BY = getattr(settings, 'TAGGIT_TAG_CLOUD_ORDER_BY', 'name')