from datetime import datetime
from persistent import Persistent
from persistent.mapping import PersistentMapping
from repoze.folder import Folder
import dateutil.parser
from dateutil.tz import tzutc


class Root(PersistentMapping):
    __parent__ = __name__ = None


class SharedItems(Folder):
    """A folder to hold the shared items
    """
    title = "Shared Items"


class SharedItem(Persistent):
    """An item shared to the CS Portal Pool
    """

    def __init__(self, Title='', portal_type='', Creator='', Modified=None,
                 url='', Description='', Subject=[]):
        self.Title = Title
        self.portal_type = portal_type
        self.url = url
        self.Creator = Creator
        if Modified is None:
            Modified = datetime.utcnow()
        self.Modified = Modified
        self.Description = Description
        self.Subject = Subject

    def update_from_entry(self, entry):
        """Update the item based on the feed entry
        """
        if 'title' in entry:
            self.Title = entry['title']
        if 'push_portal_type' in entry:
            self.portal_type = entry['push_portal_type']
        if 'author' in entry:
            self.Creator = entry['author']
        if 'updated' in entry:
            mod_date = dateutil.parser.parse(entry['updated'])
            self.Modified = mod_date.astimezone(tzutc())
        if 'link' in entry:
            self.url = entry['link']
        if 'summary' in entry:
            self.Description = entry['summary']
        if 'tags' in entry:
            self.Subject = [i['term'] for i in entry['tags']]


def appmaker(zodb_root):
    if not 'app_root' in zodb_root:
        app_root = Root()
        zodb_root['app_root'] = app_root

        shared_items = SharedItems()
        app_root.shared = shared_items
        shared_items.__name__ = 'shared'
        shared_items.__parent__ = app_root

        import transaction
        transaction.commit()
    return zodb_root['app_root']

@@ -575,16 +575,31 @@
 bject=%5B%5D
+, Category=None
 ):%0A     
@@ -885,16 +885,49 @@
  Subject
+%0A        self.Category = Category
 %0A%0A    de
@@ -1647,16 +1647,115 @@
 tags'%5D%5D%0A
+        if 'category' in entry:%0A            self.Category = %5Bi%5B'term'%5D for i in entry%5B'category'%5D%5D%0A
 %0A%0Adef ap

Add Category field to the model for indexing. refs #638