views.py 5.1 KB

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