index.py 2.6 KB

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