def xml_plugins(request, qg_version=None, stable_only=None, package_name=None): """ The XML file accepted parameters: * qgis: qgis version * stable_only: 0/1 * package_name: Plugin.package_name """ qg_version = qg_version if qg_version is not None else vjust(request.GET.get('qgis', '1.8.0'), fillchar='0', level=2, force_zero=True) stable_only = stable_only if stable_only is not None else request.GET.get('stable_only', '0') package_name = package_name if package_name is not None else request.GET.get('package_name', None) filters = {} version_filters = {} object_list = [] if qg_version: filters.update({'pluginversion__min_qg_version__lte' : qg_version}) version_filters.update({'min_qg_version__lte' : qg_version}) filters.update({'pluginversion__max_qg_version__gte' : qg_version}) version_filters.update({'max_qg_version__gte' : qg_version}) # Get all versions for the given plugin if package_name: filters.update({'package_name' : package_name}) try: plugin = Plugin.approved_objects.get(**filters) plugin_version_filters = copy.copy(version_filters) plugin_version_filters.update({'plugin' : plugin}) for plugin_version in PluginVersion.stable_objects.filter(**plugin_version_filters): object_list.append(plugin_version) if stable_only != '1': for plugin_version in PluginVersion.experimental_objects.filter(**plugin_version_filters): object_list.append(plugin_version) except Plugin.DoesNotExist: pass else: trusted_users = User.objects.filter(Q(user_permissions__codename='can_approve', user_permissions__content_type__app_label='plugins') | Q(is_superuser=True)).distinct() for plugin in Plugin.approved_objects.filter(**filters).prefetch_related('created_by'): is_trusted = plugin.created_by in trusted_users plugin_version_filters = copy.copy(version_filters) plugin_version_filters.update({'plugin' : plugin}) try: data = PluginVersion.stable_objects.filter(**plugin_version_filters)[0] setattr(data, 'is_trusted', is_trusted) object_list.append(data) except IndexError: pass if stable_only != '1': try: data = PluginVersion.experimental_objects.filter(**plugin_version_filters)[0] setattr(data, 'is_trusted', is_trusted) object_list.append(data) except IndexError: pass return render_to_response('plugins/plugins.xml', {'object_list': object_list}, content_type='text/xml', context_instance=RequestContext(request))
def xml_plugins(request): """ The XML file accepted parameters: * qgis: qgis version * stable_only: 0/1 * package_name: Plugin.package_name """ qg_version = vjust(request.GET.get('qgis', '1.8.0'), fillchar='0', level=2, force_zero=True) stable_only = request.GET.get('stable_only', '0') package_name = request.GET.get('package_name', None) filters = {} version_filters = {} object_list = [] if qg_version: filters.update({'pluginversion__min_qg_version__lte' : qg_version}) version_filters.update({'min_qg_version__lte' : qg_version}) filters.update({'pluginversion__max_qg_version__gte' : qg_version}) version_filters.update({'max_qg_version__gte' : qg_version}) # Get all versions for the given plugin if package_name: filters.update({'package_name' : package_name}) try: plugin = Plugin.approved_objects.get(**filters) plugin_version_filters = copy.copy(version_filters) plugin_version_filters.update({'plugin' : plugin}) for plugin_version in PluginVersion.stable_objects.filter(**plugin_version_filters): object_list.append(plugin_version) if stable_only != '1': for plugin_version in PluginVersion.experimental_objects.filter(**plugin_version_filters): object_list.append(plugin_version) except Plugin.DoesNotExist: pass else: for plugin in Plugin.approved_objects.filter(**filters): plugin_version_filters = copy.copy(version_filters) plugin_version_filters.update({'plugin' : plugin}) try: object_list.append(PluginVersion.stable_objects.filter(**plugin_version_filters)[0]) except IndexError: pass if stable_only != '1': try: object_list.append(PluginVersion.experimental_objects.filter(**plugin_version_filters)[0]) except IndexError: pass return render_to_response('plugins/plugins.xml', {'object_list': object_list}, content_type='text/xml', context_instance=RequestContext(request))
def xml_plugins(request): """ The XML file accepted parameters: * qgis: qgis version * stable_only: 0/1 * package_name: Plugin.package_name """ qg_version = vjust(request.GET.get('qgis', '1.8.0'), fillchar='0', level=2, force_zero=True) stable_only = request.GET.get('stable_only', '0') package_name = request.GET.get('package_name', None) filters = {} version_filters = {} object_list = [] if qg_version: filters.update({'pluginversion__min_qg_version__lte' : qg_version}) version_filters.update({'min_qg_version__lte' : qg_version}) filters.update({'pluginversion__max_qg_version__gte' : qg_version}) version_filters.update({'max_qg_version__gte' : qg_version}) # Get all versions for the given plugin if package_name: filters.update({'package_name' : package_name}) try: plugin = Plugin.approved_objects.get(**filters) plugin_version_filters = copy.copy(version_filters) plugin_version_filters.update({'plugin' : plugin}) for plugin_version in PluginVersion.stable_objects.filter(**plugin_version_filters): object_list.append(plugin_version) if stable_only != '1': for plugin_version in PluginVersion.experimental_objects.filter(**plugin_version_filters): object_list.append(plugin_version) except Plugin.DoesNotExist: pass else: for plugin in Plugin.approved_objects.filter(**filters): plugin_version_filters = copy.copy(version_filters) plugin_version_filters.update({'plugin' : plugin}) try: object_list.append(PluginVersion.stable_objects.filter(**plugin_version_filters)[0]) except IndexError: pass if stable_only != '1': try: object_list.append(PluginVersion.experimental_objects.filter(**plugin_version_filters)[0]) except IndexError: pass return render_to_response('plugins/plugins.xml', {'object_list': object_list}, mimetype='text/xml', context_instance=RequestContext(request))
def xml_plugins(request): """ The XML file accepted parameters: * qgis: qgis version * stable_only: 0/1 """ qg_version = vjust(request.GET.get('qgis')) stable_only = request.GET.get('stable_only', '0') filters = {} version_filters = {} object_list = [] if qg_version: filters.update({'pluginversion__min_qg_version__lte' : qg_version}) version_filters.update({'min_qg_version__lte' : qg_version}) filters.update({'pluginversion__max_qg_version__gte' : qg_version}) version_filters.update({'max_qg_version__gte' : qg_version}) for plugin in Plugin.approved_objects.filter(**filters): plugin_version_filters = copy.copy(version_filters) plugin_version_filters.update({'plugin' : plugin}) try: object_list.append(PluginVersion.stable_objects.filter(**plugin_version_filters)[0]) except IndexError: pass if stable_only != '1': try: object_list.append(PluginVersion.experimental_objects.filter(**plugin_version_filters)[0]) except IndexError: pass return render_to_response('plugins/plugins.xml', {'object_list': object_list}, mimetype='text/xml', context_instance=RequestContext(request))
def xml_plugins_new(request, qg_version=None, stable_only=None, package_name=None): """ The XML file accepted parameters: * qgis: qgis version * stable_only: 0/1 * package_name: Plugin.package_name """ qg_version = qg_version if qg_version is not None else vjust(request.GET.get('qgis', '1.8.0'), fillchar='0', level=2, force_zero=True) stable_only = stable_only if stable_only is not None else request.GET.get('stable_only', '0') package_name = package_name if package_name is not None else request.GET.get('package_name', None) filters = {} version_filters = {} object_list = [] if qg_version: filters.update({'pluginversion__min_qg_version__lte' : qg_version}) version_filters.update({'min_qg_version__lte' : qg_version}) filters.update({'pluginversion__max_qg_version__gte' : qg_version}) version_filters.update({'max_qg_version__gte' : qg_version}) # Get all versions for the given plugin if package_name: filters.update({'package_name' : package_name}) try: plugin = Plugin.approved_objects.get(**filters) plugin_version_filters = copy.copy(version_filters) plugin_version_filters.update({'plugin' : plugin}) for plugin_version in PluginVersion.stable_objects.filter(**plugin_version_filters): object_list.append(plugin_version) if stable_only != '1': for plugin_version in PluginVersion.experimental_objects.filter(**plugin_version_filters): object_list.append(plugin_version) except Plugin.DoesNotExist: pass else: # Fast lane: uses raw queries trusted_users_ids = ''' (SELECT DISTINCT "auth_user"."id" FROM "auth_user" LEFT OUTER JOIN "auth_user_user_permissions" ON ( "auth_user"."id" = "auth_user_user_permissions"."user_id" ) LEFT OUTER JOIN "auth_permission" ON ( "auth_user_user_permissions"."permission_id" = "auth_permission"."id" ) LEFT OUTER JOIN "django_content_type" ON ( "auth_permission"."content_type_id" = "django_content_type"."id" ) WHERE (("auth_permission"."codename" = 'can_approve' AND "django_content_type"."app_label" = 'plugins') OR "auth_user"."is_superuser" = True)) ''' sql = ''' SELECT DISTINCT ON (pv.plugin_id) pv.*, pv.created_by_id IN %(trusted_users_ids)s AS is_trusted FROM %(pv_table)s pv WHERE ( pv.approved = True AND pv."max_qg_version" >= '%(qg_version)s' AND pv."min_qg_version" <= '%(qg_version)s' AND pv.experimental = %(experimental)s ) ORDER BY pv.plugin_id, pv.version DESC ''' object_list_new = PluginVersion.objects.raw(sql % { 'pv_table': PluginVersion._meta.db_table, 'p_table': Plugin._meta.db_table, 'qg_version': qg_version, 'experimental': 'False', 'trusted_users_ids': str(trusted_users_ids), }) if stable_only != '1': # Do the query object_list_new = [o for o in object_list_new] object_list_new += [o for o in PluginVersion.objects.raw(sql % { 'pv_table': PluginVersion._meta.db_table, 'p_table': Plugin._meta.db_table, 'qg_version': qg_version, 'experimental': 'True', 'trusted_users_ids': str(trusted_users_ids), })] return render_to_response('plugins/plugins.xml', {'object_list': object_list_new}, content_type='text/xml', context_instance=RequestContext(request))
def xml_plugins(request, qg_version=None, stable_only=None, package_name=None): """ The XML file accepted parameters: * qgis: qgis version * stable_only: 0/1 * package_name: Plugin.package_name """ qg_version = qg_version if qg_version is not None else vjust( request.GET.get('qgis', '1.8.0'), fillchar='0', level=2, force_zero=True) stable_only = stable_only if stable_only is not None else request.GET.get( 'stable_only', '0') package_name = package_name if package_name is not None else request.GET.get( 'package_name', None) filters = {} version_filters = {} object_list = [] if qg_version: filters.update({'pluginversion__min_qg_version__lte': qg_version}) version_filters.update({'min_qg_version__lte': qg_version}) filters.update({'pluginversion__max_qg_version__gte': qg_version}) version_filters.update({'max_qg_version__gte': qg_version}) # Get all versions for the given plugin) if package_name: filters.update({'package_name': package_name}) try: plugin = Plugin.approved_objects.get(**filters) plugin_version_filters = copy.copy(version_filters) plugin_version_filters.update({'plugin': plugin}) for plugin_version in PluginVersion.stable_objects.filter( **plugin_version_filters): object_list.append(plugin_version) if stable_only != '1': for plugin_version in PluginVersion.experimental_objects.filter( **plugin_version_filters): object_list.append(plugin_version) except Plugin.DoesNotExist: pass else: # Checked the cached plugins qgis_version = request.GET.get('qgis', None) qgis_filename = 'plugins_{}.xml'.format(qgis_version) folder_name = os.path.join(settings.MEDIA_ROOT, 'cached_xmls') path_file = os.path.join(folder_name, qgis_filename) if os.path.exists(path_file): return HttpResponse(open(path_file).read(), content_type='application/xml') trusted_users_ids = list( zip(*User.objects.filter( Q(user_permissions__codename='can_approve', user_permissions__content_type__app_label='plugins') | Q(is_superuser=True)).distinct().values_list('id')))[0] qs = Plugin.approved_objects.filter(**filters).annotate( is_trusted=RawSQL( '%s.created_by_id in (%s)' % (Plugin._meta.db_table, (',').join([str(tu) for tu in trusted_users_ids])), ())) for plugin in qs: plugin_version_filters = copy.copy(version_filters) plugin_version_filters.update({'plugin_id': plugin.pk}) try: data = PluginVersion.stable_objects.filter( **plugin_version_filters)[0] setattr(data, 'is_trusted', plugin.is_trusted) object_list.append(data) except IndexError: pass if stable_only != '1': try: data = PluginVersion.experimental_objects.filter( **plugin_version_filters)[0] setattr(data, 'is_trusted', plugin.is_trusted) object_list.append(data) except IndexError: pass return render(request, 'plugins/plugins.xml', {'object_list': object_list}, content_type='text/xml')