Esempio n. 1
0
    def RPC__Worksheet__fork(self, origin_uuid, folder_uuid):
        """Create an exact copy of a worksheet from an origin. """
        try:
            origin = Worksheet.objects.get(uuid=origin_uuid)
        except Worksheet.DoesNotExist:
            self.return_api_error('origin-does-not-exist')
            return

        if origin.published is None:
            self.return_api_error('origin-is-not-published')
            return

        try:
            folder = Folder.objects.get(uuid=folder_uuid)
        except Folder.DoesNotExist:
            self.return_api_error('folder-does-not-exist')
            return

        worksheet = Worksheet(
            user=self.user,
            name=origin.name,
            description=origin.description,
            engine=origin.engine,
            origin=origin,
            folder=folder)
        worksheet.save()

        order = []

        for uuid in origin.get_order():
            try:
                base = Cell.objects.get(uuid=uuid)
            except Cell.DoesNotExist:
                pass
            else:
                cell = Cell(user=self.user,
                            type=base.type,
                            parent=base.parent,
                            content=base.content,
                            worksheet=worksheet)
                order.append(cell.uuid)
                cell.save()

        worksheet.set_order(order)
        worksheet.save()

        self.return_api_result({
            'uuid': worksheet.uuid,
            'name': worksheet.name,
        })
Esempio n. 2
0
    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()
Esempio n. 3
0
    def RPC__Worksheet__sync(self, uuid, force=False):
        """Synchronize a worksheet with its origin. """
        try:
            worksheet = Worksheet.objects.get(uuid=uuid)
        except Worksheet.DoesNotExist:
            self.return_api_error('does-not-exist')
            return

        if worksheet.origin is None:
            self.return_api_error('does-not-have-origin')
            return

        if not force and worksheet.modified > worksheet.created:
            self.return_api_error('worksheet-was-modified')
            return

        Cell.objects.filter(worksheet=worksheet).delete()

        order = []

        for uuid in worksheet.origin.get_order():
            try:
                base = Cell.objects.get(uuid=uuid)
            except Cell.DoesNotExist:
                pass
            else:
                cell = Cell(user=self.user,
                            type=base.type,
                            parent=base.parent,
                            content=base.content,
                            worksheet=worksheet)
                order.append(cell.uuid)
                cell.save()

        worksheet.set_order(order)
        worksheet.save()

        self.return_api_result()