# 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




class Command(BaseCommand):
    help = 'Imports bills from the NS Legislature website.'

    def get(self, path):
        return requests.get(self.base_url + path).json()

    def handle(self, *args, **options):
        url = 'http://nslegislature.ca/index.php/proceedings/status-of-bills/sort/status'
        page = requests.get(url)
        tree = html.fromstring(page.text)
        tree.make_links_absolute(url)

        for tr in tree.xpath('//tr[@valign="top"]'):
            identifier = int(tr.xpath('./td[2]//text()')[0])

            try:
                bill = Bill.objects.get(identifier=identifier)

# 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





class Command(BaseCommand):
    help = 'Imports bills from the NS Legislature website.'

    def get(self, path):
        return requests.get(self.base_url + path).json()

    def handle(self, *args, **options):
        url = 'http://nslegislature.ca/index.php/proceedings/status-of-bills/sort/status'
        page = requests.get(url)
        tree = html.fromstring(page.text)
        tree.make_links_absolute(url)

        for tr in tree.xpath('//tr[@valign="top"]'):
            identifier = int(tr.xpath('./td[2]//text()')[0])

            try:
                bill = Bill.objects.get(identifier=identifier)

            except Bill.DoesNotExist: