index.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import requests
  2. from requests.exceptions import RequestException
  3. try:
  4. from packaging.version import parse as parse_version
  5. ALLOW_VERSION_CHECK = True
  6. except ImportError:
  7. ALLOW_VERSION_CHECK = False
  8. from django.contrib.auth import get_user_model
  9. from django.http import Http404, JsonResponse
  10. from django.utils.translation import ugettext as _
  11. from misago import __version__
  12. from misago.conf import settings
  13. from misago.core.cache import cache
  14. from misago.threads.models import Post, Thread
  15. from . import render
  16. VERSION_CHECK_CACHE_KEY = "misago_version_check"
  17. UserModel = get_user_model()
  18. def admin_index(request):
  19. inactive_users_queryset = UserModel.objects.exclude(
  20. requires_activation=UserModel.ACTIVATION_NONE,
  21. )
  22. db_stats = {
  23. 'threads': Thread.objects.count(),
  24. 'posts': Post.objects.count(),
  25. 'users': UserModel.objects.count(),
  26. 'inactive_users': inactive_users_queryset.count()
  27. }
  28. return render(
  29. request, 'misago/admin/index.html', {
  30. 'db_stats': db_stats,
  31. 'address_check': check_misago_address(request),
  32. 'allow_version_check': ALLOW_VERSION_CHECK,
  33. 'version_check': cache.get(VERSION_CHECK_CACHE_KEY),
  34. }
  35. )
  36. def check_misago_address(request):
  37. set_address = settings.MISAGO_ADDRESS
  38. correct_address = request.build_absolute_uri('/')
  39. return {
  40. 'is_correct': set_address == correct_address,
  41. 'set_address': set_address,
  42. 'correct_address': correct_address,
  43. }
  44. def check_version(request):
  45. if not ALLOW_VERSION_CHECK or request.method != "POST":
  46. raise Http404()
  47. version = cache.get(VERSION_CHECK_CACHE_KEY, 'nada')
  48. if version == 'nada':
  49. try:
  50. api_url = 'https://api.github.com/repos/rafalp/Misago/releases'
  51. r = requests.get(api_url)
  52. if r.status_code != requests.codes.ok:
  53. r.raise_for_status()
  54. latest_version = r.json()[0]['tag_name']
  55. latest = parse_version(latest_version)
  56. current = parse_version(__version__)
  57. if latest > current:
  58. version = {
  59. 'is_error': True,
  60. 'message': _("Outdated: %(current)s! (latest: %(latest)s)") % {
  61. 'latest': latest_version,
  62. 'current': __version__,
  63. },
  64. }
  65. else:
  66. version = {
  67. 'is_error': False,
  68. 'message': _("Up to date! (%(current)s)") % {
  69. 'current': __version__,
  70. },
  71. }
  72. cache.set(VERSION_CHECK_CACHE_KEY, version, 180)
  73. except (RequestException, IndexError, KeyError, ValueError) as e:
  74. version = {
  75. 'is_error': True,
  76. 'message': _("Failed to connect to GitHub API. Try again later."),
  77. }
  78. return JsonResponse(version)