def add_rows(self, additions, id_name): """ @param additions: a dict of dicts. Each dict contains the fields of a row that must be added. @return: """ # update the in-memory table for id in additions: self.table.append(additions[id]) # update the spreadsheet for id in additions: additions[id][id_name] = id # to make sure this field is also filled in if it wasn't explicitly in the dict! entry = ListEntry() entry.from_dict(export_for_spreadsheet(additions[id])) for attempt in range(5): try: self._client.add_list_entry( entry, self._google_spreadsheet_key, google_spreadsheet_first_worksheet_id ) except httplib.HTTPException as e: logging.info(e.message) time.sleep(0.5) continue else: break else: # we failed all the attempts - deal with the consequences. logging.info("Retried adding rows to the spreadsheet data three times in vain.") return logging.info('On index added row with id = ' + id) if additions: self.sync_table()
def post(self): file_link = None if 'file' in self.request.POST and self.request.POST['file'].file: fileName = "{} ({})".format(self.request.POST['file'].filename, self.request.POST['name']) description = "Name: {}, Email: {}, Phone: {}, Size: {}".format(self.request.POST['name'], self.request.POST['email'], self.request.POST['phone'], self.request.POST['ad_size']) media = MediaIoBaseUpload(self.request.POST['file'].file, self.request.POST['file'].type) http = creds.creds.authorize(httplib2.Http()) drive_service = build('drive', 'v2', http=http) f = drive_service.files().insert(body = { 'mimeType': self.request.POST['file'].type, 'title': fileName, 'description': description, 'parents': [ { 'id': folder.findParent() }], }, media_body = media).execute() file_link = f['alternateLink'] sheet_key, worksheet_id = worksheet.findSheet() if sheet_key: sheetsClient = worksheet.client() newRow = ListEntry() newRow.from_dict({ 'time': str(datetime.now()), 'name': self.request.POST['name'], 'email': self.request.POST['email'], 'phone': self.request.POST['phone'], 'type': self.request.POST['ad_type'], 'size': self.request.POST['ad_size'], 'layout': self.request.POST['ad_layout'], 'font': 'ad_font' in self.request.POST and self.request.POST['ad_font'] or "", 'text': 'ad_text' in self.request.POST and self.request.POST['ad_text'] or "", 'design': 'ad_design' in self.request.POST and self.request.POST['ad_design'] or "", 'image': file_link or "none", }) sheetsClient.AddListEntry(newRow, sheet_key, worksheet_id, creds.authToken)
def add_timestamped_entry(self, worksheet_name): feed, worksheet_key = self.get_feed_for_worksheet(worksheet_name) main_entry = feed.entry[0] new_entry = ListEntry() new_entry.from_dict(main_entry.to_dict()) new_entry.set_value('timestamp', format_date_for_gdata(datetime.datetime.utcnow())) self.spr_client.add_list_entry(new_entry, self.spreadsheet_key, worksheet_key)
def dictToRow(self, d): """ A general dict -> ListEntry converter. Does not make any changs to keys or data. """ row = ListEntry() row.from_dict(d) return row
def add_row(self, spreadsheet_key=None, worksheet_id=None, values={}): spreadsheet_key = spreadsheet_key or self.default_spreadsheet_key if spreadsheet_key is None: raise Exception('spreadsheet_key is not given') worksheet_id = worksheet_id or self.default_worksheet_id if worksheet_id is None: raise Exception('sheet_id is not given') if len(values) == 0: return list_entry = ListEntry() list_entry.from_dict( {key: _convert(value) for key, value in values.iteritems()}) self.client.add_list_entry(list_entry, spreadsheet_key, worksheet_id)
def writeRow(rowDict, client, sheetKey, sheetID='od6'): fixedRow = convertRow(rowDict) listEntry = ListEntry() listEntry.from_dict(fixedRow) client.add_list_entry(listEntry, sheetKey, sheetID)