ugly, but working, preferences editor
authorTero Marttila <terom@fixme.fi>
Mon, 09 Feb 2009 06:56:04 +0200
changeset 59 8ec729c5d998
parent 58 ce028d356e1f
child 60 759369a79527
ugly, but working, preferences editor
handlers.py
log_formatter.py
preferences.py
templates/channel.tmpl
templates/index.tmpl
templates/layout.tmpl
templates/preferences.tmpl
urls.py
--- a/handlers.py	Mon Feb 09 06:19:12 2009 +0200
+++ b/handlers.py	Mon Feb 09 06:56:04 2009 +0200
@@ -26,6 +26,34 @@
         req             = request,
     )
 
+# XXX: fix this namespace crap
+@preferences.handler()
+def preferences_ (request) :
+    """
+        Preferences editor
+    """
+
+    # POST?
+    if request.is_post() :
+        # update any modified preferences
+        for pref in preferences.pref_list :
+            # get+parse new POST'd value
+            # XXX: this doesn't postprocess
+            new_value = pref.parse(request.get_post(pref.name))
+
+            # update if changed
+            if new_value != request.prefs[pref] :
+                request.prefs.set(pref.name, new_value)
+
+    # render
+    return templates.render_to_response("preferences",
+        req             = request,
+        prefs           = request.prefs,
+        preferences     = prefs,
+        timezone        = request.prefs[prefs.timezone],
+        timezones       = pytz.common_timezones,
+    )
+
 def channel_select (request, channel) :
     """
         Redirect to the appropriate channel_view
@@ -33,8 +61,8 @@
    
     return http.Redirect(urls.channel_view.build(request, channel=channel))
 
-@preferences.handler(prefs.Formatter, prefs.Timezone)
-def channel_view (request, channel, count, formatter, timezone) :
+@preferences.handler(prefs.formatter)
+def channel_view (request, channel, count, formatter) :
     """
         The main channel view page, display the most important info, and all requisite links
     """
@@ -47,7 +75,7 @@
 
     return templates.render_to_response("channel_view",
         req             = request,
-        timezone        = timezone,
+        timezone        = request.prefs[prefs.timezone],
         channel         = channel,
         count           = count,
         formatter       = formatter,
@@ -70,7 +98,7 @@
     else :
         raise http.ResponseError("Unknown filetype %r" % format)
 
-@preferences.handler(prefs.Timezone)
+@preferences.handler(prefs.timezone)
 def channel_calendar (request, channel, year, month, timezone) :
     """
         Display a list of avilable logs for some month
@@ -99,7 +127,7 @@
         days            = days,
     )
 
-@preferences.handler(prefs.Formatter, prefs.Timezone)
+@preferences.handler(prefs.formatter, prefs.timezone)
 def channel_date (request, channel, date, formatter, timezone) :
     """
         Display all log data for the given date
--- a/log_formatter.py	Mon Feb 09 06:19:12 2009 +0200
+++ b/log_formatter.py	Mon Feb 09 06:56:04 2009 +0200
@@ -77,6 +77,7 @@
     """
 
     name = 'irssi'
+    title = "Irssi (plain)"
 
     def format_html (self, lines) :
         """
--- a/preferences.py	Mon Feb 09 06:19:12 2009 +0200
+++ b/preferences.py	Mon Feb 09 06:56:04 2009 +0200
@@ -67,14 +67,17 @@
         self.values = dict(
             (name, self.preferences.pref_map[name].process(self, value)) for name, value in self.values.iteritems()
         )
-
+    
     def get (self, pref) :
         """
             Return the value for the given Preference
         """
         
         return self.values[pref.name]
-    
+
+    # support dict-access
+    __getitem__ = get
+
     def set (self, name, value_obj) :
         """
             Set a new value for the given preference
@@ -154,6 +157,10 @@
                 # load preferences
                 cookies, prefs = self.load(request)
 
+                # bind to request.prefs
+                # XXX: better way to do this? :/
+                request.prefs = prefs
+
                 # update args with new ones
                 args.update(((pref.name, prefs.get(pref)) for pref in pref_list))
 
@@ -162,6 +169,9 @@
 
                 # set cookies?
                 if prefs.set_cookies :
+                    if not cookies :
+                        cookies = Cookie.SimpleCookie('')
+
                     # update cookies
                     for key, value in prefs.set_cookies.iteritems() :
                         cookies[key] = value
@@ -201,7 +211,7 @@
     name = 'date_format'
 
     # default value
-    default = "%Y-%m%d"
+    default = "%Y-%m-%d"
 
 class Timezone (Preference) :
     """
@@ -256,22 +266,27 @@
             class LogFormatter -> fmt_name
         """
 
-        return fmt.name
+        return fmt_cls.name
     
     def process (self, prefs, fmt_cls) :
         """
             class LogFormatter -> LogFormatter(tz, time_fmt)
         """
 
-        return fmt_cls(prefs.get(Timezone), prefs.get(TimeFormat))
+        return fmt_cls(prefs[timezone], prefs[time_format])
 
 # and then build the Preferences object
 import log_formatter
 
+time_format = TimeFormat()
+date_format = DateFormat()
+timezone    = Timezone()
+formatter   = Formatter(log_formatter.FORMATTERS, log_formatter.IrssiFormatter)
+
 preferences = Preferences([
-    TimeFormat(),
-    DateFormat(),
-    Timezone(),
-    Formatter(log_formatter.FORMATTERS, log_formatter.IrssiFormatter),
+    time_format,
+    date_format,
+    timezone,
+    formatter,
 ])
 
--- a/templates/channel.tmpl	Mon Feb 09 06:19:12 2009 +0200
+++ b/templates/channel.tmpl	Mon Feb 09 06:56:04 2009 +0200
@@ -3,6 +3,7 @@
 <%def name="menu()">
 <ul>
     <li><a href="${urls.index.build(req)}">Home</a></li>
+    <li><a href="${urls.preferences.build(req)}">Preferences</a></li>
 
     <li>
         <a href="${urls.channel_view.build(req, channel=channel)}">Channel:</a>
--- a/templates/index.tmpl	Mon Feb 09 06:19:12 2009 +0200
+++ b/templates/index.tmpl	Mon Feb 09 06:56:04 2009 +0200
@@ -1,9 +1,10 @@
 <%inherit file="layout.tmpl" />
 
-<h1>Available Channels</h1>
+<div id="title">Available Channels</div>
+
 <ul>
 % for channel in channel_list :
-    <li><a href="${urls.channel_view.build(req, channel=channel.id)}">${channel.title}</a></li>
+    <li><a href="${urls.channel_view.build(req, channel=channel)}">${channel.title}</a></li>
 % endfor
 </ul>
 
--- a/templates/layout.tmpl	Mon Feb 09 06:19:12 2009 +0200
+++ b/templates/layout.tmpl	Mon Feb 09 06:56:04 2009 +0200
@@ -1,7 +1,10 @@
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
 
 <%def name="menu()">
-
+<ul>
+    <li><a href="${urls.index.build(req)}">Home</a></li>
+    <li><a href="${urls.preferences.build(req)}">Preferences</a></li>
+</ul>
 </%def>
 
 <%def name="footer_right()">
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/templates/preferences.tmpl	Mon Feb 09 06:56:04 2009 +0200
@@ -0,0 +1,40 @@
+<%inherit file="layout.tmpl" />
+
+<div id="title">Edit Preferences</div>
+
+<form action="${urls.preferences.build(req)}" method="POST">
+    <fieldset>
+        <legend>Dates / Times</legend>
+        
+        <label for="timezone">Timezone:</label>
+        <select name="timezone">
+        % for tz_name in timezones :
+            <option${' selected="selected"' if tz_name == prefs[preferences.timezone].zone else ''}>${tz_name}</option>
+        % endfor
+        </select>
+        <br/>
+
+        <label for="date_format">Date format:</label>
+        <input type="text" name="date_format" value="${prefs[preferences.date_format]}" />
+        <br/>
+        
+        <label for="time_format">Time format:</label>
+        <input type="text" name="time_format" value="${prefs[preferences.time_format]}" />
+        <br/>
+
+    </fieldset>
+
+    <fieldset>
+        <legend>Log Output</legend>
+
+        <label for="formatter">Formatter:</label>
+        <select name="formatter">
+        % for fmt_name, fmt in preferences.formatter.formatters.iteritems() :
+            <option value="${fmt_name}">${fmt.title}</option>
+        % endfor
+        </select>
+    </fieldset>
+
+    <input type="submit" value="Save" />
+</form>
+
--- a/urls.py	Mon Feb 09 06:19:12 2009 +0200
+++ b/urls.py	Mon Feb 09 06:56:04 2009 +0200
@@ -28,6 +28,7 @@
 
 # urls
 index               = url('/',                                                              handlers.index                  )
+preferences         = url('/preferences',                                                   handlers.preferences_           )
 channel_select      = url('/channel_select/?channel:cid',                                   handlers.channel_select         )
 channel_view        = url('/channels/{channel:cid}/?count:int=10',                          handlers.channel_view           )
 channel_last        = url('/channels/{channel:cid}/last/{count:int=100}/{format=html}',     handlers.channel_last           )