def handle(self, *args, **options):
     current_site = Site.objects.get_current()
     parties = InterestedParty.objects.filter(activation_key=None)
     writer = UnicodeWriter(open('whitelist-%s.csv' % datetime.now().date(), 'wb'))
     headers = ['email']
     writer.writerow(headers)
     interests = {}
     for party in random.sample(parties, parties.count()):
         print "%s %s" % (party.first_name, party.last_name)
         row = [party.email]
         writer.writerow(row)
     print interests
 def handle(self, *args, **options):
     current_site = Site.objects.get_current()
     parties = InterestedParty.objects.filter(activation_key=None)
     writer = UnicodeWriter(
         open('whitelist-%s.csv' % datetime.now().date(), 'wb'))
     headers = ['email']
     writer.writerow(headers)
     interests = {}
     for party in random.sample(parties, parties.count()):
         print "%s %s" % (party.first_name, party.last_name)
         row = [party.email]
         writer.writerow(row)
     print interests
 def handle(self, *args, **options):
     writer = UnicodeWriter(open('interesed-parties-%s.csv' % datetime.now().date(), 'wb'))
     parties = InterestedParty.objects.all()
     if len(args) > 0:
         #whitelist file location
         #one column containing email addresses to use, no header
         reader = list(csv.reader(file(args[0], 'rb')))
         reader = map(lambda x: x[0], reader)
         parties = filter(lambda x: x.email in reader, parties)
     print len(parties)
     headers = ['first_name', 'last_name', 'email', 'activation_key', 'activated_on', 'interested_in']
     writer.writerow(headers)
     interests = {}
     for party in parties:
         row = [party.first_name, party.last_name, party.email, '' if party.activation_key == None else party.activation_key.key, str(party.activated_on), '']
         writer.writerow(row)
    def handle(self, *args, **options):

        objects = {
            'requests': [],
            'governments': [],
            'agencies': [],
            'contacts': []
        }

        writer = UnicodeWriter(open('stats/requests.csv', 'wb'))
        header = [
            "id",
            "agency_id",
            "status",
            "created",
            "sent",
            "due_date",
            "fullfilled_date",
            "private",
            "num_messages",
            "contacts"
        ]
        writer.writerow(header)

        requests = list(Request.objects.all())
        for i, request in enumerate(requests):
            mb = MailBox.objects.filter(usr=request.author)
            if mb.count() < 1:
                pass
            else:
                print "Request num %s of %s" % (i, len(requests))
                mb = mb[0]
                threads = mb.get_threads(request.id)
                row = [
                    str(request.id),
                    ("" if request.agency is None else str(request.agency.id)),
                    request.get_status,
                    str(request.date_added),
                    str(request.scheduled_send_date),
                    str(request.due_date),
                    str(request.date_fulfilled),
                    str(request.private),
                    str(len(threads)),
                    ",".join([str(contact.id) for contact in request.contacts.all()])
                ]
                writer.writerow(row)
                obj = {}
                for i, key in enumerate(header):
                    obj[key] = row[i]
                objects['requests'].append(obj)

        writer = UnicodeWriter(open('stats/governments.csv', 'wb'))
        header = [
            'id',
            'name',
            'slug',
            'created',
            'agencies'
        ]
        writer.writerow(header)
        for gov in Government.objects.all():
            row = [
                str(gov.id),
                gov.name,
                gov.slug,
                str(gov.created),
                str(Agency.objects.filter(government=gov).count())
            ]
            writer.writerow(row)
            obj = {}
            for i, key in enumerate(header):
                obj[key] = row[i]
            objects['governments'].append(obj)

        writer = UnicodeWriter(open('stats/agencies.csv', 'wb'))
        header = [
            'id',
            'government_id',
            'name',
            'slug',
            'created',
            'contacts'
        ]
        writer.writerow(header)
        for agency in Agency.objects.all():
            row = [
                str(agency.id),
                str(agency.government.id),
                agency.name,
                agency.slug,
                str(agency.created),
                str(agency.contacts.all().count())
            ]
            writer.writerow(row)
            obj = {}
            for i, key in enumerate(header):
                obj[key] = row[i]
            objects['agencies'].append(obj)

        writer = UnicodeWriter(open('stats/contacts.csv', 'wb'))
        header = [
            'id',
            'agency_id',
            'first_name',
            'last_name',
            'created',
            'address',
            'phone',
            'emails'
        ]
        writer.writerow(header)

        for agency in Agency.objects.all():
            for contact in agency.contacts.all():
                row = [
                    str(contact.id),
                    str(agency.id),
                    contact.first_name,
                    contact.last_name,
                    str(contact.created),
                    ("" if contact.get_first_active_address is None else contact.get_first_active_address.content),
                    ("" if contact.get_first_active_phone is None else contact.get_first_active_phone.content),
                    ("" if contact.get_first_active_email is None else contact.get_first_active_email.content)
                ]
                writer.writerow(row)
                obj = {}
                for i, key in enumerate(header):
                    obj[key] = row[i]
                objects['contacts'].append(obj)

        with open("stats/data.json", "w") as out:
            out.write(json.dumps(objects))
    def handle(self, *args, **options):

        objects = {
            'requests': [],
            'governments': [],
            'agencies': [],
            'contacts': []
        }

        writer = UnicodeWriter(open('stats/requests.csv', 'wb'))
        header = [
            "id", "agency_id", "status", "created", "sent", "due_date",
            "fullfilled_date", "private", "num_messages", "contacts"
        ]
        writer.writerow(header)

        requests = list(Request.objects.all())
        for i, request in enumerate(requests):
            mb = MailBox.objects.filter(usr=request.author)
            if mb.count() < 1:
                pass
            else:
                print "Request num %s of %s" % (i, len(requests))
                mb = mb[0]
                threads = mb.get_threads(request.id)
                row = [
                    str(request.id),
                    ("" if request.agency is None else str(request.agency.id)),
                    request.get_status,
                    str(request.date_added),
                    str(request.scheduled_send_date),
                    str(request.due_date),
                    str(request.date_fulfilled),
                    str(request.private),
                    str(len(threads)), ",".join([
                        str(contact.id) for contact in request.contacts.all()
                    ])
                ]
                writer.writerow(row)
                obj = {}
                for i, key in enumerate(header):
                    obj[key] = row[i]
                objects['requests'].append(obj)

        writer = UnicodeWriter(open('stats/governments.csv', 'wb'))
        header = ['id', 'name', 'slug', 'created', 'agencies']
        writer.writerow(header)
        for gov in Government.objects.all():
            row = [
                str(gov.id), gov.name, gov.slug,
                str(gov.created),
                str(Agency.objects.filter(government=gov).count())
            ]
            writer.writerow(row)
            obj = {}
            for i, key in enumerate(header):
                obj[key] = row[i]
            objects['governments'].append(obj)

        writer = UnicodeWriter(open('stats/agencies.csv', 'wb'))
        header = ['id', 'government_id', 'name', 'slug', 'created', 'contacts']
        writer.writerow(header)
        for agency in Agency.objects.all():
            row = [
                str(agency.id),
                str(agency.government.id), agency.name, agency.slug,
                str(agency.created),
                str(agency.contacts.all().count())
            ]
            writer.writerow(row)
            obj = {}
            for i, key in enumerate(header):
                obj[key] = row[i]
            objects['agencies'].append(obj)

        writer = UnicodeWriter(open('stats/contacts.csv', 'wb'))
        header = [
            'id', 'agency_id', 'first_name', 'last_name', 'created', 'address',
            'phone', 'emails'
        ]
        writer.writerow(header)

        for agency in Agency.objects.all():
            for contact in agency.contacts.all():
                row = [
                    str(contact.id),
                    str(agency.id), contact.first_name, contact.last_name,
                    str(contact.created),
                    ("" if contact.get_first_active_address is None else
                     contact.get_first_active_address.content),
                    ("" if contact.get_first_active_phone is None else
                     contact.get_first_active_phone.content),
                    ("" if contact.get_first_active_email is None else
                     contact.get_first_active_email.content)
                ]
                writer.writerow(row)
                obj = {}
                for i, key in enumerate(header):
                    obj[key] = row[i]
                objects['contacts'].append(obj)

        with open("stats/data.json", "w") as out:
            out.write(json.dumps(objects))