|
1 #!/usr/bin/python2.5 |
|
2 # |
|
3 # Copyright 2009 the Melange authors. |
|
4 # |
|
5 # Licensed under the Apache License, Version 2.0 (the "License"); |
|
6 # you may not use this file except in compliance with the License. |
|
7 # You may obtain a copy of the License at |
|
8 # |
|
9 # http://www.apache.org/licenses/LICENSE-2.0 |
|
10 # |
|
11 # Unless required by applicable law or agreed to in writing, software |
|
12 # distributed under the License is distributed on an "AS IS" BASIS, |
|
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
14 # See the License for the specific language governing permissions and |
|
15 # limitations under the License. |
|
16 |
|
17 """The module which contains functions to manage tags in Melange. |
|
18 """ |
|
19 |
|
20 __authors__ = [ |
|
21 '"Daniel Hans" <daniel.m.hans@gmail.com>', |
|
22 ] |
|
23 |
|
24 |
|
25 class TagsService(object): |
|
26 """Helper class for tags corresponding to a model. |
|
27 """ |
|
28 |
|
29 def __init__(self, tag_names): |
|
30 """Defines tag names. |
|
31 """ |
|
32 |
|
33 self.tag_names = tag_names |
|
34 |
|
35 def prepareTagsForStoring(self, fields, scope): |
|
36 """For those fields which represent task values it prepares actual |
|
37 tag entries. |
|
38 |
|
39 Args: |
|
40 fields: a dict mapping tag names with values |
|
41 scope: scope for the tag that will be stored |
|
42 """ |
|
43 |
|
44 for tag_name, tag_value in fields.iteritems(): |
|
45 if tag_name in self.tag_names: |
|
46 fields[tag_name] = {'tags': tag_value, 'scope': scope} |
|
47 |
|
48 def setTagValuesForEntity(self, entity, fields): |
|
49 """Sets tag values for a particular entity based on fields dictionary. |
|
50 |
|
51 Args: |
|
52 entity: an entity which is to be tagged |
|
53 fields: a dict mapping tag names with values |
|
54 """ |
|
55 |
|
56 if not entity: |
|
57 return None |
|
58 |
|
59 for tag_name, tag_value in fields.iteritems(): |
|
60 if tag_name in self.tag_names: |
|
61 setattr(entity, tag_name, tag_value) |
|
62 |
|
63 return entity |
|
64 |
|
65 def removeAllTagsForEntity(self, entity): |
|
66 """Removes all tags defined for a particular entity. |
|
67 |
|
68 Args: |
|
69 entity: entity which the tags will be removed for |
|
70 """ |
|
71 |
|
72 self.removeTagsForEntity(entity, self.tag_names) |
|
73 |
|
74 def removeTagsForEntity(self, entity, tag_names): |
|
75 """Removes all tags values for a particular entity based on tag_names |
|
76 list with name of the tags that are to be removed. |
|
77 |
|
78 Args: |
|
79 entity: entity which the tags will be removed for |
|
80 tag_names: list of tags to remove |
|
81 """ |
|
82 |
|
83 if not entity: |
|
84 return |
|
85 |
|
86 for tag_name in tag_names: |
|
87 if tag_name in tag_names: |
|
88 cls = entity.tags_class(tag_name) |
|
89 tags = cls.get_tags_for_key(entity.key()) |
|
90 for tag in tags: |
|
91 tag.remove_tagged(entity.key()) |