Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
99 lines (82 sloc) 3.61 KB
from rpc4django import rpcmethod
from xmlrpclib import Fault
from plugins.models import *
from validator import validator
from django.db import IntegrityError
from plugins.views import plugin_notify
import StringIO
from django.utils.translation import ugettext_lazy as _
from django.core.exceptions import ValidationError, PermissionDenied
from django.core.files.uploadedfile import InMemoryUploadedFile
# Transaction
from django.db import connection
@rpcmethod(name='plugin.upload', signature=['array', 'base64'], login_required=True)
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.
request = kwargs.get('request')
package = StringIO.StringIO(
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'],
# Gets existing plugin
plugin = Plugin.objects.get(package_name=plugin_data['package_name'])
# Apply new values = 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')
if is_new:
# Takes care of tags
if cleaned_data.get('tags'):
version_data = {
'plugin' : plugin,
'min_qg_version' : cleaned_data['qgisMinimumVersion'],
'version' : cleaned_data['version'],
'created_by' : request.user,
'package' : InMemoryUploadedFile(package, 'package',
"" % 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')
new_version = PluginVersion(**version_data)
except IntegrityError:
# Avoids error: current transaction is aborted, commands ignored until
# end of transaction block
return (,
Something went wrong with that request. Please try again.