Пример #1
0
    def test_title_is_already_in_use(self, article, db):
        another = ArticleFactory.create(title="Now for Something Similar",
                                        subject_tags=[TagFactory(),
                                                      TagFactory()],
                                        authors=[UserFactory(),
                                                 UserFactory()])
        db.session.add(another)
        db.session.commit()

        # Oh noes! Titles are too similar!
        form = ArticleForm(
            title="Now for Something Similar",
            body=article.body,
            category=article.category,
            post="Post"
        )

        form.authors.choices = [(u.id, u.username) for u in User.get_all()]
        form.subject_tags.choices = [(t.id, t.name) for t in Tag.get_all()]
        form.category.choices = [(article.category, article.category)]

        form.authors.data = [auth.id for auth in article.authors]
        form.subject_tags.data = [tag.id for tag in article.subject_tags]

        assert form.validate() is False
        assert "Title already in use" in form.title.errors
Пример #2
0
    def test_validate_success(self, article, db):
        form = ArticleForm(
            title="Now for Something Completely Different",
            body=article.body,
            category=article.category,
            post="Post"
        )
        form.authors.choices = [(u.id, u.username) for u in User.get_all()]
        form.subject_tags.choices = [(t.id, t.name) for t in Tag.get_all()]
        form.category.choices = [(article.category, article.category)]

        form.authors.data = [auth.id for auth in article.authors]
        form.subject_tags.data = [tag.id for tag in article.subject_tags]

        assert form.validate() is True
Пример #3
0
 def post(self, artid=NEW_ARTICLE):
     try:
         self.prep_view_objects(artid)
     except Badself.articleIDException as baide:
         flash(baide.message)
         return redirect(url_for('media.view_article_db')) 
     
     if self.form.validate_on_submit():
         try:
             if self.article:
                 # Edit an self.article
                 self.article.authors = User.query.filter(
                     User.id.in_(self.form.data["authors"])
                 ).all()
                 self.article.title = self.form.title.data
                 self.article.body = self.form.body.data
                 self.article.is_visible = self.form.is_visible.data
                 self.article.category = self.form.category.data
                 
                 # Parse subject tag string into multiple tag object
                 tagobjs = []
                 for arttag in self.form.data["subject_tags"].split(","):
                     tagobj = Tag.query.filter_by(name=arttag).first()
                     if tagobj:
                         tagobjs.append(tagobj)
                     else:
                         tagobjs.append(Tag.create(name=arttag))
                 self.article.subject_tags = tagobjs        
                         
                 db.session.merge(self.article)
             else: 
                 # Create a new self.article
                 self.article = Article.create(
                     title=self.form.title.data,
                     body=self.form.body.data,
                     publish=self.form.is_visible.data,
                     category=self.form.category.data
                 )
                 
                 # Prepare author and subject tags for submission
                 aids = [int(x) for x in self.form.authors.data]
                 for aid in User.query.filter(User.id.in_(aids)):
                     self.article.authors.append(aid)
                 for tagstr in self.form.subject_tags.data.split(","):
                     # check to see if each tag is in DB,
                     # if not add as a new Tag object
                     taginDB = Tag.query.filter_by(name=tagstr).first()
                     if taginDB:
                         self.article.subject_tags.append(taginDB)  
                     else:
                         self.article.subject_tags.append(Tag(name=tagstr)) 
                 db.session.add(self.article)
             db.session.commit()
             flash("Article submitted!", "success")
         except InvalidRequestError as ire:
             db.session.rollback()
             db.session.flush()
             flash("Database error encountered. Article was not saved.",
                   "failure")
         return redirect(url_for('media.view_article_db'))
     else:
         flash_errors(self.form)