# Path: montanha/models.py
# 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 Legislature(models.Model):
# 
#     class Meta:
#         verbose_name = _("Legislature")
#         verbose_name_plural = _("Legislatures")
# 
#     institution = models.ForeignKey("Institution")
# 
#     original_id = models.CharField(blank=True, null=True,
#                                    max_length=512,
#                                    verbose_name=_("Original ID"))
# 
#     date_start = models.DateField(verbose_name=_("Date started"),
#                                   help_text=_("""Date in which this legislature started."""),
#                                   db_index=True)
# 
#     date_end = models.DateField(blank=True, null=True,
#                                 verbose_name=_("Date ended"),
#                                 help_text=_("""Date in which this legislature ended."""),
#                                 db_index=True)
# 
#     def __unicode__(self):
#         return u"%s's legislature starting at %s, ending at %s" % (self.institution.siglum,
#                                                                    str(self.date_start),
#                                                                    str(self.date_end))
# 
# 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 ArchivedExpense(AbstractExpense):
# 
#     class Meta:
#         verbose_name = _("Archived expense")
#         verbose_name_plural = _("Archived expenses")
# 
#     collection_run = models.ForeignKey("CollectionRun")


import threading
import time
from datetime import datetime, date
from basecollector import BaseCollector
from montanha.models import (
    Institution, Legislature, ExpenseNature, Legislator, ArchivedExpense
)
        natures = data.findAll('h3')
        for data in natures:
            nature, _ = ExpenseNature.objects.get_or_create(name=self._normalize_nature(data.text))
            rows = data.findNext().findAll('tr')[1:-1]
            for row in rows:
                columns = row.findAll('td')

                if not len(columns) == 5:
                    print u'Bad row: %s' % unicode(columns)
                    continue

                cnpj = self.normalize_cnpj_or_cpf(columns[0].getText())

                supplier_name = columns[1].getText().strip()

                try:
                    supplier_name = supplier_name.decode('utf-8')
                except Exception:
                    pass

                supplier = self.get_or_create_supplier(cnpj, supplier_name)

                docnumber = columns[2].getText()
                expensed = parse_money(columns[3].getText())

                expense = ArchivedExpense(number=docnumber,
                                          nature=nature,
                                          date=date(year, month, 1),
                                          expensed=expensed,
                                          mandate=mandate,

# Path: montanha/models.py
# 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 Legislature(models.Model):
# 
#     class Meta:
#         verbose_name = _("Legislature")
#         verbose_name_plural = _("Legislatures")
# 
#     institution = models.ForeignKey("Institution")
# 
#     original_id = models.CharField(blank=True, null=True,
#                                    max_length=512,
#                                    verbose_name=_("Original ID"))
# 
#     date_start = models.DateField(verbose_name=_("Date started"),
#                                   help_text=_("""Date in which this legislature started."""),
#                                   db_index=True)
# 
#     date_end = models.DateField(blank=True, null=True,
#                                 verbose_name=_("Date ended"),
#                                 help_text=_("""Date in which this legislature ended."""),
#                                 db_index=True)
# 
#     def __unicode__(self):
#         return u"%s's legislature starting at %s, ending at %s" % (self.institution.siglum,
#                                                                    str(self.date_start),
#                                                                    str(self.date_end))
# 
# 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 ArchivedExpense(AbstractExpense):
# 
#     class Meta:
#         verbose_name = _("Archived expense")
#         verbose_name_plural = _("Archived expenses")
# 
#     collection_run = models.ForeignKey("CollectionRun")


# Path: montanha/management/commands/collectors/cmbh.py
import threading
import time
from datetime import datetime, date
from basecollector import BaseCollector
from montanha.models import (
    Institution, Legislature, ExpenseNature, Legislator, ArchivedExpense
)

        natures = data.findAll('h3')
        for data in natures:
            nature, _ = ExpenseNature.objects.get_or_create(name=self._normalize_nature(data.text))
            rows = data.findNext().findAll('tr')[1:-1]
            for row in rows:
                columns = row.findAll('td')

                if not len(columns) == 5:
                    print u'Bad row: %s' % unicode(columns)
                    continue

                cnpj = self.normalize_cnpj_or_cpf(columns[0].getText())

                supplier_name = columns[1].getText().strip()

                try:
                    supplier_name = supplier_name.decode('utf-8')
                except Exception:
                    pass

                supplier = self.get_or_create_supplier(cnpj, supplier_name)

                docnumber = columns[2].getText()
                expensed = parse_money(columns[3].getText())

                expense = ArchivedExpense(number=docnumber,
                                          nature=nature,
                                          date=date(year, month, 1),
                                          expensed=expensed,
                                          mandate=mandate,

                                          supplier=supplier,