def page_subtitle(self, value): if not isinstance(value, list): value = [value] clusters = set() for code in value: tag = AlignmentTag.objects.get_from_full_code(code) clusters.add(truncatechars(tag.subcategory, 90)) return "Cluster: %s" % ",".join(sorted(clusters))
def list_tags(request, existing=False): standard = request.POST.get("standard") try: standard = Standard.objects.get(id=int(standard)) except (ValueError, TypeError, Standard.DoesNotExist): return HttpResponseBadRequest() grade = request.POST.get("grade") try: grade = Grade.objects.get(id=int(grade)) except (ValueError, TypeError, Grade.DoesNotExist): return HttpResponseBadRequest() category = request.POST.get("category") try: category = LearningObjectiveCategory.objects.get(id=int(category)) except (ValueError, TypeError, LearningObjectiveCategory.DoesNotExist): return HttpResponseBadRequest() if existing: tags = set( tagged.tag for tagged in TaggedMaterial.objects.filter( tag__standard=standard, tag__grade=grade, tag__category=category ).select_related() ) else: tags = AlignmentTag.objects.filter( standard=standard, grade=grade, category=category ) tags = list(dict(id=t.id, name=unicode(t), subcategory=t.subcategory, full_code=t.full_code) for t in tags) tags.sort(cmp=cmp_tags) # Group tag by subcategory and create the following structure: # tag1.subcategory # - tag1 # - tag2 (has the same subcat as tag1) # - tag3 (has the same subcat as tag2) # tag4.subcategory # - tag4 # - tag5 (has the same subcat as tag4) # .... subcategories = [] items = [] name = None for tag in tags: if tag["subcategory"] != name: # Start filling the next optgroup if name and items: subcategories.append(dict(name=truncatechars(name.rstrip("."), 90), items=items)) name = tag["subcategory"] items = [] items.append(dict(id=tag["id"], name=tag["name"], code=tag["full_code"])) if name and items: # Add the last subcategory to final results subcategories.append(dict(name=truncatechars(name.rstrip("."), 90), items=items)) # Return data organized as simple dropdown. This allows to select # subcategory (used on advanced search page). if "no-optgroups" in request.REQUEST: options = [] for subcat in subcategories: code = "cluster:" + subcat["items"][0]["code"] options.append(dict( id=code, code=code, name=subcat["name"], )) for item in subcat["items"]: item["name"] = " " * 4 + item["name"] options.append(item) return dict(options=options) # Return data organized as optgroups (used in align resource widget). else: return dict(optgroups=subcategories)