def logout(): """ Logout the user """ if "user" in session: session.pop("user", None) with app.app_context(): cache.clear() return redirect(url_for("index"))
def post(self, id): title = request.form.get("title").strip() body = request.form.get("body").strip() context = dict(title=title, body=body) context.update(self.process_additional_fields()) if not title or not body: error = "Entry can\'t have empty title or body" context.update(dict(error=error)) return self.render_template(context) model = self.get_model() check = model.check_exists(title, id) if check: error = "Entry with this title\ already exists, please choose another" context.update(dict(error=error)) return self.render_template(context) else: try: obj = self.get_object(id) func = getattr(model, self.create_method()) func(obj, **context) with app.app_context(): cache.clear() return redirect(url_for("account", username=session["user"])) except Exception as e: logger.debug(e) error = "Error processing request, see error.log for details" context.update(dict(error=error)) return self.render_template(context)
def post(self): title = request.form.get("title").strip() body = request.form.get("body").strip() user = Users.get_user_by_username(session["user"]) context = dict(title=title, body=body, author=user) additional = self.get_context() context.update(additional) if not title or not body: error = "Entry can\'t have empty title or body" context.update(dict(error=error)) return self.render_template(context) model = self.get_model() check = model.check_exists(title) if check: error = "Entry with that title already exists, choose a new one.." context.update(dict(error=error)) return self.render_template(context) else: context.update(self.process_additional_fields()) try: func = getattr(model, self.create_method()) func(**context) with app.app_context(): cache.clear() flash("Created") return redirect(url_for("account", username=session["user"])) except Exception as e: logger.debug(e) error = "Processing error see error.log for details" context.update(dict(error=error)) return self.render_template(context)
def set_info(): """Set user information""" user = Users.get_user_by_username(session['user']) real_name = request.form.get("real-name", None) description = request.form.get("description", None) user.real_name = real_name user.description = description try: user.save() except Exception as e: handle_errors("Error updating user info") abort(500) finally: with app.app_context(): cache.clear() return redirect(url_for('account_settings', username=session['user']))
def publish_article(id): article = Articles.get_article(id) if not article: abort(404) if article.author.username != session["user"]: flash("You can\'t publish other\'s peoples posts") return redirect(url_for("index")) else: try: article.publish_article() with app.app_context(): cache.clear() flash("Article has been published") except: flash("Error when publishing article") finally: return redirect(url_for("account", username=session["user"]))
def configure(): for field in app.config["SETTINGS_FIELDS"]: locals()[field] = request.form.get(field, "").strip().encode("utf-8") for field in app.config["SETTINGS_SWITCHES"]: locals()[field] = True if request.form.get(field) == "on" else False to_update = dict() for key, val in locals().items(): if key in ConfigModel._meta.get_field_names(): to_update[key] = val try: config = ConfigModel.select().get() config.save_settings(**to_update) with app.app_context(): cache.clear() flash("Settings has been updated") return redirect(url_for('account_settings', username=session['user'])) except: handle_errors("Error when saving configuration") return redirect(url_for("account_settings", username=session["user"]))
def render_template(self, context=dict()): with app.app_context(): cache.clear() context.update(self.get_context()) context.update(dict(show_title=True)) return render_template(self.get_template_name(), **context)