diff options
author | Evgeny Fadeev <evgeny.fadeev@gmail.com> | 2012-09-22 14:54:48 -0400 |
---|---|---|
committer | Evgeny Fadeev <evgeny.fadeev@gmail.com> | 2012-09-22 14:54:48 -0400 |
commit | 126e4b40cdc8f49259ac7cb2cf10246a1e8b86ca (patch) | |
tree | bbf62cea718cf23856da8d329c2e8d7dd4684c32 /askbot/context.py | |
parent | 3703cd088fa958cdd0c117fe0f5f72b594b8e6e8 (diff) | |
parent | 47a4bc923966046e6d3e76d4dcf0bfd55f2c4f66 (diff) | |
download | askbot-126e4b40cdc8f49259ac7cb2cf10246a1e8b86ca.tar.gz askbot-126e4b40cdc8f49259ac7cb2cf10246a1e8b86ca.tar.bz2 askbot-126e4b40cdc8f49259ac7cb2cf10246a1e8b86ca.zip |
merged and improved group sorting in the menu
Diffstat (limited to 'askbot/context.py')
-rw-r--r-- | askbot/context.py | 34 |
1 files changed, 23 insertions, 11 deletions
diff --git a/askbot/context.py b/askbot/context.py index 48c1cf3b..5e53febc 100644 --- a/askbot/context.py +++ b/askbot/context.py @@ -59,21 +59,33 @@ def application_settings(request): } if askbot_settings.GROUPS_ENABLED: - + #calculate context needed to list all the groups def _get_group_url(group): + """calculates url to the group based on its id and name""" group_slug = slugify(group['name']) - return reverse('users_by_group', - kwargs={'group_id': group['id'], - 'group_slug': group_slug}) - + return reverse( + 'users_by_group', + kwargs={'group_id': group['id'], 'group_slug': group_slug} + ) + #load id's and names of all groups global_group = models.tag.get_global_group() - groups = models.Group.objects.exclude_personal().exclude(id=global_group.id).order_by('name') - groups = groups.values('id', 'name') - group_list = [{'link': _get_group_url({'name': global_group.name, - 'id': global_group.id}), - 'name': global_group.name},] - for group in groups: + groups = models.Group.objects.exclude_personal() + groups = groups.exclude(id=global_group.id) + groups_data = list(groups.values('id', 'name')) + + #sort groups_data alphanumerically, but case-insensitive + groups_data = sorted( + groups_data, + lambda x, y: cmp(x['name'].lower(), y['name'].lower()) + ) + + #insert data for the global group at the first position + groups_data.insert(0, {'id': global_group.id, 'name': global_group.name}) + + #build group_list for the context + group_list = list() + for group in groups_data: link = _get_group_url(group) group_list.append({'name': group['name'], 'link': link}) context['group_list'] = simplejson.dumps(group_list) |