From e920c28bdc8af86e9d7328a79c8ed42e4986bcfc Mon Sep 17 00:00:00 2001 From: Jan Szejko Date: Thu, 29 Dec 2016 18:56:29 +0100 Subject: [PATCH] stage2 - keys and emails --- stage2/management/__init__.py | 0 stage2/management/commands/__init__.py | 0 .../commands/stage2_email_teachers.py | 42 ++++++++++++ .../commands/stage2_generate_keys.py | 22 ++++++ .../management/commands/stage2_send_keys.py | 67 +++++++++++++++++++ stage2/templates/stage2/email_key.txt | 16 +++++ 6 files changed, 147 insertions(+) create mode 100644 stage2/management/__init__.py create mode 100644 stage2/management/commands/__init__.py create mode 100644 stage2/management/commands/stage2_email_teachers.py create mode 100644 stage2/management/commands/stage2_generate_keys.py create mode 100644 stage2/management/commands/stage2_send_keys.py create mode 100644 stage2/templates/stage2/email_key.txt diff --git a/stage2/management/__init__.py b/stage2/management/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/stage2/management/commands/__init__.py b/stage2/management/commands/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/stage2/management/commands/stage2_email_teachers.py b/stage2/management/commands/stage2_email_teachers.py new file mode 100644 index 0000000..dceb831 --- /dev/null +++ b/stage2/management/commands/stage2_email_teachers.py @@ -0,0 +1,42 @@ +# -*- coding: utf-8 -*- + +from django.core.management.base import BaseCommand +from django.template.loader import render_to_string + +from contact.models import Contact +from stage2.models import Participant +from wtem.management.commands import send_mail + + +class Command(BaseCommand): + def handle(self, *args, **options): + sent = 0 + failed = 0 + + query = Participant.objects.order_by('contact__contact').distinct('contact__contact')\ + .values_list('contact__contact', flat=True) + template_name = args[0] + message = render_to_string('wtem/' + template_name + '.txt') + subject = render_to_string('wtem/' + template_name + '_subject.txt') + + answer = raw_input( + 'Send the following to %d teachers with subject "%s"\n\n %s\n\n?' % + (query.count(), subject.encode('utf8'), message.encode('utf8'))) + + if answer == 'yes': + for contact in query: + try: + self.send_message(message, subject, contact) + except Exception as e: + failed += 1 + self.stdout.write('failed sending to: ' + contact + ' - ' + str(e)) + else: + sent += 1 + self.stdout.write('message sent to: ' + contact) + + self.stdout.write('sent: %s, failed: %s' % (sent, failed)) + + def send_message(self, message, subject, email): + self.stdout.write('>>> sending to %s' % email) + send_mail(subject=subject, body=message, to=[email]) + diff --git a/stage2/management/commands/stage2_generate_keys.py b/stage2/management/commands/stage2_generate_keys.py new file mode 100644 index 0000000..c23c5f8 --- /dev/null +++ b/stage2/management/commands/stage2_generate_keys.py @@ -0,0 +1,22 @@ +# -*- coding: utf-8 -*- +from django.core.management.base import BaseCommand + +from stage2.models import Participant +from wtem.management.commands.wtem_send_results import minimum, get_submissions + + +class Command(BaseCommand): + + def handle(self, **options): + new = 0 + skipped = 0 + for s in get_submissions(): + if s.final_result >= minimum: + if not Participant.objects.filter(email=s.email).exists(): + Participant.create(s.first_name, s.last_name, s.email, contact=s.contact) + new += 1 + else: + self.stdout.write('skipping ' + s.email + ': already exists.') + skipped += 1 + + self.stdout.write('New: ' + str(new) + ', skipped: ' + str(skipped)) diff --git a/stage2/management/commands/stage2_send_keys.py b/stage2/management/commands/stage2_send_keys.py new file mode 100644 index 0000000..1c94431 --- /dev/null +++ b/stage2/management/commands/stage2_send_keys.py @@ -0,0 +1,67 @@ +# -*- coding: utf-8 -*- + +from optparse import make_option + +from django.core.management.base import BaseCommand +from wtem.management.commands import send_mail +from django.template.loader import render_to_string + +from stage2.models import Participant + + +class Command(BaseCommand): + help = 'Sends personalized links to WTEM contestants' + args = ', , ...' + + option_list = BaseCommand.option_list + ( + make_option( + '--all', + action='store_true', + dest='all', + default=False, + help='Use all available participants'), + make_option( + '--force', + action='store_true', + dest='force', + default=False, + help='Force sending key even if one was already sent') + ) + + def handle(self, *args, **options): + if len(args) or options['all']: + return self.send_keys(*args, **options) + self.stdout.write('No participants selected') + + def send_keys(self, *args, **options): + sent = 0 + failed = 0 + + participants = Participant.objects.all() + if not options['force']: + participants = participants.filter(key_sent=False) + if len(args): + participants = participants.filter(email__in=args) + + for participant in participants: + assert len(participant.key) == 30 + + try: + self.send_key(participant) + except Exception as e: + failed += 1 + self.stdout.write('failed sending to: ' + participant.email + ' - ' + str(e)) + else: + participant.key_sent = True + participant.save() + sent += 1 + self.stdout.write('key sent to: ' + participant.email) + + self.stdout.write('sent: ' + str(sent)) + + def send_key(self, participant): + self.stdout.write('>>> sending to ' + participant.email) + send_mail( + subject="Olimpiada Cyfrowa - Twój link do drugiego etapu", + body=render_to_string('stage2/email_key.txt', {'participant': participant}), + to=[participant.email]) diff --git a/stage2/templates/stage2/email_key.txt b/stage2/templates/stage2/email_key.txt new file mode 100644 index 0000000..9ce4d8a --- /dev/null +++ b/stage2/templates/stage2/email_key.txt @@ -0,0 +1,16 @@ +Poniżej znajduje się wygenerowany specjalnie dla Ciebie link, pod którym będziesz mógł/mogła rozwiązać zadania II etapu Olimpiady Cyfrowej: + +https://olimpiadacyfrowa.pl{% url 'stage2_participant' participant_id=participant.id key=participant.key %} + +I etap Olimpiady odbędzie się we wtorek 15 listopada o godz. 10:00 w sali wyznaczonej przez Komisję Szkolną. Na rozwiązanie testu będziesz mieć ok. 90 min. Aby rozwiązać test, potrzebny Ci będzie komputer ze stabilnym łączem internetowym oraz zainstalowaną i zaktualizowaną przeglądarką. + +Każdy uczestnik otrzymał indywidualny link, pod którym może rozwiązywać zadania samodzielnie. Wszelkie działania mogące świadczyć o tym, że uczestnik nie rozwiązuje testu samodzielnie, np. wchodzenie na stronę z zadaniami przez więcej niż jedną osobę, będą przez nas śledzone i poskutkują dyskwalifikacją. + +W razie dodatkowych pytań możesz kontaktować się z nami pod adresem olimpiada@nowoczesnapolska.org.pl lub numerem telefonu +48 515-502-666. + +Przypominamy, że główną nagrodą w Olimpiadzie Cyfrowej jest indeks Collegium Civitas na dowolny kierunek studiów licencjackich oraz zwolnienie z opłat czesnego przez rok. + +Powodzenia! + +Zespół Olimpiady Cyfrowej +fundacja Nowoczesna Polska \ No newline at end of file -- 2.20.1