[3526] | 1 | import grok |
---|
[3529] | 2 | import os |
---|
[4083] | 3 | from zope.component.interfaces import IFactory |
---|
| 4 | from zope.interface import implementedBy |
---|
[3827] | 5 | from waeup.interfaces import IStudentContainer |
---|
[3529] | 6 | from waeup.utils import importexport as csv |
---|
[3526] | 7 | |
---|
[3951] | 8 | class StudentContainer(grok.Container): |
---|
[3526] | 9 | """ |
---|
| 10 | The node containing the student models |
---|
| 11 | """ |
---|
[3827] | 12 | |
---|
| 13 | grok.implements(IStudentContainer) |
---|
| 14 | |
---|
[4083] | 15 | class StudentContainerFactory(grok.GlobalUtility): |
---|
| 16 | """A factory for faculty containers. |
---|
| 17 | """ |
---|
| 18 | grok.implements(IFactory) |
---|
| 19 | grok.name(u'waeup.StudentContainer') |
---|
| 20 | title = u"Create a new student container.", |
---|
| 21 | description = u"This factory instantiates new student containers." |
---|
| 22 | |
---|
| 23 | def __call__(self): |
---|
| 24 | return StudentContainer() |
---|
| 25 | |
---|
| 26 | def getInterfaces(self): |
---|
| 27 | return implementedBy(StudentContainer) |
---|
[3827] | 28 | |
---|
[3527] | 29 | class Import(grok.View): |
---|
| 30 | def render(self): |
---|
[3547] | 31 | #XXX TODO: Make import form to choose csv-file |
---|
[3527] | 32 | t = [0] |
---|
[3529] | 33 | src = os.path.join(os.path.dirname(__file__), '..', 'tests', |
---|
[3551] | 34 | 'TestData','Students.csv') |
---|
[3527] | 35 | inputfile = file(src, "r") |
---|
| 36 | num = 0 |
---|
| 37 | headers, rows = csv.readFile(inputfile) |
---|
| 38 | for i in rows: |
---|
[3529] | 39 | s = fromCsv(i) |
---|
[3527] | 40 | self.context[str(num)] = s |
---|
| 41 | num += 1 |
---|
[3529] | 42 | if num > 100: |
---|
| 43 | break |
---|
[3527] | 44 | return "%s new Students added in %s" % (num, t[0]) |
---|
| 45 | |
---|
| 46 | class Export(grok.View): |
---|
| 47 | def render(self): |
---|
[3547] | 48 | #XXX TODO: Make export form to choose csv-file |
---|
[3527] | 49 | t = [0] |
---|
| 50 | result = [] |
---|
| 51 | for s in self.context.values(): |
---|
| 52 | result.append(s.getCsv()) |
---|
| 53 | csv.writeFile(result) |
---|
| 54 | return "yeah %s" % t[0] |
---|