Use template for inventory
authorHeiko Schlittermann <hs@schlittermann.de>
Thu, 24 Dec 2015 00:31:53 +0100
changeset 59 ade26fe6ecbb
parent 58 adf016ea4348
child 60 e3d6e4095a2c
Use template for inventory
.hgignore
Makefile
once.pl
templates/footer.html
templates/inventory.html
templates/once.tt
--- a/.hgignore	Wed Dec 23 00:26:33 2015 +0100
+++ b/.hgignore	Thu Dec 24 00:31:53 2015 +0100
@@ -1,2 +1,3 @@
 .htaccess
 d/
+var/version.tt
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Makefile	Thu Dec 24 00:31:53 2015 +0100
@@ -0,0 +1,4 @@
+var/version.tt:	once.pl
+	# $@
+	@-mkdir -p $(dir $@)
+	@echo '[% vcs.version = "'`hg describe`'" %]' > $@
--- a/once.pl	Wed Dec 23 00:26:33 2015 +0100
+++ b/once.pl	Thu Dec 24 00:31:53 2015 +0100
@@ -34,12 +34,14 @@
 use CGI::Carp qw(fatalsToBrowser);
 use CGI::Pretty;
 use IO::File;
+use FindBin qw($RealBin);
 use File::Basename;
 use File::Path qw(remove_tree make_path);
 use File::Spec::Functions;
 use File::MimeInfo qw(mimetype);
 use Cwd qw(getcwd realpath);
 use Digest::MD5 qw(md5_hex md5);
+use Template;
 use experimental qw(smartmatch lexical_topic);
 
 sub humanize;     # convert numbers to human readable format
@@ -179,11 +181,8 @@
         exit 0;
     }
 
-    print header(-charset => "UTF-8"),
-      start_html(-title => "once"),
-      h1 "Ansicht: $view";
+    # save the uploaded file
 
-    # calculate the file name for the uploaded file
     if (length(my $file = param('upload'))) {
         my $uuid = gen_uuid();
         my ($delete, $expires, $days) = do {
@@ -224,22 +223,25 @@
         }
 
     }
-    print hr;
+
+    # create the view
+    print header(-charset => "UTF-8");
+
+    my %tt = (view => $view);
+    my $tt = Template->new(
+        {
+            INCLUDE_PATH => =>
+              [map { catfile($RealBin, $_) } 'var', 'templates.override', 'templates']
+        }
+    ) or die $Template::ERROR;
 
     # List the current content
     if (my @files = map { deslash $_ } glob "$user_dir/*-*/*") {
 
-        print p <<__;
-Der gültige Download-Link ist die Link-Adresse, die sich hinter
-dem Datei-Namen verbirgt. (Firefox: Rechte Maustaste, Link-Location).
-Nach Ablauf des <a href="http://de.wikipedia.org/wiki/Mindesthaltbarkeitsdatum">MHD</a>
-wird die Datei automatisch gelöscht.
-__
+        foreach (map { /(.*)/ } sort { -M $a <=> -M $b } @files) {
 
-        print start_table,
-          Tr(th { align => "left" }, [qw/Name Größe Hochgeladen Löschung/]);
+            my %file;
 
-        foreach (map { /(.*)/ } sort { -M $a <=> -M $b } @files) {
             my ($file, $dir) = fileparse($_);
             $dir = substr $dir,
               length $ONCE_VAR;    # make it relative to $ONCE_VAR
@@ -266,17 +268,19 @@
                 $d = 'nur manuell';
             }
 
-            print Tr(
-                td(a { href => "$ENV{PATH_INFO}/$dir$file" }, $file),
-                td({ align => "right" }, humanize((stat $_)[7])),
-                td(scalar localtime +(stat $_)[9]),
-                td($d),
-                td(a({ href => "?delete=$dir" }, 'remove'))
-            );
+            $file{name}          = $file;
+            $file{link}          = "$ENV{PATH_INFO}/$dir$file";
+            $file{size}          = humanize -s $_;
+            $file{uploaded}      = (stat _)[9];
+            $file{removal}{type} = $d;
+            $file{removal}{link} = "?delete=$dir";
+
+            push @{ $tt{files} }, \%file;
         }
 
-        print end_table, hr;
+        $tt->process('inventory.html', \%tt) or die $tt->error();
         return 0;
+
     }
 
     print start_multipart_form, start_table,
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/templates/footer.html	Thu Dec 24 00:31:53 2015 +0100
@@ -0,0 +1,3 @@
+[%# to be included %]
+<hr />
+<div align=right>[% vcs.version %] | Scripting Heiko Schlittermann</div>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/templates/inventory.html	Thu Dec 24 00:31:53 2015 +0100
@@ -0,0 +1,37 @@
+<html>
+<!-- some modules the template engine needs -->
+[% PROCESS once.tt -%]
+<head>
+<title>[% product %]</title>
+<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+<style type="text/css">
+    th { text-align: left; }
+</style>
+</head>
+<body>
+<h1>Ansicht: [% view %]</h1>
+<p>
+Der gültige Download-Link ist die Link-Adresse, die sich hinter
+dem Datei-Namen verbirgt. (Firefox: Rechte Maustaste,
+Link-Location).  Nach Ablauf des <a href="http://de.wikipedia.org/wiki/Mindesthaltbarkeitsdatum">MHD</a>
+wird die Datei automatisch gelöscht.
+</p>
+
+<hr />
+
+<table>
+<tr><th>Name</th><th>Größe</th><th>Hochgeladen</th><th>Löschung</hr><th>Aktion</th>
+</tr>
+[% FOREACH file IN files %]
+<tr>
+    <td><a href="[% file.link %]">[% file.name %]</a></td>
+    <td align="right">[% file.size %]</td>
+    <td>[% date.format(file.uploaded, "%c") %]</td>
+    <td>[% file.removal.type %]</td>
+    <td><a href="[% file.removal.link %]">remove</a></td>
+</td>
+[% END %]
+</table>
+[% INCLUDE footer.html %]
+</body>
+</html>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/templates/once.tt	Thu Dec 24 00:31:53 2015 +0100
@@ -0,0 +1,5 @@
+[% USE date %]
+[% PROCESS version.tt %]
+[%
+    product = 'ONCE'
+%]