- print 'Automagical Accessor set method',classname,get_name,tagname,primary_key,secondary_key
- print 'Warning: PLC/Accessors/Factory is an ongoing work'
- return None
+ # locate the object
+ if isinstance (id_or_name, int):
+ filter={primary_key:id_or_name}
+ else:
+ filter={secondary_key:id_or_name}
+ objs = table_class(self.api, filter,[primary_key])
+ if not objs:
+ raise PLCInvalidArgument, "Cannot set tag on %s %r"%(objclass.__name__,id_or_name)
+ primary_id = objs[0][primary_key]
+
+ # search tag type & create if needed
+ tag_types = TagTypes (self.api, {'tagname':tagname})
+ if tag_types:
+ tag_type = tag_types[0]
+ else:
+ # not found: create it
+ tag_type_fields = {'tagname':tagname,
+ 'category' : category,
+ 'description' : description,
+ 'min_role_id': tag_min_role_id}
+ tag_type = TagType (self.api, tag_type_fields)
+ tag_type.sync()
+ # proceed
+ tag_type_id = tag_type['tag_type_id']
+ filter = {'tag_type_id':tag_type_id}
+ if isinstance (id_or_name,int):
+ filter[primary_key]=id_or_name
+ else:
+ filter[secondary_key]=id_or_name
+ joins = joins_class (self.api,filter)
+ if not joins:
+ join = join_class (self.api)
+ join['tag_type_id']=tag_type_id
+ join[primary_key]=primary_id
+ join[value_key]=tagvalue
+ join.sync()
+ else:
+ joins[0][value_key]=tagvalue
+ joins[0].sync()
+
+ # attach it