def handle_noargs(self, **options): extension = options.get('ext') force = options.get('force') if not extension.startswith("."): extension = ".%s" % extension try: site = Site.objects.get_current() except: site = None if site is None: raise CommandError("Please make sure to have the sites contrib " "app installed and setup with a site object") if not type(settings.TEMPLATE_DIRS) in (tuple, list): raise CommandError("Please make sure settings.TEMPLATE_DIRS is a " "list or tuple.") templatedirs = [d for d in settings.TEMPLATE_DIRS + app_template_dirs if os.path.isdir(d)] for templatedir in templatedirs: for dirpath, subdirs, filenames in os.walk(templatedir): for f in [f for f in filenames if f.endswith(extension) and not f.startswith(".")]: path = os.path.join(dirpath, f) name = path.split(templatedir)[1][1:] try: t = Template.objects.get(name__exact=name) except Template.DoesNotExist: if force == False: confirm = raw_input( "\nA '%s' template doesn't exist in the database.\n" "Create it with '%s'?" " (y/[n]): """ % (name, path)) if confirm.lower().startswith('y') or force: t = Template(name=name, content=open(path, "r").read()) t.save() t.sites.add(site) else: while 1: confirm = raw_input( "\n%s exists in the database.\n" "(1) Overwrite %s with '%s'\n" "(2) Overwrite '%s' with %s\n" "Type 1 or 2 or press <Enter> to skip: " % (t.__repr__(), t.__repr__(), path, path, t.__repr__())) if confirm == '' or confirm in ('1', '2'): if confirm == '1': t.content = open(path, 'r').read() t.save() t.sites.add(site) elif confirm == '2': open(path, 'w').write(t.content) break
def test_form_template(self): template_params = { 'name' : 'page12.html', 'content' : 'test' } template = Template(**template_params) template.save() source_params = { 'source' : 'test-source', 'slug' : 'test-source' } source = Source(**source_params) source.save() form_params = { 'name' : 'test', 'slug' : 'test', 'template' : template, 'form_type' : source, 'height' : '100', 'width' : '200' } form = ContactForm(**form_params) embed_code = """<iframe style="border: none; overflow: hidden;" src="page12.html" frameborder="0" scrolling="no" width="200" height="100"></iframe>""" assert form.embed_code == embed_code, True
def handle_noargs(self, **options): extension = options.get('ext') force = options.get('force') if not extension.startswith("."): extension = ".%s" % extension try: site = Site.objects.get_current() except: raise CommandError("Please make sure to have the sites contrib " "app installed and setup with a site object") if not type(settings.TEMPLATE_DIRS) in (tuple, list): raise CommandError("Please make sure settings.TEMPLATE_DIRS is a " "list or tuple.") templatedirs = [d for d in settings.TEMPLATE_DIRS + app_template_dirs if os.path.isdir(d)] for templatedir in templatedirs: for dirpath, subdirs, filenames in os.walk(templatedir): for f in [f for f in filenames if f.endswith(extension) and not f.startswith(".")]: path = os.path.join(dirpath, f) name = path.split(templatedir)[1][1:] try: t = Template.on_site.get(name__exact=name) except Template.DoesNotExist: if force == False: confirm = raw_input( "\nA '%s' template doesn't exist in the database.\n" "Create it with '%s'?" " (y/[n]): """ % (name, path)) if confirm.lower().startswith('y') or force: t = Template(name=name, content=open(path, "r").read()) t.save() t.sites.add(site) else: while 1: confirm = raw_input( "\n%s exists in the database.\n" "(1) Overwrite %s with '%s'\n" "(2) Overwrite '%s' with %s\n" "Type 1 or 2 or press <Enter> to skip: " % (t.__repr__(), t.__repr__(), path, path, t.__repr__())) if confirm == '' or confirm in ('1', '2'): if confirm == '1': t.content = open(path, 'r').read() t.save() t.sites.add(site) elif confirm == '2': open(path, 'w').write(t.content) break
def test_contact_page(self): template_params = { "name" : "page12.html", "content" : "test" } template = Template(**template_params) template.save() response = self.client.get(template.name) assert response.content, "test"
def _clone_template(design, new_template_name, sites): """ create a new DB-Template entry with the content from the design template """ template_path = design.template source, origin = find_template(template_path) new_template = DBTemplateModel(name=new_template_name, content=source) new_template.save(force_insert=True) new_template.sites = sites new_template.save(force_update=True) return new_template
def test_basiscs(self): t1 = Template(name='base.html', content='<html><head></head><body>{% block content %}Welcome at {{ title }}{% endblock %}</body></html>') t1.save() self.assertEqual(Site.objects.get_current(), t1.sites.all()[0]) self.assertTrue("Welcome at" in t1.content) t2 = Template(name='sub.html', content='{% extends "base.html" %}{% block content %}This is {{ title }}{% endblock %}') t2.save() t2.sites.add(self.site2) self.assertEqual(list(Template.objects.filter(sites=self.site1)), [t1, t2]) self.assertEqual(list(t2.sites.all()), [self.site1, self.site2])
def _clone_template(design, new_template_name, sites): """ create a new DB-Template entry with the content from the design template """ template_path = design.template source = get_template_source(template_path) new_template = DBTemplateModel(name=new_template_name, content=source) new_template.save(force_insert=True) new_template.sites = sites new_template.save(force_update=True) return new_template
def get_or_create_common_template(template_name, extension='.html', app_first=False, force=True, delete=False, prefix='', notfix=None): site = Site.objects.get_current() if not extension.startswith("."): extension = ".%s" % extension if app_first: tpl_dirs = merge(app_template_dirs, DIRS) else: tpl_dirs = merge(DIRS, app_template_dirs) templatedirs = [d for d in tpl_dirs if os.path.isdir(d)] template = None for templatedir in templatedirs: for dirpath, subdirs, filenames in os.walk(templatedir): for f in [ f for f in filenames if f.endswith(extension) and not f.startswith(".") ]: path = os.path.join(dirpath, f) name = path.split(templatedir)[1] if name.startswith('/'): name = name[1:] if template_name in name and prefix in path: if notfix and notfix in path: continue try: t = Template.on_site.get(name__exact=name) except Template.DoesNotExist: t = Template(name=name, content=codecs.open(path, "r").read()) t.save(sync_themes=False) t.sites.add(site) else: if force: t.content = codecs.open(path, 'r').read() t.save(sync_themes=False) t.sites.add(site) template = t return template
def get_or_create_common_template(template_name, extension='.html', app_first=False, force=True, delete=False, prefix='', notfix=None): site = Site.objects.get_current() if not extension.startswith("."): extension = ".%s" % extension if app_first: tpl_dirs = merge(app_template_dirs, DIRS) else: tpl_dirs = merge(DIRS, app_template_dirs) templatedirs = [d for d in tpl_dirs if os.path.isdir(d)] template = None for templatedir in templatedirs: for dirpath, subdirs, filenames in os.walk(templatedir): for f in [f for f in filenames if f.endswith(extension) and not f.startswith(".")]: path = os.path.join(dirpath, f) name = path.split(templatedir)[1] if name.startswith('/'): name = name[1:] if template_name in name and prefix in path: if notfix and notfix in path: continue try: t = Template.on_site.get(name__exact=name) except Template.DoesNotExist: t = Template(name=name, content=codecs.open(path, "r").read()) t.save(sync_themes=False) t.sites.add(site) else: if force: t.content = codecs.open(path, 'r').read() t.save(sync_themes=False) t.sites.add(site) template = t return template
def save(self, **kwargs): ''' If it's object creation first save with None as self.default_page, create default page basing on user settings, and save again. Same happens for self.template. If it's an update just save. ''' if self.pk is None: super(BCardSite, self).save(**kwargs) path = settings.BCARD_DEFAULT_PATH def_page = BCardPage(bcard=self, path=path) def_page.title = settings.BCARD_DEFAULT_PAGE_TITLE def_page.content = settings.BCARD_DEFAULT_PAGE_CONTENT def_page.save() self.default_page = def_page def_template_content = loader.get_template( settings.BCARD_DEFAULT_TEMPLATE).render(Context()) def_template_name = '{0}/{1}'.format( self, settings.BCARD_DEFAULT_TEMPLATE_NAME) def_template = DBTemplate( name=def_template_name, content=def_template_content) def_template.save() self.template = def_template super(BCardSite, self).save(**kwargs)
def handle(self, **options): extension = options.get('ext') force = options.get('force') overwrite = options.get('overwrite') app_first = options.get('app_first') delete = options.get('delete') if not extension.startswith("."): extension = ".%s" % extension try: site = Site.objects.get_current() except: raise CommandError("Please make sure to have the sites contrib " "app installed and setup with a site object") if app_first: tpl_dirs = app_template_dirs + DIRS else: tpl_dirs = DIRS + list(app_template_dirs) templatedirs = [d for d in tpl_dirs if os.path.isdir(d)] for templatedir in templatedirs: for dirpath, subdirs, filenames in os.walk(templatedir): for f in [ f for f in filenames if f.endswith(extension) and not f.startswith(".") ]: path = os.path.join(dirpath, f) name = path.split(templatedir)[1] if name.startswith('/'): name = name[1:] try: t = Template.on_site.get(name__exact=name) except Template.DoesNotExist: if not force: confirm = raw_input( "\nA '%s' template doesn't exist in the " "database.\nCreate it with '%s'?" " (y/[n]): " "" % (name, path)) if force or confirm.lower().startswith('y'): with io.open(path, encoding='utf-8') as f: t = Template(name=name, content=f.read()) t.save() t.sites.add(site) else: while 1: if overwrite == ALWAYS_ASK: confirm = raw_input( "\n%(template)s exists in the database.\n" "(1) Overwrite %(template)s with '%(path)s'\n" "(2) Overwrite '%(path)s' with %(template)s\n" "Type 1 or 2 or press <Enter> to skip: " % { 'template': t.__repr__(), 'path': path }) else: confirm = overwrite if confirm in ('', FILES_TO_DATABASE, DATABASE_TO_FILES): if confirm == FILES_TO_DATABASE: with io.open(path, encoding='utf-8') as f: t.content = f.read() t.save() t.sites.add(site) if delete: try: os.remove(path) except OSError: raise CommandError( u"Couldn't delete %s" % path) elif confirm == DATABASE_TO_FILES: with io.open(path, 'w', encoding='utf-8') as f: f.write(t.content) if delete: t.delete() break
def handle(self, **options): extension = options.get('ext') force = options.get('force') overwrite = options.get('overwrite') app_first = options.get('app_first') delete = options.get('delete') if not extension.startswith("."): extension = ".%s" % extension try: site = Site.objects.get_current() except: raise CommandError("Please make sure to have the sites contrib " "app installed and setup with a site object") if app_first: tpl_dirs = app_template_dirs + DIRS else: tpl_dirs = DIRS + app_template_dirs templatedirs = [d for d in tpl_dirs if os.path.isdir(d)] for templatedir in templatedirs: for dirpath, subdirs, filenames in os.walk(templatedir): for f in [f for f in filenames if f.endswith(extension) and not f.startswith(".")]: path = os.path.join(dirpath, f) name = path.split(templatedir)[1] if name.startswith('/'): name = name[1:] try: t = Template.on_site.get(name__exact=name) except Template.DoesNotExist: if not force: confirm = raw_input( "\nA '%s' template doesn't exist in the " "database.\nCreate it with '%s'?" " (y/[n]): """ % (name, path)) if force or confirm.lower().startswith('y'): with io.open(path, encoding='utf-8') as f: t = Template(name=name, content=f.read()) t.save() t.sites.add(site) else: while 1: if overwrite == ALWAYS_ASK: confirm = raw_input( "\n%(template)s exists in the database.\n" "(1) Overwrite %(template)s with '%(path)s'\n" "(2) Overwrite '%(path)s' with %(template)s\n" "Type 1 or 2 or press <Enter> to skip: " % {'template': t.__repr__(), 'path': path}) else: confirm = overwrite if confirm in ('', FILES_TO_DATABASE, DATABASE_TO_FILES): if confirm == FILES_TO_DATABASE: with io.open(path, encoding='utf-8') as f: t.content = f.read() t.save() t.sites.add(site) if delete: try: os.remove(path) except OSError: raise CommandError( u"Couldn't delete %s" % path) elif confirm == DATABASE_TO_FILES: with io.open(path, 'w', encoding='utf-8') as f: f.write(t.content) if delete: t.delete() break