Mercurial > public > sg101
view forums/permissions.py @ 943:cf9918328c64
Haystack tweaks for Django 1.7.7.
I had to upgrade to Haystack 2.3.1 to get it to work with Django
1.7.7. I also had to update the Xapian backend. But I ran into
problems.
On my laptop anyway (Ubuntu 14.0.4), xapian gets mad when search terms
are greater than 245 chars (or something) when indexing. So I created
a custom field that would simply omit terms greater than 64 chars and
used this field everywhere I previously used a CharField.
Secondly, the custom search form was broken now. Something changed in
the Xapian backend and exact searches stopped working. Fortunately the
auto_query (which I was using originally and broke during an upgrade)
started working again. So I cut the search form back over to doing an
auto_query. I kept the form the same (3 fields) because I didn't want
to change the form and I think it's better that way.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Wed, 13 May 2015 20:25:07 -0500 |
parents | ee87ea74d46b |
children |
line wrap: on
line source
""" This module does permissions checking for the forums application. """ from django.core.cache import cache # How long (in secs) to cache group information for various entities: CATEGORY_TIMEOUT = 4 * 60 * 60 FORUM_TIMEOUT = 4 * 60 * 60 USER_TIMEOUT = 15 * 60 def can_access(category, user): """ This function returns True if the given user can access the forum category and False otherwise. """ if user.is_superuser: return True # If this category has no groups assigned to it, return True. Else, return # True if the user belongs to a group that has been assigned to this # category, and False otherwise. # Get the groups assigned to this category. cat_groups = get_category_groups(category) if len(cat_groups) == 0: return True # No groups => public category user_groups = get_user_groups(user) return bool(user_groups & cat_groups) def can_moderate(forum, user): """ Returns True if the user can moderate the forum. """ # Get the simple cases out of the way first: if not user.is_authenticated(): return False elif user.is_superuser: return True # If we get here, we have to see if there is an intersection between the # user's groups and the forum's moderator groups. forum_groups = get_forum_groups(forum) user_groups = get_user_groups(user) return bool(user_groups & forum_groups) def can_post(topic, user): """ Returns True if the user can post in the topic and False otherwise. """ if not user.is_authenticated(): return False if user.is_superuser or can_moderate(topic.forum, user): return True return not topic.locked and can_access(topic.forum.category, user) def get_user_groups(user): """ Returns a set of group ID's that the user belongs to. """ user_groups_key = '%s_groups' % user.username return _get_groups(user_groups_key, user.groups.all(), USER_TIMEOUT) def get_forum_groups(forum): """ Returns a set of group ID's of the forum's moderator groups. """ forum_groups_key = 'forum_%d_mods' % forum.id return _get_groups(forum_groups_key, forum.moderators.all(), FORUM_TIMEOUT) def get_category_groups(category): """ Returns a set of group ID's of the groups that can access this forum category. """ cat_groups_key = 'cat_%d_groups' % category.id return _get_groups(cat_groups_key, category.groups.all(), CATEGORY_TIMEOUT) def _get_groups(key, qs, timeout): """ This internal function contains the code common to the get_xxx_groups() functions. Returns a set of group ID's from the cache. If the set is not found in the cache, the set is generated from the queryset qs and cached with the given timeout. key - the cache key for the set of group ID's qs - the query set of groups to query if the set is not in the cache timeout - the cache timeout to use """ groups = cache.get(key) if groups is None: groups = set([g.id for g in qs]) cache.set(key, groups, timeout) return groups