Exemplo n.º 1
0
    def test_search(self):
        binding, created = Binding.from_dict({"name": "Broschiert"})
        self.assertTrue(created)
        self.assertIsNotNone(binding.id)

        binding, created = Binding.from_dict({"name": "Paperback"})
        self.assertTrue(created)
        self.assertIsNotNone(binding.id)

        binding, created = Binding.from_dict({"name": "Leather Binding"})
        self.assertTrue(created)
        self.assertIsNotNone(binding.id)

        bindings = Binding.objects.all()
        self.assertEquals(3, len(bindings))

        bindings = Binding.search("leather")
        self.assertEquals(1, len(bindings))

        bindings = Binding.search("er")
        self.assertEquals(3, len(bindings))
Exemplo n.º 2
0
def _binding(args, file: TextIO = sys.stdout):
    binding: Optional[Binding] = None
    if args.subparser == "add":
        binding, created = Binding.from_dict({"name": args.name})
        if created:
            stdout.write(
                _('Successfully added binding "%(name)s" with id "%(pk)d".') %
                {
                    "name": binding.name,
                    "pk": binding.pk
                },
                "=",
                file=file,
            )
            binding.print(file)
        else:
            stdout.write(
                _('The binding "%(name)s" already exists with id "%(pk)d", aborting...'
                  ) % {
                      "name": binding.name,
                      "pk": binding.pk
                  },
                "",
                file=file,
            )
    elif args.subparser == "delete":
        binding = Binding.get(args.binding)
        if binding:
            binding.delete()
            stdout.write(
                _('Successfully deleted binding with id "%(pk)d".') %
                {"pk": binding.pk},
                "",
                file=file,
            )
        else:
            stdout.write(_("No binding found."), "", file=file)
    elif args.subparser == "edit":
        binding = Binding.get(args.binding)
        if binding:
            binding.edit(args.field, args.value)
            stdout.write(
                _('Successfully edited binding "%(name)s" with id "%(pk)d".') %
                {
                    "name": binding.name,
                    "pk": binding.pk
                },
                "",
                file=file,
            )
            binding.print(file)
        else:
            stdout.write(_("No binding found."), "", file=file)
    elif args.subparser == "info":
        binding = Binding.get(args.binding)
        if binding:
            binding.print(file)
        else:
            stdout.write(_("No binding found."), "", file=file)
    elif args.subparser == "list":
        if args.search:
            bindings = Binding.search(args.search)
        else:
            bindings = Binding.objects.all()
        stdout.write([_("Id"), _("Name")], "=", [0.05], file=file)
        for i, has_next in lookahead(bindings):
            stdout.write([i.id, i.name],
                         "_" if has_next else "=", [0.05],
                         file=file)