# Path: legislature/models.py
# class Action(models.Model):
#     description = models.TextField()
#     date = models.DateField(blank=True, null=True)
#     text = models.TextField(blank=True, null=True)
#     bill = models.ForeignKey(Bill, blank=True, null=True)  # had to be NULL for migration
# 
#     class Meta:
#         ordering = ['date']
#         unique_together = ('bill', 'description')
# 
#     def __str__(self):
#         return self.description
# 
# class Bill(models.Model):
#     STATUS_CHOICES = (
#         ('1st', 'First Reading'),
#         ('2nd', 'Second Reading'),
#         ('LA', 'Law Amendments Committee'),
#         ('PL', 'Private & Local Bills Committee'),
#         ('WH', 'Committee of the Whole House'),
#         ('3rd', 'Third Reading'),
#         ('RA', 'Royal Assent'),
#     )
# 
#     identifier = models.PositiveIntegerField()
#     title = models.TextField()
#     description = models.TextField()
#     classification = models.TextField()
#     creator = models.ForeignKey(Speaker, blank=True, null=True, on_delete=models.SET_NULL)
#     modified = models.DateField()
#     status = models.CharField(max_length=3, choices=STATUS_CHOICES)
#     slug = models.TextField()
#     url = models.URLField()
#     law_amendments_committee_submissions_url = models.URLField()
# 
#     def __str__(self):
#         return self.title
# 
#     @models.permalink
#     def get_absolute_url(self):
#         return ('legislature:bill-view', (), {'slug': self.slug})


import datetime
import re
import requests
from django.core.management.base import BaseCommand
from django.forms.models import model_to_dict
from lxml import html
from speeches.models import Speaker
from legislature.models import Action, Bill

            # @see http://nslegislature.ca/index.php/proceedings/bills/liquor_control_act_-_bill_52
            matches = div.xpath('./p[not(@class)]//@href')
            if matches:
                bill.creator = Speaker.objects.get(sources__url=matches[0].rstrip('/'))

            # Some descriptions are identical to titles.
            description = div.xpath('./h3//text()')[1].rstrip('*\n ')
            if bill.title != description:
                bill.description = description

            if div.xpath('.//text()[contains(.,"Law Amendments Committee")]'):
                bill.classification = 'Public Bills'
            elif div.xpath('.//text()[contains(.,"Private and Local Bills")]'):
                bill.classification = 'Private and Local Bills'

            if not bill.pk or as_dict != model_to_dict(bill):
                bill.save()

            for tr in div.xpath('.//tr'):
                matches = tr.xpath('./td//text()')
                if matches and matches[0] != 'View':  # Skip link to statute
                    description = tr.xpath('./th//text()')[0].strip()
                    if description == 'Second Reading Passed':
                        description = 'Second Reading'

                    # Avoids importing duplicates.
                    try:
                        Action.objects.get(description=description, bill=bill)
                    except Action.DoesNotExist:

# Path: legislature/models.py
# class Action(models.Model):
#     description = models.TextField()
#     date = models.DateField(blank=True, null=True)
#     text = models.TextField(blank=True, null=True)
#     bill = models.ForeignKey(Bill, blank=True, null=True)  # had to be NULL for migration
# 
#     class Meta:
#         ordering = ['date']
#         unique_together = ('bill', 'description')
# 
#     def __str__(self):
#         return self.description
# 
# class Bill(models.Model):
#     STATUS_CHOICES = (
#         ('1st', 'First Reading'),
#         ('2nd', 'Second Reading'),
#         ('LA', 'Law Amendments Committee'),
#         ('PL', 'Private & Local Bills Committee'),
#         ('WH', 'Committee of the Whole House'),
#         ('3rd', 'Third Reading'),
#         ('RA', 'Royal Assent'),
#     )
# 
#     identifier = models.PositiveIntegerField()
#     title = models.TextField()
#     description = models.TextField()
#     classification = models.TextField()
#     creator = models.ForeignKey(Speaker, blank=True, null=True, on_delete=models.SET_NULL)
#     modified = models.DateField()
#     status = models.CharField(max_length=3, choices=STATUS_CHOICES)
#     slug = models.TextField()
#     url = models.URLField()
#     law_amendments_committee_submissions_url = models.URLField()
# 
#     def __str__(self):
#         return self.title
# 
#     @models.permalink
#     def get_absolute_url(self):
#         return ('legislature:bill-view', (), {'slug': self.slug})


# Path: legislature/management/commands/load_bills.py
import datetime
import re
import requests
from django.core.management.base import BaseCommand
from django.forms.models import model_to_dict
from lxml import html
from speeches.models import Speaker
from legislature.models import Action, Bill


            # @see http://nslegislature.ca/index.php/proceedings/bills/liquor_control_act_-_bill_52
            matches = div.xpath('./p[not(@class)]//@href')
            if matches:
                bill.creator = Speaker.objects.get(sources__url=matches[0].rstrip('/'))

            # Some descriptions are identical to titles.
            description = div.xpath('./h3//text()')[1].rstrip('*\n ')
            if bill.title != description:
                bill.description = description

            if div.xpath('.//text()[contains(.,"Law Amendments Committee")]'):
                bill.classification = 'Public Bills'
            elif div.xpath('.//text()[contains(.,"Private and Local Bills")]'):
                bill.classification = 'Private and Local Bills'

            if not bill.pk or as_dict != model_to_dict(bill):
                bill.save()

            for tr in div.xpath('.//tr'):
                matches = tr.xpath('./td//text()')
                if matches and matches[0] != 'View':  # Skip link to statute
                    description = tr.xpath('./th//text()')[0].strip()
                    if description == 'Second Reading Passed':
                        description = 'Second Reading'

                    # Avoids importing duplicates.
                    try:
                        Action.objects.get(description=description, bill=bill)
                    except Action.DoesNotExist:

                        action = Action(description=description, bill=bill)