Ejemplo n.º 1
0
 def test_zipfile_with_git(self, mock_namelist):
     mock_namelist.return_value = ['.git']
     with self.assertRaisesMessage(
             Exception,
             ("For security reasons, zip file cannot contain "
              "'.git' directory")):
         validator(self.package)
Ejemplo n.º 2
0
 def test_zipfile_with_gitignore(self, mock_namelist):
     """test if .gitignore will not raise ValidationError"""
     mock_namelist.return_value = ['.gitignore']
     with self.assertRaises(ValidationError) as cm:
         validator(self.package)
     exception = cm.exception
     self.assertNotEqual(
         exception.message,
         "For security reasons, zip file cannot contain '.git' directory")
Ejemplo n.º 3
0
 def clean_package(self):
     package         = self.cleaned_data.get('package')
     try:
         self.cleaned_data.update(validator(package))
     except ValidationError, e:
         msg = unicode(_('File upload must be a valid QGIS Python plugin compressed archive.'))
         raise ValidationError("%s %s" % (msg, ','.join(e.messages)))
Ejemplo n.º 4
0
 def clean(self):
     package         = self.cleaned_data.get('package')
     try:
         self.cleaned_data.update(validator(package))
     except ValidationError, e:
         msg = unicode(_("There were errors reading plugin package (please check also your plugin's metadata)."))
         raise ValidationError("%s %s" % (msg, ','.join(e.messages)))
Ejemplo n.º 5
0
    def clean(self):
        """
        Only read package if uploaded
        """
        # Override package
        changelog = self.cleaned_data.get('changelog')

        if self.files:
            package         = self.cleaned_data.get('package')
            try:
                cleaned_data = validator(package)
                if ('experimental' in dict(cleaned_data)
                    and 'experimental' in self.cleaned_data
                    and dict(cleaned_data)['experimental'] != self.cleaned_data['experimental']):
                    msg = unicode(_("The 'experimental' flag in the form does not match the 'experimental' flag in the plugins package metadata.<br />"))
                    raise ValidationError(mark_safe("%s" % msg))
                self.cleaned_data.update(cleaned_data)
            except ValidationError, e:
                msg = unicode(_("There were errors reading plugin package (please check also your plugin's metadata).<br />"))
                raise ValidationError(mark_safe("%s %s" % (msg, '<br />'.join(e.messages))))
            # Populate instance
            self.instance.min_qg_version = self.cleaned_data.get('qgisMinimumVersion')
            self.instance.max_qg_version = self.cleaned_data.get('qgisMaximumVersion')
            self.instance.version        = PluginVersion.clean_version(self.cleaned_data.get('version'))
            self.instance.server         = self.cleaned_data.get('server')
            # Check plugin name
            if self.cleaned_data.get('package_name') and self.cleaned_data.get('package_name') != self.instance.plugin.package_name:
                raise ValidationError(_('Plugin name mismatch: the plugin main folder name in the compressed file (%s) is different from the original plugin package name (%s).') % (self.cleaned_data.get('package_name'), self.instance.plugin.package_name))
Ejemplo n.º 6
0
    def clean(self):
        """
        Only read package if uploaded
        """

        # Override package
        changelog = self.cleaned_data.get('changelog')

        if self.files:
            package = self.cleaned_data.get('package')
            try:
                self.cleaned_data.update(validator(package))
            except ValidationError, e:
                msg = unicode(
                    _("There were errors reading plugin package (please check also your plugin's metadata).<br />"
                      ))
                raise ValidationError(
                    mark_safe("%s %s" % (msg, '<br />'.join(e.messages))))
            # Populate instance
            self.instance.min_qg_version = self.cleaned_data.get(
                'qgisMinimumVersion')
            self.instance.max_qg_version = self.cleaned_data.get(
                'qgisMaximumVersion')
            self.instance.version = PluginVersion.clean_version(
                self.cleaned_data.get('version'))
            # Check plugin name
            if self.cleaned_data.get('package_name') and self.cleaned_data.get(
                    'package_name') != self.instance.plugin.package_name:
                raise ValidationError(
                    _('Plugin name mismatch: the plugin main folder name in the compressed file (%s) is different from the original plugin package name (%s).'
                      ) % (self.cleaned_data.get('package_name'),
                           self.instance.plugin.package_name))
Ejemplo n.º 7
0
 def clean_package(self):
     package = self.cleaned_data.get('package')
     try:
         self.cleaned_data.update(validator(package))
     except ValidationError, e:
         msg = unicode(
             _('File upload must be a valid QGIS Python plugin compressed archive.'
               ))
         raise ValidationError("%s %s" % (msg, ','.join(e.messages)))
Ejemplo n.º 8
0
 def clean(self):
     package = self.cleaned_data.get('package')
     try:
         self.cleaned_data.update(validator(package))
     except ValidationError, e:
         msg = unicode(
             _("There were errors reading plugin package (please check also your plugin's metadata)."
               ))
         raise ValidationError("%s %s" % (msg, ','.join(e.messages)))
Ejemplo n.º 9
0
 def clean_package(self):
     """
     Populates cleaned_data with metadata from the zip package
     """
     package         = self.cleaned_data.get('package')
     try:
         self.cleaned_data.update(validator(package))
     except ValidationError, e:
         msg = unicode(_("There were errors reading plugin package (please check also your plugin's metadata)."))
         raise ValidationError(mark_safe("%s %s" % (msg, ','.join(e.messages))))
Ejemplo n.º 10
0
 def test_valid_metadata(self):
     self.assertTrue(validator(
         InMemoryUploadedFile(
             self.valid_metadata_link,
             field_name='tempfile',
             name='testfile.zip',
             content_type='application/zip',
             size=39889,
             charset='utf8'
         )
     ))
Ejemplo n.º 11
0
 def clean_package(self):
     """
     Populates cleaned_data with metadata from the zip package
     """
     package = self.cleaned_data.get('package')
     try:
         self.cleaned_data.update(validator(package))
     except ValidationError, e:
         msg = unicode(
             _("There were errors reading plugin package (please check also your plugin's metadata)."
               ))
         raise ValidationError(
             mark_safe("%s %s" % (msg, ','.join(e.messages))))
Ejemplo n.º 12
0
    def clean(self):
        """
        Only read package if uploaded
        """

        # Override package
        changelog = self.cleaned_data.get('changelog')

        if self.files:
            package         = self.cleaned_data.get('package')
            try:
                self.cleaned_data.update(validator(package))
            except ValidationError, e:
                msg = unicode(_("There were errors reading plugin package (please check also your plugin's metadata)."))
                raise ValidationError("%s %s" % (msg, ','.join(e.messages)))
            # Populate instance
            self.instance.min_qg_version = self.cleaned_data.get('qgisMinimumVersion')
            self.instance.version        = PluginVersion.clean_version(self.cleaned_data.get('version'))
            # Check plugin name
            if self.cleaned_data.get('package_name') and self.cleaned_data.get('package_name') != self.instance.plugin.package_name:
                raise ValidationError(_('Plugin name mismatch: the plugin main folder name in the compressed file (%s) is different from the original plugin package name (%s).') % (self.cleaned_data.get('package_name'), self.instance.plugin.package_name))
Ejemplo n.º 13
0
def plugin_upload(package, **kwargs):
    """
    Creates a new plugin or updates an existing one
    Returns an array containing the ID (primary key) of the plugin and the ID of the version.
    """
    try:
        request = kwargs.get('request')
        package = StringIO.StringIO(package.data)
        try:
            cleaned_data = dict(validator(package))
        except ValidationError, e:
            msg = unicode(_('File upload must be a valid QGIS Python plugin compressed archive.'))
            raise Fault(1, "%s %s" % (msg, ','.join(e.messages)))

        plugin_data = {
            'name'              : cleaned_data['name'],
            'package_name'      : cleaned_data['package_name'],
            'description'       : cleaned_data['description'],
            'created_by'        : request.user,
            'icon'              : cleaned_data['icon_file'],
            'author'            : cleaned_data['author'],
            'email'             : cleaned_data['email'],
            'about'             : cleaned_data['about'],
        }

        # Gets existing plugin
        try:
            plugin = Plugin.objects.get(package_name=plugin_data['package_name'])
            # Apply new values
            plugin.name         = plugin_data['name']
            plugin.description  = plugin_data['description']
            plugin.icon         = plugin_data['icon']
            is_new = False
        except Plugin.DoesNotExist:
            plugin = Plugin(**plugin_data)
            is_new = True

        # Optional Metadata:
        if cleaned_data.get('homepage'):
            plugin.homepage = cleaned_data.get('homepage')
        if cleaned_data.get('tracker'):
            plugin.tracker = cleaned_data.get('tracker')
        if cleaned_data.get('repository'):
            plugin.repository = cleaned_data.get('repository')
        if cleaned_data.get('deprecated'):
            plugin.deprecated = cleaned_data.get('deprecated')

        plugin.save()

        if is_new:
            plugin_notify(plugin)

        # Takes care of tags
        if cleaned_data.get('tags'):
            plugin.tags.set(*[t.strip().lower() for t in cleaned_data.get('tags').split(',')])

        version_data =  {
            'plugin'            : plugin,
            'min_qg_version'    : cleaned_data['qgisMinimumVersion'],
            'version'           : cleaned_data['version'],
            'created_by'        : request.user,
            'package'           : InMemoryUploadedFile(package, 'package',
                                    "%s.zip" % plugin.package_name, 'application/zip',
                                    package.len, 'UTF-8'),
            'approved'          : request.user.has_perm('plugins.can_approve') or plugin.approved,
        }

        # Optional version metadata
        if cleaned_data.get('experimental'):
            version_data['experimental'] = cleaned_data.get('experimental')
        if cleaned_data.get('changelog'):
            version_data['changelog'] = cleaned_data.get('changelog')
        if cleaned_data.get('qgisMaximumVersion'):
            version_data['max_qg_version'] = cleaned_data.get('qgisMaximumVersion')

        new_version = PluginVersion(**version_data)
        new_version.clean()
        new_version.save()
Ejemplo n.º 14
0
def plugin_upload(package, **kwargs):
    """

    Creates a new plugin or updates an existing one

    Returns an array containing the ID (primary key) of the plugin and the ID of the version.

    """
    try:
        request = kwargs.get("request")
        package = StringIO.StringIO(package.data)
        try:
            cleaned_data = dict(validator(package))
        except ValidationError, e:
            msg = unicode(_("File upload must be a valid QGIS Python plugin compressed archive."))
            raise ValidationError("%s %s" % (msg, ",".join(e.messages)))

        plugin_data = {
            "name": cleaned_data["name"],
            "package_name": cleaned_data["package_name"],
            "description": cleaned_data["description"],
            "created_by": request.user,
            "icon": cleaned_data["icon_file"],
            "author": cleaned_data["author"],
            "email": cleaned_data["email"],
        }

        # Gets existing plugin
        try:
            plugin = Plugin.objects.get(package_name=plugin_data["package_name"])
            # Apply new values
            plugin.name = plugin_data["name"]
            plugin.description = plugin_data["description"]
            plugin.icon = plugin_data["icon"]
            is_new = False
        except Plugin.DoesNotExist:
            plugin = Plugin(**plugin_data)
            is_new = True

        # Optional Metadata:
        if cleaned_data.get("homepage"):
            plugin.homepage = cleaned_data.get("homepage")
        if cleaned_data.get("tracker"):
            plugin.tracker = cleaned_data.get("tracker")
        if cleaned_data.get("repository"):
            plugin.repository = cleaned_data.get("repository")
        if cleaned_data.get("deprecated"):
            plugin.deprecated = cleaned_data.get("deprecated")

        plugin.save()

        if is_new:
            plugin_notify(plugin)

        # Takes care of tags
        if cleaned_data.get("tags"):
            plugin.tags.set(*[t.strip().lower() for t in cleaned_data.get("tags").split(",")])

        version_data = {
            "plugin": plugin,
            "min_qg_version": cleaned_data["qgisMinimumVersion"],
            "version": cleaned_data["version"],
            "created_by": request.user,
            "package": InMemoryUploadedFile(
                package, "package", "%s.zip" % plugin.package_name, "application/zip", package.len, "UTF-8"
            ),
            "approved": request.user.has_perm("plugins.can_approve") or plugin.approved,
        }

        # Optional version metadata
        if cleaned_data.get("experimental"):
            version_data["experimental"] = cleaned_data.get("experimental")
        if cleaned_data.get("changelog"):
            version_data["changelog"] = cleaned_data.get("changelog")
        if cleaned_data.get("qgisMaximumVersion"):
            version_data["max_qg_version"] = cleaned_data.get("qgisMaximumVersion")

        new_version = PluginVersion(**version_data)
        new_version.clean()
        new_version.save()
Ejemplo n.º 15
0
 def test_zipfile_with_pyc_file(self, mock_namelist):
     mock_namelist.return_value = ['.pyc']
     with self.assertRaisesMessage(
             Exception,
             'For security reasons, zip file cannot contain .pyc file'):
         validator(self.package)