Ignore:
Timestamp:
13 Jun 2016, 01:19:01 (8 years ago)
Author:
uli
Message:

Implement csv extractor.

This simple reader is not very robust.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.kofa/branches/uli-scores-upload/src/waeup/kofa/students/browser.py

    r13923 r13926  
    1818"""UI components for students and related components.
    1919"""
     20import csv
    2021import sys
    2122import grok
    2223import pytz
     24from cStringIO import StringIO
    2325from urllib import urlencode
    2426from datetime import datetime
     
    31043106        return list(coursetickets)
    31053107
     3108    def _extract_uploadfile(self, uploadfile):
     3109        """Get a mapping of student-ids to scores.
     3110
     3111        The mapping is constructed by reading contents from `uploadfile`.
     3112        """
     3113        data = StringIO(uploadfile.read())  # ensure we have something seekable
     3114        reader = csv.DictReader(data)
     3115        result = dict()
     3116        for row in reader:
     3117            if not 'student_id' in row or not 'score' in row:
     3118                continue
     3119            result[row['student_id']] = row['score']
     3120        return result
     3121
    31063122    def update(self,  *args, **kw):
    31073123        form = self.request.form
     
    31293145                return
    31303146            formvals = dict(zip(form['sids'], form['scores']))
     3147            if form['uploadfile']:
     3148                formvals = self._extract_uploadfile(form['uploadfile'])
    31313149            for ticket in editable_tickets:
    31323150                score = ticket.score
    31333151                sid = ticket.student.student_id
     3152                if sid not in formvals:
     3153                    continue
    31343154                if formvals[sid] == '':
    31353155                    score = None
Note: See TracChangeset for help on using the changeset viewer.