root/cherokee/trunk/admin/ModuleGeoip.py

Revision 2105, 2.5 kB (checked in by taher, 2 months ago)

--

Line 
1 from Form import *
2 from Table import *
3 from Module import *
4 from flags import *
5
6 import validations
7
8 ISO3166_URL      = "http://www.iso.org/iso/country_codes/iso_3166_code_lists/english_country_names_and_code_elements.htm"
9 NOTE_NEW_COUNTRY = "Add the initial country. It's possible to add more later on."
10 NOTE_ADD_COUNTRY = "Pick an additional country to add to the country list."
11 NOTE_COUNTRIES   = "List of countries from the client IPs. It must use the " + \
12     "<a target=\"_blank\" href=\"%s\">ISO 3166</a> country notation." % (ISO3166_URL)
13
14 class ModuleGeoip (Module, FormHelper):
15     validation = [('tmp!new_rule!value', validations.is_safe_id_list)]
16
17     def __init__ (self, cfg, prefix, submit_url):
18         FormHelper.__init__ (self, 'geoip', cfg)
19         Module.__init__ (self, 'geoip', cfg, prefix, submit_url)
20
21     def _render_new_entry (self):
22         cfg_key = '%s!value'%(self._prefix)
23         flags = OptionFlags (cfg_key)
24         button = '<input type="submit" value="Add" />'
25
26         table = TableProps()
27         self.AddProp (table, 'Country', cfg_key, str(flags) + button, NOTE_NEW_COUNTRY)
28         return str(table)
29
30     def _render_modify_entry (self):
31         cfg_key = '%s!countries'%(self._prefix)
32         key_val = self._cfg.get_val (cfg_key, "")
33
34         # Text entry
35         table = TableProps()
36         self.AddPropEntry (table, 'Countries', cfg_key, NOTE_COUNTRIES)
37
38         # Flags
39         cfg_key_fake = 'tmp!add_county'
40         flags = OptionFlags (cfg_key_fake)
41        
42         # Button
43         button = '<input type="button" value="Add" onClick="flags_add_to_key(\'%s\',\'%s\',\'%s\',\'%s\');"/>' % (
44                  cfg_key_fake, cfg_key, key_val, '/ajax/update')
45
46         content = ADD_FLAGS_TO_KEY_JS + str(flags) + button
47         self.AddProp (table, 'Add Country', "", content, NOTE_ADD_COUNTRY)
48
49         return str(table)
50    
51     def _op_render (self):
52         if self._prefix.startswith('tmp!'):
53             return self._render_new_entry()
54
55         return self._render_modify_entry()
56        
57     def _op_apply_changes (self, uri, post):
58         self.ApplyChangesPrefix (self._prefix, None, post)
59
60     def apply_cfg (self, values):
61         if not values.has_key('value'):
62             print "ERROR, a 'value' entry is needed!"
63
64         cfg_key  = '%s!match!countries'%(self._prefix)
65         contries = values['value']
66         self._cfg[cfg_key] = contries
67
68     def get_name (self):
69         return self._cfg.get_val ('%s!match!countries'%(self._prefix))
70
71     def get_type_name (self):
72         return self._id.capitalize()
Note: See TracBrowser for help on using the browser.