Ejemplo n.º 1
0
 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)
Ejemplo n.º 2
0
 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)
Ejemplo n.º 3
0
    def get_db(self, kwargs=dict()):

        """
        Return a new database connection object
        provided values defined in configuration file
        """
        # config for heroku
        if "DATABASE_URL" in os.environ:
            url = parse.urlparse(os.environ.get("DATABASE_URL"))
            DATABASE = {
                "database": url.path[1:],
                "user": url.username,
                "password": url.password,
                "host": url.hostname,
                "port": url.port
            }
            return PostgresqlDatabase(**DATABASE)
        dtype = self.app.config.get("DATABASE", None)
        dname = self.app.config.get("DATABASE_NAME", None)
        if self.app.config.get("TESTING", False):
            return self._define_db_connection("sqlite", ":memory:")
        if not dtype or not dname:
            raise ValueError("Database type and name must be defined")
        if dtype in ("postgres", "mysql"):
            username = self.app.config.get("DB_USERNAME")
            password = self.app.config.get("DB_PASSWORD", None)
            if not username:
                raise ValueError("%s requires username to connect" % dtype)
            kwargs.update(dict(
                user=username,
            ))
            if password:
                if dtype == "postgres":
                    kwargs.update(dict(
                        password=password
                    ))
                elif dtype == "mysql":
                    kwargs.update(dict(
                        passwd=password
                    ))
        try:
            return self._define_db_connection(dtype, dname, **kwargs)
        except Exception as e:
            logger.debug(e)
Ejemplo n.º 4
0
 def get_db(self, kwargs=dict()):
     """
     Return a new database connection object
     provided values defined in configuration file
     """
     # config for heroku
     if "DATABASE_URL" in os.environ:
         url = parse.urlparse(os.environ.get("DATABASE_URL"))
         DATABASE = {
             "database": url.path[1:],
             "user": url.username,
             "password": url.password,
             "host": url.hostname,
             "port": url.port
         }
         return PostgresqlDatabase(**DATABASE)
     dtype = self.app.config.get("DATABASE", None)
     dname = self.app.config.get("DATABASE_NAME", None)
     if self.app.config.get("TESTING", False):
         return self._define_db_connection("sqlite", ":memory:")
     if not dtype or not dname:
         raise ValueError("Database type and name must be defined")
     if dtype in ("postgres", "mysql"):
         username = self.app.config.get("DB_USERNAME")
         password = self.app.config.get("DB_PASSWORD", None)
         if not username:
             raise ValueError("%s requires username to connect" % dtype)
         kwargs.update(dict(user=username, ))
         if password:
             if dtype == "postgres":
                 kwargs.update(dict(password=password))
             elif dtype == "mysql":
                 kwargs.update(dict(passwd=password))
     try:
         return self._define_db_connection(dtype, dname, **kwargs)
     except Exception as e:
         logger.debug(e)