ShaDir.py 4.55 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
##############################################################################
#
# Copyright (c) 20011 Nexedi SA and Contributors. All Rights Reserved.
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsability of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# garantees and support are strongly adviced to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
##############################################################################


import hashlib
import json
import validictory
from Products.ERP5Type.Document import newTempFile


def WebSection_getDocumentValue(self, key, portal=None, language=None,\
                                            validation_state=None, **kw):
  """
    API SHADIR

     - POST /<key>
        + parameters required:
           * file: the name of the file
           * urlmd5: mdsum of orginal url
           * sha512: the hash (sha512) of the file content

        + parameters not required:
           * valid-until: the date which the file must be expired
           * architecture: computer architecture

       Used to add information on shadir server.


     - GET /<key>
       Return list of information for a given key
       Raise HTTP error (404) if key does not exist
  """
57 58 59
  if self.REQUEST.get('REQUEST_METHOD') in ('PUT',):
    return

60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
  if portal is None:
    portal = self.getPortalObject()

  data_set = portal.portal_catalog.getResultValue(portal_type='Data Set',
                                                  reference=key)

  # Return the SIGNATURE file, if the document exists.
  if data_set is not None:
    document_list = [json.loads(document.getData()) \
                       for document in data_set.getFollowUpRelatedValueList()]

    temp_file = newTempFile(self, '%s.txt' % key)
    temp_file.setData(json.dumps(document_list))
    temp_file.setContentType('application/json')
    return temp_file.getObject()

  return None

def WebSection_setObject(self, id, ob, **kw):
  """
    Make any change related to the file uploaded.
  """
  portal = self.getPortalObject()
  data = self.REQUEST.get('BODY')
  schema = self.WebSite_getJSONSchema()
  structure = json.loads(data)
  validictory.validate(structure, schema)

88 89
  file_name = structure[0].get('file', None)
  expiration_date = structure[0].get('expiration_date', None)
90 91 92 93 94 95 96

  data_set = portal.portal_catalog.getResultValue(portal_type='Data Set',
                                                  reference=id)
  if data_set is None:
    data_set = portal.data_set_module.newContent(portal_type='Data Set',
                                                 reference=id)
    data_set.publish()
97

98 99

  reference = hashlib.sha512(data).hexdigest()
100 101 102
  ob.setFilename(file_name)
  ob.setFollowUp(data_set.getRelativeUrl())
  ob.setContentType('application/json')
103
  ob.setReference(reference)
104 105
  if expiration_date is not None:
    ob.setExpirationDate(expiration_date)
106
  return ob
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131

def WebSection_putFactory(self, name, typ, body):
  """
   API SHACACHE
     - PUT /<key>
        + parameters required:
          * data:  it is the file content
       The key is the file name.
  """
  portal = self.getPortalObject()
  if name is None:
    name = 'shacache'
  document = portal.portal_contributions.newContent(data=body,
                                                    filename=name,
                                                    discover_metadata=False)

  # We can only change the state of the object after all the activities and
  # interaction workflow, to avoid any security problem.
  document.activate(after_path_and_method_id=(document.getPath(), \
            ('convertToBaseFormat', 'Document_tryToConvertToBaseFormat', \
             'immediateReindexObject', 'recursiveImmediateReindexObject')))\
            .publish()

  return document