admin.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. # encoding=utf-8
  2. import logging
  3. import tornado.web
  4. from terroroftinytown.tracker.base import BaseHandler
  5. from terroroftinytown.tracker.form import BlockUsernameForm, UnblockUsernameForm, \
  6. DeleteAllErrorReportsForm, AutoDeleteErrorReportsForm, \
  7. DeleteOneErrorReportForm
  8. from terroroftinytown.tracker.model import BlockedUser, ErrorReport, Result,\
  9. GlobalSetting
  10. from tornado.web import HTTPError
  11. logger = logging.getLogger(__name__)
  12. class AdminHandler(BaseHandler):
  13. @tornado.web.authenticated
  14. def get(self):
  15. # Nothing useful to show for now
  16. self.redirect(self.reverse_url('projects.overview'))
  17. # self.render('admin/overview/index.html')
  18. class BannedHandler(BaseHandler):
  19. @tornado.web.authenticated
  20. def get(self):
  21. form = BlockUsernameForm()
  22. unblock_form = UnblockUsernameForm(self.request.arguments)
  23. self.render(
  24. 'admin/overview/banned.html',
  25. form=form, unblock_form=unblock_form,
  26. usernames=BlockedUser.all_blocked_usernames()
  27. )
  28. @tornado.web.authenticated
  29. def post(self):
  30. action = self.get_argument('action', None)
  31. form = BlockUsernameForm(self.request.arguments)
  32. unblock_form = UnblockUsernameForm(self.request.arguments)
  33. message = None
  34. if action == 'remove':
  35. if unblock_form.validate():
  36. username = self.get_argument('username')
  37. logger.info(self.user_audit_text('Unblocked "%s"'), username)
  38. BlockedUser.unblock_username(username)
  39. message = 'User unblocked.'
  40. else:
  41. if form.validate():
  42. username = form.username.data
  43. logger.info(self.user_audit_text('Blocked "%s"'), username)
  44. BlockedUser.block_username(username)
  45. message = 'User blocked.'
  46. self.render(
  47. 'admin/overview/banned.html',
  48. message=message,
  49. form=form, unblock_form=unblock_form,
  50. usernames=BlockedUser.all_blocked_usernames(),
  51. )
  52. class ErrorReportsListHandler(BaseHandler):
  53. @tornado.web.authenticated
  54. def get(self):
  55. project_id=self.get_argument('project_id', None)
  56. error_reports = ErrorReport.all_reports(
  57. offset_id=int(self.get_argument('offset_id', 0)),
  58. project_id=project_id)
  59. auto_delete_form = AutoDeleteErrorReportsForm(
  60. enabled=GlobalSetting.get_value(
  61. GlobalSetting.AUTO_DELETE_ERROR_REPORTS)
  62. )
  63. delete_one_form = DeleteOneErrorReportForm()
  64. self.render(
  65. 'admin/overview/error_reports.html',
  66. error_reports=error_reports,
  67. delete_all_form=DeleteAllErrorReportsForm(),
  68. auto_delete_form=auto_delete_form,
  69. next_offset_id=error_reports[-1]['id'] if error_reports else 0,
  70. project_id=project_id,
  71. count=ErrorReport.get_count() if project_id is None else 0,
  72. item_action_form=delete_one_form,
  73. )
  74. class ErrorReportsDeleteAllHandler(BaseHandler):
  75. @tornado.web.authenticated
  76. def post(self):
  77. form = DeleteAllErrorReportsForm(self.request.arguments)
  78. if form.validate():
  79. ErrorReport.delete_all()
  80. self.redirect(self.reverse_url('admin.error_reports'))
  81. else:
  82. raise HTTPError(400)
  83. class ErrorReportsDeleteOneHandler(BaseHandler):
  84. @tornado.web.authenticated
  85. def post(self, report_id):
  86. form = DeleteOneErrorReportForm(self.request.arguments)
  87. if form.validate():
  88. ErrorReport.delete_one(report_id)
  89. self.redirect(self.reverse_url('admin.error_reports'))
  90. else:
  91. raise HTTPError(400)
  92. class AutoDeleteErrorReportsSettingHandler(BaseHandler):
  93. @tornado.web.authenticated
  94. def post(self):
  95. form = AutoDeleteErrorReportsForm(self.request.arguments)
  96. if form.validate():
  97. GlobalSetting.set_value(
  98. GlobalSetting.AUTO_DELETE_ERROR_REPORTS, form.enabled.data
  99. )
  100. self.redirect(self.reverse_url('admin.error_reports'))
  101. else:
  102. raise HTTPError(400)
  103. class ResultsHandler(BaseHandler):
  104. @tornado.web.authenticated
  105. def get(self):
  106. args = {k: self.get_argument(k, default) for (k, default) in [('offset_id', 0), ('limit',1000), ('project_id',None)]}
  107. results = tuple(Result.get_results(**args))
  108. self.render(
  109. 'admin/overview/results.html',
  110. count=Result.get_count() if args['project_id'] is None else 0,
  111. results=results,
  112. next_higher_offset_id=int(results[0]['id'])+int(args['limit']) if results else 0,
  113. next_lower_offset_id=int(results[-1]['id'])-1 if results else 0,
  114. **args)