source: trunk/plugins/tickettypedsystem/ticketsystem/tickettypes/util.py @ 415

Revision 415, 4.5 KB checked in by aculapov, 5 years ago (diff)
  • templates version
Line 
1# -*- coding: utf-8 -*-
2#
3# Copyright 2008 Optaros, Inc
4
5from trac.core import Component, ExtensionPoint, implements
6from ticketsystem.inireader import TicketConfigurationReader
7from ticketsystem.api import IOperationsProvider
8
9class TicketMetadataProvider(Component):
10   
11    def __init__(self, *args, **kwargs):
12        # init the parent
13        super(TicketMetadataProvider, self).__init__(*args, **kwargs)
14        self.reader = TicketConfigurationReader(self.env)
15
16    def get_defined_realms(self):
17        return self.reader.get_resources()
18   
19    def get_ticket_types(self, resource):
20        return self.reader.get_groupings(resource)
21       
22    def get_relationship_filters(self, resource, type):
23        # see if the resource has metadata
24        if resource.realm in self.reader.get_resources(): 
25            return self.reader.get_relationships(resource, type)
26        return None
27   
28    def get_field_filters(self, resource, type):
29        # see if the resource has metadata
30        if resource.realm in self.reader.get_resources(): 
31            return self.reader.get_filters_for_grouping(resource, type)
32        return None
33   
34    def get_ticket_group_field(self, resource):
35        return self.reader.get_groupings(resource)[0]
36
37
38class OperationsSystem(Component):
39    operations = ExtensionPoint(IOperationsProvider)
40    pass
41
42class SynchronizeFields(Component):
43   
44    implements(IOperationsProvider)
45   
46    def get_operation_name(self):
47        return 'synchronize'
48   
49    def register_for_events(self):
50        yield 'change'
51        yield 'create'
52   
53    def run(self, args):
54        """
55        Make the resources have the same values of there attributes.
56       
57        @param args: tuple
58        list[trac.resource.Resource], list[String]
59        tickets, ticket field names
60        @return list[trac.resource.Resource]
61        """
62        resources, attributes = args
63        # create the store data dictionary
64        data = dict.fromkeys(attributes, 
65                               dict.fromkeys([res.id for res in resources], 
66                                               dict()))
67        # init  the data dictionary
68        for attribute in data.keys() :
69            for resource in resources :
70                if data[attribute][resource.id].has_key(resource[attribute]) :
71                    data[attribute][resource.id][resource[attribute]] += 1
72                else :
73                    data[attribute][resource.id][resource[attribute]] = 1
74       
75        # create a store for the min values
76        values = dict().fromkeys(attributes)
77       
78        def get_min_key(d):
79            # get the first values
80            min_key = d.keys()[0]
81            min_value = d[d.keys()[0]]
82            # get the min
83            for key in d.keys():
84                if min_value > d[key] :
85                    min_value = d[key]
86                    min_key = key
87            return min_key
88       
89        # synchronize all resources to the same unique value
90        for attribute in data.keys() :
91            for resource in data[attribute].keys() :
92                values[attribute] = get_min_key(data[attribute][resource])
93       
94        # copy the values
95        for resource in resources :
96            for att in attributes :
97                resource[att] = values[att]
98       
99        return resources
100   
101class CopyFields(Component):
102   
103    implements(IOperationsProvider)
104   
105    def get_operation_name(self):
106        return 'copy'
107   
108    def register_for_events(self):
109        yield 'change'
110        yield 'create'
111   
112    def run(self, args):
113        from_resource, to_resources, attributes = args
114        pass
115   
116class SumFields(Component):
117   
118    implements(IOperationsProvider)
119   
120    def get_operation_name(self):
121        return 'sum'
122   
123    def register_for_events(self):
124        yield 'change'
125        yield 'create'
126   
127    def run(self, args):
128        resource, from_resources, attribute = args
129        pass
130
131class AvgFields(Component):
132   
133    implements(IOperationsProvider)
134   
135    def get_operation_name(self):
136        return 'avg'
137   
138    def register_for_events(self):
139        yield 'change'
140        yield 'create'
141       
142    def run(self, args):
143        resource, from_resources, attribute = args
144
145class ProcentFields(Component):
146   
147    implements(IOperationsProvider)
148   
149    def get_operation_name(self):
150        return 'procent'
151   
152    def register_for_events(self):
153        yield 'change'
154        yield 'create'
155           
156    def run(self, args):
157        resource, from_resources, attribute =args
158   
Note: See TracBrowser for help on using the repository browser.