Exemplo n.º 1
0
    def post(self, request, filename):
        lang_code = request.data["langCode"] if request.data[
            "langCode"] else None
        lang_name = request.data["langName"] if request.data[
            "langName"] else None
        file = request.data["file"] if request.data["file"] else None

        response = {"success": False}

        if lang_code == 'en':
            response["error"] = "not_allowed"
            return Response(response, status=400)

        if lang_code is not None and file is not None:
            fs = FileSystemStorage()
            uuid_name = str(time.time()) + str(uuid.uuid4())
            uploaded_file = fs.save("tmp/" + uuid_name, file)
            uploaded_file_url = fs.url(uploaded_file)

            try:
                with open(uploaded_file_url) as json_file:
                    translation = json.load(json_file)
                    localization = FileUtility.save_localization_file(
                        lang_code, lang_name, translation)

                    response["success"] = True
                    response["localization"] = localization
                    return Response(response, status=200)
            except Exception as e:
                logger.error("Error: ", str(e))
                response["error"] = str(e)
        else:
            response["error"] = "invalid_params"

        return Response(response, status=400)
Exemplo n.º 2
0
    def get(self, request):
        lang = request.query_params.get("lang")
        if lang is None:
            lang = "en"
        json_data = FileUtility.open_localization_file(lang)

        Response(json_data, status=200)
Exemplo n.º 3
0
    def list(self, request, *args, **kwargs):
        id = self.request.query_params.get('id')
        file_format = self.request.query_params.get('file_format')
        chapters = self.request.query_params.getlist('chapters[]')

        if id is None:
            id = kwargs.get("pk", None)

        if len(chapters) > 0:
            takes = Take.objects.filter(chunk__chapter__in=chapters)
        else:
            takes = Take.objects.filter(chunk__chapter__project=id)

        if len(takes) > 0:
            language_slug = takes[0].chunk.chapter.project.language.slug
            language_name = takes[0].chunk.chapter.project.language.name
            book_slug = takes[0].chunk.chapter.project.book.slug
            book_name = takes[0].chunk.chapter.project.book.name
            version_slug = takes[0].chunk.chapter.project.version.slug
            project = {
                "lang_slug": language_slug,
                "lang_name": language_name,
                "book_slug": book_slug,
                "book_name": book_name,
                "ver_slug": version_slug
            }

            zip_it = Download(ArchiveIt(), AudioUtility(), FileUtility())

            root_dir = zip_it.file_utility.root_dir(['media', 'export'])
            take_location_list = []
            take_names_list = []
            for take in takes:
                file_name = zip_it.file_utility.file_name(take.location)
                if file_name in take_names_list:
                    continue
                take_names_list.append(file_name)
                location = {
                    "type":
                    "take",
                    "fname":
                    file_name,
                    "src":
                    take.location,
                    "dst":
                    zip_it.file_utility.create_path(
                        root_dir, language_slug, version_slug, book_slug,
                        str(take.chunk.chapter).zfill(2))
                }
                take_location_list.append(location)

            task_id = zip_it.export(project, take_location_list, root_dir,
                                    file_format, request.user)
            return Response({
                "response": "processing",
                "task_id": task_id
            },
                            status=202)
        else:
            return Response({"response": "no_takes_in_project"}, status=200)
Exemplo n.º 4
0
    def list(self, request, *args, **kwargs):
        id = self.request.query_params.get('id')
        file_format = self.request.query_params.get('file_format')
        chapters = self.request.query_params.getlist('chapters[]')

        if id is None:
            id = kwargs.get("pk", None)

        if len(chapters) > 0:
            takes = Take.objects.filter(chunk__chapter__in=chapters) \
                .order_by('chunk__chapter__number', 'chunk__startv')
        else:
            takes = Take.objects.filter(chunk__chapter__project=id) \
                .order_by('chunk__chapter__number', 'chunk__startv')

        if len(takes) > 0:
            mode_type = "MULTI" if takes[
                0].chunk.chapter.project.mode.unit == 1 else "SINGLE"

            project = {
                "lang_slug": takes[0].chunk.chapter.project.language.slug,
                "lang_name": takes[0].chunk.chapter.project.language.name,
                "book_slug": takes[0].chunk.chapter.project.book.slug,
                "book_name": takes[0].chunk.chapter.project.book.name,
                "book_number": takes[0].chunk.chapter.project.book.number,
                "version_slug": takes[0].chunk.chapter.project.version.slug,
                "version_name": takes[0].chunk.chapter.project.version.name,
                "anthology_slug":
                takes[0].chunk.chapter.project.anthology.slug,
                "anthology_name":
                takes[0].chunk.chapter.project.anthology.name,
                "mode_slug": takes[0].chunk.chapter.project.mode.slug,
                "mode_name": takes[0].chunk.chapter.project.mode.name,
                "mode_type": mode_type
            }

            zip_it = Download(ArchiveIt(), AudioUtility(), FileUtility())

            task_id = zip_it.download(project, takes, file_format,
                                      request.user)
            return Response({
                "response": "processing",
                "task_id": task_id
            },
                            status=202)
        else:
            return Response({"response": "no_takes_in_project"}, status=200)
Exemplo n.º 5
0
    def post(self, request, filename):
        """ Normal upload """
        if request.data["file"]:
            arch_project = ZipIt()
            file_to_upload = request.data["file"]

            fs = FileSystemStorage()
            filename_to_upload = fs.save("tmp/" + file_to_upload.name,
                                         file_to_upload)
            uploaded_file_url = fs.url(filename_to_upload)

            if filename == "tr":
                arch_project = TrIt()
            up = Upload(arch_project, None, FileUtility())

            user_data = {}
            file_name = "unknown_file_name"

            if "HTTP_TR_FILE_NAME" in request.META:
                file_name = request.META["HTTP_TR_FILE_NAME"]

            if request.user.is_anonymous:
                user_hash = "unknown_user_hash"

                if "HTTP_TR_USER_HASH" in request.META:
                    user_hash = request.META["HTTP_TR_USER_HASH"]

                user = User.objects.filter(icon_hash=user_hash).first()
                if user:
                    user_data["icon_hash"] = user.icon_hash
                    user_data["name_audio"] = user.name_audio
                else:
                    user_data["icon_hash"] = user_hash
                    user_data["name_audio"] = None
            else:
                user_data["icon_hash"] = request.user.icon_hash
                user_data["name_audio"] = request.user.name_audio

            task_id = up.upload(uploaded_file_url, user_data, file_name)
            return Response({
                "response": "processing",
                "task_id": task_id
            },
                            status=202)
        else:
            return Response({"response": "no file"}, status=200)
Exemplo n.º 6
0
    def list(self, request, *args, **kwargs):
        id = self.request.query_params.get('id')
        if id is None:
            id = kwargs.get("pk", None)
        takes = Take.objects.filter(chunk__chapter__project=id, published=True)
        if takes.count() > 0:
            language_slug = takes[0].chunk.chapter.project.language.slug
            book_slug = takes[0].chunk.chapter.project.book.slug
            version_slug = takes[0].chunk.chapter.project.version.slug
            tr_it = Download(ArchiveIt(), AudioUtility(), FileUtility())
            root_folder = tr_it.file_utility.root_dir(['media', 'tmp'])
            filename = language_slug + "_" + version_slug + "_" + book_slug

            tr_it.file_utility.create_folder_path(root_folder, language_slug,
                                                  version_slug, book_slug)
            try:
                for take in takes:
                    chapter_folder = tr_it.file_utility.create_chapter_path(
                        root_folder, language_slug, version_slug, book_slug,
                        str(take.chunk.chapter).zfill(2))
                    file_path = tr_it.file_utility.copy_files(
                        take.location, chapter_folder)
                    if file_path.endswith('.wav'):
                        file_path_mp3 = file_path.replace('.wav', '.mp3')
                        meta = {
                            "anthology":
                            take.chunk.chapter.project.anthology.slug,
                            "language":
                            take.chunk.chapter.project.language.slug,
                            "version":
                            take.chunk.chapter.project.version.slug,
                            "slug":
                            take.chunk.chapter.project.book.slug,
                            "book_number":
                            str(take.chunk.chapter.project.book.number).zfill(
                                2),
                            "mode":
                            take.chunk.chapter.project.mode.slug,
                            "chapter":
                            str(take.chunk.chapter).zfill(2),
                            "startv":
                            take.chunk.startv,
                            "endv":
                            take.chunk.endv,
                            "markers":
                            take.markers
                        }
                        tr_it.audio_utility.write_meta(file_path,
                                                       file_path_mp3, meta)
                        tr_it.file_utility.remove_file(file_path)
                tr_it.file_utility.compile_into_tr(root_folder)
                filename = tr_it.file_utility.create_tr_path(
                    'media', 'tmp', filename)
                tr_it.file_utility.rename(root_folder + ".tr", filename)
                tr_it.file_utility.remove_dir(root_folder)
            except Exception as e:
                return Response({"error": str(e)}, status=400)

            return Response(
                {"location": tr_it.file_utility.relative_path(filename)},
                status=200)

        return Response(status=400)