コード例 #1
0
ファイル: trackings.py プロジェクト: adviti/melange
  def _startShipmentSync(self, request, *args, **kwargs):
    """Start syncing shipment data.

    POST Args:
      program_key: the key of the program which task is runnig for.
      sheet_content: sheet content data in JSON format.
      sheet_type: 'usa' or 'intl'
      shipment_info_id: id of the shipment info object that task is running 
                        for.
    """
    params = dicts.merge(request.POST, request.GET)
    redirect = RedirectHelper(None, None)

    if 'program_key' not in params:
      logging.error("missing program_key in params: '%s'" % params)
      return responses.terminateTask()

    if 'sheet_content' not in params:
      logging.error("missing sheet_content in params: '%s'" % params)
      return responses.terminateTask()

    if 'sheet_type' not in params:
      logging.error("missing sheet_type in params: '%s'" % params)
      return responses.terminateTask()

    if 'shipment_info_id' not in params:
      logging.error("missing shipment_info_id in params: '%s'" % params)
      return responses.terminateTask()

    self.setProgram(params['program_key'])
    self.setShipmentInfo(int(params['shipment_info_id']))

    self.__shipment_info.status = 'syncing'
    self.__shipment_info.put()

    sheet_content = StringIO.StringIO(
        simplejson.loads(params['sheet_content']))
    sheet_type = params['sheet_type']

    sheet_rows = [row for row in csv.reader(sheet_content)]

    if sheet_type == 'usa':
      column_indexes = self.findColumnIndexes(
          sheet_rows[0], self.USA_EXPECTED_COLUMNS)

    elif sheet_type == 'intl':
      column_indexes = self.findColumnIndexes(
          sheet_rows[0], self.INTL_EXPECTED_COLUMNS)

    params = {
        'program_key': params['program_key'],
        'shipment_info_id': params['shipment_info_id'],
        'column_indexes': simplejson.dumps(column_indexes),
        'sheet_rows': simplejson.dumps(sheet_rows[1:]),
    }

    taskqueue.add(url=redirect.urlOf('shipment_sync_task_continue'),
                  params=params)
    return responses.terminateTask()
コード例 #2
0
ファイル: trackings.py プロジェクト: adviti/melange
  def _continueShipmentSync(self, request, *args, **kwargs):
    """Continue syncing shipment data.

    POST Args:
      program_key: the key of the program which sync is being done for.
      shipment_info_id: id of the shipment info object that task is running
                        for.
      column_indexes: column indexes for specific columns in JSON format.
      sheet_rows: spreadsheets CSV chunk data in JSON format.
    """
    timekeeper = Timekeeper(20000)
    params = dicts.merge(request.POST, request.GET)
    redirect = RedirectHelper(None, None)

    if 'program_key' not in params:
      logging.error("missing program_key in params: '%s'" % params)
      return responses.terminateTask()

    if 'shipment_info_id' not in params:
      logging.error("missing shipment_info_id in params: '%s'" % params)
      return responses.terminateTask()

    self.setProgram(params['program_key'])
    self.setShipmentInfo(int(params['shipment_info_id']))

    if 'sheet_rows' not in params:
      logging.error("missing sheet_rows data in params: '%s'" % params)
      return responses.terminateTask()

    if 'column_indexes' not in params:
      logging.error("missing column_indexes data in params: '%s'" % params)
      return responses.terminateTask()

    column_indexes = simplejson.loads(params['column_indexes'])
    sheet_rows = simplejson.loads(params['sheet_rows'])

    try:
      for remain, row in timekeeper.iterate(sheet_rows):

        if len(row) < len(column_indexes):
          row.extend((len(column_indexes) - len(row)) * [''])
        data = self.getRowData(row, column_indexes)
        link_id = data['link_id']

        q = GSoCProfile.all().filter('scope', self.__program)
        q.filter('link_id', link_id)
        profile = q.get()

        if not profile:
          logging.error("Profile link_id '%s' for program '%s' is not found" %
                        (link_id, self.__program.name))
          continue #continue to next row

        if not profile.is_student:
          logging.error("Profile link_id '%s' is not a student" %
                        link_id)
          continue

        tracking = data['tracking']
        date_shipped = data['date_shipped']
        notes = data['notes']
        full_address = " ".join([
          data['address_1'], data['address_2'], data['city'],
          data.get('state', ''), data.get('zip', ''),
          data.get('zippostal_code', ''), data.get('country', '')
        ])
        self.updateShipmentDataForStudent(
            profile, tracking, date_shipped, notes, full_address)

    except DeadlineExceededError:
      if remain:
        remaining_rows = sheet_rows[(-1 * remain):]
        params = {
            'program_key': params.get('program_key'),
            'sheet_rows': simplejson.dumps(remaining_rows),
            'column_indexes': params.get('column_indexes'),
            'shipment_info_id': params.get('shipment_info_id'),
        }
        taskqueue.add(
            url=redirect.urlOf('shipment_sync_task_continue'), params=params)
        return responses.terminateTask()

    self.finishSync()
    return responses.terminateTask()