# Path: shownotes/models.py
# class Show(models.Model):
#     id = models.IntegerField(primary_key=True)
#     name = models.TextField()
#     last_updated = models.DateTimeField()
# 
#     def __unicode__(self):
#         return u'{}: {}'.format(self.id, self.name)
# 
#     @classmethod
#     def exists(cls, number):
#         return Show.objects.filter(pk=number).count() == 1
# 
# class Note(models.Model):
#     show = models.ForeignKey(Show)
#     topic = models.ForeignKey(Topic)
#     title = models.TextField()
# 
#     def text_entry(self):
#         try:
#             return TextEntry.get_by_note(self)[0].text
#         except:
#             return ''
# 
#     def urls(self):
#         return UrlEntry.get_by_note(self)
# 
#     def __unicode__(self):
#         return u'{} [{}]: {}'.format(
#             self.topic, self.show.id, self.title[:20])
# 
#     @classmethod
#     def get_by_topic(cls, topic):
#         return Note.objects.filter(topic=topic)
# 
# class TextEntry(models.Model):
#     note = models.ForeignKey(Note)
#     text = models.TextField()
# 
#     def text_html(self):
#         return self.text.replace('\n', '<br>')
# 
#     def __unicode__(self):
#         return u'{}: {}'.format('text', self.text[:20])
# 
#     @classmethod
#     def get_by_note(cls, note):
#         return TextEntry.objects.filter(note=note)
# 
# class UrlEntry(models.Model):
#     note = models.ForeignKey(Note)
#     text = models.TextField()
#     url = models.TextField()
# 
#     def link(self):
#         return u'<a href={}>{}</a>'.format(self.url, self.url)
# 
#     def __unicode__(self):
#         return u'{}: {}'.format('url', self.text[:20])
# 
#     @classmethod
#     def get_by_note(cls, note):
#         return UrlEntry.objects.filter(note=note)
# 
# class Topic(models.Model):
#     name = models.CharField(max_length=100)
# 
#     def note_count(self):
#         return Note.objects.filter(topic__id=self.id).count()
# 
#     def __unicode__(self):
#         return self.name
# 
#     @classmethod
#     def get_by_id(cls, *ids):
#         if len(ids) == 0:
#             return []
#         qs = [Q(id=x) for x in ids]
#         query = reduce(lambda x, y: x | y, qs)
#         return Topic.objects.filter(query)
# 
#     @classmethod
#     def exists(cls, name):
#         return Topic.objects.filter(name=name).count() > 0
# 
# class ShowSource(models.Model):
#     OPML = 'opml'
#     HTML = 'html'
#     SOURCE_CHOICES = ((OPML, 'opml'), (HTML, 'html'))
#     filetype = models.CharField(max_length=4, choices=SOURCE_CHOICES)
#     text = models.TextField()
#     show_number = models.IntegerField(unique=True)
# 
#     @classmethod
#     def exists(cls, number):
#         return ShowSource.objects.filter(show_number=number).count() == 1


import opml
import common.netutils as netutils
import re
from datetime import datetime
from shownotes.models import Show, Note, TextEntry, UrlEntry, \
    Topic, ShowSource
from BeautifulSoup import BeautifulSoup
from django.db import transaction
from django.utils import html

    @transaction.atomic
    def save(self):
        if self.exists():
            show = Show.objects.get(pk=self.number)
            if self.date_modified() > show.last_updated:
                show.delete()
        if not self.exists():
            self._insert_show()
            self._insert_entries()

    def date_modified(self):
        try:
            date_string = self.data.dateModified
            return datetime.strptime(
                date_string, '%a, %d %b %Y %H:%M:%S %Z')
        except ValueError:
            return datetime.fromtimestamp(0)

    def exists(self):
        return Show.objects.filter(pk=self.number).count() == 1

    def _show_number(self):
        match = number_pattern.search(self.data.title[:])
        if match:
            return int(match.group())
        raise ValueError

    def _delete_show(self):
        assert self.exists()

# Path: shownotes/models.py
# class Show(models.Model):
#     id = models.IntegerField(primary_key=True)
#     name = models.TextField()
#     last_updated = models.DateTimeField()
# 
#     def __unicode__(self):
#         return u'{}: {}'.format(self.id, self.name)
# 
#     @classmethod
#     def exists(cls, number):
#         return Show.objects.filter(pk=number).count() == 1
# 
# class Note(models.Model):
#     show = models.ForeignKey(Show)
#     topic = models.ForeignKey(Topic)
#     title = models.TextField()
# 
#     def text_entry(self):
#         try:
#             return TextEntry.get_by_note(self)[0].text
#         except:
#             return ''
# 
#     def urls(self):
#         return UrlEntry.get_by_note(self)
# 
#     def __unicode__(self):
#         return u'{} [{}]: {}'.format(
#             self.topic, self.show.id, self.title[:20])
# 
#     @classmethod
#     def get_by_topic(cls, topic):
#         return Note.objects.filter(topic=topic)
# 
# class TextEntry(models.Model):
#     note = models.ForeignKey(Note)
#     text = models.TextField()
# 
#     def text_html(self):
#         return self.text.replace('\n', '<br>')
# 
#     def __unicode__(self):
#         return u'{}: {}'.format('text', self.text[:20])
# 
#     @classmethod
#     def get_by_note(cls, note):
#         return TextEntry.objects.filter(note=note)
# 
# class UrlEntry(models.Model):
#     note = models.ForeignKey(Note)
#     text = models.TextField()
#     url = models.TextField()
# 
#     def link(self):
#         return u'<a href={}>{}</a>'.format(self.url, self.url)
# 
#     def __unicode__(self):
#         return u'{}: {}'.format('url', self.text[:20])
# 
#     @classmethod
#     def get_by_note(cls, note):
#         return UrlEntry.objects.filter(note=note)
# 
# class Topic(models.Model):
#     name = models.CharField(max_length=100)
# 
#     def note_count(self):
#         return Note.objects.filter(topic__id=self.id).count()
# 
#     def __unicode__(self):
#         return self.name
# 
#     @classmethod
#     def get_by_id(cls, *ids):
#         if len(ids) == 0:
#             return []
#         qs = [Q(id=x) for x in ids]
#         query = reduce(lambda x, y: x | y, qs)
#         return Topic.objects.filter(query)
# 
#     @classmethod
#     def exists(cls, name):
#         return Topic.objects.filter(name=name).count() > 0
# 
# class ShowSource(models.Model):
#     OPML = 'opml'
#     HTML = 'html'
#     SOURCE_CHOICES = ((OPML, 'opml'), (HTML, 'html'))
#     filetype = models.CharField(max_length=4, choices=SOURCE_CHOICES)
#     text = models.TextField()
#     show_number = models.IntegerField(unique=True)
# 
#     @classmethod
#     def exists(cls, number):
#         return ShowSource.objects.filter(show_number=number).count() == 1


# Path: shownotes/management/loaders.py
import opml
import common.netutils as netutils
import re
from datetime import datetime
from shownotes.models import Show, Note, TextEntry, UrlEntry, \
    Topic, ShowSource
from BeautifulSoup import BeautifulSoup
from django.db import transaction
from django.utils import html


    @transaction.atomic
    def save(self):
        if self.exists():
            show = Show.objects.get(pk=self.number)
            if self.date_modified() > show.last_updated:
                show.delete()
        if not self.exists():
            self._insert_show()
            self._insert_entries()

    def date_modified(self):
        try:
            date_string = self.data.dateModified
            return datetime.strptime(
                date_string, '%a, %d %b %Y %H:%M:%S %Z')
        except ValueError:
            return datetime.fromtimestamp(0)

    def exists(self):
        return Show.objects.filter(pk=self.number).count() == 1

    def _show_number(self):
        match = number_pattern.search(self.data.title[:])
        if match:
            return int(match.group())
        raise ValueError

    def _delete_show(self):
        assert self.exists()

        Show.objects.get(pk=self.number).delete()