utils.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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
  23. category.has_child(item.category)):
  24. top_categories_map[item.category_id] = category
  25. item.top_category = category
  26. else:
  27. # item from other category's scope
  28. for category in categories:
  29. if category.level == 1 and (
  30. category == item.category or
  31. category.has_child(item.category)):
  32. top_categories_map[item.category_id] = category
  33. item.top_category = category
  34. def add_likes_to_posts(user, posts):
  35. if user.is_anonymous():
  36. return
  37. posts_map = {}
  38. for post in posts:
  39. posts_map[post.id] = post
  40. post.is_liked = False
  41. queryset = PostLike.objects.filter(
  42. liker=user, post_id__in=posts_map.keys())
  43. for like in queryset.values('post_id'):
  44. posts_map[like['post_id']].is_liked = True
  45. SUPPORTED_THREAD_ROUTES = {
  46. 'misago:thread': 'pk',
  47. 'misago:thread-post': 'pk',
  48. 'misago:thread-last': 'pk',
  49. 'misago:thread-new': 'pk',
  50. 'misago:thread-unapproved': 'pk',
  51. }
  52. def get_thread_id_from_url(request, url):
  53. try:
  54. clean_url = six.text_type(url).strip()
  55. bits = urlparse(clean_url)
  56. except:
  57. return None
  58. if bits.netloc and bits.netloc != request.get_host():
  59. return None
  60. if bits.path.startswith(request.get_host()):
  61. clean_path = bits.path.lstrip(request.get_host())
  62. else:
  63. clean_path = bits.path
  64. try:
  65. wsgi_alias = request.path[:len(request.path_info) * -1]
  66. resolution = resolve(clean_path[len(wsgi_alias):])
  67. except:
  68. return None
  69. if not resolution.namespaces:
  70. return None
  71. url_name = '{}:{}'.format(':'.join(resolution.namespaces), resolution.url_name)
  72. kwargname = SUPPORTED_THREAD_ROUTES.get(url_name)
  73. if not kwargname:
  74. return None
  75. try:
  76. return int(resolution.kwargs.get(kwargname))
  77. except (TypeError, ValueError):
  78. return None