def articles_comment_internal(req, uri=None, id=None): if not uri and not id: raise SERVER_RETURN(state.HTTP_NOT_FOUND) article = Article(id) article.uri = uri if uri and not article.get(req, key='uri'): raise SERVER_RETURN(state.HTTP_NOT_FOUND) if id and not article.get(req): raise SERVER_RETURN(state.HTTP_NOT_FOUND) comment = ArticleComment() comment.bind(req.form, user_agent=req.user_agent, ip=req.remote_addr) robot = True if req.form.getfirst("robot", "", str) else False qid = int(req.form.getfirst("qid", '0', str), 16) question, answer = robot_questions[qid] check = req.form.getfirst("answer", "", str) == answer if robot or not check: rv = RobotError(comment=comment, check=check) return (article, rv) if req.login: comment.author_id = req.login.id rv = comment.add(req, parent=req.form.getfirst('parent', '', str)) return (article, rv)
def articles_remove_tag(req, id, tag_id): check_login(req) match_right(req, module_rights) check_token(req, req.form.get('token'), uri='/admin/articles/%d' % id) article = Article(id) article.remove_tag(req, tag_id) req.content_type = 'application/json' return '{}'
def articles_stats(req, arg): id = arg if isinstance(arg, int) else None uri = arg if isinstance(arg, unicode) else None article = Article(id) article.uri = uri if uri and not article.inc_data_key(req, key='uri', visits=1): raise SERVER_RETURN(state.HTTP_NOT_FOUND) if id and not article.inc_data_key(req, visits=1): raise SERVER_RETURN(state.HTTP_NOT_FOUND) return send_json(req, {'Ok': True})
def articles_append_tag(req, id, tag_id): check_login(req) match_right(req, module_rights) check_token(req, req.form.get('token'), uri='/admin/articles/%d' % id) article = Article(id) if not article.append_tag(req, tag_id): return send_json(req, {'reason': 'integrity_error'}) req.content_type = 'application/json' return '{}'
def admin_articles(req): check_login(req) match_right(req, module_rights) show = req.args.getfirst('show', '', uni) pager = Pager(sort='desc') pager.bind(req.args) kwargs = {} if show == 'ready': pager.set_params(show=show) kwargs['state'] = 2 kwargs['public_date'] = 0 elif show == 'drafts': pager.set_params(show=show) kwargs['state'] = 1 else: show = None if not do_check_right(req, right_editor): kwargs['author_id'] = req.login.id items = Article.list(req, pager, **kwargs) return generate_page(req, "admin/articles.html", pager=pager, items=items, show=show)
def item_list(req, pager, perex=False, **kwargs): perex = ", perex, format " if perex else "" join = "" public = kwargs.pop("public", False) tag = kwargs.pop("tag", None) if tag: kwargs["t.name"] = tag join = """ JOIN articles_tags at ON (at.article_id = a.article_id) JOIN tags t ON (t.tag_id = at.tag_id) """ keys = list("%s %s %%s" % (k, "in" if islistable(v) else "=") for k, v in kwargs.items()) if public: # public is alias key keys.append("public_date > 0") keys.append("state != 0") cond = "WHERE " + " AND ".join(keys) if keys else "" tran = req.db.transaction(req.log_info) c = tran.cursor(DictCursor) c.execute( """ SELECT a.article_id, serial_id, author_id, email, l.name AS author, create_date, public_date, title, uri, locale, state %s FROM articles a JOIN logins l ON (a.author_id = l.login_id) %s %s ORDER BY %s %s LIMIT %%s, %%s """ % (perex, join, cond, pager.order, pager.sort), tuple(kwargs.values()) + (pager.offset, pager.limit), ) items = [] for row in iter(c.fetchone, None): item = Article() item.from_row(row) items.append(item) # endwhile c.execute("SELECT count(*) FROM articles a %s %s" % (join, cond), kwargs.values()) pager.total = c.fetchone()["count(*)"] tran.commit() return items
def admin_articles_add(req): check_login(req) match_right(req, module_rights) article = Article() if req.method == 'POST': article.bind(req.form, req.login.id) error = article.add(req) if error: return generate_page(req, "admin/articles_mod.html", article=article, error=error) redirect(req, '/admin/articles/%d' % article.id) # end article.state = 2 if do_check_right(req, right_editor) else 1 return generate_page(req, "admin/articles_mod.html", article=article)
def articles_rss(req): pager = Pager(limit=5, sort='desc', order='create_date') items = Article.list(req, pager, perex=True, public=1) for it in items: if it.format == FORMAT_RST: it.perex = rst2html(it.perex) return generate_page(req, "articles_rss.xml", content_type="application/xml", pager=pager, items=items, lang=get_lang(req), tzname=tzname, webmaster=req.server_admin)
def admin_articles_enable(req, id): check_login(req, '/log_in?referer=/admin/articles') match_right(req, module_rights) check_referer(req, '/admin/articles') article = Article(id) if not article.get(req): raise SERVER_RETURN(state.HTTP_NOT_FOUND) if (not do_check_right(req, right_editor)) \ and (not (article.author_id == req.login.id and article.public_date.year == 1970)): raise SERVER_RETURN(state.HTTP_FORBIDDEN) n_state = int(req.uri.endswith('/enable')) n_state = (n_state * 2) if article.public_date.year > 1970 else n_state article.set_state(req, n_state) redirect(req, '/admin/articles')
def admin_articles_mod(req, id): check_login(req) match_right(req, module_rights) article = Article(id) if not article.get(req): raise SERVER_RETURN(state.HTTP_NOT_FOUND) if (not do_check_right(req, right_editor) and article.author_id != req.login.id): raise SERVER_RETURN(state.HTTP_FORBIDDEN) Codebook = build_class('tags') pager = Pager(order='value', limit=-1) tags = Codebook.list(req, Codebook, pager) if req.method == 'POST': article.bind(req.form) error = article.mod(req) if error != article: return generate_page(req, "admin/articles_mod.html", article=article, error=error) if not article.get(req): raise SERVER_RETURN(state.HTTP_NOT_FOUND) return generate_page(req, "admin/articles_mod.html", article=article, token=create_token(req), tags=tags)
def articles_list_full(req, locale=None, tag=None): pager = Pager(limit=5, sort='desc', order='create_date') pager.bind(req.args) kwargs = {'locale': (locale, '')} if locale else {} items = Article.list(req, pager, perex=True, public=1, tag=tag, **kwargs) for it in items: if it.format == FORMAT_RST: it.perex = rst2html(it.perex) lang = locale if locale else get_lang(req) return generate_page(req, "articles_list.html", pager=pager, items=items, lang=lang, staticmenu=req.cfg.get_static_menu(req))
def articles_detail(req, arg): id = arg if isinstance(arg, int) else None uri = arg if isinstance(arg, unicode) else None article = Article(id) article.uri = uri if uri and not article.get(req, key='uri'): raise SERVER_RETURN(state.HTTP_NOT_FOUND) if id and not article.get(req): raise SERVER_RETURN(state.HTTP_NOT_FOUND) if article.public_date.year == 1970: if req.login is None: raise SERVER_RETURN(state.HTTP_FORBIDDEN) if not do_match_right(req, module_rights): raise SERVER_RETURN(state.HTTP_FORBIDDEN) if (not do_check_right(req, right_editor) and article.author_id != req.login.id): raise SERVER_RETURN(state.HTTP_FORBIDDEN) return articles_detail_internal(req, article)
def pages(self): self.pages = {} formatings = {'xhtml': (1, 'html'), 'self': (2, 'rst')} if not path.exists(self.dst_page_path): makedirs(self.dst_page_path) print("Migrating pages:") with self.dst_db.transaction(logger=info) as dst_cur: with self.src_db.transaction(logger=info) as src_cur: src_cur.execute(""" SELECT text_id, user_id, l.shortcut, t.name, formating, text FROM tb_morias_text AS t LEFT JOIN tb_falias_language AS l ON (l.lang_id = t.lang_id) ORDER BY text_id """) for row in src_cur: text_id, user_id, shortcut, name, formating, text = row file_name = "%s_%s.%s" % (Article.make_uri(name), shortcut.lower(), formatings[formating][1]) dst_cur.execute(""" INSERT INTO page_files (author_id, name, title, locale, format) VALUES (%s, %s, %s, %s, %d) """, (self.users.get(user_id, None), file_name, name, shortcut, formatings[formating][0])) page_id = dst_cur.lastrowid dst_path = self.dst_page_path + '/' + file_name with open(dst_path, 'w+') as src: src.write(text.encode('utf-8')) self.pages[text_id] = (page_id, self.users.get(user_id, None)) print("\t%s (%s)" % (name, file_name)) print("-> need to regenerate outputs if not dynamics are set")
def articles_tags(req, id): check_login(req) match_right(req, module_rights) return send_json(req, {'tags': Article.tags(req, id)}, cls=ObjectEncoder)
def articles(self): self.articles = {} print("Migrating articles:") with self.dst_db.transaction(logger=info) as dst_cur: with self.src_db.transaction(logger=info) as src_cur: src = "/clanek/(?P<uri>\w+)$" dst = '/a/{0}' dst_cur.execute(""" INSERT INTO redirects (src, dst, code, state) VALUES (%s, %s, 301, 1) """, (src, dst)) print("\t%s -> %s" % (src, dst)) src = "/rss_articles.rss" dst = '/articles/rss.xml' dst_cur.execute(""" INSERT INTO redirects (src, dst, code, state) VALUES (%s, %s, 301, 1) """, (src, dst)) print("\t%s -> %s" % (src, dst)) src_cur.execute(""" SELECT article_id, user_id, category_tree, l.shortcut, create_date, public, forum, title, seo_url, formating, home_text, body_text, counter, rating_count, rating_value FROM tb_morias_article AS a LEFT JOIN tb_falias_language AS l ON (l.lang_id = a.lang_id) ORDER BY article_id """) for row in src_cur: article_id, user_id, category_tree, shortcut, create_date, \ public, forum, title, seo_url, formating, home_text, \ body_text, counter, rating_count, rating_value = row author_id = self.users[user_id] uri = Article.make_uri(title) create_date = int(mktime(create_date.timetuple())) public_date = create_date if public else 0 formating = 2 if formating == 'wiki' else 1 data = {'visits': counter, 'rating_count': rating_count, 'rating_value': rating_value, 'discussion': bool(forum)} dst_cur.execute(""" INSERT INTO articles (uri, author_id, create_date, public_date, title, locale, perex, body, format, state, data) VALUES (%s, %d, %d, %d, %s, %s, %s, %s, %d, 2, %s) """, (uri, author_id, create_date, public_date, title, shortcut, home_text, body_text, formating, dumps(data))) article_new_id = dst_cur.lastrowid dst_cur.execute(""" INSERT INTO articles_tags (article_id, tag_id) VALUES (%d, %d) """, (article_new_id, self.categories[category_tree])) self.articles[article_id] = (article_new_id, author_id) print("\t%s" % title) if seo_url != uri: src = "/clanek/%s" % seo_url dst = "/a/%s" % uri dst_cur.execute(""" INSERT INTO redirects (src, dst, code, state) VALUES (%s, %s, 301, 1) """, (src, dst)) print("\t%s \n\t-> %s" % (src, dst))