views.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. from django.shortcuts import render, get_list_or_404, get_object_or_404
  2. from django.conf import settings
  3. from .models import Networks, Channels, Users
  4. from webgui.common import convert_units
  5. db = settings.BOT_DB
  6. def networks(request):
  7. networks = get_list_or_404(Networks.objects.all())
  8. channels = Channels.objects.all()
  9. users = Users.objects.all()
  10. messages = db.all("SELECT channel_network, messages, actions, notices FROM messages")
  11. totalnetworks = convert_units.shorten_number(len(networks))
  12. totalchannels = convert_units.shorten_number(len(channels))
  13. totalusers = convert_units.shorten_number(len(users))
  14. networkslist = {}
  15. for network in networks:
  16. networkslist[network.name] = {'channelcount': 0, 'usercount': 0, 'messagecount': 0, 'xpspentcount': 0, 'coinspentcount': 0, 'coingivencount': 0}
  17. networkslist[network.name]['host'] = network.host
  18. networkslist[network.name]['port'] = network.port
  19. networkslist[network.name]['usessl'] = network.use_ssl
  20. if network.password:
  21. networkslist[network.name]['password'] = True
  22. isxpspent = False
  23. for user in users:
  24. networkslist[user.network.name]['usercount'] += 1
  25. if user.xp_spent > 1:
  26. isxpspent = True
  27. networkslist[user.network.name]['xpspentcount'] += user.xp_spent
  28. networkslist[user.network.name]['coinspentcount'] += user.coin_spent
  29. networkslist[user.network.name]['coingivencount'] += user.coin_given
  30. for channel in channels:
  31. networkslist[channel.network.name]['channelcount'] += 1
  32. totalmessages = 0
  33. for record in messages:
  34. totalmessages += record[1] + record[2] + record[3]
  35. networkslist[record[0]]['messagecount'] += record[1] + record[2] + record[3]
  36. totalmessages = convert_units.shorten_number(totalmessages)
  37. context = {
  38. 'networks': networks,
  39. 'channels': channels,
  40. 'users': users,
  41. 'messages': messages,
  42. 'totalnetworks': totalnetworks,
  43. 'totalchannels': totalchannels,
  44. 'totalusers': totalusers,
  45. 'totalmessages': totalmessages,
  46. 'networkslist': networkslist,
  47. 'isxpspent': isxpspent,
  48. }
  49. return render(request, 'stats/networks.html', context)
  50. def network(request, network_name):
  51. network = get_object_or_404(Networks, name=network_name)
  52. channels = Channels.objects.filter(network=network_name)
  53. users = Users.objects.filter(network=network_name)
  54. messages = db.all("SELECT channel, \"user\", messages, actions, notices FROM messages WHERE channel_network='" + network_name + "'")
  55. joins = db.all("SELECT channel, \"user\", joins FROM joins WHERE channel_network='" + network_name + "'")
  56. kicks = db.all("SELECT channel, \"user\", given, received FROM kicks WHERE channel_network='" + network_name + "'")
  57. totalchannelstats = convert_units.shorten_number(len(channels))
  58. totaluserstats = convert_units.shorten_number(len(users))
  59. totalmessagestats = 0
  60. totalactionstats = 0
  61. totalnoticestats = 0
  62. for message in messages:
  63. totalmessagestats += message[2]
  64. totalactionstats += message[3]
  65. totalnoticestats += message[4]
  66. totalmessagestats = convert_units.shorten_number(totalmessagestats)
  67. totalactionstats = convert_units.shorten_number(totalactionstats)
  68. totalnoticestats = convert_units.shorten_number(totalnoticestats)
  69. channelslist = {}
  70. for channel in channels:
  71. channelslist[channel.name] = {'joincount': 0, 'messagecount': 0, 'kickcount': 0}
  72. userslist = {}
  73. totalcoinspent = 0
  74. totalcoingiven = 0
  75. totalxpstats = 0
  76. totalcoinstats = 0
  77. isxpspent = False
  78. for user in users:
  79. userslist[user.name] = {'joincount': 0, 'messagecount': 0, 'kgivencount': 0, 'kreceivedcount': 0, 'xpspent': user.xp_spent, 'coin': user.coin}
  80. totalcoinspent += user.coin_spent - user.coin_given
  81. totalcoingiven += user.coin_given
  82. if not user.xp_spent == 0:
  83. isxpspent = True
  84. totalxpstats += user.xp_spent
  85. totalcoinstats += user.coin
  86. totalcoinstats = convert_units.shorten_number(totalcoinstats)
  87. totalxpstats = convert_units.shorten_number(totalxpstats)
  88. for record in joins:
  89. channelslist[record[0]]['joincount'] += 1
  90. userslist[record[1]]['joincount'] += 1
  91. for record in messages:
  92. channelslist[record[0]]['messagecount'] += record[2] + record[3] + record[4]
  93. userslist[record[1]]['messagecount'] += record[2] + record[3] + record[4]
  94. for record in kicks:
  95. channelslist[record[0]]['kickcount'] += record[2]
  96. userslist[record[1]]['kgivencount'] += record[2]
  97. userslist[record[1]]['kreceivedcount'] += record[3]
  98. context = {
  99. 'network': network,
  100. 'channels': channels,
  101. 'users': users,
  102. 'totalchannelstats': totalchannelstats,
  103. 'totaluserstats': totaluserstats,
  104. 'totalmessagestats': totalmessagestats,
  105. 'totalactionstats': totalactionstats,
  106. 'totalnoticestats': totalnoticestats,
  107. 'channelslist': channelslist,
  108. 'userslist': userslist,
  109. 'totalcoinspent': totalcoinspent,
  110. 'totalcoingiven': totalcoingiven,
  111. 'kicks': kicks,
  112. 'isxpspent': isxpspent,
  113. 'totalxpstats': totalxpstats,
  114. 'totalcoinstats': totalcoinstats,
  115. }
  116. return render(request, 'stats/network.html', context)