Exemplo n.º 1
0
def get_html_file(wpobj):
    """ finds html file to use for the content, if any.
        returns empty string on failure.
    """

    if hasattr(wpobj, 'html_url'):
        htmlattr = 'html_url'
    elif hasattr(wpobj, 'contentfile'):
        htmlattr = 'contentfile'
    else:
        log.error('Object doesn\'t have a html file attribute!: '
                  '{}'.format(wpobj.__name__))
        return ''
    if not hasattr(wpobj, 'alias'):
        log.error('Object doesn\'t have an \'alias\' attribute!: '
                  '{}'.format(wpobj.__name__))
        return ''
    # Get objects html_url/contentfile
    obj_file = getattr(wpobj, htmlattr)
    if not obj_file:
        # use default location if no manual override is set.
        possiblefile = 'static/html/{}.html'.format(wpobj.alias)
        html_file = utilities.get_absolute_path(possiblefile)
    elif obj_file.lower() == 'none':
        # html files can be disabled by putting None in the
        # html_url/contentfile field.
        return ''
    else:
        if os.path.isfile(obj_file):
            # already absolute
            html_file = obj_file
        else:
            # try absolute path
            html_file = utilities.get_absolute_path(obj_file)
    return html_file
Exemplo n.º 2
0
    def test_get_absolute_path(self):
        """ get_absolute_path() returns only static directories, with no '..'.
        """
        # Common case.
        self.assertEqual(
            '{}/css/main.min.css'.format(settings.STATIC_ROOT),
            utils.get_absolute_path('/static/css/main.min.css'),
            msg='failed to get absolute path for known file.'
        )

        # Guard against '..' tricks.
        self.assertEqual(
            '',
            utils.get_absolute_path('/static/../html/index.html'),
            msg='failed to guard against \'..\'!'
        )

        # Guard against non-static files.
        self.assertEqual(
            '',
            utils.get_absolute_path('{}/wp_main'.format(
                settings.BASE_PARENT
            )),
            msg='failed to guard against non-static file!'
        )

        # Guard against multiple //.
        self.assertEqual(
            '{}/css/main.min.css'.format(settings.STATIC_ROOT),
            utils.get_absolute_path('///static//css/main.min.css'),
            msg='failed to fix slightly malformed url.'
        )
Exemplo n.º 3
0
def get_html_file(wpobj):
    """ finds html file to use for the content, if any.
        returns empty string on failure.
    """

    if hasattr(wpobj, 'html_url'):
        htmlattr = 'html_url'
    elif hasattr(wpobj, 'contentfile'):
        htmlattr = 'contentfile'
    else:
        _log.error('Object doesn\'t have a html file attribute!: '
                   '{}'.format(wpobj.__name__))
        return ''
    if not hasattr(wpobj, 'alias'):
        _log.error('Object doesn\'t have an \'alias\' attribute!: '
                   '{}'.format(wpobj.__name__))
        return ''
    # Get objects html_url/contentfile
    obj_file = getattr(wpobj, htmlattr)
    if not obj_file:
        # use default location if no manual override is set.
        possiblefile = 'static/html/{}.html'.format(wpobj.alias)
        html_file = utilities.get_absolute_path(possiblefile)
    elif obj_file.lower() == "none":
        # html files can be disabled by putting None in the
        # html_url/contentfile field.
        return ''
    else:
        if os.path.isfile(obj_file):
            # already absolute
            html_file = obj_file
        else:
            # try absolute path
            html_file = utilities.get_absolute_path(obj_file)
    return html_file
Exemplo n.º 4
0
def get_screenshots_dir(project):
    """ determine screenshots directory for project """
    if project.screenshot_dir == "":
        # try default location
        aliasdir = 'static/images/{}'.format(project.alias)
        images_dir = utilities.get_absolute_path(aliasdir)
    else:
        if os.path.isdir(project.screenshot_dir):
            # project path was absolute
            images_dir = project.screenshot_dir
        else:
            # needs absolute?
            images_dir = utilities.get_absolute_path(project.screenshot_dir)
    return images_dir
Exemplo n.º 5
0
def get_screenshots_dir(project):
    """ determine screenshots directory for project """
    if project.screenshot_dir == "":
        # try default location
        aliasdir = 'static/images/{}'.format(project.alias)
        images_dir = utilities.get_absolute_path(aliasdir)
    else:
        if os.path.isdir(project.screenshot_dir):
            # project path was absolute
            images_dir = project.screenshot_dir
        else:
            # needs absolute?
            images_dir = utilities.get_absolute_path(project.screenshot_dir)
    return images_dir
Exemplo n.º 6
0
def get_using_paths(dir_path, absolute_path=None, proj=None):
    """ When given a dir as a path,
        find out which file is preferred to use first.
    """

    if absolute_path is None:
        absolute_path = utilities.get_absolute_path(dir_path)

    if not os.path.isdir(absolute_path):
        return (None, None)

    try:
        files = os.listdir(absolute_path)
    except Exception as ex:
        files = []
        log.debug('unable to listdir: {}\n  {}'.format(
            absolute_path,
            ex,
        ))

    # no files in this directory, or bad dir.
    if not files:
        return (None, None)

    default_file = utilities.get_relative_path(
        utilities.append_path(dir_path, files[0])
    )
    # Dir has files, get most important to show.
    if proj is None:
        # No project, try first file.
        return (
            default_file,
            utilities.get_absolute_path(default_file),
        )

    # Has project, see if source_file is good. if so, use it.
    if proj.source_file:
        absolute_path = utilities.get_absolute_path(proj.source_file)
        return (
            utilities.get_relative_path(absolute_path),
            absolute_path,
        )

    # Just use first file found for project.
    return (
        default_file,
        utilities.get_absolute_path(default_file),
    )
Exemplo n.º 7
0
def get_post_body(post):
    """ retrieves body for post.
        if html_url is set, we will try to load the file
        if loading fails, or it is not set, we will use post.body.
    """

    if post is None:
        log.error('post is None!')
        return ''

    # Try for automatic slug-based html file.
    slugfile = '{}.html'.format(post.slug)
    content = htmltools.load_html_template(slugfile)
    if content:
        return content

    # Try the posts's html_url as a template.
    absolute_path = utilities.get_absolute_path(post.html_url)
    if absolute_path:
        # Load the html_url template content.
        return htmltools.load_html_file(
            absolute_path,
            context={
                'post': post
            })

    # No valid html_url, using post body as a Template.
    return htmltools.render_html_str(post.body, context={'post': post})
Exemplo n.º 8
0
def get_screenshots(images_dir, noscript_image=None):
    """ Retrieves html formatted screenshots box for all images in
        a directory.
        Returns None on failure.
        Arguments:
            images_dir          : Relative dir containing all the images.

        Keyword Arguments:
            noscript_image      : Path to image to show for <noscript> tag.

    """
    # accceptable image formats (last 4 chars)
    formats = ['.png', '.jpg', '.gif', '.bmp', '.jpeg']

    if os.path.isdir(images_dir):
        dirpath = images_dir
    else:
        # Make sure we are using the right dir.
        # get absolute path for images dir.
        dirpath = utilities.get_absolute_path(images_dir)
    if not dirpath:
        log.error('No images dir: {!r} -> {!r}'.format(images_dir, dirpath))
        return None

    # Get useable relative dir (user-passed may be wrong format)
    relative_dir = utilities.get_relative_path(dirpath)

    # find acceptable pics
    try:
        all_files = os.listdir(dirpath)
    except Exception as ex:
        log.error('Can\'t list dir: {}\n{}'.format(dirpath, ex))
        return None

    # Help functions for building screenshots.
    def relative_img(filename):
        return os.path.join(relative_dir, filename)

    def good_format(filename):
        return os.path.splitext(filename)[-1] in formats

    # Build acceptable pics list
    good_pics = [relative_img(f) for f in all_files if good_format(f)]

    # auto-pick noscript image if needed
    if (len(good_pics) > 0) and (noscript_image is None):
        noscript_image = good_pics[0]
    else:
        # no good pics.
        noscript_image = None

    # Render from template.
    screenshots = render_clean(
        'home/imageviewer.html',
        context={
            'images': good_pics,
            'noscript_image': noscript_image,
        })
    return screenshots
Exemplo n.º 9
0
def get_screenshots(images_dir, noscript_image=None):
    """ Retrieves html formatted screenshots box for all images in
        a directory.
        Returns None on failure.
        Arguments:
            images_dir          : Relative dir containing all the images.

        Keyword Arguments:
            noscript_image      : Path to image to show for <noscript> tag.

    """
    # accceptable image formats (last 4 chars)
    formats = ['.png', '.jpg', '.gif', '.bmp', '.jpeg']

    if os.path.isdir(images_dir):
        dirpath = images_dir
    else:
        # Make sure we are using the right dir.
        # get absolute path for images dir.
        dirpath = utilities.get_absolute_path(images_dir)
    if not dirpath:
        log.error('No images dir: {!r} -> {!r}'.format(images_dir, dirpath))
        return None

    # Get useable relative dir (user-passed may be wrong format)
    relative_dir = utilities.get_relative_path(dirpath)

    # find acceptable pics
    try:
        all_files = os.listdir(dirpath)
    except Exception as ex:
        log.error('Can\'t list dir: {}\n{}'.format(dirpath, ex))
        return None

    # Help functions for building screenshots.
    def relative_img(filename):
        return os.path.join(relative_dir, filename)

    def good_format(filename):
        return os.path.splitext(filename)[-1] in formats

    # Build acceptable pics list
    good_pics = [relative_img(f) for f in all_files if good_format(f)]

    # auto-pick noscript image if needed
    if (len(good_pics) > 0) and (noscript_image is None):
        noscript_image = good_pics[0]
    else:
        # no good pics.
        noscript_image = None

    # Render from template.
    screenshots = render_clean('home/imageviewer.html',
                               context={
                                   'images': good_pics,
                                   'noscript_image': noscript_image,
                               })
    return screenshots
Exemplo n.º 10
0
def download(request, file_path):
    """ provides download of files,
        tracks download count of projects and possibly others
        by checking file's project owner, incrementing the count,
        and then redirecting to the actual file.
    """

    # File path may be an incomplete path to the actual location.
    # /cedit/source is just as good as /static/files/cedit/source
    # so we must grab the real (absolute) path, and then convert
    # it to a valid static path.

    # location on disk
    absolute_path = utilities.get_absolute_path(file_path)
    # location relative to site
    if settings.SERVER_LOCATION == 'local':
        static_path = absolute_path.replace(settings.STATIC_PARENT, '')
    else:
        # remote, file is actually located at the static_server dir.
        # but we will need to forward the user to the /static url.
        static_path = absolute_path.replace(settings.STATIC_ROOT, '')
        if static_path.startswith('/'):
            static_path = '/static{}'.format(static_path)
        else:
            static_path = '/static/{}'.format(static_path)

    # used for local only.
    if (not static_path.startswith('/')):
        static_path = '/{}'.format(static_path)

    if not absolute_path:
        # File doesn't exist. Return an error.
        log.debug('file doesn\'t exist: {}'.format(file_path))
        alert_message = 'Sorry, that file doesn\'t exist.'
        main_content = '\n'.join((
            '<div class=\'wp-block\'>',
            '<a href=\'/\'><span>Click here to go home.</span></a>',
            '</div>'
        ))
        context = {
            'main_content': mark_safe(main_content),
            'alert_message': mark_safe(alert_message),
        }
        response = responses.clean_response(
            'home/main.html',
            context=context,
            request=request)
    else:
        # redirect to actual file.
        # log.debug("redirecting to: " + static_path)
        response = responses.redirect_response(static_path)
        # File to track? (not a directory)
        if os.path.isfile(absolute_path):
            # see if its a trackable model's file, increment it's count.
            dltools.increment_dl_count(file_path, absolute_path)

    return response
Exemplo n.º 11
0
def download(request, file_path):
    """ provides download of files,
        tracks download count of projects and possibly others
        by checking file's project owner, incrementing the count,
        and then redirecting to the actual file.
    """

    # File path may be an incomplete path to the actual location.
    # /cedit/source is just as good as /static/files/cedit/source
    # so we must grab the real (absolute) path, and then convert
    # it to a valid static path.

    # location on disk
    absolute_path = utilities.get_absolute_path(file_path)
    # location relative to site
    if settings.SERVER_LOCATION == 'local':
        static_path = absolute_path.replace(settings.STATIC_PARENT, '')
    else:
        # remote, file is actually located at the static_server dir.
        # but we will need to forward the user to the /static url.
        static_path = absolute_path.replace(settings.STATIC_ROOT, '')
        if static_path.startswith('/'):
            static_path = '/static{}'.format(static_path)
        else:
            static_path = '/static/{}'.format(static_path)

    # used for local only.
    if (not static_path.startswith('/')):
        static_path = '/{}'.format(static_path)

    if not absolute_path:
        # File doesn't exist. Return an error.
        log.debug('file doesn\'t exist: {}'.format(file_path))
        alert_message = 'Sorry, that file doesn\'t exist.'
        main_content = '\n'.join(
            ('<div class=\'wp-block\'>',
             '<a href=\'/\'><span>Click here to go home.</span></a>',
             '</div>'))
        context = {
            'main_content': mark_safe(main_content),
            'alert_message': mark_safe(alert_message),
        }
        response = responses.clean_response('home/main.html',
                                            context=context,
                                            request=request)
    else:
        # redirect to actual file.
        # log.debug("redirecting to: " + static_path)
        response = responses.redirect_response(static_path)
        # File to track? (not a directory)
        if os.path.isfile(absolute_path):
            # see if its a trackable model's file, increment it's count.
            dltools.increment_dl_count(file_path, absolute_path)

    return response
Exemplo n.º 12
0
def get_html_file(project):
    """ finds html file to use for project content, if any
        returns empty string on failure.
    """

    if project.html_url == "":
        # use default location if no manual override is set.
        aliashtml = 'static/html/{}.html'.format(project.alias)
        html_file = utilities.get_absolute_path(aliashtml)
    elif project.html_url.lower() == 'none':
        # html files can be disabled by putting None in the html_url field.
        return ""
    else:
        if os.path.isfile(project.html_url):
            # already absolute
            html_file = project.html_url
        else:
            # try absolute path
            html_file = utilities.get_absolute_path(project.html_url)
    return html_file
Exemplo n.º 13
0
def get_html_file(project):
    """ finds html file to use for project content, if any 
        returns empty string on failure.
    """
    
    if project.html_url == "":
        # use default location if no manual override is set.
        aliashtml = 'static/html/{}.html'.format(project.alias)
        html_file = utilities.get_absolute_path(aliashtml)
    elif project.html_url.lower() == "none":
        # html files can be disabled by putting None in the html_url field.
        return ""
    else:
        if os.path.isfile(project.html_url):
            # already absolute
            html_file = project.html_url
        else:
            # try absolute path
            html_file = utilities.get_absolute_path(project.html_url)
    return html_file
Exemplo n.º 14
0
def get_screenshots_dir(miscobj):
    """ Retrieves screenshots directory for this misc object, if
        screenshots are available.
        Otherwise, returns None.
    """

    possibledir = os.path.join('static/images', miscobj.alias)
    imagedir = utilities.get_absolute_path(possibledir)
    if not os.path.isdir(imagedir):
        # log.debug('No screenshots dir: {}'.format(possibledir))
        return None
    return imagedir
Exemplo n.º 15
0
def get_screenshots_dir(miscobj):
    """ Retrieves screenshots directory for this misc object, if
        screenshots are available.
        Otherwise, returns None.
    """

    possibledir = os.path.join('static/images', miscobj.alias)
    imagedir = utilities.get_absolute_path(possibledir)
    if not os.path.isdir(imagedir):
        # log.debug('No screenshots dir: {}'.format(possibledir))
        return None
    return imagedir
Exemplo n.º 16
0
def get_download_file(project):
    """ determines location of download file, 
        or returns empty string if we can't find it anywhere. """
    
    surl = project.download_url
    if surl == "":
        return ""
    if utilities.is_file_or_dir(surl):
        # already absolute
        return surl
    else:
        # try absolute_path
        return utilities.get_absolute_path(surl)
Exemplo n.º 17
0
def get_using_paths(dir_path, absolute_path=None, proj=None):
    """ When given a dir as a path, 
       find out which file is preferred to use first.
    """
    
    if absolute_path is None:
        absolute_path = utilities.get_absolute_path(dir_path)

    if not os.path.isdir(absolute_path):
        return (None, None)
    
    try:
        files = os.listdir(absolute_path)
    except Exception as ex:
        files = []
        _log.debug("unable to listdir: " + absolute_path + '\n' + str(ex))
        
    # no files in this directory, or bad dir.
    if len(files) == 0:
        return (None, None)

    # dir has files, get most important to show.
    if proj is None:
        # no project, try first file.
        static_path = utilities.append_path(dir_path, files[0])
        absolute_path = utilities.get_absolute_path(static_path)

    else:
        # has project, see if source_file is good. if so, use it.
        if proj.source_file:
            static_path = proj.source_file
            absolute_path = utilities.get_absolute_path(static_path)
            #_log.debug("dir passed: using source_file: " + absolute_path)
        else:
            # just use first file found.
            static_path = utilities.append_path(dir_path, files[0])
            absolute_path = utilities.get_absolute_path(static_path)

    return (static_path, absolute_path)
Exemplo n.º 18
0
def get_screenshots(images_dir, noscript_image=None):
    """ Retrieves html formatted screenshots box for all images in
        a directory.
        Returns None on failure.
        Arguments:
            images_dir          : Relative dir containing all the images.

        Keyword Arguments:
            noscript_image      : Path to image to show for <noscript> tag.

    """
    # accceptable image formats (last 4 chars)
    formats = [".png", ".jpg", ".gif", ".bmp", "jpeg"]

    # Make sure we are using the right dir.
    # get absolute path for images dir,
    # if none exists then delete the target_replacement.
    images_dir = utilities.get_absolute_path(images_dir)
    if not images_dir:
        return None

    # Get useable relative dir (user-passed may be wrong format)
    relative_dir = utilities.get_relative_path(images_dir)

    # find acceptable pics
    try:
        all_files = os.listdir(images_dir)
    except Exception as ex:
        _log.debug("Can't list dir: " + images_dir + '\n' + str(ex))
        return None

    # Help functions for building screenshots.
    relative_img = lambda filename: os.path.join(relative_dir, filename)
    good_format = lambda filename: (filename[-4:] in formats)

    # Build acceptable pics list
    good_pics = [relative_img(f) for f in all_files if good_format(f)]

    # auto-pick noscript image if needed
    if (len(good_pics) > 0) and (noscript_image is None):
        noscript_image = good_pics[0]
    else:
        # no good pics.
        noscript_image = None

    # Render from template.
    screenshots = render_clean("home/screenshots.html",
                               context_dict={'images': good_pics,
                                             'noscript_image': noscript_image,
                                             })
    return screenshots
Exemplo n.º 19
0
    def get_images(self):
        """ Retrieves html formatted images box for all images in
            a directory.
            Returns None on failure.
            Arguments:
                images_dir          : Relative dir containing all the images.
        """
        # accceptable image formats
        formats = ('.png', '.jpg', '.gif', '.bmp', '.jpeg')

        # Make sure we are using the right dir.
        # get absolute path for images dir,
        # if none exists then no screenshots are given.
        images_dir = utilities.get_absolute_path(self.images_dir)
        if not images_dir:
            return None

        # Get useable relative dir (user-passed may be wrong format)
        relative_dir = utilities.get_relative_path(images_dir)

        # find acceptable pics
        try:
            all_files = os.listdir(images_dir)
        except Exception as ex:
            log.debug('Can\'t list dir: {}\n{}'.format(images_dir, ex))
            return None

        # Build acceptable pics list
        good_pics = [
            os.path.join(relative_dir, f)
            for f in all_files
            if os.path.splitext(f)[-1] in formats
        ]

        # auto-pick noscript image if needed
        if len(good_pics) > 0:
            noscript_image = good_pics[0]
        else:
            # no good pics.
            noscript_image = None

        # Render from template.
        c = {
            'images': good_pics,
            'noscript_image': noscript_image,
        }
        return htmltools.render_clean(
            'home/imageviewer.html',
            context=c)
Exemplo n.º 20
0
    def test_get_absolute_path(self):
        """ get_absolute_path() returns only static directories, with no '..'.
        """
        # Common case.
        self.assertEqual('{}/css/main.min.css'.format(settings.STATIC_ROOT),
                         utils.get_absolute_path('/static/css/main.min.css'),
                         msg='failed to get absolute path for known file.')

        # Guard against '..' tricks.
        self.assertEqual('',
                         utils.get_absolute_path('/static/../html/index.html'),
                         msg='failed to guard against \'..\'!')

        # Guard against non-static files.
        self.assertEqual('',
                         utils.get_absolute_path('{}/wp_main'.format(
                             settings.BASE_PARENT)),
                         msg='failed to guard against non-static file!')

        # Guard against multiple //.
        self.assertEqual(
            '{}/css/main.min.css'.format(settings.STATIC_ROOT),
            utils.get_absolute_path('///static//css/main.min.css'),
            msg='failed to fix slightly malformed url.')
Exemplo n.º 21
0
    def get_download_file_abs(self):
        """ Determines location of download file,
            or returns empty string if we can't find it anywhere.
        """
        if not self.project.download_url:
            log.debug('Project has no download_url: {!r} -> {!r}'.format(
                self.project,
                self.project.download_url
            ))
            return ''
        if utilities.is_file_or_dir(self.project.download_url):
            # already absolute
            return self.project.download_url

        # try absolute_path
        return utilities.get_absolute_path(self.project.download_url)
Exemplo n.º 22
0
    def get_images(self):
        """ Retrieves html formatted images box for all images in
            a directory.
            Returns None on failure.
            Arguments:
                images_dir          : Relative dir containing all the images.
        """
        # accceptable image formats
        formats = ('.png', '.jpg', '.gif', '.bmp', '.jpeg')

        # Make sure we are using the right dir.
        # get absolute path for images dir,
        # if none exists then no screenshots are given.
        images_dir = utilities.get_absolute_path(self.images_dir)
        if not images_dir:
            return None

        # Get useable relative dir (user-passed may be wrong format)
        relative_dir = utilities.get_relative_path(images_dir)

        # find acceptable pics
        try:
            all_files = os.listdir(images_dir)
        except Exception as ex:
            log.debug('Can\'t list dir: {}\n{}'.format(images_dir, ex))
            return None

        # Build acceptable pics list
        good_pics = [
            os.path.join(relative_dir, f) for f in all_files
            if os.path.splitext(f)[-1] in formats
        ]

        # auto-pick noscript image if needed
        if len(good_pics) > 0:
            noscript_image = good_pics[0]
        else:
            # no good pics.
            noscript_image = None

        # Render from template.
        c = {
            'images': good_pics,
            'noscript_image': noscript_image,
        }
        return htmltools.render_clean('home/imageviewer.html', context=c)
Exemplo n.º 23
0
def get_scriptkid_image():
    """ returns a random image filename from /images/scriptkid """
    
    import random
    image_dir = utilities.get_absolute_path("images/scriptkids")
    goodexts = ("jpeg", ".jpg", ".png", ".gif", ".bmp")
    try:
        images = [img for img in os.listdir(image_dir)
                  if img[-4:].lower() in goodexts]
    except Exception as ex:
        _log.error("can't do listdir() on: " + image_dir + '\n' + str(ex))
        return None
    if not images:
        _log.error("images was empty!")
        return None
    
    randomindex = random.randint(0, len(images) - 1)
    return os.path.join(image_dir, images[randomindex])
Exemplo n.º 24
0
def load_html_file(filename, request=None, context=None):
    """ Try rendering a file as a Template, if that fails return the raw
        file content.
        This can all be short-circuited by passing in a pre-loaded template
        with: template=loader.get_template(templatename)
        Arguments:
            filename  : File name for template/html file.
            request   : Request() to build template with.
            context   : Context dict for template.
    """

    # Try rendering file as a template.
    content = load_html_template(filename, request=request, context=context)
    if content:
        return content

    # no template, probably a filename. check it:
    log.debug('No template, falling back to HTML: {}'.format(filename))
    if not os.path.isfile(filename):
        # try getting absolute path
        spath = utilities.get_absolute_path(filename)
        if os.path.isfile(spath):
            filename = spath
        else:
            # no file found.
            log.debug('No file found at: {}'.format(filename))
            return ''

    try:
        with open(filename) as fhtml:
            # Successful file open, return the contents.
            return fhtml.read()
    except IOError as exIO:
        log.error('Cannot open file: {}\n{}'.format(filename, exIO))
    except OSError as exOS:
        log.error((
            'Possible bad permissions opening file: {}\n{}'
        ).format(filename, exOS))
    except Exception as ex:
        log.error('General error opening file: {}\n{}'.format(filename, ex))
    return ''
Exemplo n.º 25
0
def get_post_body(post_):
    """ retrieves body for post.
        if html_url is set, we will try to load the file
        if loading fails, or it is not set, we will use post.body.
    """
    
    # TODO: Html content needs to be tied into template render.
    #       see: htmltools.load_html_file(), projects.tools.get_html_content(),
    #            misc.tools.get_long_desc()
    if post_ is None:
        _log.error("post_ = None!")
        return ""
    
    absolute_path = utilities.get_absolute_path(post_.html_url)
    if absolute_path == "":
        # no valid html_url
        return post_.body
    
    # load html file content
    scontent = htmltools.load_html_file(absolute_path)
    return scontent
Exemplo n.º 26
0
def load_html_file(filename, request=None, context=None):
    """ Try rendering a file as a Template, if that fails return the raw
        file content.
        This can all be short-circuited by passing in a pre-loaded template
        with: template=loader.get_template(templatename)
        Arguments:
            filename  : File name for template/html file.
            request   : Request() to build template with.
            context   : Context dict for template.
    """

    # Try rendering file as a template.
    content = load_html_template(filename, request=request, context=context)
    if content:
        return content

    # no template, probably a filename. check it:
    log.debug('No template, falling back to HTML: {}'.format(filename))
    if not os.path.isfile(filename):
        # try getting absolute path
        spath = utilities.get_absolute_path(filename)
        if os.path.isfile(spath):
            filename = spath
        else:
            # no file found.
            log.debug('No file found at: {}'.format(filename))
            return ''

    try:
        with open(filename) as fhtml:
            # Successful file open, return the contents.
            return fhtml.read()
    except IOError as exIO:
        log.error('Cannot open file: {}\n{}'.format(filename, exIO))
    except OSError as exOS:
        log.error(('Possible bad permissions opening file: {}\n{}').format(
            filename, exOS))
    except Exception as ex:
        log.error('General error opening file: {}\n{}'.format(filename, ex))
    return ''
Exemplo n.º 27
0
def get_scriptkid_image():
    """ returns a random image filename from /static/images/scriptkids """

    import random
    image_dir = utilities.get_absolute_path('/static/images/scriptkids')
    goodexts = ('.jpeg', '.jpg', '.png', '.gif', '.bmp')
    try:
        images = [
            imgfile for imgfile in os.listdir(image_dir)
            if os.path.splitext(imgfile)[-1].lower() in goodexts
        ]
    except Exception as ex:
        log.error('Can\'t do listdir() on {!r}\n  {}'.format(
            image_dir,
            ex,
        ))
        return None
    if not images:
        log.error('Script kid images are empty!')
        return None

    randomindex = random.randint(0, len(images) - 1)
    return os.path.join(image_dir, images[randomindex])
Exemplo n.º 28
0
def get_source_files(project):
    """ returns list of all source files for a project, if any.
        uses relative static path. (/static/files/project/source/file.py)
        returns None on  failure.
    """

    if project.source_dir == "":
        source_files = []
    else:
        sabsolute = utilities.get_absolute_path(project.source_dir)
        if sabsolute == "":
            source_files = []
        else:
            # retrieve all source filenames
            file_names = os.listdir(sabsolute)

            if len(file_names) == 0:
                source_files = []
            else:
                source_files = []
                for sfilename in file_names:
                    srelativepath = utilities.get_relative_path(os.path.join(project.source_dir, sfilename))  # noqa
                    source_files.append(srelativepath)
    return source_files
Exemplo n.º 29
0
def get_source_files(project):
    """ returns list of all source files for a project, if any.
        uses relative static path. (/static/files/project/source/file.py)
        returns None on  failure.
    """
    
    if project.source_dir == "":
        source_files = []
    else:
        sabsolute = utilities.get_absolute_path(project.source_dir)
        if sabsolute == "":
            source_files = []
        else:
            # retrieve all source filenames
            file_names = os.listdir(sabsolute)

            if len(file_names) == 0:
                source_files = []
            else:
                source_files = []
                for sfilename in file_names:
                    srelativepath = utilities.get_relative_path(os.path.join(project.source_dir, sfilename))  # noqa
                    source_files.append(srelativepath)
    return source_files
Exemplo n.º 30
0
def get_post_body(post):
    """ retrieves body for post.
        if html_url is set, we will try to load the file
        if loading fails, or it is not set, we will use post.body.
    """

    if post is None:
        log.error('post is None!')
        return ''

    # Try for automatic slug-based html file.
    slugfile = '{}.html'.format(post.slug)
    content = htmltools.load_html_template(slugfile)
    if content:
        return content

    # Try the posts's html_url as a template.
    absolute_path = utilities.get_absolute_path(post.html_url)
    if absolute_path:
        # Load the html_url template content.
        return htmltools.load_html_file(absolute_path, context={'post': post})

    # No valid html_url, using post body as a Template.
    return htmltools.render_html_str(post.body, context={'post': post})
Exemplo n.º 31
0
def get_scriptkid_image():
    """ returns a random image filename from /static/images/scriptkids """

    import random
    image_dir = utilities.get_absolute_path('/static/images/scriptkids')
    goodexts = ('.jpeg', '.jpg', '.png', '.gif', '.bmp')
    try:
        images = [
            imgfile
            for imgfile in os.listdir(image_dir)
            if os.path.splitext(imgfile)[-1].lower() in goodexts
        ]
    except Exception as ex:
        log.error('Can\'t do listdir() on {!r}\n  {}'.format(
            image_dir,
            ex,
        ))
        return None
    if not images:
        log.error('Script kid images are empty!')
        return None

    randomindex = random.randint(0, len(images) - 1)
    return os.path.join(image_dir, images[randomindex])
Exemplo n.º 32
0
def load_html_file(sfile, request=None, context=None):
    """ Trys loading a template by name,
        If context is passed it is used to render the template.
        If a request was passed then RequestContext is used,
        otherwise Context is used.
        If no template is found, it trys loading html content from file.
        returns string with html content.
    """

    # TODO: project pages and blogs don't need to use the old style
    #       {{ inject_something }} tags. They need a new templatetag like:
    #       {{ post|injectsomething }} or {{ project|sourceview }}.
    # Template code is disabled until then.
    # see: projects.tools.get_html_content()

    # try:
    #    template = loader.get_template(sfile)
    # except Exception:
    #    template = None
    #
    # if template:
    # Found template for this file, use it.
    #    if context:
    #        if request:
    # Try creating a request context.
    #            try:
    #                contextobj = RequestContext(request, context)
    #            except Exception as ex:
    #                _log.error('Error creating request context from: '
    #                           '{}\n{}'.format(request, ex))
    #                return ''
    #        else:
    # No request, use normal context.
    #            contextobj = Context(context)
    #    else:
    # No context dict given, use empty context.
    #        contextobj = Context({})
    #
    # Have context, try rendering.
    #    try:
    #        content = template.render(contextobj)
    # Good content, return it.
    #        return content
    #    except Exception as ex:
    #        _log.error(''.join(['Error rendering template: {} '.format(sfile),
    #                            'Context: {}'.format(context),
    #                            '\n{}'.format(ex),
    #                            ]))
    #        return ''

    # no template, probably a filename. check it:
    if not os.path.isfile(sfile):
        # try getting absolute path
        spath = utilities.get_absolute_path(sfile)
        if os.path.isfile(spath):
            sfile = spath
        else:
            # no file found.
            _log.debug('No file found at: {}'.format(sfile))
            return ''

    try:
        with open(sfile) as fhtml:
            # Successful file open, return the contents.
            return fhtml.read()

    except IOError as exIO:
        _log.error('Cannot open file: {}\n{}'.format(sfile, exIO))
        return ''

    except OSError as exOS:
        _log.error('Possible bad permissions opening file: {}'.format(sfile) +
                   '{}'.format(exOS))
        return ''

    except Exception as ex:
        _log.error('General error opening file: {}\n{}'.format(sfile, ex))
        return ''
Exemplo n.º 33
0
def download(request, file_path):
    """ provides download of files, 
        tracks download count of projects and possibly others
        by checking file's project owner, incrementing the count,
        and then redirecting to the actual file.
    """

    # File path may be an incomplete path to the actual location.
    # /cedit/source is just as good as /static/files/cedit/source
    # so we must grab the real (absolute) path, and then convert
    # it to a valid static path.
    
    # location on disk
    absolute_path = utilities.get_absolute_path(file_path)
    # location relative to site
    if settings.SERVER_LOCATION == 'local':
        static_path = absolute_path.replace(settings.STATIC_PARENT, '')
    else:
        # remote, file is actually located at the static_server dir.
        # but we will need to forward the user to the /static url.
        static_path = absolute_path.replace(settings.STATIC_ROOT, '')
        if static_path.startswith('/'):
            static_path = '/static{}'.format(static_path)
        else:
            static_path = '/static/{}'.format(static_path)

    # used for local only.
    if (not static_path.startswith('/')):
        static_path = '/{}'.format(static_path)
    
    if not absolute_path:
        # File doesn't exist. Return an error.
        _log.debug("file doesn't exist: " + file_path)
        alert_message = "Sorry, that file doesn't exist."
        main_content = ("<div class='wp-block'>"
                        "<a href='/'><span>Click here to go home.</span></a>"
                        "</div>")
        context = {'request': request,
                   'main_content': mark_safe(main_content),
                   'alert_message': mark_safe(alert_message),
                   'extra_style_link_list':
                   [utilities.get_browser_style(request)],
                   }
        response = responses.clean_response("home/main.html", context)
    else:
        # redirect to actual file.
        #_log.debug("redirecting to: " + static_path)
        response = responses.redirect_response(static_path)
        # File to track? (not a directory)
        if os.path.isfile(absolute_path):
            # see if its a project file.
            proj = ptools.get_project_from_path(absolute_path)
            # update project's download count
            if proj is not None:
                # increment downloads for this project.
                proj.download_count += 1
                proj.save()
            # see if it's a misc file
            misc = misctools.get_by_filename(file_path)
            # update misc files count
            if misc:
                misc.download_count += 1
                misc.save()
                
            # update file tracker info
            filetracker = dltools.get_file_tracker(absolute_path)
            if filetracker is not None:
                if proj is not None:
                    dltools.update_tracker_projects(filetracker, proj)
                
                filetracker.download_count += 1
                filetracker.save()
                 
    return response
Exemplo n.º 34
0
def get_file_info(file_path):
    """ retrieves actual file content for viewer. """
    absolute_path = utilities.get_absolute_path(file_path)
    static_path = utilities.get_relative_path(file_path)
    # no file to load.
    if not absolute_path:
        _log.error('Invalid file path for viewer.get_file_content(): '
                   '{}'.format(file_path))
        raise Http404("Sorry, that file doesn't exist.")
        
    project = ptools.get_project_from_path(absolute_path)
    
    # Directory was passed, get files to use. (based on project, dir listing)
    if os.path.isdir(absolute_path):
        if project:
            static_path, absolute_path = get_using_paths(static_path,
                                                         absolute_path,
                                                         project)
            if static_path is None or absolute_path is None:
                # Raise error which will display a 404.
                raise Http404("Sorry, there was an error viewing that file.")
        else:
            raise Http404("Sorry, that file doesn't exist.")
    
    # Update project view count tracker
    miscobj = None
    if project:
        project.view_count += 1
        project.save()
    else:
        # Not a project, may be a Misc Object.
        miscobj = misctools.get_by_filename(file_path)
        logdebug('Found Misc Object: {}'.format(repr(miscobj)))
        
        # Update misc view count tracker
        if miscobj:
            miscobj.view_count += 1
            miscobj.save()
        else:
            _log.debug('get_file_content: not a project or misc object: '
                       '{}'.format(file_path))
            
    # Update file tracker
    if os.path.isfile(absolute_path):
        filetracker = dltools.get_file_tracker(absolute_path)
        if filetracker is not None:
            if project is not None:
                dltools.update_tracker_projects(filetracker,
                                                project,
                                                dosave=False)
            filetracker.view_count += 1
            filetracker.save()
    
    # Get file content
    file_content = ""
    try:
        with open(absolute_path) as fread:
            file_content = fread.read()
    except Exception as ex:
        _log.error("Error loading file: " + absolute_path + '\n' + str(ex))
        raise Http404("Sorry, I can't load that file right now.")
    
    fileinfo = {'project': project,
                'miscobj': miscobj,
                'static_path': static_path,
                'absolute_path': absolute_path,
                'file_content': file_content, }
    return fileinfo
Exemplo n.º 35
0
def get_file_info(file_path):
    """ retrieves actual file content for viewer. """
    absolute_path = utilities.get_absolute_path(file_path)
    static_path = utilities.get_relative_path(file_path)
    # no file to load.
    if not absolute_path:
        log.error(
            'Invalid file path for viewer.get_file_content(): {}'.format(
                file_path))
        raise Http404('Sorry, that file doesn\'t exist.')

    project = ptools.get_project_from_path(absolute_path)

    # Directory was passed, get files to use. (based on project, dir listing)
    if os.path.isdir(absolute_path):
        if project:
            static_path, absolute_path = get_using_paths(
                static_path,
                proj=project
            )
            if static_path is None or absolute_path is None:
                # Raise error which will display a 404.
                raise Http404('Sorry, there was an error viewing that file.')
        else:
            raise Http404('Sorry, that file doesn\'t exist.')

    # Update project view count tracker
    miscobj = None
    if project:
        project.view_count += 1
        project.save()
    else:
        # Not a project, may be a Misc Object.
        miscobj = misctools.get_by_filename(file_path)
        log.debug('Found Misc Object: {}'.format(repr(miscobj)))

        # Update misc view count tracker
        if miscobj:
            miscobj.view_count += 1
            miscobj.save()
        else:
            log.debug(
                'get_file_content: not a project or misc object: {}'.format(
                    file_path))

    # Update file tracker
    if os.path.isfile(absolute_path):
        filetracker = dltools.get_file_tracker(absolute_path)
        if filetracker is not None:
            if project is not None:
                dltools.update_tracker_projects(
                    filetracker,
                    project,
                    dosave=False
                )
            filetracker.view_count += 1
            filetracker.save()

    # Get file content
    try:
        with open(absolute_path) as fread:
            file_content = fread.read()
    except Exception as ex:
        log.error('Error loading file: {}\n{}'.format(absolute_path, ex))
        raise Http404('Sorry, I can\'t load that file right now.')

    fileinfo = {
        'project': project,
        'miscobj': miscobj,
        'static_path': static_path,
        'absolute_path': absolute_path,
        'file_content': file_content,
    }
    if project:
        fileinfo['name'] = project.name
        fileinfo['related_url'] = '/projects/{}'.format(project.alias)
    elif miscobj:
        fileinfo['name'] = miscobj.name
        fileinfo['related_url'] = '/misc/{}'.format(miscobj.alias)
    else:
        fileinfo['name'] = None
        fileinfo['related_url'] = None
    return fileinfo