Beispiel #1
0
    def test_creating_article(self):

        with test_database(db, (Users, Articles)):
            user1 = Users.create_user(username = "******", password = "******")
            article = Articles.create_article(title = "test",\
                                    body = "test",\
                                    draft = True,\
                                    author = user1)

            self.assertEquals("test", article.title)
            self.assertEquals("konrad",article.author.username)

            Articles.create_article(title = "test2",\
                                   body = "test",\
                                   draft = False, \
                                   author = user1)

            self.assertRaises(IntegrityError,\
                              lambda: Articles.create_article(title = "test",\
                                                              body = "test",\
                                                              author = user1))

            articles = Articles.get_user_articles("konrad")

            self.assertEquals(2,len(tuple(articles)))

            user2 = Users.create_user(username = "******", password = "******")
            Articles.create_article(title = "test3", \
                                   body = "test",\
                                   author = user2)

            articles = Articles.get_user_articles("konrad")
            self.assertEquals(2,len(tuple(articles)))
            articles2 = Articles.get_user_articles("malgosia")
            self.assertEquals(1,len(tuple(articles2)))
Beispiel #2
0
    def test_creating_article(self):

        with test_database(db, (Users, Articles)):
            user1 = Users.create_user(username="******", password="******")
            article = Articles.create_article(title = "test",\
                                    body = "test",\
                                    draft = True,\
                                    author = user1)

            self.assertEquals("test", article.title)
            self.assertEquals("konrad", article.author.username)

            Articles.create_article(title = "test2",\
                                   body = "test",\
                                   draft = False, \
                                   author = user1)

            self.assertRaises(IntegrityError,\
                              lambda: Articles.create_article(title = "test",\
                                                              body = "test",\
                                                              author = user1))

            articles = Articles.get_user_articles("konrad")

            self.assertEquals(2, len(tuple(articles)))

            user2 = Users.create_user(username="******", password="******")
            Articles.create_article(title = "test3", \
                                   body = "test",\
                                   author = user2)

            articles = Articles.get_user_articles("konrad")
            self.assertEquals(2, len(tuple(articles)))
            articles2 = Articles.get_user_articles("malgosia")
            self.assertEquals(1, len(tuple(articles2)))
Beispiel #3
0
def admin_login():
    user_check = Users.check_any_exist()
    if not user_check:
        return redirect(url_for("create_account"))
    if "user" in session:
        return redirect(url_for("account", username=session["user"]))

    error = None
    if request.method == "POST":
        token = session.pop('_csrf_token', None)
        if not token or token != request.form.get('_csrf_token'):
            abort(403)
        username = request.form.get("username").strip()
        password = request.form.get("password").strip()
        user = Users.get_user_by_username(username)
        if not user:
            error = "Incorrect Credentials"
            return render_template("login.html", error=error)
        else:
            if not user.check_password(password):
                error = "Incorrect Credentials"
                return render_template("login.html", error=error)
            else:
                session["user"] = user.username
                return redirect(url_for("account", username=user.username))
    return render_template("login.html", error=error)
Beispiel #4
0
def admin_login():
    user_check = Users.check_any_exist()
    if not user_check:
        return redirect(url_for("create_account"))
    if "user" in session:
        return redirect(url_for("account", username=session["user"]))

    error = None
    if request.method == "POST":
        token = session.pop('_csrf_token', None)
        if not token or token != request.form.get('_csrf_token'):
            abort(403)
        username = request.form.get("username").strip()
        password = request.form.get("password").strip()
        user = Users.get_user_by_username(username)
        if not user:
            error = "Incorrect Credentials"
            return render_template("login.html", error=error)
        else:
            if not user.check_password(password):
                error = "Incorrect Credentials"
                return render_template("login.html", error=error)
            else:
                session["user"] = user.username
                return redirect(url_for("account", username=user.username))
    return render_template("login.html", error=error)
Beispiel #5
0
    def test_saving_and_deleting_categories(self):

        with test_database(db,
                           (Users, Articles, Categories, ArticleCategories)):
            Users.create_user(username="******", password="******")
            user1 = Users.select().get()

            art = Articles.create_article(title="test article1",
                                          body="test",
                                          draft=True,
                                          author=user1)
            art2 = Articles.create_article(title="test article2",
                                           body="test",
                                           draft=True,
                                           author=user1)

            art.save_article_categories([u"cat1", u"cat2", u"cat3"])

            self.assertIn("cat1", str(tuple(art.get_article_categories())))
            self.assertIn("cat2", str(tuple(art.get_article_categories())))
            self.assertIn("cat3", str(tuple(art.get_article_categories())))

            art.save_article_categories(["cat1", "cat2", "cat3"])

            cat = Categories.select().count()

            self.assertEquals(3, cat)

            cat4 = Categories.create(name="cat4")
            cat5 = Categories.create(name="cat5")
            cat6 = Categories.create(name="cat6")

            art.save_article_categories(["cat4"])
            self.assertIn("cat4", str(tuple(art.get_article_categories())))
            self.assertEquals(4, art.get_article_categories().count())

            art2.save_article_categories(("cat1", "cat4", "cat7"))

            cats = Categories.select().group_by(Categories.name)

            self.assertIn("cat7", str(tuple(cats)))
            self.assertEquals(7, cats.count())

            art.delete_instance(recursive=True)

            self.assertEquals(7, cats.count())

            self.assertEquals(
                ArticleCategories.select().group_by(
                    ArticleCategories.id).count(), 4)
            cat4.delete_instance(recursive=True)

            self.assertEquals(
                ArticleCategories.select().group_by(
                    ArticleCategories.id).count(), 3)
Beispiel #6
0
    def test_utility_methods(self):

        with test_database(db, (Users,)):

            Users.create_user(username="******", password="******", real_name="real_name")

            self.assertEquals("real_name", Users.get_user_by_username("konrad").real_name)

            konrad = Users.get_user(1)

            self.assertTrue(konrad.check_password("test"))
            self.assertFalse(konrad.check_password("wrong_password"))
Beispiel #7
0
    def test_utility_methods(self):

        with test_database(db, (Users, )):

            Users.create_user(username="******",
                              password="******",
                              real_name="real_name")

            self.assertEquals("real_name",
                              Users.get_user_by_username("konrad").real_name)

            konrad = Users.get_user(1)

            self.assertTrue(konrad.check_password("test"))
            self.assertFalse(konrad.check_password("wrong_password"))
Beispiel #8
0
def about():

    user = Users.get_user(1)

    return render_template("about.html",
                           user=user,
                           portrait=settings["portrait"])
Beispiel #9
0
def about():

    user = Users.get_user(1)

    return render_template("about.html",
                           user=user,
                           portrait=settings["portrait"])
Beispiel #10
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)
Beispiel #11
0
    def test_saving_and_deleting_categories(self):

        with test_database(db, (Users, Articles, Categories, ArticleCategories)):
            Users.create_user(username = "******", password = "******")
            user1 = Users.select().get()

            art = Articles.create_article(title = "test article1", body = "test", draft = True, author = user1)
            art2 = Articles.create_article(title = "test article2", body = "test", draft = True, author = user1)


            art.save_article_categories([u"cat1", u"cat2", u"cat3"])


            self.assertIn("cat1", str(tuple(art.get_article_categories())))
            self.assertIn("cat2", str(tuple(art.get_article_categories())))
            self.assertIn("cat3", str(tuple(art.get_article_categories())))

            art.save_article_categories(["cat1", "cat2", "cat3"])

            cat = Categories.select().count()

            self.assertEquals(3, cat)

            cat4 = Categories.create(name = "cat4")
            cat5 = Categories.create(name = "cat5")
            cat6 = Categories.create(name = "cat6")

            art.save_article_categories(["cat4"])
            self.assertIn("cat4", str(tuple(art.get_article_categories())))
            self.assertEquals(4, art.get_article_categories().count())

            art2.save_article_categories(("cat1", "cat4", "cat7"))

            cats = Categories.select().group_by(Categories.name)

            self.assertIn("cat7", str(tuple(cats)))
            self.assertEquals(7, cats.count())

            art.delete_instance(recursive = True)

            self.assertEquals(7, cats.count())


            self.assertEquals(ArticleCategories.select().group_by(ArticleCategories.id).count(), 4)
            cat4.delete_instance(recursive = True)

            self.assertEquals(ArticleCategories.select().group_by(ArticleCategories.id).count(), 3)
Beispiel #12
0
    def test_creating_user(self):

        with test_database(db, (Users,)):
            Users.create_user(username="******", password="******")

            self.assertTrue(Users.get_user(1))
            self.assertTrue(Users.check_exists("konrad"))
            self.assertEquals(1, Users.select().count())

            Users.create_user(username="******", password="******", real_name="Malgosia Samosia", description="test")

            self.assertEquals(2, Users.select().count())

            self.assertRaises(IntegrityError, lambda: Users.create_user(username="******", password="******"))
Beispiel #13
0
def create_account():
    """
    View for creating user account
    - Checks if no users have been created - if yes redirect
    - Gets Credentials from the form
    - Writes data to db
    - Creates user directory in /uploads
    - Logs user in

    """

    error = None
    user_check = Users.check_any_exist()
    if not user_check:
        if request.method == "POST":
            username = request.form.get("username").strip()
            password = request.form.get("password").strip()
            real_name = request.form.get("real_name", None).strip()
            description = request.form.get("description", None).strip()
            if not username or not password:
                error = "All fields are required"
                return render_template("create_account.html", error=error)
            try:
                Users.create_user(username=username,
                                  password=password,
                                  description=description,
                                  real_name=real_name)
            except IOError as e:
                error = "Could not write to database, check if\
                        you have proper access\n or double\
                        check configuration options"

                return render_template("create_account.html", error=error)
            session["user"] = username
            flash("Account created")
            return redirect(url_for("account", username=username))
        else:
            return render_template("create_account.html")
    else:
        return redirect(url_for("index"))
Beispiel #14
0
def create_account():

    """
    View for creating user account
    - Checks if no users have been created - if yes redirect
    - Gets Credentials from the form
    - Writes data to db
    - Creates user directory in /uploads
    - Logs user in

    """

    error = None
    user_check = Users.check_any_exist()
    if not user_check:
        if request.method == "POST":
            username = request.form.get("username").strip()
            password = request.form.get("password").strip()
            real_name = request.form.get("real_name", None).strip()
            description = request.form.get("description", None).strip()
            if not username or not password:
                error = "All fields are required"
                return render_template("create_account.html", error=error)
            try:
                Users.create_user(username=username,
                                  password=password,
                                  description=description,
                                  real_name=real_name)
            except IOError as e:
                error = "Could not write to database, check if\
                        you have proper access\n or double\
                        check configuration options"
                return render_template("create_account.html", error=error)
            session["user"] = username
            flash("Account created")
            return redirect(url_for("account", username=username))
        else:
            return render_template("create_account.html")
    else:
        return redirect(url_for("index"))
Beispiel #15
0
def account(username):
    """ Main account view """
    if username is None:
        return redirect("/admin")
    user = Users.get_user_by_username(username)
    if not user:
        abort(404)
    articles = Articles.get_user_articles(user.username)
    projects = UserProjects.get_all_projects()
    return render_template("dashboard.html",
                           user=user,
                           articles=articles,
                           projects=projects)
Beispiel #16
0
    def test_test_gallery_methods(self):

        with test_database(db, (Users, UserImages)):
            Users.create_user(username = "******", password = "******")
            user1 = Users.select().get()

            UserImages.add_image(image_link = self.ADDRESS,\
                                description = "test",\
                                is_vertical = True, \
                                owner = user1,\
                                imgur_img = False,\
                                delete_hash = None)

            
            image = UserImages.select().get()
            self.assertFalse(image.gallery)
            UserImages.gallerify(image)
            self.assertTrue(image.gallery)
            UserImages.gallerify(image)
            self.assertFalse(image.gallery)

            for num in range(2, 10):
                UserImages.add_image(image_link = "test image %d" % num,\
                                    description = "test %d" % num,\
                                    owner = user1)

            self.assertEquals(9, UserImages.get_count())

            paginated = UserImages.get_gallery_images(1, 2)

            self.assertEquals(2, len(tuple(paginated)))

            self.assertIn(self.ADDRESS, str(tuple(paginated)))
            self.assertNotIn("test image 3", str(tuple(paginated)))

            paginated = UserImages.get_gallery_images(5, 2)

            self.assertIn("test image 9", str(tuple(paginated)))
            self.assertEquals(1, len(tuple(paginated)))
Beispiel #17
0
    def test_test_gallery_methods(self):

        with test_database(db, (Users, UserImages)):
            Users.create_user(username="******", password="******")
            user1 = Users.select().get()

            UserImages.add_image(image_link = self.ADDRESS,\
                                description = "test",\
                                is_vertical = True, \
                                owner = user1,\
                                imgur_img = False,\
                                delete_hash = None)

            image = UserImages.select().get()
            self.assertFalse(image.gallery)
            UserImages.gallerify(image)
            self.assertTrue(image.gallery)
            UserImages.gallerify(image)
            self.assertFalse(image.gallery)

            for num in range(2, 10):
                UserImages.add_image(image_link = "test image %d" % num,\
                                    description = "test %d" % num,\
                                    owner = user1)

            self.assertEquals(9, UserImages.get_count())

            paginated = UserImages.get_gallery_images(1, 2)

            self.assertEquals(2, len(tuple(paginated)))

            self.assertIn(self.ADDRESS, str(tuple(paginated)))
            self.assertNotIn("test image 3", str(tuple(paginated)))

            paginated = UserImages.get_gallery_images(5, 2)

            self.assertIn("test image 9", str(tuple(paginated)))
            self.assertEquals(1, len(tuple(paginated)))
Beispiel #18
0
def account(username):
    """ Main account view """
    if username is None:
        return redirect("/admin")
    user = Users.get_user_by_username(username)
    if not user:
        abort(404)
    articles = Articles.get_user_articles(user.username)
    projects = UserProjects.get_all_projects()
    return render_template("dashboard.html",
                           user=user,
                           articles=articles,
                           projects=projects
                           )
Beispiel #19
0
    def test_getting_article_info(self):

        with test_database(db, (Users, Articles)):
            user1 = Users.create_user(username="******", password="******")
            article = Articles.create_article(title = "test article 1", \
                                    body = "test",
                                    author = user1)

            self.assertEquals("test-article-1", article.slug)
            self.assertEquals(article,
                              Articles.get_article_by_slug("test-article-1"))
            article2 = Articles.create_article(title="test article 2",
                                               body="test",
                                               author=user1)
            self.assertEquals(article2, article.get_next_article(True))
            self.assertEquals(0, article.get_previous_article())
            self.assertEquals(article, article2.get_previous_article(True))
            self.assertEquals(0, article2.get_next_article())

            article3 = Articles.create_article(title = "test article 3",\
                                    body = "test",\
                                    draft = False,\
                                    author = user1)

            self.assertEquals(1, Articles.get_count(drafts=False))
            self.assertEquals(3, Articles.get_count(drafts=True))
            self.assertTrue(Articles.check_exists("test article 1"))
            self.assertFalse(Articles.check_exists("nonexistent"))

            for x in range(4, 10):
                title = "test article %d" % x
                Articles.create_article(title=title,
                                        body="test",
                                        author=user1,
                                        draft=False)

            self.assertEquals(9, Articles.get_count(True))

            paginated = Articles.get_index_articles(3, 3)

            self.assertEquals(7, paginated.wrapped_count())

            self.assertIn("test article 3", str(tuple(paginated)))
            self.assertNotIn("test article 1", str(tuple(paginated)))
            self.assertNotIn("test article 9", str(tuple(paginated)))

            paginated = Articles.get_index_articles(1, 3)

            self.assertIn("test article 9", str(tuple(paginated)))
            self.assertIn("test article 7", str(tuple(paginated)))
Beispiel #20
0
    def test_creating_and_deleting_image(self):
        
        with test_database(db, (Users, UserImages)):
            Users.create_user(username = "******", password = "******")
            user1 = Users.select().get()

            UserImages.add_image(image_link = self.ADDRESS,\
                                description = "test",\
                                is_vertical = True, \
                                owner = user1,\
                                imgur_img = False,\
                                delete_hash = None)


            image = UserImages.select().get()

            self.assertEquals(self.ADDRESS ,image.image_link)
            self.assertEquals("test", image.description)
            self.assertEquals(user1, image.owner)

            UserImages.add_image(image_link = self.ADDRESS,\
                                description = "test",\
                                is_vertical = True, \
                                owner = user1,\
                                imgur_img = False,\
                                delete_hash = None)

            self.assertEquals(2, UserImages.get_count())

            self.assertTrue(UserImages.check_exists(self.ADDRESS))
            self.assertFalse(UserImages.check_exists(self.ADDRESS + "bam"))
            image = UserImages.get_image(2)
            UserImages.delete_image(image)
            self.assertEquals(1, UserImages.get_count())
            image = UserImages.get_image(1)
            UserImages.delete_image(image)
            self.assertEquals(0, UserImages.get_count())
Beispiel #21
0
    def test_creating_user(self):

        with test_database(db, (Users, )):
            Users.create_user(username="******", password="******")

            self.assertTrue(Users.get_user(1))
            self.assertTrue(Users.check_exists("konrad"))
            self.assertEquals(1, Users.select().count())


            Users.create_user(username = "******",\
                              password = "******",\
                              real_name = "Malgosia Samosia", \
                              description = "test")

            self.assertEquals(2, Users.select().count())

            self.assertRaises(
                IntegrityError,
                lambda: Users.create_user(username="******", password="******"))
Beispiel #22
0
    def test_misc_article_methods(self):

        with test_database(db, (Users, Articles)):
            user1 = Users.create_user(username = "******", password = "******")
            article = Articles.create_article(title = "test article", body = "test", draft = True, author = user1)

            Articles.update_article(article, title = "changed", body = "changed")
            self.assertEquals("changed", article.title)
            self.assertEquals("changed", article.body)

            Articles.publish_article(article)
            self.assertFalse(article.draft)

            Articles.delete_article(article)
            self.assertEquals(0, Articles.get_count(True))
Beispiel #23
0
    def test_creating_and_deleting_image(self):

        with test_database(db, (Users, UserImages)):
            Users.create_user(username="******", password="******")
            user1 = Users.select().get()

            UserImages.add_image(image_link = self.ADDRESS,\
                                description = "test",\
                                is_vertical = True, \
                                owner = user1,\
                                imgur_img = False,\
                                delete_hash = None)

            image = UserImages.select().get()

            self.assertEquals(self.ADDRESS, image.image_link)
            self.assertEquals("test", image.description)
            self.assertEquals(user1, image.owner)

            UserImages.add_image(image_link = self.ADDRESS,\
                                description = "test",\
                                is_vertical = True, \
                                owner = user1,\
                                imgur_img = False,\
                                delete_hash = None)

            self.assertEquals(2, UserImages.get_count())

            self.assertTrue(UserImages.check_exists(self.ADDRESS))
            self.assertFalse(UserImages.check_exists(self.ADDRESS + "bam"))
            image = UserImages.get_image(2)
            UserImages.delete_image(image)
            self.assertEquals(1, UserImages.get_count())
            image = UserImages.get_image(1)
            UserImages.delete_image(image)
            self.assertEquals(0, UserImages.get_count())
Beispiel #24
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 #25
0
    def test_deleting_adn_updating_projects(self):

        with test_database(db, (Users, UserProjects)):

            user1 = Users.create_user("konrad", "test")
            UserProjects.create_project(title = "test project", body = "test", author = user1)

            project = UserProjects.get_project(1)
            UserProjects.update_project(project, "changed", "changed")
            project = UserProjects.get_project(1)
            self.assertEquals("changed", project.title)
            self.assertEquals("changed", project.body)

            UserProjects.delete_project(project)

            self.assertEquals(0, UserProjects.select().count())
Beispiel #26
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 #27
0
    def test_misc_article_methods(self):

        with test_database(db, (Users, Articles)):
            user1 = Users.create_user(username="******", password="******")
            article = Articles.create_article(title="test article",
                                              body="test",
                                              draft=True,
                                              author=user1)

            Articles.update_article(article, title="changed", body="changed")
            self.assertEquals("changed", article.title)
            self.assertEquals("changed", article.body)

            Articles.publish_article(article)
            self.assertFalse(article.draft)

            Articles.delete_article(article)
            self.assertEquals(0, Articles.get_count(True))
Beispiel #28
0
    def test_getting_article_info(self):

        with test_database(db, (Users, Articles)):
            user1 = Users.create_user(username = "******", password = "******")
            article = Articles.create_article(title = "test article 1", \
                                    body = "test",
                                    author = user1)

            self.assertEquals("test-article-1", article.slug)
            self.assertEquals(article, Articles.get_article_by_slug("test-article-1"))
            article2 = Articles.create_article(title = "test article 2", body = "test", author = user1)
            self.assertEquals(article2, article.get_next_article(True))
            self.assertEquals(0,article.get_previous_article())
            self.assertEquals(article, article2.get_previous_article(True))
            self.assertEquals(0, article2.get_next_article())

            article3 = Articles.create_article(title = "test article 3",\
                                    body = "test",\
                                    draft = False,\
                                    author = user1)

            self.assertEquals(1, Articles.get_count(drafts = False))
            self.assertEquals(3, Articles.get_count(drafts = True))
            self.assertTrue(Articles.check_exists("test article 1"))
            self.assertFalse(Articles.check_exists("nonexistent"))

            for x in range(4,10):
                title = "test article %d" % x
                Articles.create_article(title = title, body = "test", author = user1, draft = False)

            self.assertEquals(9, Articles.get_count(True))

            paginated = Articles.get_index_articles(3, 3)

            self.assertEquals(7, paginated.wrapped_count())

            self.assertIn("test article 3", str(tuple(paginated)))
            self.assertNotIn("test article 1", str(tuple(paginated)))
            self.assertNotIn("test article 9", str(tuple(paginated)))

            paginated = Articles.get_index_articles(1, 3)

            self.assertIn("test article 9", str(tuple(paginated)))
            self.assertIn("test article 7", str(tuple(paginated)))
Beispiel #29
0
    def test_creating_project(self):

        with test_database(db, (Users, UserProjects)):

            user1 = Users.create_user("konrad", "test")

            UserProjects.create_project(title = "test project", body = "test", author = user1)
            
            project = UserProjects.select().get()
            self.assertTrue(project)

            self.assertEquals("test project", project.title)
            self.assertEquals("konrad", project.author.username)

            UserProjects.create_project(title = "test project 2", body = "test", author = user1)
            self.assertTrue(UserProjects.check_exists("test project 2"))
            
            q = UserProjects.select()

            self.assertEquals(2, q.count())

            self.assertRaises(IntegrityError, lambda: UserProjects.create_project(title = "test project", body = "test", author = user1))
Beispiel #30
0
def about_edit():

    user = Users.get_user_by_username(session["user"])
    context = dict(additional_controls=False,
                   show_title=False,
                   body=user.about or "",
                   title_placeholder=None,
                   body_placeholder="Enter about page content...")

    if request.method == "POST":
        new_info = request.form.get("body").strip()

        try:
            user.about = new_info
            user.save()
            return redirect(url_for("account", username=session["user"]))
        except:
            context.update(error="Error when saving info,\
                           see error log for details")
            return render_template("scratchpad.html", **context)
    else:
        return render_template("scratchpad.html", **context)
Beispiel #31
0
def about_edit():

    user = Users.get_user_by_username(session["user"])
    context = dict(additional_controls=False,
                   show_title=False,
                   body=user.about or "",
                   title_placeholder=None,
                   body_placeholder="Enter about page content...")

    if request.method == "POST":
        new_info = request.form.get("body").strip()

        try:
            user.about = new_info
            user.save()
            return redirect(url_for("account", username=session["user"]))
        except:
            context.update(error="Error when saving info,\
                           see error log for details")
            return render_template("scratchpad.html", **context)
    else:
        return render_template("scratchpad.html", **context)
Beispiel #32
0
def index(page):
    articles_per_page = settings.get("articles_per_page")
    articles = Articles.get_index_articles(page, articles_per_page)
    count = articles.wrapped_count()
    show_pagination = count > articles_per_page
    articles_written = count > 0
    if not articles_written and page != 1:
        abort(404)
    pagination = Pagination(page, articles_per_page, count)
    user = Users.get_user(1)
    images = dict()
    images['logo'] = settings['logo']
    images['portrait'] = settings['portrait']
    images['bg'] = settings['bg']
    if not user:
        return redirect(url_for('create_account'))
    return render_template("index.html",
                           pagination=pagination,
                           articles=articles,
                           images=images,
                           articles_written=articles_written,
                           show_pagination=show_pagination,
                           user=user)
Beispiel #33
0
def index(page):
    articles_per_page = settings.get("articles_per_page")
    articles = Articles.get_index_articles(page, articles_per_page)
    count = articles.wrapped_count()
    show_pagination = count > articles_per_page
    articles_written = count > 0
    if not articles_written and page != 1:
        abort(404)
    pagination = Pagination(page, articles_per_page, count)
    user = Users.get_user(1)
    images = dict()
    images['logo'] = settings['logo']
    images['portrait'] = settings['portrait']
    images['bg'] = settings['bg']
    if not user:
        return redirect(url_for('create_account'))
    return render_template("index.html",
                           pagination=pagination,
                           articles=articles,
                           images=images,
                           articles_written=articles_written,
                           show_pagination=show_pagination,
                           user=user
                           )
Beispiel #34
0
    def test_sanity(self):

        with test_database(db, (Users,)):
            self.assertTrue(Users.table_exists())
            self.assertFalse(Users.check_any_exist())
Beispiel #35
0
def account_settings(username):
    user = Users.get_user_by_username(username)
    if not user:
        abort(404)

    return render_template("settings_panel.html", user=user)
Beispiel #36
0
    def test_getting_similar_articles(self):

        with test_database(db, (Users, Articles, Categories, ArticleCategories)):
            Users.create_user(username = "******", password = "******")
            user1 = Users.select().get()


            for x in range(1, 8):
                globals()["article" + str(x)] = Articles.create_article(title = "test article" + str(x), body = "test", draft = True, author = user1)

            for y in range(1, 5):
                globals()["category" + str(y)] = Categories.create(name = "test" + str(y))


            ArticleCategories.create(article = article1, category = category1)
            ArticleCategories.create(article = article1, category = category2)
            ArticleCategories.create(article = article1, category = category3)

            ArticleCategories.create(article = article2, category = category1)
            ArticleCategories.create(article = article2, category = category2)
            ArticleCategories.create(article = article2, category = category3)

            ArticleCategories.create(article = article3, category = category1)
            ArticleCategories.create(article = article3, category = category2)
            ArticleCategories.create(article = article3, category = category3)


            ArticleCategories.create(article = article4, category = category1)
            ArticleCategories.create(article = article4, category = category2)

            ArticleCategories.create(article = article5, category = category1)
            ArticleCategories.create(article = article5, category = category2)
            ArticleCategories.create(article = article5, category = category3)

            ArticleCategories.create(article = article6, category = category2)

            ArticleCategories.create(article = article7, category = category4)


            sel = article1.get_similar_articles()

            self.assertIn("test article2", str(tuple(sel)))
            self.assertIn("test article3", str(tuple(sel)))
            self.assertIn("test article5", str(tuple(sel)))
            self.assertNotIn("test article1", str(tuple(sel)))
            self.assertNotIn("test article4", str(tuple(sel)))


            sel = article1.get_similar_articles(limit = 4)


            self.assertIn("test article2", str(tuple(sel)))
            self.assertIn("test article3", str(tuple(sel)))
            self.assertIn("test article5", str(tuple(sel)))
            self.assertIn("test article4", str(tuple(sel)))
            self.assertNotIn("test article6", str(tuple(sel)))
            self.assertNotIn("test article1", str(tuple(sel)))


            sel = article1.get_similar_articles(common_categories = 3, limit = 5)

            self.assertIn("test article2", str(tuple(sel)))
            self.assertIn("test article3", str(tuple(sel)))
            self.assertIn("test article5", str(tuple(sel)))
            self.assertNotIn("test article1", str(tuple(sel)))
            self.assertNotIn("test article4", str(tuple(sel)))



            sel = article7.get_similar_articles()

            self.assertEquals(sel.wrapped_count(False), 0)

            # Test getting article categories while we're at it

            q = article1.get_article_categories()
            
            self.assertIn("test1",str(tuple(q)) )
            self.assertIn("test2",str(tuple(q)))
            self.assertNotIn("test4", str(tuple(q)))
            q = article4.get_article_categories()
            self.assertIn("test1",str(tuple(q)) )
            self.assertNotIn("test3", str(tuple(q)))

            q = article7.get_article_categories()
            self.assertIn("test4",str(tuple(q)) )
            self.assertNotIn("test3", str(tuple(q)))
Beispiel #37
0
def upload_image():
    error = None
    if request.method == "POST":
        description = request.form.get('description', None)
        if request.form.get("imgur-img"):
            image = request.files["image"]
            if not image:
                error = "No image chosen"
                return render_template("upload_image.html", error=error)

            user_id = get_config().imgur_id
            extension = split_filename(image.filename, True)
            if extension not in app.config["ALLOWED_FILENAMES"]:
                error = "Allowed extensions are %r"\
                        % (", ".join(app.config["ALLOWED_FILENAMES"]))
                return render_template("upload_image.html", error=error)

            filename = secure_filename(image.filename.strip())
            user = Users.get_user_by_username(session["user"])
            config = dict(
                image=image,
                name=filename,
                description=description)
            response = ImgurHandler(user_id, config).send_image()
            if not response["success"]:
                error = "Error uploading to imgur"
                return render_template("upload_image.html", error=error)

            response_data = response["data"]
            image_link = response_data["link"]
            is_vertical = response_data["width"] + 10 < response_data["height"]
            delete_hash = response_data["deletehash"]
            try:
                UserImages.add_image(image_link=image_link,
                                     description=description,
                                     delete_hash=delete_hash,
                                     is_vertical=is_vertical,
                                     imgur_img=True,
                                     owner=user)

                return redirect(url_for("user_images", username=user.username))
            except:
                error = "Error writing to database"
                return render_template("upload_image.html", error=error)

            return render_template("upload_image.html", error=response)

        elif request.form.get('save-link'):
            link = request.form.get('image-link', None)
            if not link:
                error = "No link given"
                return render_template("upload_image.html", error=error)

            user = Users.get_user_by_username(session["user"])
            try:
                UserImages.add_image(image_link=link,
                                     description=description,
                                     is_vertical=True,
                                     imgur_img=False,
                                     owner=user)
                return redirect(url_for("user_images", username=user.username))

            except Exception as e:
                error = "Error writing to database"
                return render_template("upload_image.html", error=error)
    else:
        return render_template("upload_image.html")
Beispiel #38
0
def upload_image():
    error = None
    if request.method == "POST":
        description = request.form.get('description', None)
        if request.form.get("imgur-img"):
            image = request.files["image"]
            if not image:
                error = "No image chosen"
                return render_template("upload_image.html", error=error)

            user_id = get_config().imgur_id
            extension = split_filename(image.filename, True)
            if extension not in app.config["ALLOWED_FILENAMES"]:
                error = "Allowed extensions are %r"\
                        % (", ".join(app.config["ALLOWED_FILENAMES"]))
                return render_template("upload_image.html", error=error)

            filename = secure_filename(image.filename.strip())
            user = Users.get_user_by_username(session["user"])
            config = dict(image=image, name=filename, description=description)
            response = ImgurHandler(user_id, config).send_image()
            if not response["success"]:
                error = "Error uploading to imgur"
                return render_template("upload_image.html", error=error)

            response_data = response["data"]
            image_link = response_data["link"]
            is_vertical = response_data["width"] + 10 < response_data["height"]
            delete_hash = response_data["deletehash"]
            try:
                UserImages.add_image(image_link=image_link,
                                     description=description,
                                     delete_hash=delete_hash,
                                     is_vertical=is_vertical,
                                     imgur_img=True,
                                     owner=user)

                return redirect(url_for("user_images", username=user.username))
            except:
                error = "Error writing to database"
                return render_template("upload_image.html", error=error)

            return render_template("upload_image.html", error=response)

        elif request.form.get('save-link'):
            link = request.form.get('image-link', None)
            if not link:
                error = "No link given"
                return render_template("upload_image.html", error=error)

            user = Users.get_user_by_username(session["user"])
            try:
                UserImages.add_image(image_link=link,
                                     description=description,
                                     is_vertical=True,
                                     imgur_img=False,
                                     owner=user)
                return redirect(url_for("user_images", username=user.username))

            except Exception as e:
                error = "Error writing to database"
                return render_template("upload_image.html", error=error)
    else:
        return render_template("upload_image.html")
Beispiel #39
0
def account_settings(username):
    user = Users.get_user_by_username(username)
    if not user:
        abort(404)

    return render_template("settings_panel.html", user=user)
Beispiel #40
0
    def test_sanity(self):

        with test_database(db, (Users, )):
            self.assertTrue(Users.table_exists())
            self.assertFalse(Users.check_any_exist())
Beispiel #41
0
    def test_getting_similar_articles(self):

        with test_database(db,
                           (Users, Articles, Categories, ArticleCategories)):
            Users.create_user(username="******", password="******")
            user1 = Users.select().get()

            for x in range(1, 8):
                globals()["article" + str(x)] = Articles.create_article(
                    title="test article" + str(x),
                    body="test",
                    draft=True,
                    author=user1)

            for y in range(1, 5):
                globals()["category" +
                          str(y)] = Categories.create(name="test" + str(y))

            ArticleCategories.create(article=article1, category=category1)
            ArticleCategories.create(article=article1, category=category2)
            ArticleCategories.create(article=article1, category=category3)

            ArticleCategories.create(article=article2, category=category1)
            ArticleCategories.create(article=article2, category=category2)
            ArticleCategories.create(article=article2, category=category3)

            ArticleCategories.create(article=article3, category=category1)
            ArticleCategories.create(article=article3, category=category2)
            ArticleCategories.create(article=article3, category=category3)

            ArticleCategories.create(article=article4, category=category1)
            ArticleCategories.create(article=article4, category=category2)

            ArticleCategories.create(article=article5, category=category1)
            ArticleCategories.create(article=article5, category=category2)
            ArticleCategories.create(article=article5, category=category3)

            ArticleCategories.create(article=article6, category=category2)

            ArticleCategories.create(article=article7, category=category4)

            sel = article1.get_similar_articles()

            self.assertIn("test article2", str(tuple(sel)))
            self.assertIn("test article3", str(tuple(sel)))
            self.assertIn("test article5", str(tuple(sel)))
            self.assertNotIn("test article1", str(tuple(sel)))
            self.assertNotIn("test article4", str(tuple(sel)))

            sel = article1.get_similar_articles(limit=4)

            self.assertIn("test article2", str(tuple(sel)))
            self.assertIn("test article3", str(tuple(sel)))
            self.assertIn("test article5", str(tuple(sel)))
            self.assertIn("test article4", str(tuple(sel)))
            self.assertNotIn("test article6", str(tuple(sel)))
            self.assertNotIn("test article1", str(tuple(sel)))

            sel = article1.get_similar_articles(common_categories=3, limit=5)

            self.assertIn("test article2", str(tuple(sel)))
            self.assertIn("test article3", str(tuple(sel)))
            self.assertIn("test article5", str(tuple(sel)))
            self.assertNotIn("test article1", str(tuple(sel)))
            self.assertNotIn("test article4", str(tuple(sel)))

            sel = article7.get_similar_articles()

            self.assertEquals(sel.wrapped_count(False), 0)

            # Test getting article categories while we're at it

            q = article1.get_article_categories()

            self.assertIn("test1", str(tuple(q)))
            self.assertIn("test2", str(tuple(q)))
            self.assertNotIn("test4", str(tuple(q)))
            q = article4.get_article_categories()
            self.assertIn("test1", str(tuple(q)))
            self.assertNotIn("test3", str(tuple(q)))

            q = article7.get_article_categories()
            self.assertIn("test4", str(tuple(q)))
            self.assertNotIn("test3", str(tuple(q)))