def wrap(request, *args, **kwargs): """ this function uses to prevent unauthorized access to some apps (such as 'staff', 'control' and so on) user must have passed 'access' option in access_list json object in order to visit page :return: render a template if user is not authorized """ user = MyUser(request.user.username) if not user.has_access(access): return render(request, 'main/admin_wrapper.html', {'unauthorized': True}) return function(request, *args, **kwargs)
def create(request): if not request.POST: return HttpResponseRedirect('/moders/') form = ReportForm(request.POST) if form.is_valid(): if not request.recaptcha_is_valid: return HttpResponseRedirect('/moders/new_report/') violator_nickname = request.POST['violator_nickname'] violator_donate = request.POST['violator_donate'] violation = request.POST['violation'] comment = request.POST['comment'].replace('\r\n', '<br>') server = request.POST['server'] evidence_text = request.POST['evidence_text'].replace('\r\n', '<br>') pinned_admin = get_username(request.user) isimportant = True if request.POST.get('isImportant') == 'on' else False iswarning = True if request.POST.get('isWarning') == 'on' else False files = request.FILES.getlist('dropZone', None) report = Reports(violator_nickname=violator_nickname, violator_donate=violator_donate, violation=violation, comment=comment, server=server, evidence_text=evidence_text, pinned_admin=pinned_admin, isImportant=isimportant, isWarning=iswarning, files=files) report.create() messages.success(request, 'Отчет сохранен.') else: messages.error(request, 'Форма заполнена неверно.') return HttpResponseRedirect('/moders/new_report/') user = MyUser(request.user.username) if user.has_access('moders'): return HttpResponseRedirect('/moders/') return HttpResponseRedirect('/moders/new_report/')
def wrap(request, *args, **kwargs): """ this function uses in main urls.py file in order to restrict access for everyone, except authorized users (users which are have 'maintenance' option in access_list), when config('MAINTENANCE') is True if user has 'maintenance' option in access_list, then it can visit on page with constant 'Maintenance access' message else it will be not able to visit page :return: render maintenance template if maintenance is on and user is not authorized """ if settings.MAINTENANCE: user = MyUser(request.user.username) if not user.has_access('maintenance'): return render(request, 'main/errors/maintenance.html') messages.warning(request, 'Maintenance access') return function(request, *args, **kwargs)
def index(request): """ `rank` dict should look like this: ranks = { 'osx11': { 'rank': ('rankosx', 'Гл. Администратор'), 'online': 1024.0, 'rating': (4.8, 10), 'applications_considered': 123, 'additional': [('expert', 'Эксперт'), ('paperwork', 'Бумажная работа')] }, """ ranks = {} for django_user in User.objects.all().exclude(username='******'): username = django_user.username user = MyUser(username) if user.has_access('staff'): continue ranks[username] = {'rank': (str, str), 'online': str, 'rating': str, 'additional': list} ranks[username]['rank'] = (user.get_rank_name(), user.get_rank_title()) ranks[username]['online'] = user.get_online() ranks[username]['rating'] = (user.get_rating(), user.get_vote_count()) ranks[username]['applications_considered'] = user.get_applications_considered() ranks[username]['additional'] = user.get_additional_ranks() ranks = dict(sorted(ranks.items(), key=lambda item: item[1]['rank'][0], reverse=True)) return render(request, 'our_team/our_team.html', {'messages': messages.get_messages(request), 'ranks': ranks})
class TestClass(TestCase): USERNAME = '******' TRACK_FOR_STAT = False DARK_THEME = False PASSWORD = '******' FIRST_NAME = 'test_user_name' LAST_NAME = 'test_user_surname' def create_user(self): self.user.create(track_for_stat=self.TRACK_FOR_STAT, dark_theme=self.DARK_THEME, password=self.PASSWORD, first_name=self.FIRST_NAME, last_name=self.LAST_NAME) def setUp(self): self.user = MyUser(self.USERNAME) def test_create_repeatedly(self): with self.assertRaises(UserIsAlreadyExistsException): self.create_user() self.create_user() def test_create_normally(self): self.create_user() self.assertEquals(self.user.user.username, self.USERNAME) self.assertEquals(self.user.user.userprofile.track_for_stat, self.TRACK_FOR_STAT) self.assertEquals(self.user.user.first_name, self.FIRST_NAME) self.assertEquals(self.user.user.last_name, self.LAST_NAME) def test_remove_nonexistent(self): with self.assertRaises(UserNotFoundException): self.user.remove() def test_manage_access_nonexistent(self): with self.assertRaises(UserNotFoundException): self.user.manage_access('control') def test_manage_access_normally(self): self.create_user() self.user.manage_access(['control']) self.assertTrue(self.user.has_access('control')) self.user.manage_access(['logs', 'moders']) self.assertTrue(self.user.has_access('logs')) self.assertTrue(self.user.has_access('moders')) def test_manage_rank_nonexistent(self): with self.assertRaises(UserNotFoundException): self.user.manage_rank('rank2') def test_manage_rank_nonexistent_rank(self): self.create_user() with self.assertRaises(RankNotFoundException): self.user.manage_rank('nonexistent_rank') def test_manage_rank_normally(self): self.create_user() rank_data = loads(self.user.user.userprofile.ranks) self.assertEquals(rank_data['rank'], 'rank0') self.user.manage_rank('rank4') rank_data = loads(self.user.user.userprofile.ranks) self.assertEquals(rank_data['rank'], 'rank4') def test_manage_additional_ranks_nonexistent(self): with self.assertRaises(UserNotFoundException): self.user.manage_additional_ranks(['paperwork']) def test_manage_additional_ranks_nonexistent_rank(self): self.create_user() with self.assertRaises(RankNotFoundException): self.user.manage_additional_ranks(['nonexistent_rank']) def test_manage_additional_ranks_normally(self): self.create_user() rank_data = loads(self.user.user.userprofile.ranks) self.assertEquals(len(rank_data['additional']), 0) self.user.manage_additional_ranks(['paperwork', 'expert']) rank_data = loads(self.user.user.userprofile.ranks) self.assertTrue('paperwork' in rank_data['additional']) self.assertTrue('expert' in rank_data['additional']) def test_manage_additional_ranks_save_automatic_assigned_ranks(self): self.create_user() self.user.manage_additional_ranks( ['expert', 'publicaccept', 'online500']) self.user.manage_additional_ranks(['paperwork']) self.assertEquals(self.user.get_additional_ranks_names_only(), ['paperwork', 'online500', 'publicaccept']) def test_toggle_tracking_nonexistent(self): with self.assertRaises(UserNotFoundException): self.user.toggle_tracking() def test_toggle_tracking_normally(self): self.create_user() self.user.toggle_tracking() self.assertEquals(self.user.user.userprofile.track_for_stat, False if self.TRACK_FOR_STAT else True) self.user.toggle_tracking() self.assertEquals(self.user.user.userprofile.track_for_stat, True if self.TRACK_FOR_STAT else False) def test_toggle_dark_theme_nonexistent(self): with self.assertRaises(UserNotFoundException): self.user.toggle_dark_theme() def test_toggle_dark_theme_normally(self): self.create_user() self.user.toggle_dark_theme() self.assertEquals(self.user.user.userprofile.dark_theme, False if self.DARK_THEME else True) self.user.toggle_dark_theme() self.assertEquals(self.user.user.userprofile.dark_theme, True if self.DARK_THEME else False) def test_get_tracked_for_stat(self): self.create_user() self.user.toggle_tracking() authorized_users = get_tracked_for_stat() self.assertEquals( authorized_users, {self.user.username: self.user.user.userprofile.color}) def test_rating_incorrect_string(self): self.create_user() with self.assertRaises(ValueError): self.user.rate('5') def test_rating_incorrect_float(self): self.create_user() with self.assertRaises(ValueError): self.user.rate(5.0) def test_rating_incorrect_more_than_five(self): self.create_user() with self.assertRaises(ValueError): self.user.rate(6) def test_rating_incorrect_less_than_one(self): self.create_user() with self.assertRaises(ValueError): self.user.rate(0) def test_rating_normal(self): self.create_user() self.assertEquals(self.user.get_rating(), 0.0) self.assertEquals(self.user.get_total_vote(), 0) self.assertEquals(self.user.get_vote_count(), 0) self.user.rate(3) self.assertEquals(self.user.get_rating(), 3.0) self.assertEquals(self.user.get_total_vote(), 3) self.assertEquals(self.user.get_vote_count(), 1) self.user.rate(5) self.assertEquals(self.user.get_rating(), 4.0) self.assertEquals(self.user.get_total_vote(), 8) self.assertEquals(self.user.get_vote_count(), 2) self.user.rate(2) self.assertEquals(self.user.get_rating(), 3.3) self.assertEquals(self.user.get_total_vote(), 10) self.assertEquals(self.user.get_vote_count(), 3) def test_add_online_negative(self): self.create_user() with self.assertRaises(ValueError): self.user.add_online(-5) def test_add_online_normal(self): self.create_user() self.assertEquals(self.user.get_online(), 0) self.user.add_online(5) self.assertEquals(self.user.get_online(), 5) self.user.add_online(10) self.assertEquals(self.user.get_online(), 15) def test_add_online_automatic_rank_assign(self): self.create_user() self.user.add_online(502) self.assertEquals(self.user.get_additional_ranks_names_only(), ['online500']) self.user.add_online(502) self.assertEquals(self.user.get_additional_ranks_names_only(), ['online1000']) self.user.manage_additional_ranks(['paperwork', 'expert']) self.user.add_online(1000) self.assertEquals(self.user.get_additional_ranks_names_only(), ['paperwork', 'expert', 'online2000'])