def test_add_video(self): topic = Topic(name="Python", description="", abbreviation="py") channel = Channel(name="test", url="", description="", topics=[topic], source="test") video = Video( name= "Stone Age To Serverless or: How I Learned To Stop Worrying And Love The Platform", url="https://www.youtube.com/watch?v=cU-TGiWK-dc", description= "Due to a last-minute speaker dropout, Mark will be improvising on a theme", topics=[topic], channel=[channel], source="youtube", ) db.session.add(video) db.session.commit() self.assertEqual( video.name, "Stone Age To Serverless or: How I Learned To Stop Worrying And Love The Platform", ) self.assertEqual(video.url, "https://www.youtube.com/watch?v=cU-TGiWK-dc") self.assertEqual(video.channel, [channel]) self.assertEqual(video.source, "youtube")
def add_video(): post_data = request.get_json() response_object = {"status": "fail", "message": "Invalid payload."} if not post_data: return jsonify(response_object), 400 name = post_data.get("name") url = post_data.get("url") description = post_data.get("description") topics = post_data.get("topics") channel = post_data.get("channel") source = post_data.get("source") created = post_data.get("created") topic_list = extract_topics(topics) video_channels = [] channel = Channel( name=channel, url="http://youtube.com", description="", topics=topic_list, source=source, ) video_channels.append(channel) try: video = Video.query.filter_by(name=name).first() if not video: video = Video( name=name, url=url, description=description, topics=topic_list, channel=video_channels, source=source, created=created, ) db.session.add(video) db.session.commit() response_object["status"] = "success" response_object["message"] = f"{name} was added!" return jsonify(response_object), 201 else: response_object["message"] = "Sorry. That id already exists." return jsonify(response_object), 202 except (exc.IntegrityError, ValueError): db.session.rollback() return jsonify(response_object), 400 except exc.IntegrityError: db.session.rollback() return jsonify(response_object), 400
def add_video(name, url, description, topics, channel, source): video = Video( name=name, url=url, description=description, topics=topics, channel=channel, source=source, ) db.session.add(video) db.session.commit() return video
def index(): if request.method == "POST": name = request.form["name"] url = request.form["url"] description = request.form["description"] topics = request.form["topics"] channel = request.form["channel"] source = request.form["source"] topic_list = extract_topics(topics) video_channels = [] channel = Channel( name=channel, url="http://youtube.com", description="", topics=topic_list, source=source, ) video_channels.append(channel) video = Video( name=name, url=url, description=description, topics=topic_list, channel=video_channels, source=source, ) db.session.add(video) db.session.commit() videos = Video.query.all() response_object = { "status": "success", "data": { "videos": [video.to_dict() for video in videos] }, } return jsonify(response_object), 200