class Institution(models.Model):

    class Meta:
        verbose_name = _("Institution")
        verbose_name_plural = _("Institutions")

    name = models.CharField(max_length=2048,
                            verbose_name=_("Name"))

    siglum = models.CharField(max_length=10,
                              verbose_name=_("Siglum"),
                              unique=True)

    logo = ThumbnailerImageField(
        verbose_name=_('Logo'),
        upload_to='institution',
        blank=True,
        null=True)

    def __unicode__(self):
        return u"%s" % (self.name) class Expense(AbstractExpense):

    class Meta:
        verbose_name = _("Expense")
        verbose_name_plural = _("Expenses")

    def save(self, *args, **kwargs):
        if hasattr(settings, 'expense_locked_for_collection') and \
           settings.expense_locked_for_collection:
            raise RuntimeError("You should not touch Expense while collecting, use ArchivedExpense instead.")
        super(Expense, self).save(*args, **kwargs) class ExpenseNature(models.Model):

    class Meta:
        verbose_name = _("Expense nature")
        verbose_name_plural = _("Expense natures")

    original_id = models.CharField(blank=True, null=True,
                                   max_length=512,
                                   verbose_name=_("Original ID"))

    name = models.CharField(max_length=512,
                            verbose_name=_("Expense nature name"))

    def __unicode__(self):
        return u"%s" % self.name class Legislator(models.Model):

    class Meta:
        verbose_name = _("Legislator")
        verbose_name_plural = _("Legislators")

    name = models.CharField(max_length=2048,
                            verbose_name=_("Full name"))

    alternative_names = models.ManyToManyField('AlternativeLegislatorName')

    picture = ThumbnailerImageField(
        verbose_name=_('Picture'),
        upload_to='legislator',
        max_length=256,
        blank=True,
        null=True)

    site = models.URLField(
        verbose_name=_('Site'),
        blank=True,
        null=True)

    email = models.EmailField(
        verbose_name=_('Email'),
        blank=True,
        null=True)

    about = models.TextField(
        verbose_name=_('About'),
        blank=True,
        null=True)

    gender = models.CharField(
        verbose_name=_('Gender'),
        choices=(
            ('F', _('Female')),
            ('M', _('Male'))),
        blank=True,
        null=True,
        max_length=1)

    date_of_birth = models.DateField(
        verbose_name=_('Date of Birth'),
        blank=True,
        null=True)

    def __unicode__(self):
        return u"Legislator %s" % (self.name)

    @property
    def party(self):
        party = self.mandate_set.order_by("-date_start")[0].party
        if party and party.siglum:
            return party.siglum
        return u"Partido não informado" class Supplier(models.Model):

    class Meta:
        verbose_name = _("Supplier")
        verbose_name_plural = _("Suppliers")

    name = models.CharField(verbose_name=_("Name"), max_length=200)

    identifier = models.CharField(
        max_length=256,
        verbose_name=_("Supplier identifier"),
        db_index=True)

    date_opened = models.DateField(
        verbose_name=_("Date Opened"),
        blank=True,
        null=True)

    trade_name = models.CharField(
        verbose_name=_("Trade Name"),
        max_length=200,
        blank=True,
        null=True)

    status = models.CharField(
        verbose_name=_('Status'),
        max_length=10,
        blank=True,
        null=True)

    situation = models.ForeignKey(
        'SupplierSituation',
        verbose_name=_('Situation'),
        blank=True,
        null=True,
    )

    situation_date = models.DateField(
        verbose_name=_('Situation Date'),
        blank=True,
        null=True,
    )

    situation_reason = models.CharField(
        verbose_name=_('Situation Reason'),
        max_length=200,
        blank=True,
        null=True,
    )

    special_situation = models.CharField(
        verbose_name=_('Special Situation'),
        max_length=200,
        blank=True,
        null=True,
    )

    special_situation_date = models.DateField(
        verbose_name=_('Special Situation Date'),
        blank=True,
        null=True,
    )

    enterprise_type = models.CharField(
        verbose_name=_('Enterprise Type'),
        max_length=6,
        blank=True,
        null=True,
    )

    federative_officer = models.CharField(
        verbose_name=_('Federative Officer'),
        max_length=200,
        blank=True,
        null=True,
    )

    last_change = models.DateTimeField(
        verbose_name=_('Last Change'),
        auto_now=True,
        blank=True,
        null=True,
    )

    last_update = models.DateTimeField(
        verbose_name=_('Last Update'),
        blank=True,
        null=True,
    )

    email = models.EmailField(
        verbose_name=_('Email'),
        blank=True,
        null=True,
    )

    juridical_nature = models.ForeignKey(
        'SupplierJuridicalNature',
        verbose_name=_('Juridical Nature'),
        blank=True,
        null=True,
    )

    address = models.CharField(
        verbose_name=_('Address'),
        max_length=200,
        blank=True,
        null=True,
    )

    address_number = models.CharField(
        verbose_name=_('Address Number'),
        max_length=10,
        null=True,
        blank=True,
    )

    address_complement = models.CharField(
        verbose_name=_('Address Complement'),
        max_length=200,
        blank=True,
        null=True,
    )

    postal_code = models.CharField(
        verbose_name=_('CEP'),
        max_length=100,
        blank=True,
        null=True,
    )

    state = models.CharField(
        verbose_name=_('State'),
        max_length=2,
        choices=STATE_CHOICES,
        blank=True,
        null=True,
    )

    city = models.CharField(
        verbose_name=_('City'),
        max_length=100,
        blank=True,
        null=True,
    )

    neighborhood = models.CharField(
        verbose_name=_('Neighborhood'),
        max_length=100,
        blank=True,
        null=True,
    )

    phone = models.CharField(
        verbose_name=_('Phone'),
        max_length=100,
        blank=True,
        null=True,
    )

    main_activity = models.ForeignKey(
        'SupplierActivity',
        verbose_name=_('Main Activity'),
        blank=True,
        null=True,
    )

    secondary_activities = models.ManyToManyField(
        'SupplierActivity',
        verbose_name=_('Secondary Activities'),
        related_name='secondary_activities',
        blank=True,
    )

    @property
    def identifier_with_mask(self):
        try:
            if len(self.identifier) == 11:
                return Cpf().format(self.identifier)
            elif len(self.identifier) == 14:
                return Cnpj().format(self.identifier)
            else:
                return self.identifier
        except Exception:
            return self.identifier

    @property
    def identifier_label(self):
        if len(self.identifier) == 11:
            return 'CPF'
        elif len(self.identifier) == 14:
            return 'CNPJ'
        else:
            return _('Identifier')

    def __unicode__(self):
        return u"%s (%s)" % (self.name, self.identifier_with_mask) class PerNature(models.Model):
    institution = models.ForeignKey("Institution")
    legislature = models.ForeignKey("Legislature",
                                    blank=True,
                                    null=True)
    date_start = models.DateField(db_index=True)
    date_end = models.DateField(db_index=True)
    nature = models.ForeignKey("ExpenseNature")
    expensed = models.DecimalField(max_digits=20, decimal_places=2)

    class Meta:
        verbose_name = _("Per Nature")
        verbose_name_plural = _("Per Nature")

    def __unicode__(self):
        return u'{0} ({1})'.format(self.nature, self.expensed) class PerNatureByYear(models.Model):
    institution = models.ForeignKey("Institution")
    year = models.IntegerField()
    nature = models.ForeignKey("ExpenseNature")
    expensed = models.DecimalField(max_digits=20, decimal_places=2)

    class Meta:
        verbose_name = _("Per Nature By Year")
        verbose_name_plural = _("Per Nature By Year")

    def __unicode__(self):
        return u'{0} ({1})'.format(self.nature, self.expensed) class PerNatureByMonth(models.Model):
    institution = models.ForeignKey("Institution")
    date = models.DateField(db_index=True)
    nature = models.ForeignKey("ExpenseNature")
    expensed = models.DecimalField(max_digits=20, decimal_places=2)

    class Meta:
        verbose_name = _("Per Nature By Month")
        verbose_name_plural = _("Per Nature By Month")

    def __unicode__(self):
        return u'{0} ({1})'.format(self.nature, self.expensed) class PerLegislator(models.Model):
    institution = models.ForeignKey("Institution")
    legislature = models.ForeignKey("Legislature",
                                    blank=True,
                                    null=True)
    legislator = models.ForeignKey("Legislator")
    date_start = models.DateField(db_index=True)
    date_end = models.DateField(db_index=True)
    expensed = models.DecimalField(max_digits=20, decimal_places=2)

    class Meta:
        verbose_name = _("Per Legislator")
        verbose_name_plural = _("Per Legislator")

    def __unicode__(self):
        return u'{0} ({1})'.format(self.legislator, self.expensed) class BiggestSupplierForYear(models.Model):
    supplier = models.ForeignKey("Supplier")
    year = models.IntegerField(db_index=True)
    expensed = models.DecimalField(max_digits=20, decimal_places=2)

    class Meta:
        verbose_name = _("Biggest Supplier For Year")
        verbose_name_plural = _("Biggest Supplier For Year")

    def __unicode__(self):
        return u'{0} ({1})'.format(self.supplier, self.expensed) def filter_for_institution(data, institution):
    if not institution:
        return data

    if not isinstance(institution, Institution):
        institution = Institution.objects.get(siglum=institution)

    data = data.filter(mandate__legislature__institution=institution)
    return data def get_date_ranges_from_data(institution, data, consolidated_data=False, include_date_objects=True):
    """ Takes a data set and returns a dict containing in textual form:

        current_date_from: the start date that is being used for this query
        current_date_to: the end date that is being used for this query
    """
    try:
        if consolidated_data:
            cdf = data.order_by('date_start')[0].date_start
        else:
            cdf = data.order_by('date')[0].date
    except Exception:
        cdf = date.today()

    try:
        if consolidated_data:
            cdt = data.order_by('-date_end')[0].date_end
        else:
            cdt = data.order_by('-date')[0].date
    except Exception:
        cdt = date.today()

    if institution:
        if not isinstance(institution, Institution):
            institution = Institution.objects.get(siglum=institution)

        # Bound dates to the start of the first legislature to the end
        # of the last, which makes more sense to our purposes.
        first = institution.legislature_set.order_by('date_start')[0]
        last = institution.legislature_set.order_by('-date_end')[0]
    else:
        first = Legislature.objects.order_by('date_start')[0]
        last = Legislature.objects.order_by('-date_end')[0]

    min_date = first.date_start
    max_date = last.date_end

    if cdf < min_date:
        cdf = min_date

    if cdt > max_date:
        cdt = max_date

    cdf_string = cdf.strftime('%B de %Y')
    cdt_string = cdt.strftime('%B de %Y')

    d = dict(current_date_from=cdf_string, current_date_to=cdt_string)
    if include_date_objects:
        d.update(dict(cdf=cdf, cdt=cdt))

    return d def ensure_years_in_range(date_ranges, years):
    nyears = []
    cdf = date_ranges['cdf']
    cdt = date_ranges['cdt']
    for y in years:
        d = date(y, 1, 1)
        if d < cdf or d > cdt:
            continue
        nyears.append(y)
    return nyears
import codecs
import sys
from datetime import date
from django.conf import settings
from django.core.management.base import BaseCommand
from django.db.models import Sum
from montanha.models import (
    Institution, Expense, ExpenseNature, Legislator, Supplier, PerNature,
    PerNatureByYear, PerNatureByMonth, PerLegislator, BiggestSupplierForYear
)
from montanha.util import (
    filter_for_institution, get_date_ranges_from_data, ensure_years_in_range
)
# -*- coding: utf-8 -*-
#
# Copyright (©) 2014 Gustavo Noronha Silva
# Copyright (©) 2016 Marcelo Jorge Vieira
#
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU Affero General Public License as
#  published by the Free Software Foundation, either version 3 of the
#  License, or (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU Affero General Public License for more details.
#
#  You should have received a copy of the GNU Affero General Public License
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.

# This hack makes django less memory hungry (it caches queries when running
# with debug enabled.





settings.DEBUG = False
sys.stdout = codecs.getwriter("utf-8")(sys.stdout)
sys.stderr = codecs.getwriter("utf-8")(sys.stderr)


class Command(BaseCommand):
    help = "Collects data for a number of sources"

    def add_arguments(self, parser):
        parser.add_argument('house', type=str, nargs='*', default='')
        parser.add_argument(
            '--agnostic',
            action='store_true',
            dest='agnostic',
            default=False,
        )

    def handle(self, *args, **options):
        for house in options.get('house'):
            try:
                institution = Institution.objects.get(siglum__iexact=house)
                print u'Consolidating data for %s' % (institution.name)
                self.per_nature(institution)
                self.per_legislator(institution)
            except Institution.DoesNotExist:
                print u'Institution %s does not exist' % house

        if options.get('agnostic'):
            self.agnostic()

    def per_nature(self, institution):
        PerNature.objects.filter(institution=institution).delete()
        PerNatureByYear.objects.filter(institution=institution).delete()
        PerNatureByMonth.objects.filter(institution=institution).delete()


        data = Expense.objects.all()