index.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. from datetime import timedelta
  2. from django.conf import settings
  3. from django.contrib.auth import get_user_model
  4. from django.core.cache import cache
  5. from django.utils import timezone
  6. from . import render
  7. from ...threads.models import Post, Thread, Attachment
  8. from ...users.models import DataDownload
  9. VERSION_CHECK_CACHE_KEY = "misago_version_check"
  10. User = get_user_model()
  11. def admin_index(request):
  12. totals = count_db_items()
  13. checks = {
  14. "address": check_forum_address(request),
  15. "cache": check_cache(),
  16. "data_downloads": check_data_downloads(),
  17. "debug": check_debug_status(),
  18. "https": check_https(request),
  19. "inactive_users": check_inactive_users(),
  20. }
  21. return render(
  22. request,
  23. "misago/admin/dashboard/index.html",
  24. {"totals": totals, "checks": checks},
  25. )
  26. def check_cache():
  27. cache.set("misago_cache_test", "ok")
  28. return {"is_ok": cache.get("misago_cache_test") == "ok"}
  29. def check_debug_status():
  30. return {"is_ok": not settings.DEBUG}
  31. def check_https(request):
  32. return {"is_ok": request.is_secure()}
  33. def check_forum_address(request):
  34. set_address = request.settings.forum_address
  35. correct_address = request.build_absolute_uri("/")
  36. return {
  37. "is_ok": set_address == correct_address,
  38. "set_address": set_address,
  39. "correct_address": correct_address,
  40. }
  41. def check_data_downloads():
  42. cutoff = timezone.now() - timedelta(days=3)
  43. unprocessed_count = DataDownload.objects.filter(
  44. status__lte=DataDownload.STATUS_PROCESSING, requested_on__lte=cutoff
  45. ).count()
  46. return {"is_ok": unprocessed_count == 0, "count": unprocessed_count}
  47. def check_inactive_users():
  48. count = User.objects.exclude(requires_activation=User.ACTIVATION_NONE).count()
  49. return {"is_ok": count <= 10, "count": count}
  50. def count_db_items():
  51. return {
  52. "attachments": Attachment.objects.count(),
  53. "threads": Thread.objects.count(),
  54. "posts": Post.objects.count(),
  55. "users": User.objects.count(),
  56. }