source: WAeUP_SRP/base/Upload.py @ 3305

Last change on this file since 3305 was 3292, checked in by joachim, 17 years ago

implement #500

  • Property svn:keywords set to Id
File size: 4.5 KB
Line 
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
16import logging
17import csv,re,os
18import Globals
19import DateTime
20import re
21p_home = Globals.package_home(globals())
22i_home = Globals.INSTANCE_HOME
23
24storage_path = "%s/%s" % (i_home,'import')
25
26class UploadsFolder(CPSDocument): ###(
27    """
28    WAeUP UploadsFolder containing Uploads
29    """
30    meta_type = 'UploadsFolder'
31    portal_type = meta_type
32    security = ClassSecurityInfo()
33
34    security.declareProtected(View,"Title")
35    def Title(self):
36        """compose title"""
37        return "Upload Section"
38
39
40InitializeClass(UploadsFolder)
41
42def addUploadsFolder(container, id, REQUEST=None, **kw):
43    """Add a UploadFolder."""
44    ob = UploadsFolder(id, **kw)
45    return CPSBase_adder(container, ob, REQUEST=REQUEST)
46###)
47
48class Upload(CPSDocument): ###(
49    """
50    WAeUP Upload
51    """
52    meta_type = 'Upload'
53    portal_type = meta_type
54    security = ClassSecurityInfo()
55
56    security.declareProtected(View,"Title") ###(
57    def Title(self):
58        """compose title"""
59        return self.title
60    ###)
61
62    security.declareProtected(View,"checkKeys") ###(
63    def checkKeys(self):
64        """check fields in csv-headline"""
65        doc = self.getContent()
66        csv_path = os.path.join(storage_path,doc.filename)
67        importer_name = ''.join([part.capitalize() for part in self.getContent().import_layout.split('_')])
68        importer = eval("%sImport" % importer_name)(self)
69        msg = ''
70        invalid_keys = []
71        if not os.path.exists(csv_path):
72            base,ext = os.path.splitext(csv_path)
73            if os.path.exists("%(base)s.done" % vars()):
74            #import pdb;pdb.set_trace()
75                #msg = "imported"
76                pass
77            else:
78                base_path = os.path.split(csv_path)[1]
79                msg = 'No such file %(base_path)s' % vars()
80        else:
81            headline = csv.reader(open(csv_path,"rb")).next()
82            if "import_mode" not in headline:
83                msg += 'import_mode must be in heading'
84            invalid_keys = importer.checkHeadline(headline)
85            if invalid_keys:
86                msg += "invalid keys in heading"
87        return msg,invalid_keys
88
89    security.declareProtected(View,"getFields") ###(
90    def getKeys(self):
91        """return the valid keys for headline"""
92        importer_name = ''.join([part.capitalize() for part in self.getContent().import_layout.split('_')])
93        importer = eval("%sImport" % importer_name)(self)
94        keys = importer.schema.keys()
95        keys.sort()
96        return keys
97    ###)
98   
99    security.declareProtected(View,"getUploadFileInfo") ###(
100    def getUploadFileInfo(self):
101        """return the valid keys for headline"""
102        doc = self.getContent()
103        return os.stat(os.path.join(storage_path,doc.filename))
104       
105    security.declareProtected(ModifyPortalContent,"editHeadline") ###(
106    def editHeadline(self,key_pairs):
107        """edit headline"""
108        importer_name = ''.join([part.capitalize() for part in self.getContent().import_layout.split('_')])
109        importer = eval("%sImport" % importer_name)(self)
110        csv_path = os.path.join(storage_path,self.filename)
111        reader = csv.reader(open(csv_path,"rb"))
112        headline = reader.next()
113        records = [record for record in reader]
114        #import pdb;pdb.set_trace()
115        for old,new in key_pairs:
116            pos = headline.index(old)
117            del headline[pos]
118            headline.insert(pos,new)
119        writer = csv.writer(open(csv_path,"w"))
120        writer.writerow(headline)
121        writer.writerows(records)
122       
123       
124InitializeClass(Upload)
125
126
127def addUpload(container, id, REQUEST=None, **kw):
128    """Add a Upload."""
129    ob = Upload(id, **kw)
130    return CPSBase_adder(container, ob, REQUEST=REQUEST)
131###)
Note: See TracBrowser for help on using the repository browser.