def import_school(request,file_id): f = FileBlob.get_by_id(int(file_id)) if f is None: raise Http404 d = cStringIO.StringIO(f.data) r = UnicodeReader(d,encoding='utf8', delimiter=',', quotechar='"') form = None course = None season = None cskey = request.session.get('course_season_key',None) if not cskey is None: season = Season.get(str(cskey)) folder = None cfkey = request.session.get('course_folder_key',None) if not cfkey is None: folder = Folder.get(str(cfkey)) logging.info('cfkey %s cskey %s'%(cskey,cfkey)) logging.info('folder: %s'%folder) logging.info('season: %s'%season) if folder and season: cc = Course.get_COURSE_FILTER_CHOICES(str(season.key()),str(folder.key())) else: cc = Course.get_COURSE_CHOICES() logging.info('cc:%s'%cc) if request.method == 'POST': logging.info('meth post, filling form') form = TargetPickForm2(request.POST,courses = cc) if form.is_valid(): course = Course.get(form.cleaned_data['course_key']) if course is None: raise Http404 else: logging.info('meth get, blank form') form = TargetPickForm2(courses = cc) selected = None info = [] return render_to_response('admin/import_school.html', RequestContext(request, { 'info': info, 'form':form, 'course':course, 'season':season, 'folder':folder}))
def import_students(request,file_id, seq_id=None): f = FileBlob.get_by_id(int(file_id)) if f is None: raise Http404 d = cStringIO.StringIO(f.data) r = UnicodeReader(d) form = None course = None season = None cskey = request.session.get('course_season_key',None) if not cskey is None: season = Season.get(str(cskey)) folder = None cfkey = request.session.get('course_folder_key',None) if not cfkey is None: folder = Folder.get(str(cfkey)) logging.info('cfkey %s cskey %s'%(cskey,cfkey)) logging.info('folder: %s'%folder) logging.info('season: %s'%season) if folder and season: cc = Course.get_COURSE_FILTER_CHOICES(str(season.key()),str(folder.key())) else: cc = Course.get_COURSE_CHOICES() logging.info('cc:%s'%cc) if request.method == 'POST': form = TargetPickForm(request.POST,courses = cc) info = [] seq = 0 curr = None line = 0 for row in r: if len(row)>6 and (row[0].startswith('#export kurz') or row[0].startswith('#zaloha kurz')): logging.info(row) if not curr is None: curr["end_line"]=line info.append(curr) curr = dict() curr["seq"]=seq seq+=1 curr["start_line"]=line+1 curr["code"]=row[1] curr["folder"]=row[2] curr["season"]=row[3] curr["students"]=0 curr["info"]=row[6] elif len(row)>19 and not row[0].startswith('#'): curr["students"]+=1 line+=1 if not curr is None: curr["end_line"]=line info.append(curr) else: ### no header detected. curr = dict() curr["seq"]=0 curr["start_line"]=0 curr["end_line"]=line info.append(curr) selected = None if not seq_id is None: for c in info: if c["seq"]==int(seq_id): selected = c break if selected is None: raise Http404 if form is None: form = TargetPickForm(courses = cc, initial={'start_line':selected['start_line'], 'end_line':selected['end_line']}) else: if form.is_valid(): course = Course.get(form.cleaned_data['course_key']) if course is None: raise Http404 return render_to_response('admin/import_students.html', RequestContext(request, { 'info': info, 'form':form, 'selected':selected, 'course':course, 'season':season, 'folder':folder}))