Example #1
0
 def patch(self, id):
     section = SectionDAO.find_section_by_id(id)
     if not section:
         return RESOURCE_NOT_FOUND, 404
     data = request.json
     validation_result = validate_updatable_section_data(data)
     if validation_result is not None:
         return validation_result
     if data["title"] == section.title:
         return SECTION_TITLE_NOT_UPDATED, 400
     updated_section = SectionDAO.update_section(section, **data)
     return map_to_dto(updated_section), 200
Example #2
0
 def post(self):
     data = request.json
     validation_result = validate_section_data(data)
     if validation_result is not None:
         return validation_result
     title, category_title = data["title"], data["category"]
     existing_section = SectionDAO.find_section_by_title(title)
     if existing_section is not None:
         return SECTION_ALREADY_EXISTS, 400
     try:
         section = SectionDAO.create_section(title)
         category = CategoryDAO.create_or_find_category(category_title)
         category.add_category_section(section)
     except SQLAlchemyError as e:
         return {"message": f"Data cannot be persisted. Original error: {e}"}, 500
     return map_to_dto(section), 201
Example #3
0
    def post(self):
        data = request.json
        validation_result = validate_video_creation_data(data)

        if validation_result is not None:
            return validation_result
        url = data["url"]
        existing_video = VideoDAO.find_video_by_url(url)
        response = {"notes": []}

        # find authors from the list
        authors = AuthorDAO.find_authors_by_ids(data["authors"])
        if authors.count() != len(data["authors"]):
            response["notes"].append(
                "Some of the authors are not valid or they do not exist."
            )

        # find sections from the list
        sections = SectionDAO.find_sections_by_ids(data["category_sections"])
        if sections.count() != len(data["category_sections"]):
            response["notes"].append(
                "Some of the sections are not valid or they do not exist."
            )

        # remove association fields
        data.pop("authors")
        data.pop("category_sections")

        if existing_video:
            data["date_published"] = datetime.strptime(
                data["date_published"], "%Y-%m-%d"
            )
            existing_video.update(data)
            VideoDAO.replace_video_authors(existing_video, authors)
            VideoDAO.replace_video_sections(existing_video, sections)
            video = existing_video
        else:
            video = VideoDAO.create_video(
                data["title"],
                data["url"],
                data["preview_url"],
                datetime.strptime(data["date_published"], "%Y-%m-%d"),
                data["source"],
                data["channel"],
                data["duration"],
                data.get("archived"),
                data.get("free_to_reuse"),
                data.get("authorized_to_reuse"),
            )

            VideoDAO.add_video_authors(video, authors)
            VideoDAO.add_video_sections(video, sections)

        response["video"] = map_to_dto(video)
        return response, 200
Example #4
0
 def post(self, id):
     video = VideoDAO.find_video_by_id(id)
     if not video:
         return RESOURCE_NOT_FOUND, 404
     data = request.json
     validation_result = validate_video_sections_data(data)
     if validation_result:
         return validation_result, 400
     section_ids = data["sections"]
     sections = SectionDAO.find_sections_by_ids(section_ids)
     note = ""
     if sections.count() != len(section_ids):
         note = "Not all sections are valid/existing!"
     all_sections_added = VideoDAO.add_video_sections(video, sections)
     if not all_sections_added:
         note += " Duplicate video sections detected and they were not added!"
     response = {"message": "Video sections added successfully."}, 201
     if note:
         response = {"message": note}, 201
     return response
Example #5
0
 def get(self, id):
     category = CategoryDAO.find_category_by_id(id)
     if not category:
         return RESOURCE_NOT_FOUND, 404
     sections = SectionDAO.find_sections_by_category(category)
     return map_to_dto_list(sections), 200
Example #6
0
 def delete(self, id):
     result = SectionDAO.delete_section_by_id(id)
     if not result:
         return RESOURCE_NOT_FOUND, 404
     return SECTION_SUCCESSFULLY_DELETED, 200
Example #7
0
 def get(self):
     sections = SectionDAO.find_all_sections()
     result = list(map(lambda section: map_to_dto(section), sections))
     return {"sections": result}, 200
Example #8
0
    def post(self):
        payload = request.json
        video_url = payload["url"]
        video_id = extract_video_id(video_url)
        response = requests.get(
            f'https://youtube.googleapis.com/youtube/v3/videos?part=snippet%2CcontentDetails%2Cid%2Clocalizations%2Cstatistics%2CtopicDetails&id={video_id}&key={os.environ.get("API_KEY")}'
        )
        video_json = response.json()
        response = {"notes": []}

        if len(video_json["items"]) == 0:
            response["notes"].append("No video found, please check the url.")
            return response, 404
        else:
            video = video_json["items"][0]

        video_data = {
            "title": video["snippet"]["title"],
            "url": video_url,
            "preview_url": video["snippet"]["thumbnails"]["standard"]["url"],
            "date_published": video["snippet"]["publishedAt"].split("T")[0],
            "source": "YouTube",
            "channel": video["snippet"]["channelTitle"],
            "duration": video["contentDetails"]["duration"].split("T")[1],
            "archived": False,
            "free_to_reuse": video["contentDetails"]["licensedContent"],
            "authorized_to_reuse": video["contentDetails"]["licensedContent"],
            "category_sections": payload["sections"][0]["id"],
            "authors": list(payload["authors"]),
        }

        validation_result = validate_video_creation_data(video_data)

        if validation_result is not None:
            return validation_result
        url = video_data["url"]
        existing_video = VideoDAO.find_video_by_url(url)

        # find authors from the list
        authors = AuthorDAO.find_authors_by_ids(video_data["authors"])
        if authors.count() != len(video_data["authors"]):
            response["notes"].append(
                "Some of the authors are not valid or they do not exist.")

        # find sections from the list
        sections = SectionDAO.find_sections_by_ids(
            video_data["category_sections"])
        if sections.count() != len(video_data["category_sections"]):
            response["notes"].append(
                "Some of the sections are not valid or they do not exist.")

        # remove association fields
        video_data.pop("authors")
        video_data.pop("category_sections")

        if existing_video:
            video_data["date_published"] = datetime.strptime(
                video_data["date_published"], "%Y-%m-%d")
            existing_video.update(video_data)
            VideoDAO.replace_video_authors(existing_video, authors)
            VideoDAO.replace_video_sections(existing_video, sections)
            video = existing_video
        else:
            video = VideoDAO.create_video(
                video_data["title"],
                video_data["url"],
                video_data["preview_url"],
                datetime.strptime(video_data["date_published"], "%Y-%m-%d"),
                video_data["source"],
                video_data["channel"],
                video_data["duration"],
                video_data.get("archived"),
                video_data.get("free_to_reuse"),
                video_data.get("authorized_to_reuse"),
            )

            VideoDAO.add_video_authors(video, authors)
            VideoDAO.add_video_sections(video, sections)

        response["video"] = map_to_dto(video)
        return response, 200