def test_get_formatter(self): """ Tests that the proper formatter is returned """ formatters = ["MarkdownFormatter", "LinebreaksFormatter", "HtmlCodeBlockFormatter"] for formatter in formatters: obj = get_formatter(formatter) assert formatter != None self.assertEquals(formatter, obj.__name__)
def test_get_formatter(self): """ Tests that the proper formatter is returned """ formatters = [ 'MarkdownFormatter', 'LinebreaksFormatter', 'HtmlCodeBlockFormatter' ] for formatter in formatters: obj = get_formatter(formatter) assert formatter != None self.assertEquals(formatter, obj.__name__)
def main(argv=sys.argv): if len(argv) != 2: usage(argv) config_uri = argv[1] setup_logging(config_uri) settings = get_appsettings(config_uri) engine = engine_from_config(settings, 'sqlalchemy.') engine2 = create_engine('postgresql://localhost/old_sontek') DBSession.configure(bind=engine) Base.metadata.create_all(engine) conn = engine2.connect() query = """ SELECT title, slug, content, published_on, draft, markup FROM tekblog_entry """ results = conn.execute(query) username = raw_input("What is your username?: ").decode('utf-8') email = raw_input("What is your email?: ").decode('utf-8') password = getpass("What is your password?: ").decode('utf-8') with transaction.manager: admin_group = Group(name='admin', description='admin group') admin = User(user_name=username, email=email) DBSession.add(admin_group) admin.groups.append(admin_group) admin.set_password(password) DBSession.add(admin) for result in results: markup = '' if result.markup: if result.markup.lower() == 'mrk': markup = 'markdown' elif result.markup.lower() == 'restructuredtextformatter': markup = 'rst' else: markup = result.markup.lower() entry = Entry( title=result.title, content=result.content, published_on=result.published_on.replace(tzinfo=None), markup=markup, owner=admin) entry.is_published = not result.draft formatter = get_formatter(entry.markup) if entry.markup == 'markdown': lines = [] in_code = False for line in entry.content.split('\n'): if '</code>' in line: lines.append('\n') lines.append('') in_code = False elif '<code' in line: in_code = True if 'class' in line: cls = line.split('class=')[1].replace('"', '') cls = cls.replace("'", '').replace(')', '') cls = cls.replace(">", '') lines.append('\n\n :::%s\n' % cls) else: lines.append('\n\n :::\n') elif in_code: line = ' ' + line lines.append(line) else: lines.append(line) entry.content = ''.join(lines) if formatter: entry.html_content = formatter(entry.content).get_html() else: entry.html_content = entry.content DBSession.add(entry) transaction.commit()
def create_entry(self): schema = EntryAdminSchema() schema = schema.bind(request=self.request) form = HieroForm(self.request, schema) if self.request.method == 'GET': if isinstance(self.request.context, RootFactory): return dict(form=form) else: appstruct = self.request.context.__json__(self.request, convert_date=False) appstruct['tags'] = [str(t.id) for t in appstruct['tags']] return dict( form=form, appstruct=appstruct ) else: try: controls = self.request.POST.items() captured = form.validate(controls) except deform.ValidationFailure, e: return dict(form=e, errors=e.error.children) if isinstance(self.request.context, RootFactory): entry = self.Entry() else: entry = self.request.context entry.title = captured['title'] entry.owner_id = captured['owner'] entry.content = captured['content'] entry.markup = captured['markup'] tags = self.session.query(self.Tag).all() saved_tags = [] for tag in tags: if captured['tags']: if str(tag.id) in captured['tags']: saved_tags.append(tag) entry.tags = saved_tags formatter = get_formatter(captured['markup']) if formatter: entry.html_content = formatter(entry.content).get_html() else: entry.html_content = entry.content entry.category_id = captured['category'] entry.series_id = captured['series'] entry.is_featured = captured['is_featured'] entry.is_published = captured['is_published'] entry.enable_comments = captured['enable_comments'] published_on = captured['published_on'] entry.published_on = published_on.replace(tzinfo=None) if captured['slug']: entry.slug = captured['slug'] self.session.add(entry) self.session.flush() self.request.session.flash(_(u'The entry was created'), 'success') return HTTPFound( location=self.request.route_url('hiero_admin_entry_index') )
def main(): if len(sys.argv) != 3: print ("Not how you use this!") sys.exit(1) config_uri = sys.argv[1] path = os.path.abspath(sys.argv[2]) setup_logging(config_uri) settings = get_appsettings(config_uri) engine = engine_from_config(settings, "sqlalchemy.") DBSession.configure(bind=engine) Base.metadata.drop_all(engine) Base.metadata.create_all(engine) username = raw_input("What is your username?: ").decode("utf-8") email = raw_input("What is your email?: ").decode("utf-8") password = getpass("What is your password?: ").decode("utf-8") with transaction.manager: admin_group = Group(name="admin", description="admin group") admin = User(username=username, email=email, password=password) DBSession.add(admin_group) admin.groups.append(admin_group) DBSession.add(admin) DBSession.flush() cat1 = Category(title="Python", slug="python") cat2 = Category(title="Reading", slug="reading") cat3 = Category(title="Startup", slug="startup") DBSession.add_all([cat1, cat2, cat3]) for fname in os.listdir(path): if not fname.endswith(".rst"): continue with open(os.path.join(path, fname)) as f: lines = f.readlines() title = lines[0].strip() category = None tags = None status = True regex = "^:(.+):(.+)$" index = 2 added_tags = [] while True: line = lines[index] if line.strip() != "": match = re.match(regex, line) if match: name = match.group(1) value = match.group(2).strip() if name == "category": if value == "Code": category = cat1 if value == "Startup": category = cat3 if value == "Reading List": category = cat2 elif name == "date": mydate = value date = datetime.strptime(mydate, "%Y-%M-%d") # elif name == 'tags': # tags = value.split(', ') # for tag in tags: # if tag in added_tags: # tag = tag.replace(' ','_') # break # else: # new = Tag(title=tag.replace(' ', '_')) # DBSession.add(new) # DBSession.commit() # added_tags.append(tag) # break elif name == "status": if value == "draft": status = False else: # no more metadata with beginning colons break index += 1 body = "".join(lines[index:]) print "title %s" % title print "category %s" % category print "date %s" % date print "status %s" % status print "tags %r" % tags # print 'body %s' % ''.join(body) print "admin %r" % admin formatter = get_formatter("rst") # entry = Entry( title=title, content=body, html_content=formatter(body).get_html(), category=category, published_on=date, is_published=status, markup="rst", owner=admin, ) DBSession.add(entry)