parameters: (self, reltype, param)
return_statement: return new_obj
docstring: Add a new core to given relation.
docstring_summary: Add a new core to given relation.
docstring_tokens: Add a new core to given relation .
function: def _add_relation(self, reltype, param):
          """Add a new core to given relation."""
          param.validate()
          if hasattr(param, 'is_primary') and param.is_primary:
              existing = self._expand_relation(reltype)
              for obj in existing:
                  if obj.is_primary:
                      obj.is_primary = False
                      obj.save()
                      # XXX don't forget to update index
  
          # Transform param into core object
          # XXX find a better method ?
          attrs = {k: v for k, v in param.to_primitive().iteritems()
                   if v is not None}
          new_obj = self._relations[reltype].create(self.user,
                                                    self,
                                                    **attrs)
          rel_list = getattr(self, reltype)
          rel_list.append(new_obj.get_id())
          self.save()
          if self._index_class:
              self._add_relation_index(reltype, attrs)
          if hasattr(self, '_lookup_objects') and \
             reltype in self._lookup_objects:
              lookupkls = self._lookup_class
              look = lookupkls.create(user_id=self.user.user_id,
                                      value=new_obj.get_id(),
                                      contact_id=self.contact_id,
                                      type=reltype,
                                      lookup_id=new_obj.get_id())
              log.debug('Created lookup object for relation %s:%r' %
                        (reltype, look))
  
          return new_obj
function_tokens: def _add_relation ( self , reltype , param ) : param . validate ( ) if hasattr ( param , 'is_primary' ) and param . is_primary : existing = self . _expand_relation ( reltype ) for obj in existing : if obj . is_primary : obj . is_primary = False obj . save ( ) # XXX don't forget to update index # Transform param into core object # XXX find a better method ? attrs = { k : v for k , v in param . to_primitive ( ) . iteritems ( ) if v is not None } new_obj = self . _relations [ reltype ] . create ( self . user , self , * * attrs ) rel_list = getattr ( self , reltype ) rel_list . append ( new_obj . get_id ( ) ) self . save ( ) if self . _index_class : self . _add_relation_index ( reltype , attrs ) if hasattr ( self , '_lookup_objects' ) and reltype in self . _lookup_objects : lookupkls = self . _lookup_class look = lookupkls . create ( user_id = self . user . user_id , value = new_obj . get_id ( ) , contact_id = self . contact_id , type = reltype , lookup_id = new_obj . get_id ( ) ) log . debug ( 'Created lookup object for relation %s:%r' % ( reltype , look ) ) return new_obj