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

Revision 651, 5.1 KB checked in by aculapov, 5 years ago (diff)
  • cleaned up the unused files and the ones not related to the plugin
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    This component adds a level of abstraction over the raw ini file
12    information that makes easy the retrieval of settings for the upper layers.
13    """   
14   
15    def __init__(self, *args, **kwargs):
16        # init the parent
17        super(TicketMetadataProvider, self).__init__(*args, **kwargs)
18        self.reader = TicketConfigurationReader(self.env)
19
20    def get_defined_realms(self):
21        """
22        Returns a list with the names of the resources that have been defined.
23        """
24        return self.reader.get_resources()
25   
26    def get_ticket_types(self, resource):
27        """
28        Returns a list with the ticket types that have been defined.
29        """
30        return self.reader.get_groupings(resource)
31       
32    def get_relationship_filters(self, resource, type):
33        """
34        Returns the settings related to relationships.
35        """
36        # see if the resource has metadata
37        if resource.realm in self.reader.get_resources(): 
38            return self.reader.get_relationships(resource, type)
39        return None
40   
41    def get_field_filters(self, resource, type):
42        """
43        Returns the names of the ticket fields that are allowed per ticket
44        type.
45        """
46        # see if the resource has metadata
47        if resource.realm in self.reader.get_resources(): 
48            return self.reader.get_filters_for_grouping(resource, type)
49        return None
50   
51    def get_ticket_group_field(self, resource):
52        return self.reader.get_groupings(resource)[0]
53
54
55
56class OperationsSystem(Component):
57    operations = ExtensionPoint(IOperationsProvider)
58    pass
59
60class SynchronizeFields(Component):
61   
62    implements(IOperationsProvider)
63   
64    def get_operation_name(self):
65        return 'synchronize'
66   
67    def register_for_events(self):
68        yield 'change'
69        yield 'create'
70   
71    def run(self, args):
72        """
73        Make the resources have the same values of there attributes.
74       
75        @param args: tuple
76        list[trac.resource.Resource], list[String]
77        tickets, ticket field names
78        @return list[trac.resource.Resource]
79        """
80        resources, attributes = args
81        # create the store data dictionary
82        data = dict.fromkeys(attributes, 
83                               dict.fromkeys([res.id for res in resources], 
84                                               dict()))
85        # init  the data dictionary
86        for attribute in data.keys() :
87            for resource in resources :
88                if data[attribute][resource.id].has_key(resource[attribute]) :
89                    data[attribute][resource.id][resource[attribute]] += 1
90                else :
91                    data[attribute][resource.id][resource[attribute]] = 1
92       
93        # create a store for the min values
94        values = dict().fromkeys(attributes)
95       
96        def get_min_key(d):
97            # get the first values
98            min_key = d.keys()[0]
99            min_value = d[d.keys()[0]]
100            # get the min
101            for key in d.keys():
102                if min_value > d[key] :
103                    min_value = d[key]
104                    min_key = key
105            return min_key
106       
107        # synchronize all resources to the same unique value
108        for attribute in data.keys() :
109            for resource in data[attribute].keys() :
110                values[attribute] = get_min_key(data[attribute][resource])
111       
112        # copy the values
113        for resource in resources :
114            for att in attributes :
115                resource[att] = values[att]
116       
117        return resources
118   
119class CopyFields(Component):
120   
121    implements(IOperationsProvider)
122   
123    def get_operation_name(self):
124        return 'copy'
125   
126    def register_for_events(self):
127        yield 'change'
128        yield 'create'
129   
130    def run(self, args):
131        from_resource, to_resources, attributes = args
132        pass
133   
134class SumFields(Component):
135   
136    implements(IOperationsProvider)
137   
138    def get_operation_name(self):
139        return 'sum'
140   
141    def register_for_events(self):
142        yield 'change'
143        yield 'create'
144   
145    def run(self, args):
146        resource, from_resources, attribute = args
147        pass
148
149class AvgFields(Component):
150   
151    implements(IOperationsProvider)
152   
153    def get_operation_name(self):
154        return 'avg'
155   
156    def register_for_events(self):
157        yield 'change'
158        yield 'create'
159       
160    def run(self, args):
161        resource, from_resources, attribute = args
162
163class ProcentFields(Component):
164   
165    implements(IOperationsProvider)
166   
167    def get_operation_name(self):
168        return 'procent'
169   
170    def register_for_events(self):
171        yield 'change'
172        yield 'create'
173           
174    def run(self, args):
175        resource, from_resources, attribute =args
176   
Note: See TracBrowser for help on using the repository browser.