Exemplo n.º 1
0
	def main(self):
		items = self.adsm_lists.service.GetListItems(self.args.list, self.args.view).listitems.data.row

		method_idx = 1
		batch = Element('Batch')\
		       .append(Attribute('OnError', 'Continue'))\
		       .append(Attribute('ListVersion', 1))

		def update(b):
			if not self.args.d:
				updates = Element('ns1:updates').append(b)
				print self.adsm_lists.service.UpdateListItems(listName=self.args.list, updates=updates)

		for item in items:
			method = Element('Method')\
			        .append(Attribute('ID', method_idx))\
			        .append(Attribute('Cmd', 'Delete'))\
			        .append(Element('Field')\
			          .append(Attribute('Name', 'ID'))\
			          .setText(item['_ows_ID']))
			batch.append(method)
			print method
			method_idx += 1

			if len(batch) > 20:
				update(batch)
				batch.detachChildren()

		if len(batch) > 0:
			update(batch)
Exemplo n.º 2
0
	def sync_to_list_by_comparison(self, list_uuid, query, viewFields, list_items_compare_key, ext_items, ext_items_compare_key, compare_f, field_map, content_type='Item', folder=None, fuzzy=False, max_dist=4, commit=True):
		table = self.keyed_listitems(list_uuid, query=query, fields=viewFields, folder=folder, key_field=list_items_compare_key)
		if fuzzy:
			fuzzy_table = self.fuzzy_keyed_listitems(list_uuid, query=query, fields=viewFields, folder=folder, key_field=list_items_compare_key)

		method_idx = 1
		batch = Element('Batch')\
		       .append(Attribute('OnError', 'Continue'))\
		       .append(Attribute('ListVersion', 1))
		if folder:
			batch.append(Attribute('RootFolder', folder))

		def update(b):
			if commit:
				updates = Element('ns1:updates').append(b)
				print self.adsm_lists.service.UpdateListItems(listName=list_uuid, updates=updates)

		for ext_item in ext_items:
			list_item = table.get(ext_item[ext_items_compare_key])
			if not list_item and fuzzy:
				list_item = self.fuzzy_match(fuzzy_table, ext_item[ext_items_compare_key], max_dist=4)

			method_cmd = compare_f(ext_item, list_item)
			if not method_cmd:
				continue
			item_id = list_item['_ows_ID'] if list_item else 'New'

			# Prepare a method for this new or update item
			method = Element('Method')\
			        .append(Attribute('ID', method_idx))\
			        .append(Attribute('Cmd', method_cmd))\
			        .append(Element('Field')\
			          .append(Attribute('Name', 'ID'))\
			          .setText(item_id))\
			        .append(Element('Field')\
			        	.append(Attribute('Name', 'ContentType'))\
			        	.setText(content_type))
			
			for dst, src in field_map:
				try:
					if not isinstance(src, basestring):
						v = src(ext_item)
					elif isinstance(ext_item, collections.Mapping):
						v = ext_item.get(src)
					else:
						v = getattr(ext_item, src, None)
				except:
					v = None
				e = Element('Field')\
				   .append(Attribute('Name', dst))\
				   .setText(v)
				method.append(e)

			batch.append(method)
			print method
			method_idx += 1

			if len(batch) > ADSMBase.batch_size:
				update(batch)
				batch.detachChildren()

		if len(batch) > 0:
			update(batch)