utils.py 2.9 KB

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