def test_profile_dynamic_fields(self): from oioioi.base.preferences import PreferencesFactory from oioioi.base.models import PreferencesSaved def callback_func(sender, **kwargs): self.assertEqual(sender.cleaned_data['dog'], 'Janusz') self.assertEqual(sender.cleaned_data['answer'], 42) try: PreferencesFactory.add_field('dog', CharField, lambda n, u: 'Andrzej', label='Doggy') PreferencesFactory.add_field('answer', IntegerField, lambda n, u: 72, label="The answer to everything") PreferencesSaved.connect(callback_func) self.assertTrue(self.client.login(username='******')) url = reverse('edit_profile') response = self.client.get(url) for text in ['Doggy', 'Andrzej', '72', 'The answer to everything']: self.assertContains(response, text) data = { 'username': '******', 'first_name': 'fn', 'last_name': 'ln', 'email': '*****@*****.**', 'dog': 'Janusz', 'answer': '42' } self.client.post(url, data, follow=True) # callback_func should be called already finally: PreferencesSaved.disconnect(callback_func) PreferencesFactory.remove_field('dog') PreferencesFactory.remove_field('answer')
def test_profile_dynamic_fields(self): from oioioi.base.preferences import PreferencesFactory from oioioi.base.models import PreferencesSaved def callback_func(sender, **kwargs): self.assertEqual(sender.cleaned_data['dog'], 'Janusz') self.assertEqual(sender.cleaned_data['answer'], 42) try: PreferencesFactory.add_field( 'dog', CharField, lambda n, u: 'Andrzej', label='Doggy' ) PreferencesFactory.add_field( 'answer', IntegerField, lambda n, u: 72, label="The answer to everything" ) PreferencesSaved.connect(callback_func) self.assertTrue(self.client.login(username='******')) url = reverse('edit_profile') response = self.client.get(url) for text in ['Doggy', 'Andrzej', '72', 'The answer to everything']: self.assertContains(response, text) data = {'username': '******', 'first_name': 'fn', 'last_name': 'ln', 'email': '*****@*****.**', 'dog': 'Janusz', 'answer': '42'} self.client.post(url, data, follow=True) # callback_func should be called already finally: PreferencesSaved.disconnect(callback_func) PreferencesFactory.remove_field('dog') PreferencesFactory.remove_field('answer')
def serialize_ranking(self, key): partial_key = self.get_partial_key(key) rounds = list(self._rounds_for_key(key)) pis = list(self._filter_pis_for_ranking(partial_key, ProblemInstance.objects.filter(round__in=rounds)). select_related('problem').prefetch_related('round')) users = self.filter_users_for_ranking(key, User.objects.all()) results = UserResultForProblem.objects \ .filter(problem_instance__in=pis, user__in=users) \ .prefetch_related('problem_instance__round') \ .select_related('submission_report', 'problem_instance', 'problem_instance__contest') data = self._get_users_results(pis, results, rounds, users) self._assign_places(data, itemgetter('sum')) return {'rows': data, 'problem_instances': self._get_pis_with_visibility(key, pis), 'participants_on_page': getattr(settings, 'PARTICIPANTS_ON_PAGE', 100)} def update_rankings_with_user_callback(sender, **kwargs): user = sender.instance contests = Contest.objects.filter(probleminstance__submission__user=user) for contest in contests: Ranking.invalidate_contest(contest) PreferencesSaved.connect(update_rankings_with_user_callback)
def serialize_ranking(self, key): partial_key = self.get_partial_key(key) rounds = list(self._rounds_for_key(key)) pis = list(self._filter_pis_for_ranking(partial_key, ProblemInstance.objects.filter(round__in=rounds)). select_related('problem').prefetch_related('round')) users = self.filter_users_for_ranking(key, User.objects.all()) results = UserResultForProblem.objects \ .filter(problem_instance__in=pis, user__in=users) \ .prefetch_related('problem_instance__round') \ .select_related('submission_report', 'problem_instance', 'problem_instance__contest') data = self._get_users_results(pis, results, rounds, users) self._assign_places(data, itemgetter('sum')) return {'rows': data, 'problem_instances': self._get_pis_with_visibility(key, pis), 'participants_on_page': getattr(settings, 'PARTICIPANTS_ON_PAGE', 100)} def update_rankings_with_user_callback(sender, **kwargs): user = sender.instance contests = Contest.objects.filter(probleminstance__submission__user=user) for contest in contests: Ranking.invalidate_contest(contest) PreferencesSaved.connect(update_rankings_with_user_callback)
consents = None if hasattr(user, 'consents'): consents = user.consents else: consents = Consents(user=user) if ('terms_accepted' in form.cleaned_data): consents.terms_accepted = form.cleaned_data['terms_accepted'] if ('marketing_consent' in form.cleaned_data): consents.marketing_consent = form.cleaned_data['marketing_consent'] if ('partner_consent' in form.cleaned_data): consents.partner_consent = form.cleaned_data['partner_consent'] consents.save() PreferencesSaved.connect(save_consents) class CustomCaptchaTextInput(CaptchaTextInput): template_name = 'captcha/custom_field.html' class RegistrationFormWithNames(RegistrationForm): class Media(object): js = ('js/refresh-simple-captcha.js',) def __init__(self, *args, **kwargs): extra = kwargs.pop('extra', {}) super(RegistrationFormWithNames, self).__init__(*args, **kwargs) adjust_username_field(self) tmp_fields = list(self.fields.items())