def show_home(): """ Homepage of the blog """ last = Articles.select().order_by(desc(Articles.timestamp)).first() lasts_art = Articles.select().order_by(desc(Articles.timestamp))[1:4] list_of_dict = [] for item in lasts_art: list_of_dict.append(fill_informations(item)) return render_template('blog/home.html', last=last, lasts=list_of_dict, socials=socials)
def get_sitemap(): """ Returns a sitemap with all articles (WIP) """ articles = Articles.select() return render_template('components/sitemap-articles.xml', articles=articles)
def show_type(type, page): """ Displays list.html with the lasts articles of a given type. Returns an error if the page is empty or if type doesn't exists. """ if type not in types: return render_template('components/erreur.html', erreur="Le type souhaité est invalide !") type_data = types[type] data = Articles.select( lambda a: a.type == type).order_by( desc( Articles.timestamp))[ page * 10:page * 10 + 10] articles = [] for item in data: articles.append(fill_informations(item)) return render_template('blog/list.html', template="type", type=type, icon=type_data['icon'], name=type_data['name'], articles=articles, total_articles=count( a for a in Articles if a.type == type), page=page)
def show_category(category, page): """ Displays the list page with the lasts articles of a given category. Returns an error if the page is empty or if category doesn't exists. """ if category not in categories: return render_template( 'components/erreur.html', erreur="La catégorie souhaitée est invalide !") category_data = categories[category] data = Articles.select( lambda a: a.category == category).order_by( desc( Articles.timestamp))[ page * 10: page * 10 + 10] articles = [] for item in data: articles.append(fill_informations(item)) return render_template('blog/list.html', template="Catégorie", type=category, name=category_data['name'], icon=category_data['icon'], articles=articles, total_articles=count( a for a in Articles if a.category == category), page=page)
def get_rss(): """ Generates the RSS feed """ articles = Articles.select().order_by(desc(Articles.timestamp)) rss_xml = render_template('components/flux.xml', articles=articles) response = make_response(rss_xml) response.headers['Content-Type'] = 'application/xml' return response
def process_search(keyword, page): """ Process the search when a keyword is given """ try: articles, total_articles = Articles.search_articles(keyword, page) except NoArticlesFound: error = "Votre recherche n'a donné aucun résultat. Vous pouvez entrer un autre mot clé :" lasts = Articles.get_articles_in_range(0, 3) return render_template('page/search.html', error=error, lasts=lasts) return render_template('blog/list.html', articles=articles, total_articles=total_articles, template="site", type="Recherche", name="Recherche", icon="magnify", keyword=keyword, page=page)
def show_search(keyword, page): """ Search into articles """ if keyword is None: keyword = request.args.get('q', default="", type=str) if page == 0: page = request.args.get('page', default=0, type=int) if keyword != "": return PageController.process_search(keyword, page) else: lasts = Articles.get_articles_in_range(0, 3) return render_template('page/search.html', lasts=lasts)
def show_article(url): """ Displays an article using a given URL """ try: article = Articles.get(url=url) author = User.get(username=article.author) if article.article_language == "markdown": htmlarticle = Markup( markdown.markdown( article.content, extensions=['extra'])) else: htmlarticle = '' return render_template('blog/article.html', article=article, author=author, htmlarticle=htmlarticle) except Exception as e: print(e) error = "La page recherchée n'existe pas! (404)" return render_template('components/erreur.html', erreur=error), 404
def create_post(user): permissions = str(user.permissions) username = str(user.username) if permissions.find("BLOG_WRITE") != -1: if request.method == 'POST': # An authorized user wants to submit a new article. # Send the data to MySQL article = Articles(title=request.form['title'], type=request.form['type'], category=request.form['category'], description=request.form['description'], url=request.form['url'], banner=request.form['banner'], timestamp=time.time(), author=username, content=request.form['md-editor'], labels=request.form['labels'].split(','), article_language="markdown") commit() return redirect('/article/' + request.form['url']) return render_template('members/new-article.html') error = "Vous n'avez pas la permission de visiter cette page (403)" return render_template('components/erreur.html', erreur=error), 403