general.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. from django.shortcuts import render, get_object_or_404
  2. from django.http import HttpResponseRedirect
  3. from django.urls import reverse
  4. from django.forms import modelformset_factory
  5. #from django.views import generic
  6. from django.contrib.auth.decorators import login_required, permission_required
  7. from website.settings import APPLICATION_NAME
  8. from rotbot.models import Network, Host, Channel, User, Message, Action, Notice, Join, Kick, CurseWord, CurseAdjective
  9. from rotbot.forms import CurseWordForm, CurseAdjectiveForm
  10. from rotbot.views.common import default_keywords, shorten_number, total_messages
  11. def index(request):
  12. networks = Network.objects.all()
  13. hosts = Host.objects.all()
  14. channels = Channel.objects.all()
  15. users = User.objects.all()
  16. joins = Join.objects.all()
  17. kicks = Kick.objects.all()
  18. messages = Message.objects.all()
  19. actions = Action.objects.all()
  20. notices = Notice.objects.all()
  21. cursewords = CurseWord.objects.all()
  22. context = {
  23. 'title': 'RotBot',
  24. 'icon': 'hashtag',
  25. 'description': 'Index of RotBot',
  26. 'keywords': default_keywords('index'),
  27. 'total_networks':shorten_number(networks.count()),
  28. 'total_hosts':shorten_number(hosts.count()),
  29. 'total_channels':shorten_number(channels.count()),
  30. 'total_users':shorten_number(users.count()),
  31. 'total_joins':shorten_number(joins.count()),
  32. 'total_kicks':shorten_number(kicks.count()),
  33. 'total_messages':shorten_number(messages.count()),
  34. 'total_actions':shorten_number(actions.count()),
  35. 'total_notices':shorten_number(notices.count()),
  36. 'total_cursewords':shorten_number(cursewords.count()),
  37. }
  38. return render(request, 'rotbot/index.html', context)
  39. def user(request, user_slug):
  40. user = get_object_or_404(User, slug=user_slug)
  41. network = Network.objects.get(id=user.network.id)
  42. messages = Message.objects.filter(user=user)
  43. actions = Action.objects.filter(user=user)
  44. notices = Notice.objects.filter(user=user)
  45. joins = Join.objects.filter(user=user)
  46. kicks = Kick.objects.filter(kicker=user)
  47. kicked = Kick.objects.filter(kicked=user)
  48. if user.last_event_type == 'pm':
  49. last_event_type = 'speaking privately'
  50. if user.last_event_type == 'pa':
  51. last_event_type = 'sending a private action'
  52. if user.last_event_type == 'pn':
  53. last_event_type = 'sending a private notice'
  54. if user.last_event_type == 'cm':
  55. last_event_type = 'speaking publicly'
  56. if user.last_event_type == 'ca':
  57. last_event_type = 'sending a public action'
  58. if user.last_event_type == 'cn':
  59. last_event_type = 'sending a public notice'
  60. if user.last_event_type == 'ct':
  61. last_event_type = 'changing the topic'
  62. if user.last_event_type == 'ck':
  63. last_event_type = 'changing the password'
  64. if user.last_event_type == 'ci':
  65. last_event_type = 'inviting'
  66. if user.last_event_type == 'cj':
  67. last_event_type = 'joining'
  68. if user.last_event_type == 'cp':
  69. last_event_type == 'parting'
  70. if user.last_event_type == 'ck':
  71. last_event_type = 'kicking'
  72. if user.last_event_type == 'kd':
  73. last_event_type = 'getting kicked'
  74. if user.last_event_type == 'mc':
  75. last_event_type = 'changing modes'
  76. if user.last_event_type == 'nc':
  77. last_event_type = 'nick change'
  78. if user.last_event_type =='sq':
  79. last_event_type = 'quitting'
  80. context = {
  81. 'parent_title': network.name,
  82. 'parent_url': reverse('rotbot:network', args=(network.slug,)),
  83. 'parent_icon': 'sitemap',
  84. 'title': user.name,
  85. 'icon': 'hashtag',
  86. 'description': 'Details of user %s on network %s' % (user.name, network.name),
  87. 'keywords': default_keywords() + '%s, %s' % (network.name, user.name),
  88. 'user': user,
  89. 'network': network,
  90. 'total_messages': shorten_number(total_messages(messages)),
  91. 'total_actions': shorten_number(total_messages(actions)),
  92. 'total_notices': shorten_number(total_messages(notices)),
  93. 'total_joins': shorten_number(joins.count()),
  94. 'total_kicks': shorten_number(kicked.count()),
  95. 'total_kicked': shorten_number(kicked.count()),
  96. 'last_event_type': last_event_type,
  97. }
  98. return render(request, 'rotbot/user.html', context)
  99. @login_required
  100. @permission_required('rotbot.add_curseword', raise_exception=True)
  101. def add_curseword(request):
  102. CurseWordFormSet = modelformset_factory(CurseWord, form=CurseWordForm, extra=4)
  103. CurseAdjectiveFormSet = modelformset_factory(CurseAdjective, form=CurseAdjectiveForm, extra=4)
  104. if request.method == 'POST':
  105. curseword_formset = CurseWordFormSet(request.POST)
  106. curseadjective_formset = CurseAdjectiveFormSet(request.POST)
  107. if curseword_formset.is_valid() and curseadjective_formset.is_valid():
  108. new_cursewords = curseword_formset.save(commit=False)
  109. new_curseadjectives = curseadjective_formset.save(commit=False)
  110. for record in new_cursewords:
  111. record.word = record.word.lower()
  112. record.save()
  113. for record in new_curseadjectives:
  114. record.word = record.word.lower()
  115. record.save()
  116. return HttpResponseRedirect(reverse('rotbot:networks'))
  117. else:
  118. curseword_formset = CurseWordFormSet(queryset=CurseWord.objects.none())
  119. curseadjective_formset = CurseWordFormSet(queryset=CurseAdjective.objects.none())
  120. else: # Not a POST request.
  121. curseword_formset = CurseWordFormSet(queryset=CurseWord.objects.none())
  122. curseadjective_formset = CurseAdjectiveFormSet(queryset=CurseAdjective.objects.none())
  123. context = {
  124. 'parent_title': 'RotBot',
  125. 'parent_url': reverse('rotbot:networks'),
  126. 'parent_icon': 'sitemap',
  127. 'title': 'Add curseword',
  128. 'icon': 'book dead',
  129. 'description': 'Add a curseword to RotBot\'s vocabulary.',
  130. 'keywords': default_keywords(),
  131. 'curseword_formset': curseword_formset,
  132. 'curseadjective_formset': curseadjective_formset,
  133. }
  134. return render(request, 'rotbot/add_curseword.html', context)