def RPC__Worksheet__save(self, uuid, cells): """Store cells (and their order) associated with a worksheet. """ try: worksheet = Worksheet.objects.get(user=self.user, uuid=uuid) except Worksheet.DoesNotExist: self.return_api_error('does-not-exist') else: order = [] for data in cells: uuid = data['uuid'] type = data['type'] content = data['content'] try: cell = Cell.objects.get(user=self.user, uuid=uuid) except Cell.DoesNotExist: cell = Cell(uuid=uuid, user=self.user, worksheet=worksheet, content=content, type=type) else: cell.content = content cell.type = type order.append(uuid) cell.save() worksheet.set_order(order) worksheet.save() uuids = set(order) for cell in Cell.objects.filter(user=self.user, worksheet=worksheet): if cell.uuid not in uuids: cell.delete() self.return_api_result()