views.py 4.5 KB

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