Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from rest_framework import generics
- from rest_framework.views import APIView
- from rest_framework.response import Response
- from rest_framework import permissions
- from rest_framework.exceptions import APIException
- from plata.shop.models import Order, OrderItem
- from mandriva.lib.my import provision_user
- from mmp.serializers import SPProductSerializer, SPProductappstream, SPProductMSSSectionSerializer, SPProductPriceSerializer
- from mmp import serializers, models
- from mmp.models import ProductRelease, SPProduct, SPProductMSSSection, SPProductMSSModule, SPProductCategories
- from mmp.signals import product_downloaded
- from mmp.views import restrict_for_current_user
- from django.views.decorators.csrf import csrf_exempt
- from rest_framework.decorators import api_view
- #from django.utils import simplejson
- class AddonByReleaseList(generics.ListAPIView):
- """
- List of addons of the current release.
- """
- serializer_class = SPProductSerializer
- paginate_by = None
- def get_queryset(self):
- release = ProductRelease.objects.get(product__slug=self.kwargs["product"],
- version=self.kwargs["version"])
- queryset = SPProduct.api_addons.get_for_release(release)
- return restrict_for_current_user(queryset, self.request.user)
- class AddonByProductList(generics.ListAPIView):
- """
- List of addons by supported product
- """
- serializer_class = SPProductSerializer
- paginate_by = None
- def get_queryset(self):
- product = SPProduct.objects.get(slug=self.kwargs["slug"],
- type=SPProduct.PRODUCT)
- queryset = SPProduct.api_addons.get_for_product(product)
- return restrict_for_current_user(queryset, self.request.user)
- class SectionByReleaseList(generics.ListAPIView):
- """
- List of MSS sections of the current release.
- """
- serializer_class = SPProductMSSSectionSerializer
- paginate_by = None
- def get_queryset(self):
- release = ProductRelease.objects.get(product__slug=self.kwargs["product"],
- version=self.kwargs["version"])
- return SPProductMSSSection.objects.get_for_release(release)
- class SectionByProductList(generics.ListAPIView):
- """
- List of MSS sections of the current release.
- """
- serializer_class = SPProductMSSSectionSerializer
- paginate_by = None
- def get_queryset(self):
- product = SPProduct.objects.get(slug=self.kwargs["slug"],
- type=SPProduct.PRODUCT)
- return SPProductMSSSection.objects.get_for_product(product)
- class ModuleAccess(permissions.BasePermission):
- """
- Check if the user has access to the addon's MSS module
- """
- def has_permission(self, request, view, obj=None):
- return obj is None or obj.product.can_download(request.user)
- class ModuleDownload(generics.RetrieveAPIView):
- """
- Module download endpoint
- """
- permission_classes = (permissions.IsAuthenticated, ModuleAccess)
- model = SPProductMSSModule
- def get(self, request, *args, **kwargs):
- file = self.get_object().file
- if os.path.exists(file.path):
- try:
- import mimetypes
- mimetype, encoding = mimetypes.guess_type(file.path)
- except:
- mimetype = "application/force-download"
- response = HttpResponse()
- response["Content-Disposition"] = "attachment; filename=%s" % os.path.basename(file.path)
- response["Content-Type"] = mimetype
- response["X-Sendfile"] = file.path
- product_downloaded.send(sender=self, user=request.user, product=self.get_object().product)
- return response
- else:
- mail_admins("Package download broken", "URL: %s" % request.build_absolute_uri())
- return Response(status=404)
- class LicenseServerException(APIException):
- status_code = 503
- detail = 'Service temporarily unavailable, try again later.'
- def __init__(self, status_code=None, detail=None):
- if status_code is not None:
- self.status_code = status_code
- if detail is not None:
- self.detail = detail
- class SPProductAppStream(generics.ListAPIView):
- """
- """
- permission_classes = (permissions.AllowAny,)
- def get(self, request, format=None):
- """
- Return a list of services
- """
- result = []
- category = models.Category
- #recupere id category
- id_category = category.objects.get(slug='appstream').pk
- list_product_appstream = SPProductCategories.objects.filter(category_id=id_category)
- #parcoure toutes les produits appstream
- for t in list_product_appstream:
- produit=SPProduct.objects.filter(pk=t.spproduct_id).filter(mandriva_featured=True)
- if produit:
- result.append(SPProductappstream(produit, many=True).data)
- response = HttpResponse()
- response["Content-Type"] = 'application/json'
- x = json.dumps(result)
- return Response(x)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement