def admin_categories(): sidenav = getSideNav() session = Session() items = session.query(Category).order_by(Category.name) session.close() context = { "sidenav": sidenav, "categories": items, "endpoint": "categories", "property": "admin", } return render_template("admin/categories.html", **context)
def editcategory(link): session = Session() category = session.query(Category).filter_by(link=link).first() session.close() context = { "sidenav": getSideNav(), "tags": tags, "modules": modules, "category": category, "endpoint": "editcategory", "property": "admin", } return render_template("admin/edit_category.html", **context)
def edititem(cat, link): session = Session() if cat == "modules": item = session.query(Module).filter_by(link=link).first() elif cat == "tags": item = session.query(Tag).filter_by(link=link).first() session.close() context = { "sidenav": getSideNav(), "item": item, "category": cat, "endpoint": "edit", "property": "admin", } return render_template("admin/edit_item.html", **context)
def admin_db(id): if current_user.is_authenticated and current_user.is_admin: sidenav = getSideNav() session = Session() item = session.query(Article).filter_by(id=int(id)).first() print(item.title, item.content, item.summary) session.close() context = { "sidenav": sidenav, "article": item, "endpoint": "db", "property": "admin", } return render_template("admin/db.html", **context) return redirect(url_for("index"))
def create_content(category_id): cid = int(category_id) session = Session() sidenav = getSideNav() cat = [item for item in sidenav if item["id"] == int(category_id)][0] tags = session.query(Tag) modules = session.query(Module) session.close() context = { "category": cat, "sidenav": sidenav, "tags": tags, "modules": modules, "endpoint": "create_" + cat["name"], "property": "admin", } return render_template("admin/create_content.html", **context)
def index(): session = Session() latest = ( session.query(Article) .filter_by(draft=0) .order_by(desc(Article.date_created)) .limit(20) .all() ) session.close() context = { "sidenav": getSideNav(), "content": latest, "title": "Welcome to Beginner Python!", "endpoint": "/", "property": "front", } return render_template("index.html", **context)
def edit(link, module=None): session = Session() tags = session.query(Tag) modules = session.query(Module) if module: article = session.query(Article).filter_by(link=module + "/" + link).first() else: article = session.query(Article).filter_by(link=link).first() print(article.link) article.summary = replaceBr(article.summary) article.content = replaceBr(article.content) session.close() context = { "sidenav": getSideNav(), "tags": tags, "modules": modules, "article": article, "endpoint": "edit", "property": "admin", } return render_template("admin/edit_content.html", **context)
def category(category_link): sidenav = getSideNav() cat = [item for item in sidenav if item["link"] == category_link][0] session = Session() if cat["active"] == False: session.close() return redirect(url_for("index")) else: category = session.query(Category).filter_by(id=int(cat["id"])).first() category.viewCount = category.viewCount + 1 session.commit() items = session.query(Article).filter_by(draft=0, category_id=int(cat["id"])) session.close() context = { "cat_id": cat["id"], "sidenav": sidenav, "articles": items, "endpoint": cat["name"], "property": "front", } return render_template("category.html", **context)
def tag(tag_link): session = Session() tag = session.query(Tag).filter_by(link=tag_link).first() tag.clickCount = tag.clickCount + 1 session.commit() articles = ( session.query(Article) .filter_by(draft=0) .join(articleTags) .filter(articleTags.c.tag_id == tag.id) .order_by(Article.date_created) ) session.close() context = { "sidenav": getSideNav(), "content": articles, "title": f"{tag.title}", "endpoint": "tag", "property": "front", } return render_template("index.html", **context)
def module(module_link): session = Session() module = session.query(Module).filter_by(link=module_link).first() module.clickCount = module.clickCount + 1 session.commit() articles = ( session.query(Article) .filter_by(draft=0) .join(articleModules) .filter(articleModules.c.module_id == module.id) .order_by(Article.date_created) ) session.close() context = { "sidenav": getSideNav(), "title": f"{module.title}", "content": articles, "endpoint": "module", "property": "front", } return render_template("index.html", **context)
def login(): session = Session() form = LoginForm() if form.validate_on_submit(): user = session.query(Useraccount).filter_by(email=form.email.data).first() if user and check_password_hash(user.password, form.password.data): login_user(user, remember=form.remember.data) user.last_login = datetime.now() session.commit() session.close() return redirect(url_for("admin")) else: flash("Your email or password doesn't match.", "danger") session.close() context = { "form": form, "sidenav": getSideNav(), "property": "front", "endpoint": "login", } return render_template("login.html", **context)
def page(category, link, module=None): session = Session() if module: article = session.query(Article).filter_by(link=module + "/" + link).first() else: article = session.query(Article).filter_by(link=link).first() article.viewCount = article.viewCount + 1 session.commit() session.close() sidenav = getSideNav() session = Session() if module: article = session.query(Article).filter_by(link=module + "/" + link).first() else: article = session.query(Article).filter_by(link=link).first() session.close() # Some unnecessary elements the editor places into the html structure needfixing article.content = article.content.replace(' contenteditable="true"', "") article.content = article.content.replace("ck ck-widget__selection-handle", "hide") article.summary = article.summary.replace(' contenteditable="true"', "") article.summary = article.summary.replace("ck ck-widget__selection-handle", "hide") # Fix removed <br> tags in code blocks by replacing them with \n article.summary = replaceBr(article.summary) article.content = replaceBr(article.content) if (current_user.is_authenticated and current_user.is_admin) or article.draft == 0: context = { "sidenav": sidenav, "article": article, "endpoint": "article_view", "property": "front", } return render_template("page.html", **context) return redirect(url_for("index"))
def admin_category(category_link): sidenav = getSideNav() try: cat = [item for item in sidenav if item["link"] == category_link][0] except IndexError: cat = [item for item in sidenav if item["title"] == category_link][0] cid = cat["id"] session = Session() if cat["name"] == "Modules": items = session.query(Module).order_by(Module.name) elif cat["name"] == "Tags": items = session.query(Tag).order_by(Tag.name) elif category_link == "messages": items = session.query(Message).order_by(Message.title) for i in range(items.count()): items[i].title = urllib.parse.quote(items[i].title, safe='') print(items[i].title) print(items[0].title) else: items = ( session.query(Article) .filter_by(category_id=int(cid)) .order_by(desc(Article.id)) ) draft = session.query(Article).filter_by(category_id=int(cid), draft=1).count() live = session.query(Article).filter_by(category_id=int(cid), draft=0).count() session.close() context = { "category": cat, "sidenav": sidenav, "articles": items, "property": "admin", } if cat["name"] not in ["Modules", "Tags"] and category_link != "messages": context["draft"] = draft context["live"] = live return render_template("admin/category.html", **context)
def register(): if os.environ.get("PRODUCTION", "DEV") != "DEV": return redirect(url_for("admin")) session = Session() form = RegistrationForm() if form.validate_on_submit(): hashed_pw = generate_password_hash(form.password.data).decode("utf-8") user = Useraccount( email=form.email.data, password=hashed_pw, displayname=form.displayname.data ) session.add(user) session.commit() flash(f"Your registration for {form.email.data} was successful!", "success") session.close() return redirect(url_for("login")) session.close() context = { "form": form, "sidenav": getSideNav(), "property": "front", "endpoint": "register", } return render_template("register.html", **context)
def create_category(): sidenav = getSideNav() context = {"sidenav": sidenav, "endpoint": "createcategory", "property": "admin"} return render_template("admin/create_category.html", **context)
def admin(): context = {"sidenav": getSideNav(), "endpoint": "admin", "property": "admin"} return render_template("admin/admin.html", **context)