Beispiel #1
0
 def delete_image(image):
     try:
         image.delete_instance()
         return 1
     except Exception as e:
         handle_errors("Error deleting image")
         raise
Beispiel #2
0
    def update_article(article, title, body, **kwargs):

        """
        Updates an article
        Arguments:
            :article (object) - instance of the article to be updated
            :title  (string/unicode)(required) - title of the article
            :body   (string/unicode)(required) - body of the article
            :categories (list/tuple) (optional) - list containing
            names of article categories
            :series (string/unicode) (optional) - article series
            :article_image (string) - url to be used as title
            image for article
            :article_thumbnail (string) - url for articles thumbnail
        """

        try:
            series = kwargs.get("series", None)
            article_image = kwargs.get("article_image")
            article_thumbnail = kwargs.get("article_thumbnail")
            article.title = title
            article.body = body
            article.date_updated = datetime.datetime.utcnow()
            article.series = series
            article.article_image = article_image
            article.article_thumbnail = article_thumbnail
            article.save()
            article_categories = kwargs.get("categories", None)
            article.save_article_categories(article_categories, True)
            return article
        except Exception as e:
            handle_errors("Error updating article")
            raise
Beispiel #3
0
 def delete_image(image):
     try:
         image.delete_instance()
         return 1
     except Exception as e:
         handle_errors("Error deleting image")
         raise
Beispiel #4
0
    def update_article(article, title, body, **kwargs):
        """
        Updates an article
        Arguments:
            :article (object) - instance of the article to be updated
            :title  (string/unicode)(required) - title of the article
            :body   (string/unicode)(required) - body of the article
            :categories (list/tuple) (optional) - list containing
            names of article categories
            :series (string/unicode) (optional) - article series
            :article_image (string) - url to be used as title
            image for article
            :article_thumbnail (string) - url for articles thumbnail
        """

        try:
            series = kwargs.get("series", None)
            article_image = kwargs.get("article_image")
            article_thumbnail = kwargs.get("article_thumbnail")
            article.title = title
            article.body = body
            article.date_updated = datetime.datetime.utcnow()
            article.series = series
            article.article_image = article_image
            article.article_thumbnail = article_thumbnail
            article.save()
            article_categories = kwargs.get("categories", None)
            article.save_article_categories(article_categories, True)
            return article
        except Exception as e:
            handle_errors("Error updating article")
            raise
Beispiel #5
0
 def delete_article(self):
     """ Deletes an article"""
     try:
         self.delete_instance()
         return 1
     except Exception as e:
         handle_errors("Error deleting article")
         raise
Beispiel #6
0
 def delete_article(self):
     """ Deletes an article"""
     try:
         self.delete_instance()
         return 1
     except Exception as e:
         handle_errors("Error deleting article")
         raise
Beispiel #7
0
    def gallerify(image):

        try:
            is_gallerified = image.gallery
            image.gallery = not is_gallerified
            image.save()

        except Exception as e:
            handle_errors("Error updating image")
Beispiel #8
0
    def gallerify(image):

        try:
            is_gallerified = image.gallery
            image.gallery = not is_gallerified
            image.save()

        except Exception as e:
            handle_errors("Error updating image")
Beispiel #9
0
    def publish_article(self):
        """Reverses 'draft' status of the article"""
        try:
            self.draft = not self.draft
            self.save()

        except Exception as e:
            handle_errors("Error publishing article")
            raise
Beispiel #10
0
    def publish_article(self):
        """Reverses 'draft' status of the article"""
        try:
            self.draft = not self.draft
            self.save()

        except Exception as e:
            handle_errors("Error publishing article")
            raise
Beispiel #11
0
 def delete_project(project):
     """
     Delete project
     """
     try:
         project.delete_instance()
         return 1
     except Exception as e:
         handle_errors("error deleting project")
         raise
Beispiel #12
0
 def delete_project(project):
     """
     Delete project
     """
     try:
         project.delete_instance()
         return 1
     except Exception as e:
         handle_errors("error deleting project")
         raise
Beispiel #13
0
    def create_user(username, password, description=None, real_name=None):
        """ Create new user """

        try:
            return Users.create(username=username,
                                hash=generate_password_hash(password),
                                description=description,
                                real_name=real_name).get_id()

        except:
            handle_errors("Error creating user")
            raise
Beispiel #14
0
    def get_index_articles(page, per_page):

        """
        Returns paginated articles for the index page
        Arguments:
            :page - current page
            :per_page - number of articles to be displayed per page
        """

        try:
            return Articles.select().where(Articles.draft == False).paginate(page, per_page)
        except:
            handle_errors("Error getting articles")
Beispiel #15
0
    def create_user(username, password, description=None, real_name=None):

        """ Create new user """

        try:
            return Users.create(username=username,
                                hash=generate_password_hash(password),
                                description=description,
                                real_name=real_name).get_id()

        except:
            handle_errors("Error creating user")
            raise
Beispiel #16
0
    def get_user_articles(username):

        """
        Get all articles belonging to user
        Arguments:
            :username - username of articles' author
        """

        try:
            return Articles.select().join(Users).where(Users.username == username)

        except:
            handle_errors("Error getting articles")
Beispiel #17
0
    def get_user_articles(username):
        """
        Get all articles belonging to user
        Arguments:
            :username - username of articles' author
        """

        try:
            return Articles.\
                   select().\
                   join(Users).\
                   where(Users.username == username)

        except:
            handle_errors("Error getting articles")
Beispiel #18
0
    def create_config(**kwargs):

        """ Create new config """

        to_create = dict()

        for key, value in kwargs.items():
            if key in ConfigModel._meta.get_field_names() and value is not None:
                to_create[key] = value
        try:
            ConfigModel.create(**to_create)
            return 1
        except Exception as e:
            handle_errors("Error creating config file... Aborting")
            raise
Beispiel #19
0
    def get_index_articles(page, per_page):
        """
        Returns paginated articles for the index page
        Arguments:
            :page - current page
            :per_page - number of articles to be displayed per page
        """

        try:
            return Articles\
                    .select()\
                    .where(Articles.draft == False)\
                    .paginate(page, per_page)
        except:
            handle_errors("Error getting articles")
Beispiel #20
0
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']))
Beispiel #21
0
    def create_project(title, body, author, **kwargs):
        """
        Create new project
        Arguments:
            :title (unicode) - title of the project
            :body (unicode) - body of the project
            :author (object) - object containing author info

        """
        if len(title) > 255:
            raise ValueError("Title must be at most 255 characters long")
        try:
            UserProjects.create(title=title, body=body, author=author)
        except Exception as e:
            handle_errors("error creating project")
            raise
Beispiel #22
0
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']))
Beispiel #23
0
    def create_article(title, body, author, draft=True, **kwargs):

        """
        Creates new article
        Returns instance of the created article

        Arguments:
            :title  (string/unicode)(required) - title of the article
            :body   (string/unicode)(required) - body of the article
            :author (object) - Users object containing author\'s info
            :draft  (bool) (optional)- whether article is published
            :categories (list/tuple) (optional) - list containing
            names of article categories
            :series (string/unicode) (optional) - article series
            :article_image (string) - url to be used as title
            image for article
            :article_thumbnail (string) - url for articles thumbnail
        """

        if len(title) > 255:
            raise ValueError("Title must be at most 255 characters")
        try:
            series = kwargs.get("series", None)
            article_image = kwargs.get("article_image", None)
            article_thumbnail = kwargs.get("article_thumbnail", None)
            article = Articles.create(
                title=title,
                slug=slugify(title),
                body=body,
                author=author,
                draft=draft,
                series=series,
                article_image=article_image,
                article_thumbnail=article_thumbnail,
            )
            article_categories = kwargs.get("categories", None)
            if article_categories:
                try:
                    article.save_article_categories(article_categories)
                except:
                    raise
            return article
        except Exception as e:
            handle_errors("Error creating article")
            raise
Beispiel #24
0
    def create_project(title, body, author, **kwargs):
        """
        Create new project
        Arguments:
            :title (unicode) - title of the project
            :body (unicode) - body of the project
            :author (object) - object containing author info

        """
        if len(title) > 255:
            raise ValueError("Title must be at most 255 characters long")
        try:
            UserProjects.create(title=title,
                                body=body,
                                author=author)
        except Exception as e:
            handle_errors("error creating project")
            raise
Beispiel #25
0
    def save_settings(self, **kwargs):

        """
        Update settings given a dictionary of values
        """

        to_save = dict()
        for key, value in kwargs.items():
            if key in self._meta.get_field_names() and value is not None:
                to_save[key] = value
        try:
            for k, v in to_save.items():
                setattr(self, k, v)
            self.save()

        except Exception as e:
            handle_errors("Error updating configuration")
            raise
Beispiel #26
0
 def add_image(image_link,
               description,
               owner,
               is_vertical=True,
               imgur_img=False,
               delete_hash=None):
     try:
         UserImages.create(
             image_link=image_link,
             description=description,
             is_vertical=is_vertical,
             owner=owner,
             imgur_img=imgur_img,
             delete_hash=delete_hash,
         )
         return 1
     except Exception as e:
         handle_errors("Error creating image")
         raise
Beispiel #27
0
def delete_image(id):
    image = UserImages.get_image(id)
    if not image:
        abort(404)
    if image.owner.username != session["user"]:
        flash("Don't try to delete other\'s dude\'s pictures...dude")
        return redirect(url_for("index"))
    else:
        try:
            UserImages.delete_image(image)
        except:
            error = "Error occured when writing to database"
            flash(error)
            return redirect(url_for("index"))
        if image.imgur_img:
            resp = ImgurHandler(get_config().imgur_id).delete_image(image.delete_hash)
            if not resp["success"]:
                handle_errors(resp)
        return redirect(url_for("user_images", username=session["user"]))
Beispiel #28
0
 def add_image(image_link,
               description,
               owner,
               is_vertical=True,
               imgur_img=False,
               delete_hash=None):
     try:
         UserImages.create(
             image_link=image_link,
             description=description,
             is_vertical=is_vertical,
             owner=owner,
             imgur_img=imgur_img,
             delete_hash=delete_hash,
         )
         return 1
     except Exception as e:
         handle_errors("Error creating image")
         raise
Beispiel #29
0
    def create_article(title, body, author, draft=True, **kwargs):
        """
        Creates new article
        Returns instance of the created article

        Arguments:
            :title  (string/unicode)(required) - title of the article
            :body   (string/unicode)(required) - body of the article
            :author (object) - Users object containing author\'s info
            :draft  (bool) (optional)- whether article is published
            :categories (list/tuple) (optional) - list containing
            names of article categories
            :series (string/unicode) (optional) - article series
            :article_image (string) - url to be used as title
            image for article
            :article_thumbnail (string) - url for articles thumbnail
        """

        if len(title) > 255:
            raise ValueError("Title must be at most 255 characters")
        try:
            series = kwargs.get("series", None)
            article_image = kwargs.get("article_image", None)
            article_thumbnail = kwargs.get("article_thumbnail", None)
            article = Articles.create(title=title,
                                      slug=slugify(title),
                                      body=body,
                                      author=author,
                                      draft=draft,
                                      series=series,
                                      article_image=article_image,
                                      article_thumbnail=article_thumbnail)
            article_categories = kwargs.get("categories", None)
            if article_categories:
                try:
                    article.save_article_categories(article_categories)
                except:
                    raise
            return article
        except Exception as e:
            handle_errors("Error creating article")
            raise
Beispiel #30
0
    def update_project(project, title, body, **kwargs):
        """
        Update project information
        Arguments:
            :project (object) - instance of project
            :title (unicode) - new title
            :body (unicode) - new body
            """

        if len(title) > 255:
            raise ValueError("Title must be at most 255 characters long")
        try:
            project.title = title
            project.body = body
            project.date_updated = datetime.now()
            project.save()
            return 1
        except Exception as e:
            handle_errors("error updating article")
            raise
Beispiel #31
0
def delete_image(id):
    image = UserImages.get_image(id)
    if not image:
        abort(404)
    if image.owner.username != session["user"]:
        flash("Don't try to delete other\'s dude\'s pictures...dude")
        return redirect(url_for("index"))
    else:
        try:
            UserImages.delete_image(image)
        except:
            error = "Error occured when writing to database"
            flash(error)
            return redirect(url_for("index"))
        if image.imgur_img:
            resp = ImgurHandler(get_config().imgur_id).delete_image(
                image.delete_hash)
            if not resp["success"]:
                handle_errors(resp)
        return redirect(url_for("user_images", username=session["user"]))
Beispiel #32
0
    def save_article_categories(self, category_names, update=False):
        """
        Create Categories and ArticleCategories table if
        category doesn\'t exist or article doesn\'t have the category yet
        Arguments:
            : category_names (list/tuple) - iterable containing
            categories' names
            : update (bool) - if True performs a check whether
            user has deleted any categories
        """

        if not category_names:
            return

        try:
            new_categories = set(category_names)
            own_categories = self.get_article_categories().iterator()

            for name in new_categories:
                if not Categories.select()\
                   .where(Categories.name == name).exists():
                    cat = Categories.create(name=name)
                    ArticleCategories.create(article=self, category=cat)

            existing_categories = set([field.name for field in own_categories])
            to_add = list(new_categories.difference(existing_categories))
            for name in to_add:
                cat = Categories.select().where(Categories.name == name).get()
                ArticleCategories.create(article=self, category=cat)
            if update:
                q = list(existing_categories.difference(new_categories))
                if q:
                    to_remove = Categories.select().where(Categories.name << q)
                    delete_query = ArticleCategories\
                                   .delete()\
                                   .where((ArticleCategories.article == self)\
                                           & (ArticleCategories.category << to_remove))
                    delete_query.execute()
        except Exception as e:
            handle_errors("error saving article categories")
            raise e
Beispiel #33
0
    def update_project(project, title, body, **kwargs):

        """
        Update project information
        Arguments:
            :project (object) - instance of project
            :title (unicode) - new title
            :body (unicode) - new body
            """

        if len(title) > 255:
            raise ValueError("Title must be at most 255 characters long")
        try:
            project.title = title
            project.body = body
            project.date_updated = datetime.now()
            project.save()
            return 1
        except Exception as e:
            handle_errors("error updating article")
            raise
Beispiel #34
0
    def save_article_categories(self, category_names, update=False):

        """
        Create Categories and ArticleCategories table if
        category doesn\'t exist or article doesn\'t have the category yet
        Arguments:
            : category_names (list/tuple) - iterable containing
            categories' names
            : update (bool) - if True performs a check whether
            user has deleted any categories
        """

        if not category_names:
            return

        try:
            new_categories = set(category_names)
            own_categories = self.get_article_categories().iterator()

            for name in new_categories:
                if not Categories.select().where(Categories.name == name).exists():
                    cat = Categories.create(name=name)
                    ArticleCategories.create(article=self, category=cat)

            existing_categories = set([field.name for field in own_categories])
            to_add = list(new_categories.difference(existing_categories))
            for name in to_add:
                cat = Categories.select().where(Categories.name == name).get()
                ArticleCategories.create(article=self, category=cat)
            if update:
                q = list(existing_categories.difference(new_categories))
                if q:
                    to_remove = Categories.select().where(Categories.name << q)
                    delete_query = ArticleCategories.delete().where(
                        (ArticleCategories.article == self) & (ArticleCategories.category << to_remove)
                    )
                    delete_query.execute()
        except Exception as e:
            handle_errors("error saving article categories")
            raise e
Beispiel #35
0
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"]))
Beispiel #36
0
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"]))