views.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. if user.coin_spent > 1:
  28. iscoinspent = True
  29. networkslist[user.network.name]['xpspentcount'] += user.xp_spent
  30. networkslist[user.network.name]['coinspentcount'] += user.coin_spent
  31. networkslist[user.network.name]['coingivencount'] += user.coin_given
  32. for channel in channels:
  33. networkslist[channel.network.name]['channelcount'] += 1
  34. totalmessages = 0
  35. for record in messages:
  36. totalmessages += record[1] + record[2] + record[3]
  37. networkslist[record[0]]['messagecount'] += record[1] + record[2] + record[3]
  38. totalmessages = convert_units.shorten_number(totalmessages)
  39. context = {
  40. 'networks': networks,
  41. 'channels': channels,
  42. 'users': users,
  43. 'messages': messages,
  44. 'totalnetworks': totalnetworks,
  45. 'totalchannels': totalchannels,
  46. 'totalusers': totalusers,
  47. 'totalmessages': totalmessages,
  48. 'networkslist': networkslist,
  49. 'isxpspent': isxpspent,
  50. 'iscoinspent': iscoinspent,
  51. }
  52. return render(request, 'stats/networks.html', context)
  53. def network(request, network_name):
  54. network = get_object_or_404(Networks, name=network_name)
  55. channels = Channels.objects.filter(network=network_name)
  56. users = Users.objects.filter(network=network_name)
  57. messages = db.all("SELECT channel, \"user\", messages, actions, notices FROM messages WHERE channel_network='" + network_name + "'")
  58. joins = db.all("SELECT channel, \"user\", joins FROM joins WHERE channel_network='" + network_name + "'")
  59. kicks = db.all("SELECT channel, \"user\", given, received FROM kicks WHERE channel_network='" + network_name + "'")
  60. totalchannelstats = convert_units.shorten_number(len(channels))
  61. totaluserstats = convert_units.shorten_number(len(users))
  62. totalmessagestats = 0
  63. totalactionstats = 0
  64. totalnoticestats = 0
  65. for message in messages:
  66. totalmessagestats += message[2]
  67. totalactionstats += message[3]
  68. totalnoticestats += message[4]
  69. totalmessagestats = convert_units.shorten_number(totalmessagestats)
  70. totalactionstats = convert_units.shorten_number(totalactionstats)
  71. totalnoticestats = convert_units.shorten_number(totalnoticestats)
  72. channelslist = {}
  73. for channel in channels:
  74. channelslist[channel.name] = {'joincount': 0, 'messagecount': 0, 'kickcount': 0}
  75. userslist = {}
  76. totalcoinspent = 0
  77. totalcoingiven = 0
  78. totalxpstats = 0
  79. totalcoinstats = 0
  80. isxpspent = False
  81. for user in users:
  82. userslist[user.name] = {'joincount': 0, 'messagecount': 0, 'kgivencount': 0, 'kreceivedcount': 0, 'xpspent': user.xp_spent, 'coin': user.coin}
  83. totalcoinspent += user.coin_spent - user.coin_given
  84. totalcoingiven += user.coin_given
  85. if not user.xp_spent == 0:
  86. isxpspent = True
  87. totalxpstats += user.xp_spent
  88. totalcoinstats += user.coin
  89. totalcoinstats = convert_units.shorten_number(totalcoinstats)
  90. totalxpstats = convert_units.shorten_number(totalxpstats)
  91. for record in joins:
  92. channelslist[record[0]]['joincount'] += 1
  93. userslist[record[1]]['joincount'] += 1
  94. for record in messages:
  95. channelslist[record[0]]['messagecount'] += record[2] + record[3] + record[4]
  96. userslist[record[1]]['messagecount'] += record[2] + record[3] + record[4]
  97. for record in kicks:
  98. channelslist[record[0]]['kickcount'] += record[2]
  99. userslist[record[1]]['kgivencount'] += record[2]
  100. userslist[record[1]]['kreceivedcount'] += record[3]
  101. context = {
  102. 'network': network,
  103. 'channels': channels,
  104. 'users': users,
  105. 'totalchannelstats': totalchannelstats,
  106. 'totaluserstats': totaluserstats,
  107. 'totalmessagestats': totalmessagestats,
  108. 'totalactionstats': totalactionstats,
  109. 'totalnoticestats': totalnoticestats,
  110. 'channelslist': channelslist,
  111. 'userslist': userslist,
  112. 'totalcoinspent': totalcoinspent,
  113. 'totalcoingiven': totalcoingiven,
  114. 'kicks': kicks,
  115. 'isxpspent': isxpspent,
  116. 'totalxpstats': totalxpstats,
  117. 'totalcoinstats': totalcoinstats,
  118. }
  119. return render(request, 'stats/network.html', context)