Mercurial > hg > bitsyblog
comparison bitsyblog/settings.py @ 0:e3823be6a423
initial commit of bitsyblog, from https://svn.openplans.org/svn/standalone/bitsyblog/trunk/
author | k0s <k0scist@gmail.com> |
---|---|
date | Sat, 12 Sep 2009 16:06:57 -0400 |
parents | |
children | e54a6405a496 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:e3823be6a423 |
---|---|
1 """user settings / preferences""" | |
2 | |
3 import parser # dateutils.parser with mods to retain strftime format | |
4 import urllib2 | |
5 import utils | |
6 | |
7 class InvalidSettingError(Exception): | |
8 """error when trying to validate a setting""" | |
9 | |
10 class Setting(object): | |
11 def __init__(self, name, value=None): | |
12 self.name = name | |
13 self.value = value | |
14 self.error_message = 'Invalid value for %s' % name | |
15 | |
16 def validator(self, value): | |
17 return True | |
18 | |
19 def set(self, value): | |
20 if not self.validator(value): | |
21 raise InvalidSettingError(self.error_message) | |
22 self.value = value | |
23 | |
24 class DateFormat(Setting): | |
25 def __init__(self): | |
26 Setting.__init__(self, 'Date format') | |
27 | |
28 def set(self, format): | |
29 value = parser.parser()._parse(format) | |
30 if value: | |
31 self.value = value.format | |
32 else: | |
33 raise InvalidSettingError('unrecognized date format: %s' % format) | |
34 | |
35 | |
36 class CSSFile(Setting): | |
37 def __init__(self): | |
38 Setting.__init__(self, 'CSS file') | |
39 | |
40 def set(self, value): | |
41 if not hasattr(value, 'file'): | |
42 return True # blank set: don't do anything | |
43 css = value.file.read() | |
44 try: | |
45 validcss = utils.validate_css(css) | |
46 except urllib2.URLError: | |
47 raise InvalidSettingError('Could not validate CSS (sorry!)') | |
48 if not validcss: | |
49 raise InvalidSettingError('%s is not valid css' % filename) | |
50 filename = value.filename | |
51 if not filename.endswith('.css'): | |
52 filename = '%s.css' % filename | |
53 self.value = dict(filename=filename, css=css) | |
54 | |
55 user = [ DateFormat(), | |
56 Setting('Subject'), | |
57 Setting('Friends'), | |
58 Setting('Stylesheet'), | |
59 ] | |
60 | |
61 form = user[:] | |
62 form += [ CSSFile() ] |