source: WAeUP_SRP/base/Upload.py @ 3318

Last change on this file since 3318 was 3316, checked in by joachim, 17 years ago

allow ignore,
check for duplicates

  • Property svn:keywords set to Id
File size: 4.8 KB
RevLine 
[3277]1#-*- mode: python; mode: fold -*-
2from Globals import InitializeClass
3from AccessControl import ClassSecurityInfo
4
5from Products.CMFCore.utils import UniqueObject, getToolByName
6from Products.CMFCore.permissions import View
7from Products.CMFCore.permissions import ModifyPortalContent
8from Products.CPSCore.CPSBase import CPSBase_adder, CPSBaseFolder
9#from Products.CPSCore.CPSBase import CPSBaseDocument as BaseDocument
10from Products.CPSDocument.CPSDocument import CPSDocument
11from Products.CPSCore.CPSBase import CPSBaseBTreeFolder as BaseBTreeFolder
12from Products.WAeUP_SRP.WAeUPTables import AccommodationTable,NOT_OCCUPIED
13from WAeUPImport import ApplicationImport,CertificateImport,CertificateCourseImport
14from WAeUPImport import CourseImport,CourseResultImport
15from WAeUPImport import DepartmentImport,FacultyImport,StudentImport,VerdictImport
[3316]16from WAeUPImport import NO_KEY,IGNORE
[3314]17 
[3277]18import logging
19import csv,re,os
20import Globals
21import DateTime
22import re
23p_home = Globals.package_home(globals())
24i_home = Globals.INSTANCE_HOME
25storage_path = "%s/%s" % (i_home,'import')
26
27class UploadsFolder(CPSDocument): ###(
28    """
29    WAeUP UploadsFolder containing Uploads
30    """
31    meta_type = 'UploadsFolder'
32    portal_type = meta_type
33    security = ClassSecurityInfo()
34
35    security.declareProtected(View,"Title")
36    def Title(self):
37        """compose title"""
38        return "Upload Section"
39
40
41InitializeClass(UploadsFolder)
42
43def addUploadsFolder(container, id, REQUEST=None, **kw):
44    """Add a UploadFolder."""
45    ob = UploadsFolder(id, **kw)
46    return CPSBase_adder(container, ob, REQUEST=REQUEST)
47###)
48
49class Upload(CPSDocument): ###(
50    """
51    WAeUP Upload
52    """
53    meta_type = 'Upload'
54    portal_type = meta_type
55    security = ClassSecurityInfo()
[3314]56    NO_KEY = NO_KEY
[3316]57    IGNORE = IGNORE
[3277]58    security.declareProtected(View,"Title") ###(
59    def Title(self):
60        """compose title"""
61        return self.title
62    ###)
63
64    security.declareProtected(View,"checkKeys") ###(
65    def checkKeys(self):
66        """check fields in csv-headline"""
[3292]67        doc = self.getContent()
68        csv_path = os.path.join(storage_path,doc.filename)
[3277]69        importer_name = ''.join([part.capitalize() for part in self.getContent().import_layout.split('_')])
70        importer = eval("%sImport" % importer_name)(self)
71        msg = ''
72        invalid_keys = []
[3314]73        keys = []
[3277]74        if not os.path.exists(csv_path):
[3292]75            base,ext = os.path.splitext(csv_path)
76            if os.path.exists("%(base)s.done" % vars()):
77            #import pdb;pdb.set_trace()
78                #msg = "imported"
79                pass
80            else:
81                base_path = os.path.split(csv_path)[1]
82                msg = 'No such file %(base_path)s' % vars()
[3277]83        else:
[3314]84            reader = csv.reader(open(csv_path,"rb"))
85            headline = reader.next()
86            values = reader.next()
[3277]87            if "import_mode" not in headline:
88                msg += 'import_mode must be in heading'
89            invalid_keys = importer.checkHeadline(headline)
90            if invalid_keys:
91                msg += "invalid keys in heading"
[3316]92            err,keys = importer.getHeadlineFields(headline,values)
93            if err:
94                msg += err
[3314]95        return msg,keys
96    ###)
97   
[3292]98    security.declareProtected(View,"getFields") ###(
[3277]99    def getKeys(self):
100        """return the valid keys for headline"""
101        importer_name = ''.join([part.capitalize() for part in self.getContent().import_layout.split('_')])
102        importer = eval("%sImport" % importer_name)(self)
103        keys = importer.schema.keys()
104        keys.sort()
105        return keys
[3292]106    ###)
[3277]107   
[3292]108    security.declareProtected(View,"getUploadFileInfo") ###(
109    def getUploadFileInfo(self):
110        """return the valid keys for headline"""
111        doc = self.getContent()
112        return os.stat(os.path.join(storage_path,doc.filename))
[3314]113    ###)
114   
[3277]115    security.declareProtected(ModifyPortalContent,"editHeadline") ###(
116    def editHeadline(self,key_pairs):
117        """edit headline"""
118        importer_name = ''.join([part.capitalize() for part in self.getContent().import_layout.split('_')])
119        importer = eval("%sImport" % importer_name)(self)
120        csv_path = os.path.join(storage_path,self.filename)
121        reader = csv.reader(open(csv_path,"rb"))
122        headline = reader.next()
123        records = [record for record in reader]
124        #import pdb;pdb.set_trace()
[3316]125        for old,new,pos in key_pairs:
[3277]126            del headline[pos]
127            headline.insert(pos,new)
128        writer = csv.writer(open(csv_path,"w"))
129        writer.writerow(headline)
130        writer.writerows(records)
131       
132       
133InitializeClass(Upload)
134
135
136def addUpload(container, id, REQUEST=None, **kw):
137    """Add a Upload."""
138    ob = Upload(id, **kw)
139    return CPSBase_adder(container, ob, REQUEST=REQUEST)
140###)
Note: See TracBrowser for help on using the repository browser.