コード例 #1
0
ファイル: wikiidevice.py プロジェクト: TUM-MZ/creyoco
    def store_images(self, page):
        #for storing files
        local_path = Path.joinpath(settings.MEDIA_ROOT,
                                   settings.WIKI_CACHE_DIR)  # creyoco/exedjango/exeapp_media/wiki_cache_images

        #for url
        global_path = Path.joinpath(settings.MEDIA_URL, settings.WIKI_CACHE_DIR)
        #create directory structure
        if not os.path.isdir(local_path):
            os.makedirs(local_path)

        soup = BeautifulSoup(page.html())
        #save file and update link in content
        for img in soup.findAll('img'):
            image = "http:" + img['src']
            filename = img['src'].split('/')[-1]
            filename = slugify(Path._get_namebase(filename)) + Path._get_ext(filename)  #sanitizing filename
            file_path = Path.joinpath(local_path, Path(filename))
            urllib.request.urlretrieve(image, file_path)
            img['src'] = Path.joinpath(global_path, Path.basename(filename))
        #update image hyperlink to wiki for bigger version image
        for image_link in soup.findAll("a", {"class": "image"}):
            image_link['href'] = "http://wikipedia.org" + image_link['href']

        for link in soup.findAll("a"):
            if link['href'].startswith("/wiki"):
                link['href'] = "http://wikipedia.org" + link['href']
                link['target'] = "_blank"
            elif link['href'].startswith("//"):
                link['href'] = "http:" + link['href']
                link['target'] = "_blank"
        page = soup.prettify()
        return page
コード例 #2
0
ファイル: package.py プロジェクト: TUM-MZ/creyoco
    def import_package(self, filename, user):
        package = None
        try:
            zipped_file = zipfile.ZipFile(filename, "r")
        except zipfile.BadZipFile:
            log.error("File %s is not a zip file" % filename)
            return False

        temp_dir = tempfile.mkdtemp()
        try:
            for file_in_zip in zipped_file.namelist():
                if file_in_zip.endswith(".json"):
                    zipped_file.extractall(path=temp_dir)
                    with open(Path.joinpath(Path(temp_dir), Path(file_in_zip))) as json_file:
                        json_data = json.load(json_file)
                        package = Package(title=json_data['title'], user=user)
                        self._copy_resources_from_zip(json_data['files'], temp_dir, package)
                        dublincore = DublinCore.objects.create()
                        package.dublincore = dublincore
                        dublincore.save()
                        package.save()
                        order = PackageOrder.objects.filter(user=user).aggregate(Max('sort_order'))['sort_order__max']
                        package_order = PackageOrder(package=package, user=user, sort_order=order)
                        package_order.save()
                        Node.objects.import_node(json_data['nodes'][0], package, None)

        finally:
            Path.rmtree(temp_dir)
        if package:
            return True
        else:
            return False
コード例 #3
0
    def renderPreview(self):
        """
        Returns an XHTML string for previewing this block
        """
        if self.idevice.page_list:
            print("\n\n######################\n")
            print("render preview ")
            filename = Path.joinpath(Path(settings.MEDIA_ROOT),
                                     Path.relpath(self.idevice.pdf_file.path))
            modified_filename = Path._get_namebase(
                filename) + "-modified-" + str(
                    self.idevice.id) + Path._get_ext(filename)
            pages = pages_from_range(self.idevice.page_list)

            output = PdfFileWriter()
            input = PdfFileReader(open(filename, "rb"))
            for page in pages:
                output.addPage(input.getPage(page - 1))
            with open(modified_filename, 'wb') as output_pdf:
                output.write(output_pdf)
            self.idevice.modified_pdf_file = Path(settings.MEDIA_URL).joinpath(
                Path(settings.MEDIA_ROOT).relpathto(Path(modified_filename)))

            print("\n\n######################\n")

        template = self.COMMON_PREVIEW if self.use_common_content else \
            self.preview_template
        return self._render_view(template)
コード例 #4
0
ファイル: freetextidevice.py プロジェクト: TUM-MZ/creyoco
 def from_dict(self, dic):
     print(dic)
     self.edit = dic['edit']
     soup = BeautifulSoup(dic['content'])
     images = soup.findAll("img")
     for img in images:
         img['src'] = Path.joinpath(self.parent_node.package.user.profile.media_url, img['src'])
     self.content = soup.prettify()
     self.date_created = datetime.now()
     self.save()
     FreeTextVersion.objects.create(idevice=self, content=self.content, date_created=self.date_created)
     return self
コード例 #5
0
ファイル: package.py プロジェクト: TUM-MZ/creyoco
    def _copy_resources_from_zip(self, list, dir, pack):
        wiki_dir = Path(os.path.join(settings.MEDIA_ROOT, settings.WIKI_CACHE_DIR))
        nonwiki_dir = Path(pack.user.profile.media_path)
        for f in list:
            f = Path(f).name
            f = Path(os.path.join(dir, f))
            if f.name.startswith("wiki__"):
                f2 = Path.joinpath(wiki_dir, Path(f.name))
                if Path.exists(wiki_dir) is False:
                    wiki_dir.mkdir()
                if Path.exists(f2) is False:
                    Path.copyfile(f, f2)

            else:
                f2 = Path.joinpath(nonwiki_dir, Path(f.name))
                if Path.exists(nonwiki_dir) is False:
                    nonwiki_dir.mkdir()
                if Path.exists(f2) is False:
                    try:
                        Path.copyfile(f, f2)
                    except FileNotFoundError:
                        # some icons may be missing
                        pass
コード例 #6
0
    def _copy_resources_from_zip(self, list, dir, pack):
        wiki_dir = Path(
            os.path.join(settings.MEDIA_ROOT, settings.WIKI_CACHE_DIR))
        nonwiki_dir = Path(pack.user.profile.media_path)
        for f in list:
            f = Path(f).name
            f = Path(os.path.join(dir, f))
            if f.name.startswith("wiki__"):
                f2 = Path.joinpath(wiki_dir, Path(f.name))
                if Path.exists(wiki_dir) is False:
                    wiki_dir.mkdir()
                if Path.exists(f2) is False:
                    Path.copyfile(f, f2)

            else:
                f2 = Path.joinpath(nonwiki_dir, Path(f.name))
                if Path.exists(nonwiki_dir) is False:
                    nonwiki_dir.mkdir()
                if Path.exists(f2) is False:
                    try:
                        Path.copyfile(f, f2)
                    except FileNotFoundError:
                        # some icons may be missing
                        pass
コード例 #7
0
 def from_dict(self, dic):
     print(dic)
     self.edit = dic['edit']
     soup = BeautifulSoup(dic['content'])
     images = soup.findAll("img")
     for img in images:
         img['src'] = Path.joinpath(
             self.parent_node.package.user.profile.media_url, img['src'])
     self.content = soup.prettify()
     self.date_created = datetime.now()
     self.save()
     FreeTextVersion.objects.create(idevice=self,
                                    content=self.content,
                                    date_created=self.date_created)
     return self
コード例 #8
0
    def import_package(self, filename, user):
        package = None
        try:
            zipped_file = zipfile.ZipFile(filename, "r")
        except zipfile.BadZipFile:
            log.error("File %s is not a zip file" % filename)
            return False

        temp_dir = tempfile.mkdtemp()
        try:
            for file_in_zip in zipped_file.namelist():
                if file_in_zip.endswith(".json"):
                    zipped_file.extractall(path=temp_dir)
                    with open(Path.joinpath(Path(temp_dir),
                                            Path(file_in_zip))) as json_file:
                        json_data = json.load(json_file)
                        package = Package(title=json_data['title'], user=user)
                        self._copy_resources_from_zip(json_data['files'],
                                                      temp_dir, package)
                        dublincore = DublinCore.objects.create()
                        package.dublincore = dublincore
                        dublincore.save()
                        package.save()
                        order = PackageOrder.objects.filter(
                            user=user).aggregate(
                                Max('sort_order'))['sort_order__max']
                        package_order = PackageOrder(package=package,
                                                     user=user,
                                                     sort_order=order)
                        package_order.save()
                        Node.objects.import_node(json_data['nodes'][0],
                                                 package, None)

        finally:
            Path.rmtree(temp_dir)
        if package:
            return True
        else:
            return False
コード例 #9
0
    def renderPreview(self):
        """
        Returns an XHTML string for previewing this block
        """
        if self.idevice.page_list:
            print("\n\n######################\n")
            print("render preview ")
            filename = Path.joinpath(Path(settings.MEDIA_ROOT),Path.relpath(self.idevice.pdf_file.path))
            modified_filename = Path._get_namebase(filename) + "-modified-"+ str(self.idevice.id)+ Path._get_ext(filename)
            pages = pages_from_range(self.idevice.page_list)

            output = PdfFileWriter()
            input = PdfFileReader(open(filename, "rb"))
            for page in pages:
                output.addPage(input.getPage(page-1))
            with open(modified_filename,'wb') as output_pdf:
                output.write(output_pdf)
            self.idevice.modified_pdf_file = Path(settings.MEDIA_URL).joinpath(Path(settings.MEDIA_ROOT).relpathto(Path(modified_filename)))

            print("\n\n######################\n")

        template = self.COMMON_PREVIEW if self.use_common_content else \
            self.preview_template
        return self._render_view(template)
コード例 #10
0
def generateTinyMCEmath(request, package, tinyMCEwin, tinyMCEwin_name, \
                         tinyMCEfield, latex_source, math_fontsize, \
                         preview_image_filename, preview_math_srcfile):

    """
    Based off of handleTinyMCEimageChoice(),
    handleTinyMCEmath() is similar in that it places a .gif math image
    (and a corresponding .tex LaTeX source file) into the previews dir.
    Rather than copying the image from a user-selected directory, though,
    this routine actually generates the math image using mimetex.
    """
    server_filename = ""
    callback_errors = ""
    errors = 0

    webDir = Path(G.application.tempWebDir)
    previewDir = webDir.joinpath('previews')

    if not previewDir.exists():
        log.debug("image previews directory does not yet exist; " \
                + "creating as %s " % previewDir)
        previewDir.makedirs()
    elif not previewDir.isdir():
        client.alert(\
            _('Preview directory %s is a file, cannot replace it') \
            % previewDir)
        log.error("Couldn't preview tinyMCE-chosen image: " +
                  "Preview dir %s is a file, cannot replace it" \
                  % previewDir)
        callback_errors = "Preview dir is a file, cannot replace"
        errors += 1

    # if errors == 0:
    #    localImagePath = Path(local_filename)
    #    if not localImagePath.exists() or not localImagePath.isfile():
    #        client.alert( \
    #             _(u'Image file %s is not found, cannot preview it') \
    #             % localImagePath)
    #        log.error("Couldn't find tinyMCE-chosen image: %s" \
    #                % localImagePath)
    #        callback_errors = "Image file %s not found, cannot preview" \
    #                % localImagePath
    #        errors += 1

    # the mimetex usage code was swiped from the Math iDevice:
    if latex_source != "":

        # first write the latex_source out into the preview_math_srcfile,
        # such that it can then be passed into the compile command:
        math_filename = previewDir.joinpath(preview_math_srcfile)
        math_filename_str = math_filename.abspath().encode('utf-8')
        log.info("handleTinyMCEmath: using LaTeX source: " + latex_source)
        log.debug("writing LaTeX source into \'" \
                + math_filename_str + "\'.")
        math_file = open(math_filename, 'wb')
        # do we need to append a \n here?:
        math_file.write(latex_source)
        math_file.flush()
        math_file.close()


        try:
            use_latex_sourcefile = math_filename_str
            tempFileName = compile(use_latex_sourcefile, math_fontsize, \
                    latex_is_file=True)
        except Exception as e:
            client.alert(_('MimeTeX compile failed!\n%s' % str(e)))
            log.error("handleTinyMCEmath unable to compile LaTeX using "\
                + "mimetex, error = " + str(e))
            raise

        # copy the file into previews
        server_filename = previewDir.joinpath(preview_image_filename);
        log.debug("handleTinyMCEmath copying math image from \'"\
                + tempFileName + "\' to \'" \
                + server_filename.abspath().encode('utf-8') + "\'.");
        shutil.copyfile(tempFileName, \
                server_filename.abspath().encode('utf-8'));

        # Delete the temp file made by compile
        Path(tempFileName).remove()
    return
コード例 #11
0
def previewTinyMCEimage(request, package, tinyMCEwin, tinyMCEwin_name, \
                         tinyMCEfield, local_filename, preview_filename):

    """
    Once an image is selected in the file browser that is spawned by the
    TinyMCE image dialog, copy this file (which is local to the user's
    machine) into the server space, under a preview directory
    (after checking if this exists, and creating it if necessary).
    Note that this IS a "cheat", in violation of the client-server
    separation, but can be done since we know that the eXe server is
    actually sitting on the client host.
    """
    server_filename = ""
    callback_errors = ""
    errors = 0

    log.debug('handleTinyMCEimageChoice: image local = ' + local_filename
            + ', base=' + os.path.basename(local_filename))

    webDir = Path(G.application.tempWebDir)
    previewDir = webDir.joinpath('previews')

    if not previewDir.exists():
        log.debug("image previews directory does not yet exist; " \
                + "creating as %s " % previewDir)
        previewDir.makedirs()
    elif not previewDir.isdir():
        client.alert(\
            _('Preview directory %s is a file, cannot replace it') \
            % previewDir)
        log.error("Couldn't preview tinyMCE-chosen image: " +
                  "Preview dir %s is a file, cannot replace it" \
                  % previewDir)
        callback_errors = "Preview dir is a file, cannot replace"
        errors += 1

    if errors == 0:
        log.debug('handleTinyMCEimageChoice: originally, local_filename='
                + local_filename)
        local_filename = str(local_filename, 'utf-8')
        log.debug('handleTinyMCEimageChoice: in unicode, local_filename='
                + local_filename)

        localImagePath = Path(local_filename)
        log.debug('handleTinyMCEimageChoice: after Path, localImagePath= '
                + localImagePath);
        if not localImagePath.exists() or not localImagePath.isfile():
            client.alert(\
                 _('Local file %s is not found, cannot preview it') \
                 % localImagePath)
            log.error("Couldn't find tinyMCE-chosen image: %s" \
                    % localImagePath)
            callback_errors = "Image file %s not found, cannot preview" \
                    % localImagePath
            errors += 1

    try:
        # joinpath needs its join arguments to already be in Unicode:
        # preview_filename = toUnicode(preview_filename);
        # but that's okay, cuz preview_filename is now URI safe, right?
        log.debug('URIencoded preview filename=' + preview_filename);

        server_filename = previewDir.joinpath(preview_filename);
        log.debug("handleTinyMCEimageChoice copying image from \'"\
                + local_filename + "\' to \'" \
                + server_filename.abspath() + "\'.");
        shutil.copyfile(local_filename, \
                server_filename.abspath());

        # new optional description file to provide the
        # actual base filename, such that once it is later processed
        # copied into the resources directory, it can be done with
        # only the basename.   Otherwise the resource filenames
        # are too long for some users, preventing them from making
        # backup CDs of the content, for example.
        #
        # Remember that the full path of the
        # file is only used here as an easy way to keep the names
        # unique WITHOUT requiring a roundtrip call from the Javascript
        # to this server, and back again, a process which does not
        # seem to work with tinyMCE in the mix.  BUT, once tinyMCE's
        # part is done, and this image processed, it can be returned
        # to just its basename, since the resource parts have their
        # own unique-ification mechanisms already in place.

        descrip_file_path = Path(server_filename + ".exe_info")
        log.debug("handleTinyMCEimageChoice creating preview " \
                + "description file \'" \
                + descrip_file_path.abspath() + "\'.");
        descrip_file = open(descrip_file_path, 'wb')

        # safety measures against TinyMCE, otherwise it will
        # later take ampersands and entity-escape them into '&',
        # and filenames with hash signs will not be found, etc.:
        unspaced_filename = local_filename.replace(' ', '_')
        unhashed_filename = unspaced_filename.replace('#', '_num_')
        unamped_local_filename = unhashed_filename.replace('&', '_and_')
        log.debug("and setting new file basename as: "
                + unamped_local_filename);
        my_basename = os.path.basename(unamped_local_filename)
        descrip_file.write(("basename=" + my_basename).encode('utf-8'))

        descrip_file.flush()
        descrip_file.close()

    except Exception as e:
        client.alert(_('SAVE FAILED!\n%s' % str(e)))
        log.error("handleTinyMCEimageChoice unable to copy local image "\
                + "file to server prevew, error = " + str(e))
        raise
コード例 #12
0
 def copy_json(self):
     self.create_json()
     Path.copyfile(self.json_file, Path.joinpath(self.output_dir, Path("a.json")))
コード例 #13
0
 def copy_json(self):
     self.create_json()
     Path.copyfile(self.json_file,
                   Path.joinpath(self.output_dir, Path("a.json")))
コード例 #14
0
def generateTinyMCEmath(request, package, tinyMCEwin, tinyMCEwin_name, \
                         tinyMCEfield, latex_source, math_fontsize, \
                         preview_image_filename, preview_math_srcfile):
    """
    Based off of handleTinyMCEimageChoice(),
    handleTinyMCEmath() is similar in that it places a .gif math image
    (and a corresponding .tex LaTeX source file) into the previews dir.
    Rather than copying the image from a user-selected directory, though,
    this routine actually generates the math image using mimetex.
    """
    server_filename = ""
    callback_errors = ""
    errors = 0

    webDir = Path(G.application.tempWebDir)
    previewDir = webDir.joinpath('previews')

    if not previewDir.exists():
        log.debug("image previews directory does not yet exist; " \
                + "creating as %s " % previewDir)
        previewDir.makedirs()
    elif not previewDir.isdir():
        client.alert(\
            _('Preview directory %s is a file, cannot replace it') \
            % previewDir)
        log.error("Couldn't preview tinyMCE-chosen image: " +
                  "Preview dir %s is a file, cannot replace it" \
                  % previewDir)
        callback_errors = "Preview dir is a file, cannot replace"
        errors += 1

    # if errors == 0:
    #    localImagePath = Path(local_filename)
    #    if not localImagePath.exists() or not localImagePath.isfile():
    #        client.alert( \
    #             _(u'Image file %s is not found, cannot preview it') \
    #             % localImagePath)
    #        log.error("Couldn't find tinyMCE-chosen image: %s" \
    #                % localImagePath)
    #        callback_errors = "Image file %s not found, cannot preview" \
    #                % localImagePath
    #        errors += 1

    # the mimetex usage code was swiped from the Math iDevice:
    if latex_source != "":

        # first write the latex_source out into the preview_math_srcfile,
        # such that it can then be passed into the compile command:
        math_filename = previewDir.joinpath(preview_math_srcfile)
        math_filename_str = math_filename.abspath().encode('utf-8')
        log.info("handleTinyMCEmath: using LaTeX source: " + latex_source)
        log.debug("writing LaTeX source into \'" \
                + math_filename_str + "\'.")
        math_file = open(math_filename, 'wb')
        # do we need to append a \n here?:
        math_file.write(latex_source)
        math_file.flush()
        math_file.close()

        try:
            use_latex_sourcefile = math_filename_str
            tempFileName = compile(use_latex_sourcefile, math_fontsize, \
                    latex_is_file=True)
        except Exception as e:
            client.alert(_('MimeTeX compile failed!\n%s' % str(e)))
            log.error("handleTinyMCEmath unable to compile LaTeX using "\
                + "mimetex, error = " + str(e))
            raise

        # copy the file into previews
        server_filename = previewDir.joinpath(preview_image_filename)
        log.debug("handleTinyMCEmath copying math image from \'"\
                + tempFileName + "\' to \'" \
                + server_filename.abspath().encode('utf-8') + "\'.")
        shutil.copyfile(tempFileName, \
                server_filename.abspath().encode('utf-8'))

        # Delete the temp file made by compile
        Path(tempFileName).remove()
    return
コード例 #15
0
def previewTinyMCEimage(request, package, tinyMCEwin, tinyMCEwin_name, \
                         tinyMCEfield, local_filename, preview_filename):
    """
    Once an image is selected in the file browser that is spawned by the
    TinyMCE image dialog, copy this file (which is local to the user's
    machine) into the server space, under a preview directory
    (after checking if this exists, and creating it if necessary).
    Note that this IS a "cheat", in violation of the client-server
    separation, but can be done since we know that the eXe server is
    actually sitting on the client host.
    """
    server_filename = ""
    callback_errors = ""
    errors = 0

    log.debug('handleTinyMCEimageChoice: image local = ' + local_filename +
              ', base=' + os.path.basename(local_filename))

    webDir = Path(G.application.tempWebDir)
    previewDir = webDir.joinpath('previews')

    if not previewDir.exists():
        log.debug("image previews directory does not yet exist; " \
                + "creating as %s " % previewDir)
        previewDir.makedirs()
    elif not previewDir.isdir():
        client.alert(\
            _('Preview directory %s is a file, cannot replace it') \
            % previewDir)
        log.error("Couldn't preview tinyMCE-chosen image: " +
                  "Preview dir %s is a file, cannot replace it" \
                  % previewDir)
        callback_errors = "Preview dir is a file, cannot replace"
        errors += 1

    if errors == 0:
        log.debug('handleTinyMCEimageChoice: originally, local_filename=' +
                  local_filename)
        local_filename = str(local_filename, 'utf-8')
        log.debug('handleTinyMCEimageChoice: in unicode, local_filename=' +
                  local_filename)

        localImagePath = Path(local_filename)
        log.debug('handleTinyMCEimageChoice: after Path, localImagePath= ' +
                  localImagePath)
        if not localImagePath.exists() or not localImagePath.isfile():
            client.alert(\
                 _('Local file %s is not found, cannot preview it') \
                 % localImagePath)
            log.error("Couldn't find tinyMCE-chosen image: %s" \
                    % localImagePath)
            callback_errors = "Image file %s not found, cannot preview" \
                    % localImagePath
            errors += 1

    try:
        # joinpath needs its join arguments to already be in Unicode:
        # preview_filename = toUnicode(preview_filename);
        # but that's okay, cuz preview_filename is now URI safe, right?
        log.debug('URIencoded preview filename=' + preview_filename)

        server_filename = previewDir.joinpath(preview_filename)
        log.debug("handleTinyMCEimageChoice copying image from \'"\
                + local_filename + "\' to \'" \
                + server_filename.abspath() + "\'.")
        shutil.copyfile(local_filename, \
                server_filename.abspath())

        # new optional description file to provide the
        # actual base filename, such that once it is later processed
        # copied into the resources directory, it can be done with
        # only the basename.   Otherwise the resource filenames
        # are too long for some users, preventing them from making
        # backup CDs of the content, for example.
        #
        # Remember that the full path of the
        # file is only used here as an easy way to keep the names
        # unique WITHOUT requiring a roundtrip call from the Javascript
        # to this server, and back again, a process which does not
        # seem to work with tinyMCE in the mix.  BUT, once tinyMCE's
        # part is done, and this image processed, it can be returned
        # to just its basename, since the resource parts have their
        # own unique-ification mechanisms already in place.

        descrip_file_path = Path(server_filename + ".exe_info")
        log.debug("handleTinyMCEimageChoice creating preview " \
                + "description file \'" \
                + descrip_file_path.abspath() + "\'.")
        descrip_file = open(descrip_file_path, 'wb')

        # safety measures against TinyMCE, otherwise it will
        # later take ampersands and entity-escape them into '&',
        # and filenames with hash signs will not be found, etc.:
        unspaced_filename = local_filename.replace(' ', '_')
        unhashed_filename = unspaced_filename.replace('#', '_num_')
        unamped_local_filename = unhashed_filename.replace('&', '_and_')
        log.debug("and setting new file basename as: " +
                  unamped_local_filename)
        my_basename = os.path.basename(unamped_local_filename)
        descrip_file.write(("basename=" + my_basename).encode('utf-8'))

        descrip_file.flush()
        descrip_file.close()

    except Exception as e:
        client.alert(_('SAVE FAILED!\n%s' % str(e)))
        log.error("handleTinyMCEimageChoice unable to copy local image "\
                + "file to server prevew, error = " + str(e))
        raise