Example #1
0
    def csv_import_handler(self, request, suffix=''):  # pylint: disable=unused-argument
        """
        Endpoint that handles CSV uploads.
        """
        if not self.runtime.user_is_staff:
            return Response('not allowed', status_code=403)

        _ = self.runtime.service(self, "i18n").ugettext

        try:
            score_file = request.POST['csv'].file
        except KeyError:
            data = {'error_rows': [1], 'error_messages': [_('missing file')]}
        else:
            log.info('Processing %d byte score file %s for %s', score_file.size, score_file.name, self.location)
            block_id = self.location
            block_weight = self.weight
            processor = ScoreCSVProcessor(
                block_id=str(block_id),
                max_points=block_weight,
                user_id=self.runtime.user_id)
            processor.process_file(score_file, autocommit=True)
            data = processor.status()
            log.info('Processed file %s for %s -> %s saved, %s processed, %s error. (async=%s)',
                     score_file.name,
                     block_id,
                     data.get('saved', 0),
                     data.get('total', 0),
                     len(data.get('error_rows', [])),
                     data.get('waiting', False))
        return Response(json_body=data)
Example #2
0
 def get_results_handler(self, request, suffix=''):  # pylint: disable=unused-argument
     """
     Endpoint to poll for celery results.
     """
     if not self.runtime.user_is_staff:
         return Response('not allowed', status_code=403)
     try:
         result_id = request.POST['result_id']
     except KeyError:
         data = {'message': 'missing'}
     else:
         results = ScoreCSVProcessor().get_deferred_result(result_id)
         if results.ready():
             data = results.get()
             log.info('Got results from celery %r', data)
         else:
             data = {'waiting': True, 'result_id': result_id}
             log.info('Still waiting for %s', result_id)
     return Response(json_body=data)
    def csv_export_handler(self, request, suffix=''):  # pylint: disable=unused-argument
        """
        Endpoint that handles CSV downloads.
        """
        if not self.runtime.user_is_staff:
            return Response('not allowed', status_code=403)

        track = request.GET.get('track', None)
        cohort = request.GET.get('cohort', None)

        buf = io.StringIO()
        ScoreCSVProcessor(block_id=str(self.location),
                          max_points=self.weight,
                          display_name=self.display_name,
                          track=track,
                          cohort=cohort).write_file(buf)
        resp = Response(buf.getvalue())
        resp.content_type = 'text/csv'
        resp.content_disposition = 'attachment; filename="%s.csv"' % self.location
        return resp