utils.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. from django.urls import resolve
  2. from django.utils import six
  3. from django.utils.six.moves.urllib.parse import urlparse
  4. from .models import PostLike
  5. def add_categories_to_items(root_category, categories, items):
  6. categories_dict = {}
  7. for category in categories:
  8. categories_dict[category.pk] = category
  9. top_categories_map = {}
  10. for item in items:
  11. item.top_category = None
  12. item.category = categories_dict[item.category_id]
  13. if item.category == root_category:
  14. continue
  15. elif item.category.parent_id == root_category.pk:
  16. item.top_category = item.category
  17. elif item.category_id in top_categories_map:
  18. item.top_category = top_categories_map[item.category_id]
  19. elif root_category.has_child(item.category):
  20. # item in subcategory resolution
  21. for category in categories:
  22. if (category.parent_id == root_category.pk and category.has_child(item.category)):
  23. top_categories_map[item.category_id] = category
  24. item.top_category = category
  25. else:
  26. # item from other category's scope
  27. for category in categories:
  28. category_is_parent = category.has_child(item.category)
  29. if category.level == 1 and (category == item.category or category_is_parent):
  30. top_categories_map[item.category_id] = category
  31. item.top_category = category
  32. def add_likes_to_posts(user, posts):
  33. if user.is_anonymous:
  34. return
  35. posts_map = {}
  36. for post in posts:
  37. posts_map[post.id] = post
  38. post.is_liked = False
  39. queryset = PostLike.objects.filter(liker=user, post_id__in=posts_map.keys())
  40. for like in queryset.values('post_id'):
  41. posts_map[like['post_id']].is_liked = True
  42. SUPPORTED_THREAD_ROUTES = {
  43. 'misago:thread': 'pk',
  44. 'misago:thread-post': 'pk',
  45. 'misago:thread-last': 'pk',
  46. 'misago:thread-new': 'pk',
  47. 'misago:thread-unapproved': 'pk',
  48. }
  49. def get_thread_id_from_url(request, url):
  50. try:
  51. clean_url = six.text_type(url).strip()
  52. bits = urlparse(clean_url)
  53. except:
  54. return None
  55. if bits.netloc and bits.netloc != request.get_host():
  56. return None
  57. if bits.path.startswith(request.get_host()):
  58. clean_path = bits.path.lstrip(request.get_host())
  59. else:
  60. clean_path = bits.path
  61. try:
  62. wsgi_alias = request.path[:len(request.path_info) * -1]
  63. resolution = resolve(clean_path[len(wsgi_alias):])
  64. except:
  65. return None
  66. if not resolution.namespaces:
  67. return None
  68. url_name = '{}:{}'.format(':'.join(resolution.namespaces), resolution.url_name)
  69. kwargname = SUPPORTED_THREAD_ROUTES.get(url_name)
  70. if not kwargname:
  71. return None
  72. try:
  73. return int(resolution.kwargs.get(kwargname))
  74. except (TypeError, ValueError):
  75. return None