1 | import grok |
---|
2 | import os |
---|
3 | from zope.component.interfaces import IFactory |
---|
4 | from zope.interface import implementedBy |
---|
5 | from waeup.interfaces import IStudentContainer |
---|
6 | from waeup.utils import importexport as csv |
---|
7 | |
---|
8 | class StudentContainer(grok.Container): |
---|
9 | """ |
---|
10 | The node containing the student models |
---|
11 | """ |
---|
12 | |
---|
13 | grok.implements(IStudentContainer) |
---|
14 | |
---|
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) |
---|
28 | |
---|
29 | class Import(grok.View): |
---|
30 | def render(self): |
---|
31 | #XXX TODO: Make import form to choose csv-file |
---|
32 | t = [0] |
---|
33 | src = os.path.join(os.path.dirname(__file__), '..', 'tests', |
---|
34 | 'TestData','Students.csv') |
---|
35 | inputfile = file(src, "r") |
---|
36 | num = 0 |
---|
37 | headers, rows = csv.readFile(inputfile) |
---|
38 | for i in rows: |
---|
39 | s = fromCsv(i) |
---|
40 | self.context[str(num)] = s |
---|
41 | num += 1 |
---|
42 | if num > 100: |
---|
43 | break |
---|
44 | return "%s new Students added in %s" % (num, t[0]) |
---|
45 | |
---|
46 | class Export(grok.View): |
---|
47 | def render(self): |
---|
48 | #XXX TODO: Make export form to choose csv-file |
---|
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] |
---|