source: WAeUP_SRP/base/Upload.py @ 3277

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

implemented new Upload functionallity

  • Property svn:keywords set to Id
File size: 4.0 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
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        csv_path = os.path.join(storage_path,self.filename)
66        importer_name = ''.join([part.capitalize() for part in self.getContent().import_layout.split('_')])
67        importer = eval("%sImport" % importer_name)(self)
68        msg = ''
69        invalid_keys = []
70        if not os.path.exists(csv_path):
71            msg = 'No such file %(csv_path)s' % vars()
72        else:
73            headline = csv.reader(open(csv_path,"rb")).next()
74            if "import_mode" not in headline:
75                msg += 'import_mode must be in heading'
76            invalid_keys = importer.checkHeadline(headline)
77            if invalid_keys:
78                msg += "invalid keys in heading"
79        return msg,invalid_keys
80
81    security.declareProtected(View,"checkFields") ###(
82    def getKeys(self):
83        """return the valid keys for headline"""
84        importer_name = ''.join([part.capitalize() for part in self.getContent().import_layout.split('_')])
85        importer = eval("%sImport" % importer_name)(self)
86        keys = importer.schema.keys()
87        keys.sort()
88        return keys
89   
90    security.declareProtected(ModifyPortalContent,"editHeadline") ###(
91    def editHeadline(self,key_pairs):
92        """edit headline"""
93        importer_name = ''.join([part.capitalize() for part in self.getContent().import_layout.split('_')])
94        importer = eval("%sImport" % importer_name)(self)
95        csv_path = os.path.join(storage_path,self.filename)
96        reader = csv.reader(open(csv_path,"rb"))
97        headline = reader.next()
98        records = [record for record in reader]
99        #import pdb;pdb.set_trace()
100        for old,new in key_pairs:
101            pos = headline.index(old)
102            del headline[pos]
103            headline.insert(pos,new)
104        writer = csv.writer(open(csv_path,"w"))
105        writer.writerow(headline)
106        writer.writerows(records)
107       
108       
109InitializeClass(Upload)
110
111
112def addUpload(container, id, REQUEST=None, **kw):
113    """Add a Upload."""
114    ob = Upload(id, **kw)
115    return CPSBase_adder(container, ob, REQUEST=REQUEST)
116###)
Note: See TracBrowser for help on using the repository browser.