def test_get_by_id(self): art = Article("On the nature of foo") art.save() retrieved = Article.get_by_id(art.id) assert retrieved == art
def test_created_at_defaults_to_datetime(self): art = Article("On the nature of foo") art.save() assert bool(art.created_at) assert isinstance(art.created_at, dt.datetime)
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)