Changeset 269


Ignore:
Timestamp:
Nov 19, 2009, 11:07:21 AM (10 years ago)
Author:
jjbot
Message:

Added constraint handling and logging to AtomicServices2 and IbidasSimpleMessage?.

Location:
web
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • web/IbidasAtomicServices2.py

    r268 r269  
     1import logging
     2
    13from IbidasSimpleMessage import IbidasSimpleMessage
    24
    35class AtomicServices:
     6
     7    LOG_FILENAME = "./ibidas_services.log"
     8
    49    def __init__(self, cd):
    510        self.cd = cd
    6 
    7     def getAvailableSets(self):
    8         msg = IbidasSimpleMessage()
     11        self.logger = None
     12        self.__setupLogger()
     13        self.logger.debug('Started AtomicServices')
     14
     15    def __setupLogger(self):
     16        """Setup logger for this class."""
     17        logging.basicConfig(
     18                format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
     19                filename=self.LOG_FILENAME,
     20                filemode='w'
     21        )
     22        logger = logging.getLogger('IbidasAtomicServices')
     23        logger.setLevel(logging.DEBUG)
     24        self.logger = logger
     25
     26    def getAvailableSets(self, msg=None):
     27        """Returns all the available datasets."""
     28        self.logger.debug("Called getAvailableSets")
     29        if msg is None:
     30            msg = IbidasSimpleMessage()
    931        sets = self.cd.set[:, ('set_id', 'name')]
    1032        msg.addSets(sets)
     
    1234        return msg.finalize()
    1335
    14     def getAvailableItemRelationTypes(self):
     36    def getAvailableItemRelationTypes(self, msg=None):
    1537        """Returns all the types associated with item_child_links.
    1638        """
    17         print "Called getAvailableItemRelationTypes"
    18         msg = IbidasSimpleMessage()
     39        self.logger.debug("Called getAvailableItemRelationTypes")
     40        if msg is None:
     41            msg = IbidasSimpleMessage()
    1942        types = self.cd.term.join(self.cd.item_child_link, 'term_id', 'type_id').up()
    2043        types = types.union(types)
     
    2548        """Returns all the types associated with items.
    2649        """
    27         print "Called getAvailableItemTypes"
     50        self.logger.debug("Called getAvailableItemTypes")
    2851        msg = IbidasSimpleMessage()
    2952        types = self.cd.term.join(self.cd.item, 'term_id', 'type_id').up()
     
    3356
    3457    def __addItemContraints(self, query, args):
    35         print "__addItemConstraints"
     58        """Adds the set and type constaints to items."""
     59        self.logger.debug("__addItemConstraints")
    3660        if(args.has_key('sets') & (len(args['sets']) > 0)):
    37             print "Adding set contraint"
    38             sets = args['sets']
    39             sets = [int(x) for x in sets]
    40             query = query[_.source_id.within(*sets)]
     61            self.logger.debug("Adding item set contraint")
     62            set_ids = [int(x) for x in args['sets']]
     63            query = query[_.source_id.within(*set_ids)]
     64            self.logger.debug(
     65                    "Number of items left after set constraint: "
     66                    + str(len(query)))
    4167        if(args.has_key('itemtypes') & (len(args['itemtypes']) > 0)):
    42             print "Adding type contraint"
     68            self.logger.debug("Adding item type contraint")
    4369            item_types = args['itemtypes']
    44             item_types = [int(x) for x in item_types]
    45             item_types = args['itemtypes']
    46             query = query[_.type_id.within(*item_types)]
     70            item_type_ids = [int(x) for x in args['itemtypes']]
     71            query = query[_.type_id.within(*item_type_ids)]
     72        self.logger.debug("Returning query")
     73        return query
     74
     75    def __addItemlinkConstraints(self, query, args):
     76        """Adds the set and type constraints to itemlinks."""
     77        self.logger.debug("__addItemlinkConstraints")
     78        if(args.has_key('sets') & (len(args['sets']) > 0)):
     79            self.logger.debug("Adding link set constraint")
     80            set_ids = [int(x) for x in args['sets']]
     81            query = query[_.source_id.within(*set_ids)]
     82        if(args.has_key('itemrelationtypes') & (len(args['itemrelationtypes']) > 0)):
     83            self.logger.debug("Adding link type constraint")
     84            type_ids = [int(x) for x in args['itemrelationtypes']]
     85            query = query[_.type_id.within(*type_ids)]
     86        logger.debug("Returning query")
    4787        return query
    4888
     
    5090        """Returns all the direct children of the provided items.
    5191        """
    52         print "getItemChildren"
    53         print args
     92        self.logger.debug("getItemChildren")
    5493        assert args.has_key('items'), \
    5594                'No item_ids provided to search children with.'
    5695        msg = IbidasSimpleMessage()
    5796       
    58         items = args['items']
    59         ids = [int(x) for x in items]
    60         items = self.cd.item[_.item_id.within(*ids)]
    61 
    62         #FIXME: add link type & set contraints
    63         links = self.cd.item_child_link.join(items, 'parent_id', 'item_id')
     97        item_ids = [int(x) for x in args['items']]
     98        parents = self.cd.item[_.item_id.within(*item_ids)]
     99        self.logger.debug("Number of matched parent items: " +
     100                str(len(parents)))
     101
     102        links = self.cd.item_child_link.join(parents, 'parent_id', 'item_id')
     103        links = self.__addItemlinkConstraints(links, args)
    64104       
    65105        children = self.cd.item.join(links, 'item_id', 'child_id').up()
     106        self.logger.debug("Nr of children before constraints: " + str(len(children)))
    66107        children = self.__addItemContraints(children, args)
     108        self.logger.debug("Nr of children after constraints: " + str(len(children)))
    67109       
    68110        links = links.join(children, 'child_id', 'item_id').up()
     111
    69112        msg.addItems(children)
    70113        msg.addItemLinks(links)
     
    75118        """Returs all the direct parents of the provided items.
    76119        """
    77         print "getItemParents"
    78         print args
    79         items = args['items']
    80         msg = IbidasSimpleMessage()
    81         ids = [int(x) for x in items]
    82         items = self.cd.item[_.item_id.within(*ids)]
    83 
    84         links = self.cd.item_child_link.join(items, 'child_id', 'item_id')
    85         msg.addItemLinks(links)
    86         items = self.cd.item.join(links, 'item_id', 'parent_id').up()
    87         print "Nr of parents found: ", len(items)
     120        self.logger.debug("getItemParents")
     121        assert args.has_key('items'), \
     122                'No item_ids provided to search parents with.'
     123        msg = IbidasSimpleMessage()
     124        item_ids = [int(x) for x in args['items']]
     125        children = self.cd.item[_.item_id.within(*item_ids)]
     126        self.logger.debug("Nr of matched child items: " +
     127                str(len(children)))
     128
     129        links = self.cd.item_child_link.join(children, 'child_id', 'item_id')
     130        links = self.__addItemlinkConstraints(links, args)
     131        parents = self.cd.item.join(links, 'item_id', 'parent_id').up()
     132        parents = self.__addItemConstraints(parents, args)
     133        links = links.join(parents, 'parent_id', 'item_id')
     134        self.logger.debug("Nr of parents found: " + str(len(parents)))
     135       
     136        msg.addItemLinks(links)
    88137        msg.addItems(items)
    89138
     
    93142        """Returns all the direct neighbours of the provided items.
    94143        """
    95         print "getItemNeighbours"
     144        self.logger.debug("getItemNeighbours")
    96145        print args
    97146        items = args['items']
     
    115164        """Returns all the links between the provided items."""
    116165        # FIXME: add constraints
    117         items = args['items']
    118         msg = IbidasSimpleMessage()
    119         item_ids = [int(x) for x in items]
     166        self.logger.debug("Called getItemLinks")
     167        msg = IbidasSimpleMessage()
     168        assert args.has_key('items'), \
     169                "No items provided to get links for."
     170        item_ids = [int(x) for x in args['items']]
    120171        items = self.cd.item[_.item_id.within(*item_ids)]
    121172        links = self.cd.item_child_link.join(items, 'parent_id', 'item_id').up()
     
    153204        provided set_id."""
    154205        source_ids = sourceID['items']
    155         print "getSetContentBySourceID called"
    156         print "source_id: ", sourceID
     206        self.logger.debug("Called getSetContentBySourceID")
     207        self.logger.debug("source_id: " + str(sourceID))
    157208        msg = IbidasSimpleMessage()
    158209        items = self.cd.item[_.source_id.within(*source_ids)]
  • web/IbidasSimpleMessage.py

    r268 r269  
    1515    ITEM_STR        = 'items'
    1616    ITEM_FN_STR     = 'item_fn'
     17    ITEM_FT_STR     = 'item_ft'
    1718    ITEMLINK_STR    = 'itemlinks'
    1819    ITEMLINK_FN_STR = 'itemlink_fn'
     20    ITEMLINK_FT_STR = 'itemlink_ft'
    1921    SET_STR         = 'sets'
    2022    SET_FN_STR      = 'set_fn'
     23    SET_FT_STR      = 'set_ft'
    2124    SETLINK_STR     = 'setlinks'
    2225    SETLINK_FN_STR  = 'setlink_fn'
     26    SETLINK_FT_STR  = 'setlink_ft'
    2327    TERM_STR        = 'terms'
    2428    TERM_FN_STR     = 'term_fn'
     29    TERM_FT_STR     = 'term_ft'
    2530    TERMLINK_STR    = 'termlinks'
    2631    TERMLINK_FN_STR = 'termlink_fn'
     32    TERMLINK_FT_STR = 'termlink_ft'
    2733   
    2834    ITEM_FN = ('item_id', 'type_id', 'source_id', 'display_name')
     
    97103    def finalize(self):
    98104        """Creates the message to be send to the client.
     105        All entries follow the same pattern: the data is extracted from the
     106        Ibidas table object and the column names and types are added.
     107        The table data is stored as a list of tuples and the column names and
     108        types are stored as lists of strings.
    99109        """
    100110        out = {}
     
    102112            out[self.ITEM_STR] = self.items.to_python()
    103113            out[self.ITEM_FN_STR] = self.item_fn
     114            out[self.ITEM_FT_STR] = [str(x) for x in self.items.fields.type()]
    104115        if not self.itemlinks is None:
    105116            out[self.ITEMLINK_STR] = self.itemlinks.to_python()
    106117            out[self.ITEMLINK_FN_STR] = self.itemlink_fn
     118            out[self.ITEMLINK_FT_STR] = [str(x) for x in self.itemlinks.fields.type()]
    107119        if not self.terms is None:
    108120            out[self.TERM_STR] = self.terms.to_python()
    109121            out[self.TERM_FN_STR] = self.term_fn
     122            out[self.TERM_FT_STR] = [str(x) for x in self.terms.fields.type()]
    110123        if not self.termlinks is None:
    111124            out[self.TERMLINK_STR] = self.termlinks.to_python()
    112125            out[self.TERMLINK_FN_STR] = self.termlink_fn
     126            out[self.TERMLINK_FT_STR] = [str(x) for x in self.termlinks.fields.type()]
    113127        if not self.sets is None:
    114128            out[self.SET_STR] = self.sets.to_python()
    115129            out[self.SET_FN_STR] = self.set_fn
     130            out[self.SET_FT_STR] = [str(x) for x in self.sets.fields.type()]
    116131        if not self.setlinks is None:
    117132            out[self.SETLINK_STR] = self.setlinks.to_python()
    118133            out[self.SETLINK_FN_STR] = self.setlink_fn
     134            out[self.SETLINK_FT_STR] = [str(x) for x in self.setlinks.fields.type()]
    119135        print out
    120136        return out
Note: See TracChangeset for help on using the changeset viewer.