def handle(self, *args, **options): if len(args) == 0 or len(args) > 3: raise CommandError, "arguments: slug [LANGUAGE_CODE] [SITE_ID]" slug = args[0] if len(args) >= 2: lang = args[1] else: lang = settings.LANGUAGE_CODE if len(args) >= 3: site = Site.objects.get(id=args[2]) else: site = Site.objects.get_current() block = FlatBlock(header="[%s]" % slug, content="Empty flatblock", slug=slug, lang_code=lang, site=site) try: block.save() except IntegrityError, e: raise CommandError, "A flatblock with this name already exists"
def handle(self, *args, **options): if len(args) != 1: raise CommandError("This command requires the slug of the new " "flatblock as its first argument") slug = args[0] block = FlatBlock(header="[{0}]".format(slug), content="Empty flatblock", slug=slug) try: block.save() except IntegrityError: raise CommandError("A flatblock with this name already exists")
def testCacheRemoval(self): """ If a block is deleted it should also be removed from the cache. """ block = FlatBlock(slug="test", content="CONTENT") block.save() tpl = template.Template( '{% load flatblock_tags %}{% flatblock "test" 100 %}') # We fill the cache by rendering the block tpl.render(template.Context({})) cache_key = "%stest" % settings.CACHE_PREFIX self.assertNotEqual(None, cache.get(cache_key)) block.delete() self.assertEqual(None, cache.get(cache_key))
def handle(self, *args, **options): save_nodes = (len(args) and args[0] == 'create') templ_list = [] flatblock_nodes = [] print_nodes = [] #get list of templates for templ_dir in settings.TEMPLATE_DIRS: templ_list += [ os.path.join(dp, f) for dp, dn, fn in os.walk(os.path.expanduser(templ_dir)) for f in fn ] #load templates and get FlatBlockNode slugs for templ in templ_list: try: t = get_template(templ) flatblock_nodes += [ node.slug for node in t.nodelist.get_nodes_by_type(FlatBlockNode) ] except: pass #distinct slugs flatblock_nodes = list(set(flatblock_nodes)) #check if flatblocks have entry in database for node in flatblock_nodes: if FlatBlock.objects.filter(slug=node).count() == 0: #if create argument was supplied, save empty nodes if save_nodes: block = FlatBlock(header="[{0}]".format(node), content="Generated flatblock", slug=node) block.save() print_nodes.append(node) if len(print_nodes): if save_nodes: print "Following nodes were created:" print "\n".join(print_nodes) else: print "All FlatBlock items are in database"
def test_flatblock(): """ render a template with a flatblock to ensure the a compatible version of the flatblocks package is installed """ fb = FlatBlock(header="test-header", slug="test-slug", content="test-content",) fb.save() t = Template( """ {% load flatblocks %} {% flatblock "test-slug" %} """ ) text = t.render(Context()) assert "test-header" in text assert "test-content" in text
def testSaveForceUpdate(self): block = FlatBlock(slug='missing') with self.assertRaises(ValueError): block.save(force_update=True)
def testSaveKwargs(self): block = FlatBlock(slug='missing') # block.slug = 'missing' self.assertRaises(ValueError, block.save, force_update=True) block = FlatBlock.objects.get(slug='block') self.assertRaises(db.IntegrityError, block.save, force_insert=True)