Revision 4d52f59d

b/edumanage/management/commands/contacts.py
1
# -*- coding: utf-8 -*- vim:encoding=utf-8:
2
# vim: tabstop=4:shiftwidth=4:softtabstop=4:expandtab
3
import warnings
4
warnings.simplefilter("ignore", DeprecationWarning)
5
import sys, locale, codecs
6
# sort greek utf-8 properly
7
locale.setlocale(locale.LC_COLLATE, ('el_GR', 'UTF8'))
8
# https://wiki.python.org/moin/PrintFails
9
sys.stdout = codecs.getwriter(locale.getpreferredencoding())(sys.stdout)
10

  
11
from optparse import make_option
12
from django.core.management.base import BaseCommand, CommandError
13
from django.conf import settings
14
from edumanage.models import *
15
from accounts.models import *
16
from registration.models import RegistrationProfile
17

  
18
class Command(BaseCommand):
19
    option_list = BaseCommand.option_list + (
20
        make_option('--mail-list',
21
                    action='store_true',
22
                    dest='maillist',
23
                    default=False,
24
                    help='Return only emails (output suitable for a mailing list)'),
25
        )
26
    args = ''
27
    help = 'Prints institution contacts in CSV format'
28

  
29
    def handle(self, *args, **options):
30
	users = User.objects.all()
31
        if not options['maillist']:
32
            self.stdout.write(
33
		u'"%s","%s","%s"' % (
34
                    u"Φορέας",
35
                    u"Διαχειριστής",
36
                    "email"
37
                    )
38
		+ "\n")
39
	data = [
40
		(u.get_profile().institution.get_name('el'),
41
		 u.first_name + " " + u.last_name,
42
		 m)
43
		for u in users if
44
		len(u.registrationprofile_set.all()) > 0 and
45
		u.registrationprofile_set.all()[0].activation_key == "ALREADY_ACTIVATED"
46
                for m in u.email.split(';')
47
		]
48
	data.sort(key=lambda d: unicode(d[0]))
49
	for (foreas, onoma, email) in data:
50
            if options['maillist']:
51
                self.stdout.write(
52
                    u'{email}\t{onoma}'.format(
53
                        email=email,
54
                        onoma=onoma
55
                        )
56
                    + "\n")
57
            else:
58
		self.stdout.write(
59
                    u'"{foreas}","{onoma}","{email}"'.format(
60
                        onoma=onoma,
61
                        email=email,
62
                        foreas=foreas
63
                        )
64
                    + "\n")

Also available in: Unified diff