changeset 164:db98e843b83c

text_x_arnica, arnica_slides: implemented sendcached from 1.8 repository (this version currently needs the 1.8 dev repository)
author Reimar Bauer <rb.proj AT googlemail DOT com>
date Mon, 14 Jul 2008 16:12:05 +0200
parents 2676d2cbfaea
children 7332323c8a6f
files data/plugin/action/arnica_slides.py data/plugin/parser/text_x_arnica.py
diffstat 2 files changed, 37 insertions(+), 74 deletions(-) [+]
line wrap: on
line diff
--- a/data/plugin/action/arnica_slides.py	Mon Jul 14 09:06:30 2008 +0200
+++ b/data/plugin/action/arnica_slides.py	Mon Jul 14 16:12:05 2008 +0200
@@ -15,7 +15,7 @@
 
 from MoinMoin import  caching, config, wikiutil
 from MoinMoin.Page import Page
-from MoinMoin.action import ActionBase, AttachFile
+from MoinMoin.action import ActionBase, AttachFile, sendcached
 from MoinMoin.formatter.text_html import Formatter
 from MoinMoin.packages import packLine, unpackLine
 from MoinMoin.util import filesys, timefuncs
@@ -27,48 +27,6 @@
 
 action_name = __name__.split('.')[-1]
 
-def send_cache_file(request, pagename, arena_dir, target):
-    """ sends a cached thumbnail or webnail to the browser
-    @param request: request object
-    @param pagenam:  the name of the page
-    @param arena_dir: the name of the cache arena
-    @target: the name of the file in the cache arena
-    """
-    # XXX 1.8 will have a slightly different sendcache action included.
-    _ = request.getText
-    msg = None
-    if not request.user.may.read(pagename):
-        msg = _('You are not allowed to view attachments of this page.')
-
-    fpath = os.path.join(arena_dir, target)
-    if not target or not os.path.exists(fpath) or not wikiutil.isPicture(fpath):
-        msg = _("Attachment '%(filename)s' does not exist!") % {"filename": target}
-
-    if msg:
-        request.theme.add_msg(msg, "error")
-        Page(request, pagename).send_page()
-        return
-
-    timestamp = timefuncs.formathttpdate(int(os.path.getmtime(fpath)))
-    if request.if_modified_since == timestamp:
-        request.emit_http_headers(["Status: 304 Not modified"])
-    else:
-        mt = wikiutil.MimeType(filename=target)
-        content_type = mt.content_type()
-        mime_type = mt.mime_type()
-
-        content_dispo = 'inline'
-        filename_enc = target.encode(config.charset)
-
-        request.emit_http_headers([
-            'Content-Type: %s' % content_type,
-            'Last-Modified: %s' % timestamp,
-            'Content-Length: %d' % os.path.getsize(fpath),
-            'Content-Disposition: %s; filename="%s"' % (content_dispo, filename_enc),
-                                 ])
-        # send data
-        request.send_file(open(fpath, 'rb'))
-
 def image_rotate(file_name, angle, img_type):
     """ rotates the image and dependent images
 
@@ -219,7 +177,7 @@
     @param image_for_webnail: identifier for AttchFile or arnica_slides
     """
     if not image_for_webnail:
-        url = getAttachUrl(request.getScriptname(), pagename, "")
+        url = sendcached.get_url(request, "")
     else:
         url = AttachFile.getAttachUrl(pagename, "", request)
 
@@ -287,7 +245,7 @@
 
     for filename in text:
         if not image_for_webnail:
-            url = getAttachUrl(request.getScriptname(), pagename, filename)
+            url = sendcached.get_url(request, filename)
         else:
             url = AttachFile.getAttachUrl(pagename, filename, request)
 
@@ -302,12 +260,6 @@
 
     return txt
 
-def getAttachUrl(base_url, pagename, filename):
-    """ Get URL that points to cache of `filename` on page `pagename`. """
-    url = "%(base_url)s/%(pagename)s?action=arnica_slides&do=view&target=%(filename)s" % {'base_url': base_url,
-                                                                                          'pagename': pagename,
-                                                                                          'filename': filename}
-    return wikiutil.escape(url)
 
 class arnica_slides(ActionBase):
     """ arnica_slides page action
@@ -381,7 +333,7 @@
 
         web = {}
         if not image_for_webnail:
-            web['src'] = getAttachUrl(request.getScriptname(), pagename, target)
+            web['src'] = sendcached.get_url(request, target)
         else:
             web['src']  = AttachFile.getAttachUrl(pagename, target, request)
 
--- a/data/plugin/parser/text_x_arnica.py	Mon Jul 14 09:06:30 2008 +0200
+++ b/data/plugin/parser/text_x_arnica.py	Mon Jul 14 16:12:05 2008 +0200
@@ -14,11 +14,11 @@
     @license: GNU GPL, see COPYING for details.
 """
 
-import os, re
+import os, re, StringIO
 from random import randint
 
 from MoinMoin import caching, config, wikiutil
-from MoinMoin.action import AttachFile
+from MoinMoin.action import AttachFile, sendcached
 from MoinMoin.filter import EXIF
 from MoinMoin.packages import packLine
 from MoinMoin.Page import Page
@@ -32,6 +32,8 @@
 
 parser_name = __name__.split('.')[-1]
 
+secret = 'arnica'
+
 class _DataBrowserWidget(DataBrowserWidget):
     """ we have to remove the used form in format otherwise the needed form elements
         becomes a nested form and this do raise lot's of strict html4.01 errors"""
@@ -185,11 +187,6 @@
     files = [fn for fn in files if wikiutil.isPicture(fn) and not fn.startswith('tmp.')]
     return files
 
-def getCacheUrl(url, filename):
-    """ Get URL that points to cache of `filename` on page `pagename`. """
-    url_action = "%(url)s?action=arnica_slides&do=view&target=%(filename)s" % {'url': url,
-                                                                               'filename': filename}
-    return wikiutil.escape(url_action)
 
 class Parser:
     """ arnica parser """
@@ -368,7 +365,7 @@
      "image_for_webnail": self.image_for_webnail,
      "target": self.webimg[idx],
      "images": packLine([self.webimg[idx]] + self.webimg),
-     "thumbnail": getCacheUrl(Page(self.request, self.pagename).url(self.request), self.thumb[idx]),
+     "thumbnail": sendcached.get_url(self.request, self.thumb[idx]),
      "thumbnail_width": self.thumbnail_width,
      "html_tools": self.html_show_tools(idx),
      "date_html": self.html_show_date(idx),
@@ -395,12 +392,16 @@
             fname, ext = os.path.splitext(attfile)
             if ext in ('.gif', '.png'):
                 self.imgtype.append('PNG')
-                webnail = 'tmp.webnail_%s.png' % fname
-                thumbfile = 'tmp.thumbnail_%s.png' % fname
+                file_name = 'tmp.webnail_%s.png' % fname
+                webnail = sendcached.cache_key(self.request, itemname=self.pagename, attachname=attfile, secret=secret) + 'W'
+                file_name = 'tmp.thumbnail_%s.png' % fname
+                thumbfile = sendcached.cache_key(self.request, itemname=self.pagename, attachname=attfile, secret=secret) + 'T'
             else:
                 self.imgtype.append("JPEG")
-                webnail = 'tmp.webnail_%s.jpg' % fname
-                thumbfile = 'tmp.thumbnail_%s.jpg' % fname
+                file_name = 'tmp.webnail_%s.jpg' % fname
+                webnail = sendcached.cache_key(self.request, itemname=self.pagename, attachname=attfile, secret=secret) + 'W'
+                file_name = 'tmp.thumbnail_%s.jpg' % fname
+                thumbfile = sendcached.cache_key(self.request, itemname=self.pagename, attachname=attfile, secret=secret) + 'T'
 
             att_file = os.path.join(path, attfile)
             if os.path.exists(att_file):
@@ -504,13 +505,15 @@
             return
 
         page = Page(self.request, self.pagename)
-        cache_web = caching.CacheEntry(self.request, page, webnail, scope='item', use_pickle=True)
-        cache_thumb = caching.CacheEntry(self.request, page, thumbnail, scope='item', use_pickle=True)
 
-        arena_dir = caching.get_arena_dir(self.request, page, 'item')
-        webf = os.path.join(arena_dir, webnail)
-        thumbf = os.path.join(arena_dir, thumbnail)
-
+        cache_web = caching.CacheEntry(self.request, sendcached.sendcached_arena, webnail+'.data',
+                                        sendcached.sendcached_scope, do_locking=False)
+        webf = cache_web._filename()
+        
+        cache_thumb = caching.CacheEntry(self.request, sendcached.sendcached_arena, thumbnail+'.data',
+                                        sendcached.sendcached_scope, do_locking=False)
+        thumbf = cache_thumb._filename()
+        
         if self.renew:
             cache_web.remove()
             cache_thumb.remove()
@@ -527,10 +530,18 @@
             if not cache_web.exists():
                 if not self.image_for_webnail:
                     im_obj.thumbnail((int(self.webnail_width.strip('px')), int(self.webnail_width.strip('px'))), Image.ANTIALIAS)
+                    # ToDo find a way to add the im_obj data immediately to the cache arena
+                    im_obj.save(webf, image_type)
+                    st = os.stat(webf)
+                    sendcached.put_cache(self.request, webnail, 'dummy', filename=image, content_length=st.st_size)
                     im_obj.save(webf, image_type)
 
             if not cache_thumb.exists():
                 im_obj.thumbnail((int(self.thumbnail_width.strip('px')), int(self.thumbnail_width.strip('px'))), Image.ANTIALIAS)
+                # ToDo find a way to add the im_obj data immediately to the cache arena
+                im_obj.save(thumbf, image_type)
+                st = os.stat(thumbf)
+                sendcached.put_cache(self.request, thumbnail, 'dummy', filename=image, content_length=st.st_size)
                 im_obj.save(thumbf, image_type)
 
     def render(self, formatter):
@@ -608,11 +619,11 @@
                     self.webimg = self.full
                 text = self.html_arrange_thumbnails(i)
             except ValueError:
-                # ToDo use """ """
-                text = _("""You can't use as album image: \
+                text = self.formatter.text(_("""You can't use as album image: \
                          "%(album_image)s" because it does not exists or it is not listed \
-                         in your item list!""") % {"album_image": album_image, }
-            result.append(''.join(self.formatter.text(text)))
+                         in your item list!""") % {"album_image": album_image, })
+                         
+            result.append(''.join(text))
 
         # adds the last row if it is not filled up
         if result: