~azzar1/unity/add-show-desktop-key

« back to all changes in this revision

Viewing changes to ivle/webapp/media.py

Implement an authorization system in the new framework. This breaks the REST
views, but the rest have authorization information now.

Show diffs side-by-side

added added

removed removed

Lines of Context:
38
38
 
39
39
    return os.path.join(ivle.conf.root_dir, '+media', plugin, path)
40
40
 
41
 
class MediaFileView(BaseView):
 
41
class BaseMediaFileView(BaseView):
42
42
    '''A view for media files.
43
43
 
44
44
    This serves static files from directories registered by plugins.
53
53
        self.path = path
54
54
 
55
55
    def _make_filename(self, req):
56
 
        try:
57
 
            plugin = req.plugins[self.ns]
58
 
        except KeyError:
59
 
            raise NotFound()
60
 
 
61
 
        if not issubclass(plugin, MediaPlugin):
62
 
            raise NotFound()
63
 
 
64
 
        mediadir = plugin.media
65
 
        plugindir = os.path.dirname(inspect.getmodule(plugin).__file__)
66
 
 
67
 
        return os.path.join(plugindir, mediadir, self.path)
 
56
        raise NotImplementedError()
68
57
 
69
58
    def render(self, req):
70
59
        # If it begins with ".." or separator, it's illegal. Die.
87
76
        req.content_type = type
88
77
        req.sendfile(filename)
89
78
 
 
79
 
 
80
class MediaFileView(BaseMediaFileView):
 
81
    '''A view for media files.
 
82
 
 
83
    This serves static files from directories registered by plugins.
 
84
 
 
85
    Plugins wishing to export media should declare a 'media' attribute,
 
86
    pointing to the directory to serve (relative to the module's directory).
 
87
    The contents of that directory will then be available under
 
88
    /+media/python.path.to.module.
 
89
    '''
 
90
    permission = None
 
91
 
 
92
    def _make_filename(self, req):
 
93
        try:
 
94
            plugin = req.plugins[self.ns]
 
95
        except KeyError:
 
96
            raise NotFound()
 
97
 
 
98
        if not issubclass(plugin, MediaPlugin):
 
99
            raise NotFound()
 
100
 
 
101
        mediadir = plugin.media
 
102
        plugindir = os.path.dirname(inspect.getmodule(plugin).__file__)
 
103
 
 
104
        return os.path.join(plugindir, mediadir, self.path)
 
105
 
 
106
    def get_permissions(self, user):
 
107
        return set()
 
108
 
90
109
class Plugin(ViewPlugin):
91
110
    urls = [
92
111
        ('+media/:ns/*path', MediaFileView),