comparison sat_pubsub/pgsql_storage.py @ 301:05c875a13a62

categories are now stored in a dedicated table if item contain an atom entry: - database creation/update files include the new table - item data are now stored in a ItemData namedtuple
author Goffi <goffi@goffi.org>
date Wed, 25 Nov 2015 18:33:38 +0100
parents 4115999d85e9
children 087b705493a6
comparison
equal deleted inserted replaced
300:c5acb4995fde 301:05c875a13a62
606 for item_data in items_data: 606 for item_data in items_data:
607 self._storeItem(cursor, item_data, publisher) 607 self._storeItem(cursor, item_data, publisher)
608 608
609 609
610 def _storeItem(self, cursor, item_data, publisher): 610 def _storeItem(self, cursor, item_data, publisher):
611 item, access_model, item_config = item_data 611 item, access_model, item_config = item_data.item, item_data.access_model, item_data.config
612 data = item.toXml() 612 data = item.toXml()
613 613
614 cursor.execute("""UPDATE items SET date=now(), publisher=%s, data=%s 614 cursor.execute("""UPDATE items SET date=now(), publisher=%s, data=%s
615 FROM nodes 615 FROM nodes
616 WHERE nodes.node_id = items.node_id AND 616 WHERE nodes.node_id = items.node_id AND
617 nodes.node_id = %s and items.item=%s""", 617 nodes.node_id = %s and items.item=%s
618 RETURNING item_id""",
618 (publisher.full(), 619 (publisher.full(),
619 data, 620 data,
620 self.nodeDbId, 621 self.nodeDbId,
621 item["id"])) 622 item["id"]))
622 if cursor.rowcount == 1: 623 if cursor.rowcount == 1:
624 item_id = cursor.fetchone()[0];
625 self._storeCategories(cursor, item_id, item_data.categories, update=True)
623 return 626 return
624 627
625 cursor.execute("""INSERT INTO items (node_id, item, publisher, data, access_model) 628 cursor.execute("""INSERT INTO items (node_id, item, publisher, data, access_model)
626 SELECT %s, %s, %s, %s, %s FROM nodes 629 SELECT %s, %s, %s, %s, %s FROM nodes
627 WHERE node_id=%s 630 WHERE node_id=%s
631 publisher.full(), 634 publisher.full(),
632 data, 635 data,
633 access_model, 636 access_model,
634 self.nodeDbId)) 637 self.nodeDbId))
635 638
639 item_id = cursor.fetchone()[0];
640 self._storeCategories(cursor, item_id, item_data.categories)
641
636 if access_model == const.VAL_AMODEL_ROSTER: 642 if access_model == const.VAL_AMODEL_ROSTER:
637 item_id = cursor.fetchone()[0];
638 if const.OPT_ROSTER_GROUPS_ALLOWED in item_config: 643 if const.OPT_ROSTER_GROUPS_ALLOWED in item_config:
639 item_config.fields[const.OPT_ROSTER_GROUPS_ALLOWED].fieldType='list-multi' #XXX: needed to force list if there is only one value 644 item_config.fields[const.OPT_ROSTER_GROUPS_ALLOWED].fieldType='list-multi' #XXX: needed to force list if there is only one value
640 allowed_groups = item_config[const.OPT_ROSTER_GROUPS_ALLOWED] 645 allowed_groups = item_config[const.OPT_ROSTER_GROUPS_ALLOWED]
641 else: 646 else:
642 allowed_groups = [] 647 allowed_groups = []
643 for group in allowed_groups: 648 for group in allowed_groups:
644 #TODO: check that group are actually in roster 649 #TODO: check that group are actually in roster
645 cursor.execute("""INSERT INTO item_groups_authorized (item_id, groupname) 650 cursor.execute("""INSERT INTO item_groups_authorized (item_id, groupname)
646 VALUES (%s,%s)""" , (item_id, group)) 651 VALUES (%s,%s)""" , (item_id, group))
647 652
653 def _storeCategories(self, cursor, item_id, categories, update=False):
654 # TODO: handle canonical form
655 if update:
656 cursor.execute("""DELETE FROM item_categories
657 WHERE item_id=%s""", (item_id,))
658
659 for category in categories:
660 cursor.execute("""INSERT INTO item_categories (item_id, category)
661 VALUES (%s, %s)""", (item_id, category))
648 662
649 def removeItems(self, itemIdentifiers): 663 def removeItems(self, itemIdentifiers):
650 return self.dbpool.runInteraction(self._removeItems, itemIdentifiers) 664 return self.dbpool.runInteraction(self._removeItems, itemIdentifiers)
651 665
652 666