1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- from django.core.urlresolvers import resolve
- from django.utils.six.moves.urllib.parse import urlparse
- def add_categories_to_threads(root_category, categories, threads):
- categories_dict = {}
- for category in categories:
- categories_dict[category.pk] = category
- top_categories_map = {}
- for thread in threads:
- thread.top_category = None
- thread.category = categories_dict[thread.category_id]
- if thread.category == root_category:
- continue
- elif thread.category.parent_id == root_category.pk:
- thread.top_category = thread.category
- elif thread.category_id in top_categories_map:
- thread.top_category = top_categories_map[thread.category_id]
- elif root_category.has_child(thread.category):
- # thread in subcategory resolution
- for category in categories:
- if (category.parent_id == root_category.pk and
- category.has_child(thread.category)):
- top_categories_map[thread.category_id] = category
- thread.top_category = category
- else:
- # global thread in other category resolution
- for category in categories:
- if category.level == 1 and (
- category == thread.category or
- category.has_child(thread.category)):
- top_categories_map[thread.category_id] = category
- thread.top_category = category
- SUPPORTED_THREAD_ROUTES = {
- 'misago:thread': 'pk',
- 'misago:thread-post': 'pk',
- 'misago:thread-last': 'pk',
- 'misago:thread-new': 'pk',
- 'misago:thread-unapproved': 'pk',
- }
- def get_thread_id_from_url(request, url):
- try:
- bits = urlparse(url)
- except:
- return None
- if bits.netloc:
- if bits.port:
- hostname = ':'.join((bits.netloc, bits.port))
- else:
- hostname = bits.netloc
- if hostname != request.get_host():
- return None
- if bits.path.startswith(request.get_host()):
- clean_path = bits.path.lstrip(request.get_host())
- else:
- clean_path = bits.path
- try:
- wsgi_alias = request.path[:len(request.path_info)]
- resolution = resolve(clean_path[len(wsgi_alias):])
- except:
- return None
- if not resolution.namespaces:
- return None
- url_name = '{}:{}'.format(':'.join(resolution.namespaces), resolution.url_name)
- kwargname = SUPPORTED_THREAD_ROUTES.get(url_name)
- if not kwargname:
- return None
- try:
- return int(resolution.kwargs.get(kwargname))
- except (TypeError, ValueError):
- return None
|