index.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. from datetime import timedelta
  2. import requests
  3. from django.contrib.auth import get_user_model
  4. from django.core.cache import cache
  5. from django.http import Http404, JsonResponse
  6. from django.utils import timezone
  7. from django.utils.translation import gettext as _
  8. from requests.exceptions import RequestException
  9. from . import render
  10. from ... import __released__, __version__
  11. from ...conf import settings
  12. from ...threads.models import Post, Thread, Attachment
  13. from ...users.models import DataDownload
  14. VERSION_CHECK_CACHE_KEY = "misago_version_check"
  15. User = get_user_model()
  16. def admin_index(request):
  17. totals = count_db_items()
  18. checks = {
  19. "address": check_misago_address(request),
  20. "cache": check_cache(),
  21. "data_downloads": check_data_downloads(),
  22. "debug": check_debug_status(),
  23. "https": check_https(request),
  24. "released": check_release_status(),
  25. "inactive_users": check_inactive_users(totals["inactive_users"]),
  26. }
  27. return render(
  28. request,
  29. "misago/admin/dashboard/index.html",
  30. {
  31. "totals": totals,
  32. "checks": checks,
  33. "all_ok": all([c["is_ok"] for c in checks.values()]),
  34. "version_check": cache.get(VERSION_CHECK_CACHE_KEY),
  35. },
  36. )
  37. def check_cache():
  38. cache.set("misago_cache_test", "ok")
  39. return {"is_ok": cache.get("misago_cache_test") == "ok"}
  40. def check_debug_status():
  41. return {"is_ok": not settings.DEBUG}
  42. def check_https(request):
  43. return {"is_ok": request.is_secure()}
  44. def check_release_status():
  45. return {"is_ok": __released__}
  46. def check_misago_address(request):
  47. set_address = settings.MISAGO_ADDRESS
  48. correct_address = request.build_absolute_uri("/")
  49. return {
  50. "is_ok": set_address == correct_address,
  51. "set_address": set_address,
  52. "correct_address": correct_address,
  53. }
  54. def check_data_downloads():
  55. cutoff = timezone.now() - timedelta(days=3)
  56. unprocessed_count = DataDownload.objects.filter(
  57. status__lte=DataDownload.STATUS_PROCESSING, requested_on__lte=cutoff
  58. ).count()
  59. return {"is_ok": unprocessed_count == 0, "count": unprocessed_count}
  60. def check_inactive_users(inactive_count):
  61. return {"is_ok": inactive_count <= 10, "count": inactive_count}
  62. def count_db_items():
  63. return {
  64. "attachments": Attachment.objects.count(),
  65. "threads": Thread.objects.count(),
  66. "posts": Post.objects.count(),
  67. "users": User.objects.count(),
  68. "inactive_users": User.objects.exclude(
  69. requires_activation=User.ACTIVATION_NONE
  70. ).count(),
  71. }
  72. def check_version(request):
  73. if request.method != "POST":
  74. raise Http404()
  75. version = cache.get(VERSION_CHECK_CACHE_KEY, "nada")
  76. if version == "nada":
  77. try:
  78. api_url = "https://pypi.org/pypi/Misago/json"
  79. r = requests.get(api_url)
  80. r.raise_for_status()
  81. latest_version = r.json()["info"]["version"]
  82. if latest_version == __version__:
  83. version = {
  84. "is_error": False,
  85. "message": _("Up to date! (%(current)s)")
  86. % {"current": __version__},
  87. }
  88. else:
  89. version = {
  90. "is_error": True,
  91. "message": _("Outdated: %(current)s! (latest: %(latest)s)")
  92. % {"latest": latest_version, "current": __version__},
  93. }
  94. cache.set(VERSION_CHECK_CACHE_KEY, version, 180)
  95. except (RequestException, IndexError, KeyError, ValueError):
  96. version = {
  97. "is_error": True,
  98. "message": _("Failed to connect to pypi.org API. Try again later."),
  99. }
  100. return JsonResponse(version)