def get_download_module(module_name): import re if not re.search('^[a-zA-Z0-9\_\.]+$', module_name): # pragma: no cover # bad module_name raise registry_exceptions.BadDownloadModuleName("Download name isn't a valid Python module name.") try: return import_module("%s.downloader" % module_name) except: debug = getattr(settings, 'DEBUG') if debug: raise return None
def get_download_module(module_name): import re if not re.search('^[a-zA-Z0-9\_\.]+$', module_name): # pragma: no cover # bad module_name raise registry_exceptions.BadDownloadModuleName( "Download name isn't a valid Python module name.") try: downloader = None # dangerous - we are really trusting the settings creators here. exec("import %s.downloader as downloader" % module_name) return downloader except: debug = getattr(settings, 'DEBUG') if debug: raise return None
def download(request, downloadType, iid=None): """ By default, ``aristotle_mdr.views.download`` is called whenever a URL matches the pattern defined in ``aristotle_mdr.urls_aristotle``:: download/(?P<downloadType>[a-zA-Z0-9\-\.]+)/(?P<iid>\d+)/? This is passed into ``download`` which resolves the item id (``iid``), and determins if a user has permission to view the request item with that id. If a user is allowed to download this file, ``download`` iterates through each download type defined in ``ARISTOTLE_DOWNLOADS``. A download option tuple takes the following form form:: ('file_type','display_name','font_awesome_icon_name','module_name'), With ``file_type`` allowing only ASCII alphanumeric and underscores, ``display_name`` can be any valid python string, ``font_awesome_icon_name`` can be any Font Awesome icon and ``module_name`` is the name of the python module that provides a downloader for this file type. For example, included with Aristotle-MDR is a PDF downloader which has the download definition tuple:: ('pdf','PDF','fa-file-pdf-o','aristotle_mdr'), Where a ``file_type`` multiple is defined multiple times, **the last matching instance in the tuple is used**. Next, the module that is defined for a ``file_type`` is dynamically imported using ``exec``, and is wrapped in a ``try: except`` block to catch any exceptions. If the ``module_name`` does not match the regex ``^[a-zA-Z0-9\_]+$`` ``download`` raises an exception. If the module is able to be imported, ``downloader.py`` from the given module is imported, this file **MUST** have a ``download`` function defined which returns a Django ``HttpResponse`` object of some form. """ item = MDR._concept.objects.get_subclass(pk=iid) item = get_if_user_can_view(item.__class__, request.user, iid) if not item: if request.user.is_anonymous(): return redirect(reverse('friendly_login') + '?next=%s' % request.path) else: raise PermissionDenied downloadOpts = getattr(settings, 'ARISTOTLE_DOWNLOADS', "") module_name = "" for d in downloadOpts: dt = d[0] if dt == downloadType: module_name = d[3] if module_name: import re if not re.search('^[a-zA-Z0-9\-\.]+$', downloadType): # pragma: no cover # Invalid downloadType raise registry_exceptions.BadDownloadTypeAbbreviation("Download type can only be composed of letters, numbers, hyphens or periods.") elif not re.search('^[a-zA-Z0-9\_]+$', module_name): # pragma: no cover # bad module_name raise registry_exceptions.BadDownloadModuleName("Download name isn't a valid Python module name.") try: downloader = None # dangerous - we are really trusting the settings creators here. exec("import %s.downloader as downloader" % module_name) return downloader.download(request, downloadType, item) except TemplateDoesNotExist: raise Http404 raise Http404