VC: Fix error on clone page for legacy-ID events
[cds-indico.git] / indico / MaKaC / domain.py
blob210f0c602cedd60c9ef32a61d5975830ed8af3fd
1 # This file is part of Indico.
2 # Copyright (C) 2002 - 2015 European Organization for Nuclear Research (CERN).
4 # Indico is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License as
6 # published by the Free Software Foundation; either version 3 of the
7 # License, or (at your option) any later version.
9 # Indico is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with Indico; if not, see <http://www.gnu.org/licenses/>.
17 from persistent import Persistent
19 from MaKaC.common.ObjectHolders import ObjectHolder
20 from MaKaC.common.Locators import Locator
21 from MaKaC.common import filters
22 from MaKaC.conference import TrashCanManager
25 class Domain(Persistent):
26 """This class represents the domains which are configured inside the system.
27 A domain is a set of IP filters which given an IP can determine if it
28 belongs to it or not depending on the filter application result.
29 """
31 def __init__( self ):
32 self.id = ""
33 self.name = ""
34 self.description = ""
35 self.filterList = []
37 def getLocator( self ):
38 d = Locator()
39 d["domainId"] = self.getId()
40 return d
42 def setId( self, newId ):
43 self.id = str(newId)
45 def getId( self ):
46 return self.id
48 def setName( self, newName ):
49 self.name = newName.strip()
51 def getName( self ):
52 return self.name
54 def setDescription( self, newDescription ):
55 self.description = newDescription.strip()
57 def getDescription( self ):
58 return self.description
60 def setFilterList(self, fl):
61 self.filterList = fl
63 def getFilterList( self ):
64 return self.filterList
66 def addFilter( self, filter ):
67 f = filter.strip()
68 #ToDo: We have to check a filter is specified in the good format (IP)
69 if f in self.filterList:
70 return
71 self.filterList.append(f)
73 def addFiltersFromStr( self, filters ):
74 """
75 """
76 for filter in filters.split(";"):
77 self.addFilter( filter.strip() )
79 def setFiltersFromStr( self, filters ):
80 """
81 """
82 self.filterList = []
83 self.addFiltersFromStr( filters )
85 def removeFilter( self, filter ):
86 f = filter.strip()
87 if f not in self.filterList:
88 return
89 self.filterList.remove(f)
91 def belongsTo( self, IP ):
92 ip = IP.strip()
93 #ToDo: check that the IP is in the correct format
94 for filt in self.getFilterList():
95 if ip.startswith(filt):
96 return True
97 return False
100 class _DomainFFName(filters.FilterField):
101 _id="name"
103 def satisfies(self, dom):
104 for value in self._values:
105 if str(dom.getName()).lower().find((str(value).strip().lower()))!=-1:
106 return True
107 return False
110 class _DomainFilterCriteria(filters.FilterCriteria):
111 _availableFields={"name":_DomainFFName}
113 def __init__(self,criteria={}):
114 filters.FilterCriteria.__init__(self,None,criteria)
117 class DomainHolder(ObjectHolder):
118 idxName = "domains"
119 counterName = "DOMAINS"
121 def match( self, criteria):
124 crit={}
125 for f,v in criteria.items():
126 crit[f]=[v]
127 f=filters.SimpleFilter(_DomainFilterCriteria(crit),None)
128 return f.apply(self.getList())
130 def remove(self, item):
131 # METHOD HAS TO BE IMPLEMENTED...
132 ObjectHolder.remove(self, item)
133 TrashCanManager().add(item)
135 def getLength( self ):
136 return len(self.getList())
138 def getBrowseIndex( self ):
139 letters = []
140 for domain in self.getList():
141 name = domain.getName()
142 if name and not name[0].lower() in letters:
143 letters.append(name[0].lower())
144 letters.sort()
145 return letters
147 def matchFirstLetter( self, letter ):
148 list = []
149 for domain in self.getList():
150 name = domain.getName()
151 if name and name[0].lower() == letter.lower():
152 list.append(domain)
153 return list