fix Slice bugs
[plcapi.git] / PLC / PersonTags.py
1 #
2 # Thierry Parmentelat - INRIA
3 #
4 from PLC.Faults import *
5 from PLC.Parameter import Parameter
6 from PLC.Storage.AlchemyObject import AlchemyObj
7 from PLC.TagTypes import TagType, TagTypes
8 from PLC.Persons import Person
9
10 class PersonTag(AlchemyObj):
11     """
12     Representation of a row in the person_tag.
13     To use, instantiate with a dict of values.
14     """
15
16     tablename = 'person_tag'
17     fields = {
18         'person_tag_id': Parameter(int, "Person setting identifier", primary_key=True),
19         'person_id': Person.fields['person_id'],
20         'email': Person.fields['email'],
21         'tag_type_id': TagType.fields['tag_type_id'],
22         'tagname': TagType.fields['tagname'],
23         'description': TagType.fields['description'],
24         'category': TagType.fields['category'],
25         'value': Parameter(str, "Person setting value"),
26         ### relations
27
28         }
29
30     def sync(self, commit=True, validate=True):
31         AlchemyObj.sync(self, commit=Ture, validate=True)
32         if 'person_tag_id' not in self:
33             AlchemyObj.insert(self, dict(self))
34         else:
35             AlchemyObj.update(self, {'person_tag_id': self['person_tag_id']}, dict(self))
36             
37         
38     def delete(self, commit=True):
39         assert 'person_tag_id' in self
40         AlchemyObj.delete(self, dict(self))
41
42 class PersonTags(list):
43     """
44     Representation of row(s) from the person_tag table in the
45     database.
46     """
47
48     def __init__(self, api, person_tag_filter = None, columns = None):
49         if not person_tag_filter:
50             person_tags = PersonTag().select()
51         elif isinstance(person_tag_filter, (list, tuple, set, int, long)):
52             person_tags = PersonTag().select(filter={'person_tag_id': person_tag_filter})
53         elif isinstance(person_tag_filter, dict):
54             person_tags = PersonTag().select(filter=person_tag_filter)
55         else:
56             raise PLCInvalidArgument, "Wrong person setting filter %r"%person_tag_filter
57
58         self.extend(person_tags)