From 9f62ad26b7b29dc9c67696b84a4b41bce03a8932 Mon Sep 17 00:00:00 2001
From: Ivan Tyagov <ivan@nexedi.com>
Date: Thu, 18 Jul 2013 11:45:15 +0000
Subject: [PATCH] Add validator for file upload and respective script.

---
 .../your_file.xml                             | 30 +++++++-
 .../WebSection_validateFileUpload.xml         | 76 +++++++++++++++++++
 .../your_file.xml                             | 30 +++++++-
 bt5/erp5_discussion/bt/revision               |  2 +-
 4 files changed, 135 insertions(+), 3 deletions(-)
 create mode 100644 bt5/erp5_discussion/SkinTemplateItem/portal_skins/erp5_discussion/WebSection_validateFileUpload.xml

diff --git a/bt5/erp5_discussion/SkinTemplateItem/portal_skins/erp5_discussion/DiscussionThread_viewCreateNewDiscussionPostDialog/your_file.xml b/bt5/erp5_discussion/SkinTemplateItem/portal_skins/erp5_discussion/DiscussionThread_viewCreateNewDiscussionPostDialog/your_file.xml
index a9def637b7..2ac7c8ec94 100644
--- a/bt5/erp5_discussion/SkinTemplateItem/portal_skins/erp5_discussion/DiscussionThread_viewCreateNewDiscussionPostDialog/your_file.xml
+++ b/bt5/erp5_discussion/SkinTemplateItem/portal_skins/erp5_discussion/DiscussionThread_viewCreateNewDiscussionPostDialog/your_file.xml
@@ -10,10 +10,19 @@
             <key> <string>delegated_list</string> </key>
             <value>
               <list>
+                <string>external_validator</string>
                 <string>title</string>
               </list>
             </value>
         </item>
+        <item>
+            <key> <string>delegated_message_list</string> </key>
+            <value>
+              <list>
+                <string>external_validator_failed</string>
+              </list>
+            </value>
+        </item>
         <item>
             <key> <string>id</string> </key>
             <value> <string>your_file</string> </value>
@@ -24,7 +33,7 @@
               <dictionary>
                 <item>
                     <key> <string>external_validator_failed</string> </key>
-                    <value> <string>The input failed the external validator.</string> </value>
+                    <value> <string>File is too big. Please consider to use a smaller file or request an account.</string> </value>
                 </item>
               </dictionary>
             </value>
@@ -71,6 +80,12 @@
             <key> <string>values</string> </key>
             <value>
               <dictionary>
+                <item>
+                    <key> <string>external_validator</string> </key>
+                    <value>
+                      <persistent> <string encoding="base64">AAAAAAAAAAI=</string> </persistent>
+                    </value>
+                </item>
                 <item>
                     <key> <string>field_id</string> </key>
                     <value> <string>my_file_field</string> </value>
@@ -93,4 +108,17 @@
       </dictionary>
     </pickle>
   </record>
+  <record id="2" aka="AAAAAAAAAAI=">
+    <pickle>
+      <global name="Method" module="Products.Formulator.MethodField"/>
+    </pickle>
+    <pickle>
+      <dictionary>
+        <item>
+            <key> <string>method_name</string> </key>
+            <value> <string>WebSection_validateFileUpload</string> </value>
+        </item>
+      </dictionary>
+    </pickle>
+  </record>
 </ZopeData>
diff --git a/bt5/erp5_discussion/SkinTemplateItem/portal_skins/erp5_discussion/WebSection_validateFileUpload.xml b/bt5/erp5_discussion/SkinTemplateItem/portal_skins/erp5_discussion/WebSection_validateFileUpload.xml
new file mode 100644
index 0000000000..9340fe35cc
--- /dev/null
+++ b/bt5/erp5_discussion/SkinTemplateItem/portal_skins/erp5_discussion/WebSection_validateFileUpload.xml
@@ -0,0 +1,76 @@
+<?xml version="1.0"?>
+<ZopeData>
+  <record id="1" aka="AAAAAAAAAAE=">
+    <pickle>
+      <global name="PythonScript" module="Products.PythonScripts.PythonScript"/>
+    </pickle>
+    <pickle>
+      <dictionary>
+        <item>
+            <key> <string>Script_magic</string> </key>
+            <value> <int>3</int> </value>
+        </item>
+        <item>
+            <key> <string>_bind_names</string> </key>
+            <value>
+              <object>
+                <klass>
+                  <global name="NameAssignments" module="Shared.DC.Scripts.Bindings"/>
+                </klass>
+                <tuple/>
+                <state>
+                  <dictionary>
+                    <item>
+                        <key> <string>_asgns</string> </key>
+                        <value>
+                          <dictionary>
+                            <item>
+                                <key> <string>name_container</string> </key>
+                                <value> <string>container</string> </value>
+                            </item>
+                            <item>
+                                <key> <string>name_context</string> </key>
+                                <value> <string>context</string> </value>
+                            </item>
+                            <item>
+                                <key> <string>name_m_self</string> </key>
+                                <value> <string>script</string> </value>
+                            </item>
+                            <item>
+                                <key> <string>name_subpath</string> </key>
+                                <value> <string>traverse_subpath</string> </value>
+                            </item>
+                          </dictionary>
+                        </value>
+                    </item>
+                  </dictionary>
+                </state>
+              </object>
+            </value>
+        </item>
+        <item>
+            <key> <string>_body</string> </key>
+            <value> <string encoding="cdata"><![CDATA[
+
+"""\n
+  Use to check if current user is allowed to perform a file upload.\n
+"""\n
+if context.portal_membership.isAnonymousUser() and len(editor.read()) > 1048576 :\n
+  return 0\n
+return 1\n
+
+
+]]></string> </value>
+        </item>
+        <item>
+            <key> <string>_params</string> </key>
+            <value> <string>editor, request</string> </value>
+        </item>
+        <item>
+            <key> <string>id</string> </key>
+            <value> <string>WebSection_validateFileUpload</string> </value>
+        </item>
+      </dictionary>
+    </pickle>
+  </record>
+</ZopeData>
diff --git a/bt5/erp5_discussion/SkinTemplateItem/portal_skins/erp5_discussion/WebSection_viewCreateNewDiscussionThreadDialog/your_file.xml b/bt5/erp5_discussion/SkinTemplateItem/portal_skins/erp5_discussion/WebSection_viewCreateNewDiscussionThreadDialog/your_file.xml
index a9def637b7..2ac7c8ec94 100644
--- a/bt5/erp5_discussion/SkinTemplateItem/portal_skins/erp5_discussion/WebSection_viewCreateNewDiscussionThreadDialog/your_file.xml
+++ b/bt5/erp5_discussion/SkinTemplateItem/portal_skins/erp5_discussion/WebSection_viewCreateNewDiscussionThreadDialog/your_file.xml
@@ -10,10 +10,19 @@
             <key> <string>delegated_list</string> </key>
             <value>
               <list>
+                <string>external_validator</string>
                 <string>title</string>
               </list>
             </value>
         </item>
+        <item>
+            <key> <string>delegated_message_list</string> </key>
+            <value>
+              <list>
+                <string>external_validator_failed</string>
+              </list>
+            </value>
+        </item>
         <item>
             <key> <string>id</string> </key>
             <value> <string>your_file</string> </value>
@@ -24,7 +33,7 @@
               <dictionary>
                 <item>
                     <key> <string>external_validator_failed</string> </key>
-                    <value> <string>The input failed the external validator.</string> </value>
+                    <value> <string>File is too big. Please consider to use a smaller file or request an account.</string> </value>
                 </item>
               </dictionary>
             </value>
@@ -71,6 +80,12 @@
             <key> <string>values</string> </key>
             <value>
               <dictionary>
+                <item>
+                    <key> <string>external_validator</string> </key>
+                    <value>
+                      <persistent> <string encoding="base64">AAAAAAAAAAI=</string> </persistent>
+                    </value>
+                </item>
                 <item>
                     <key> <string>field_id</string> </key>
                     <value> <string>my_file_field</string> </value>
@@ -93,4 +108,17 @@
       </dictionary>
     </pickle>
   </record>
+  <record id="2" aka="AAAAAAAAAAI=">
+    <pickle>
+      <global name="Method" module="Products.Formulator.MethodField"/>
+    </pickle>
+    <pickle>
+      <dictionary>
+        <item>
+            <key> <string>method_name</string> </key>
+            <value> <string>WebSection_validateFileUpload</string> </value>
+        </item>
+      </dictionary>
+    </pickle>
+  </record>
 </ZopeData>
diff --git a/bt5/erp5_discussion/bt/revision b/bt5/erp5_discussion/bt/revision
index d55f9f7457..0973804c41 100644
--- a/bt5/erp5_discussion/bt/revision
+++ b/bt5/erp5_discussion/bt/revision
@@ -1 +1 @@
-136
\ No newline at end of file
+137
\ No newline at end of file
-- 
2.30.9