Changeset 3277 for WAeUP_SRP/base


Ignore:
Timestamp:
7 Mar 2008, 08:12:02 (17 years ago)
Author:
joachim
Message:

implemented new Upload functionallity

Location:
WAeUP_SRP/base
Files:
5 added
5 edited

Legend:

Unmodified
Added
Removed
  • WAeUP_SRP/base/WAeUPImport.py

    r3268 r3277  
    2626#from Globals import DTMLFile
    2727#from Globals import InitializeClass
    28 #from OFS.SimpleItem import SimpleItem
     28from OFS.SimpleItem import SimpleItem
    2929from zExceptions import BadRequest
    3030
     
    3333#from Products.CPSSchemas.DataModel import DataModel
    3434#from Products.CPSSchemas.StorageAdapter import MappingStorageAdapter
    35 #from Products.CMFCore.ActionProviderBase import ActionProviderBase
     35from Products.CMFCore.ActionProviderBase import ActionProviderBase
    3636#from Products.CMFCore.permissions import View
    3737#from Products.ZCatalog.ZCatalog import ZCatalog
    3838#from Products.CMFCore.permissions import ModifyPortalContent
    3939#from Products.CMFCore.permissions import ManagePortal
    40 #from Products.CMFCore.utils import UniqueObject
     40from Products.CMFCore.utils import UniqueObject
    4141#from Products.CMFCore.URLTool import URLTool
    4242from Products.CMFCore.utils import getToolByName
     
    5151from utils import makeDigest
    5252
    53 class WAeUPImport: ###(
     53class WAeUPImport(UniqueObject, SimpleItem, ActionProviderBase): ###(
    5454    """ WAeUPImport """
    5555    required_modes = ('create',)
    5656
    57     def __init__(self,waeup_tool):
     57    def __init__(self,waeup_tool): ###(
     58        self.students_folder = waeup_tool.portal_url.getPortalObject().campus.students
    5859        self.member = member = waeup_tool.portal_membership.getAuthenticatedMember()
    5960        self.import_date = DateTime.DateTime().strftime("%d/%m/%y %H:%M:%S")
    6061        self.current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S")
    6162        self.waeup_tool = waeup_tool
    62         self.students_folder = waeup_tool.portal_url.getPortalObject().campus.students
    6363        self.academics_folder = waeup_tool.portal_url.getPortalObject().campus.academics
    6464        self.schema_tool = getToolByName(waeup_tool, 'portal_schemas')
     
    115115            break
    116116        self.init_errors = ','.join(errors)
    117 
    118     def makeIdLists(self):
     117    ###)
     118
     119    def makeIdLists(self): ###(
    119120        pending_digests = []
    120121        pending = []
     
    134135            #copy2(self.pending_path,self.pending_backup)
    135136        return pending, pending_digests
     137    ###)
     138
     139    def checkHeadline(self,headline):
     140        """ check the headline of an csv.file """
     141        import_keys = [k.strip() for k in headline if not (k.strip().startswith('ignore')
     142                                                        or k.strip() in self.info.keys())]
     143        diff2schema = set(import_keys).difference(set(self.schema.keys()))
     144        diff2layout = set(import_keys).difference(set(self.layout.keys()))
     145        #if diff2schema and diff2schema != set(['id',]):
     146        if diff2schema:
     147            return list(diff2schema)
     148        return []
    136149###)
     150
    137151
    138152class ApplicationImport(WAeUPImport):###(
  • WAeUP_SRP/base/WAeUPTool.py

    r3272 r3277  
    14671467        if not pending_only:
    14681468            info['imported_from'] = import_source_fn
    1469         attrs = csv.reader(open(import_source_path,"rb")).next()
    1470         if "import_mode" not in attrs:
     1469        headline = csv.reader(open(import_source_path,"rb")).next()
     1470        if "import_mode" not in headline:
    14711471            msg = 'import_mode must be in heading'
    14721472            return msg
    1473 
    1474         import_keys = [k.strip() for k in attrs if not (k.strip().startswith('ignore')
    1475                                                         or k.strip() in info.keys())]
    1476         diff2schema = set(import_keys).difference(set(importer.schema.keys()))
    1477         diff2layout = set(import_keys).difference(set(importer.layout.keys()))
    1478         if diff2schema and diff2schema != set(['id',]):
    1479             msg = 'not ignorable key(s): "%s" found in heading' % ", ".join(diff2schema)
    1480             return msg
     1473        invalid_keys = importer.checkHeadline(headline)
     1474        if invalid_keys:
     1475            return 'not ignorable key(s): "%s" found in heading' % ", ".join(invalid_keys)
     1476
     1477        # import_keys = [k.strip() for k in headline if not (k.strip().startswith('ignore')
     1478        #                                                 or k.strip() in info.keys())]
     1479        # diff2schema = set(import_keys).difference(set(importer.schema.keys()))
     1480        # diff2layout = set(import_keys).difference(set(importer.layout.keys()))
     1481        # if diff2schema and diff2schema != set(['id',]):
     1482        #     msg = 'not ignorable key(s): "%s" found in heading' % ", ".join(diff2schema)
     1483        #     return msg
    14811484        #
    14821485        # start importing
     
    14951498        count = 0
    14961499        imported = []
    1497         old_commit_count = 0
     1500        old_commit_count = -1
    14981501        error_count = imported_count = 0
    14991502        already_in = 0
     
    15811584                logger.info(' '.join(log_list))
    15821585            finished = count > total_to_import - 1
    1583             must_commit = False
    1584             if len(imported) != old_commit_count:
    1585                 if not len(imported) % commit_after:
    1586                     must_commit = True
    1587                     old_commit_count = len(imported)
     1586            must_commit = (imported_count != old_commit_count) and (not imported_count % commit_after)
     1587            if must_commit:
     1588                old_commit_count = imported_count
    15881589
    15891590            if must_commit or finished:
  • WAeUP_SRP/base/Widgets.py

    r3265 r3277  
    372372            v = ''
    373373        sessions = self._getSessions()
     374        #import pdb;pdb.set_trace()
    374375        if not sessions.has_key(v):
    375376            datastructure.setError(widget_id, "'%s' not a valid session key" % v)
     
    437438            return 0
    438439
    439         if not value:
    440             v = value = '100'
     440        # if not value:
     441        #     v = value = '100'
    441442        #import pdb;pdb.set_trace()
    442443        levels = self._getLevels()
     
    19201921        """Get the file info from the datastructure."""
    19211922        widget_id = self.getWidgetId()
     1923        info = None
    19221924        if  datastructure.has_key(widget_id):
    19231925            fileupload = datastructure[widget_id]
     
    21482150###)
    21492151
     2152class DiscFileWidget(CPSFileWidget): ###(
     2153    """File widget with filesystem storage."""
     2154    meta_type = 'Disc File Widget'
     2155    _properties = CPSFileWidget._properties +\
     2156    (
     2157     {'id': 'path', 'type': 'string', 'mode': 'w',
     2158      'label': 'Relative Path'},
     2159    )
     2160    path = "import"
     2161    storage_path = "%s/%s" % (i_home,path)
     2162
     2163    def getStoredFileInfo(self,datastructure): ###(
     2164        datamodel = datastructure.getDataModel()
     2165        field_id = self.fields[0]
     2166        widget_id = self.getWidgetId()
     2167        file_name = datamodel[field_id]
     2168        info = {}
     2169        registry = getToolByName(self, 'mimetypes_registry')
     2170        info['mimetype'] = registry.lookupExtension('file.csv')
     2171        info['empty_file'] = True
     2172        info['size'] = 0
     2173        info['last_modified'] = ''
     2174        file_path = os.path.join(self.storage_path, file_name)
     2175        if os.path.exists(file_path):
     2176            info['empty_file'] = False
     2177            info['file_name'] = file_name
     2178            info['content_url'] = os.path.join(self.portal_url(),
     2179                                            "viewfile",
     2180                                            file_name,
     2181                                            )
     2182            info['current_filename'] =  file_name
     2183            # get the mimetype
     2184            mimetype = (registry.lookupExtension(file_name.lower()) or
     2185                        registry.lookupExtension('file.bin'))
     2186            info['mimetype'] = mimetype
     2187            info['file_path'] = file_path
     2188        else:
     2189            info['file_name'] = ''
     2190            info['content_url'] = ''
     2191            info['current_filename'] = ''
     2192            info['file_path'] = ''
     2193        return info
     2194        ###)
     2195   
     2196    def prepare(self, datastructure, **kw): ###(
     2197        """Prepare datastructure from datamodel."""
     2198        datamodel = datastructure.getDataModel()
     2199        #import pdb;pdb.set_trace()
     2200        widget_id = self.getWidgetId()
     2201        file_name = datamodel[self.fields[0]]
     2202        datastructure[widget_id] = file_name
     2203        datastructure[widget_id + '_filename'] = file_name
     2204        datastructure[widget_id+'_choice'] = "keep"
     2205    ###)
     2206
     2207    def validate(self, datastructure, **kw): ###(
     2208        """Update datamodel from user data in datastructure.
     2209        """
     2210        logger = logging.getLogger('Widgets.DiscFileWidget.validate')
     2211        datamodel = datastructure.getDataModel()
     2212        field_id = self.fields[0]
     2213        widget_id = self.getWidgetId()
     2214        store = False
     2215        fileupload = None
     2216        mimetype = None
     2217        old_file = datamodel[field_id]
     2218        choice = datastructure[widget_id+'_choice']
     2219        fileupload = datastructure[widget_id]
     2220        is_upload = isinstance(fileupload, FileUpload)
     2221        if not is_upload and not datamodel[field_id]:
     2222            if self.is_required:
     2223                return self.validateError('File upload required', {},
     2224                                          datastructure)
     2225        if choice == 'delete':
     2226            if self.is_required:
     2227                return self.validateError('cpsschemas_err_required', {},
     2228                                          datastructure)
     2229            info = self.getStoredFileInfo(datastructure)
     2230            # Remove the file in the filesystem
     2231            if info:
     2232                os.remove(info['file_path'])
     2233            datamodel[field_id] = None
     2234        elif choice == 'keep':
     2235            fileupload = datastructure[widget_id]
     2236            if isinstance(fileupload, PersistableFileUpload):
     2237                # Keeping something from the session means we
     2238                # actually want to store it.
     2239                store = True
     2240            # else:
     2241            #     # Nothing to change, don't pollute datastructure
     2242            #     # with something costly already stored, which therefore
     2243            #     # doesn't need to be kept in the session.
     2244            #     self.unprepare(datastructure)
     2245        elif choice == 'change' and is_upload:
     2246            if not fileupload:
     2247                return self.validateError('cpsschemas_err_file_empty', {},
     2248                                          datastructure)
     2249            if not isinstance(fileupload, FileUpload):
     2250                return self.validateError('cpsschemas_err_file', {},
     2251                                          datastructure)
     2252            fileupload.seek(0, 2) # end of file
     2253            size = fileupload.tell()
     2254            if not size:
     2255                return self.validateError('cpsschemas_err_file_empty', {},
     2256                                          datastructure)
     2257            if self.size_max and size > self.size_max:
     2258                max_size_str = self.getHumanReadableSize(self.size_max)
     2259                err = 'This file is too big, the allowed max size is ${max_size}'
     2260                member_id = str(self.portal_membership.getAuthenticatedMember())
     2261                logger.info('%s tried to upload picture with size %dk' %(member_id,int(size)/1000) )
     2262                err_mapping = {'max_size': max_size_str}
     2263                return self.validateError(err, err_mapping, datastructure)
     2264            store = True
     2265
     2266
     2267        # Find filename
     2268        if is_upload and store:
     2269            filename = cookId('', '', fileupload)[0].strip()
     2270            base,ext = os.path.splitext(filename)
     2271            if ext not in ("csv",".csv"):
     2272                return self.validateError('only csv files allowed', {},
     2273                                          datastructure)
     2274            datamodel[field_id] = filename
     2275            registry = getToolByName(self, 'mimetypes_registry')
     2276            mimetype = registry.lookupExtension(filename.lower())
     2277            if mimetype is not None:
     2278                mimetype = str(mimetype) # normalize
     2279            file = self.makeFile(filename, fileupload, datastructure)
     2280            # Fixup mimetype
     2281            if mimetype and file.content_type != mimetype:
     2282                file.content_type = mimetype
     2283            file_path = os.path.join(self.storage_path,filename)
     2284            #import pdb; pdb.set_trace()
     2285            data = open(file_path,"w")
     2286            data.write(str(file.data))
     2287            data.close()
     2288        return True
     2289    ###)
     2290
     2291    def render(self, mode, datastructure, **kw): ###(
     2292        render_method = 'widget_uploadfile_render'
     2293        meth = getattr(self, render_method, None)
     2294        #import pdb;pdb.set_trace()
     2295        if meth is None:
     2296            raise RuntimeError("Unknown Render Method %s for widget type %s"
     2297                               % (render_method, self.getId()))
     2298        file_info = self.getStoredFileInfo(datastructure)
     2299
     2300        return meth(mode=mode, datastructure=datastructure, **file_info)
     2301    ###)
     2302
     2303InitializeClass(DiscFileWidget)
     2304
     2305widgetRegistry.register(DiscFileWidget)
     2306###)
     2307
    21502308###########
    21512309
  • WAeUP_SRP/base/__init__.py

    r2914 r3277  
    8888        )
    8989     ),
     90     ("Upload",
     91        ("UploadsFolder",
     92         "Upload",
     93         )
     94     ),
    9095    )
    9196
  • WAeUP_SRP/base/exportimport.py

    r3006 r3277  
    7979        students = getattr(campus,'students',None)
    8080        accommodation = getattr(campus,'accommodation',None)
     81        upload = getattr(campus,'upload',None)
    8182        campus.manage_setLocalGroupRoles(groupid='CampusOfficers',
    8283                                         roles=('SectionOfficer',))
     
    103104                                         roles=('SectionReader',))
    104105            accommodation.manage_setLocalGroupRoles(groupid='AccommodationOfficers',
     106                                         roles=('SectionOfficer',))
     107        if upload is not None:
     108            upload.manage_setLocalGroupRoles(groupid='CampusOfficers',
    105109                                         roles=('SectionOfficer',))
    106110    members = getattr(site.portal_directories,'members')
Note: See TracChangeset for help on using the changeset viewer.