Advertisement
Guest User

Untitled

a guest
Nov 24th, 2014
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.17 KB | None | 0 0
  1.  
  2. from rest_framework import generics
  3. from rest_framework.views import APIView
  4. from rest_framework.response import Response
  5. from rest_framework import permissions
  6. from rest_framework.exceptions import APIException
  7.  
  8. from plata.shop.models import Order, OrderItem
  9.  
  10. from mandriva.lib.my import provision_user
  11.  
  12. from mmp.serializers import SPProductSerializer, SPProductappstream, SPProductMSSSectionSerializer, SPProductPriceSerializer
  13. from mmp import serializers, models
  14. from mmp.models import ProductRelease, SPProduct, SPProductMSSSection, SPProductMSSModule, SPProductCategories
  15. from mmp.signals import product_downloaded
  16. from mmp.views import restrict_for_current_user
  17.  
  18. from django.views.decorators.csrf import csrf_exempt
  19. from rest_framework.decorators import api_view
  20. #from django.utils import simplejson
  21.  
  22.  
  23.  
  24. class AddonByReleaseList(generics.ListAPIView):
  25. """
  26. List of addons of the current release.
  27. """
  28. serializer_class = SPProductSerializer
  29. paginate_by = None
  30.  
  31. def get_queryset(self):
  32. release = ProductRelease.objects.get(product__slug=self.kwargs["product"],
  33. version=self.kwargs["version"])
  34. queryset = SPProduct.api_addons.get_for_release(release)
  35. return restrict_for_current_user(queryset, self.request.user)
  36.  
  37.  
  38. class AddonByProductList(generics.ListAPIView):
  39. """
  40. List of addons by supported product
  41. """
  42. serializer_class = SPProductSerializer
  43. paginate_by = None
  44.  
  45. def get_queryset(self):
  46. product = SPProduct.objects.get(slug=self.kwargs["slug"],
  47. type=SPProduct.PRODUCT)
  48. queryset = SPProduct.api_addons.get_for_product(product)
  49. return restrict_for_current_user(queryset, self.request.user)
  50.  
  51.  
  52. class SectionByReleaseList(generics.ListAPIView):
  53. """
  54. List of MSS sections of the current release.
  55. """
  56. serializer_class = SPProductMSSSectionSerializer
  57. paginate_by = None
  58.  
  59. def get_queryset(self):
  60. release = ProductRelease.objects.get(product__slug=self.kwargs["product"],
  61. version=self.kwargs["version"])
  62. return SPProductMSSSection.objects.get_for_release(release)
  63.  
  64.  
  65. class SectionByProductList(generics.ListAPIView):
  66. """
  67. List of MSS sections of the current release.
  68. """
  69. serializer_class = SPProductMSSSectionSerializer
  70. paginate_by = None
  71.  
  72. def get_queryset(self):
  73. product = SPProduct.objects.get(slug=self.kwargs["slug"],
  74. type=SPProduct.PRODUCT)
  75. return SPProductMSSSection.objects.get_for_product(product)
  76.  
  77.  
  78. class ModuleAccess(permissions.BasePermission):
  79. """
  80. Check if the user has access to the addon's MSS module
  81. """
  82. def has_permission(self, request, view, obj=None):
  83. return obj is None or obj.product.can_download(request.user)
  84.  
  85.  
  86. class ModuleDownload(generics.RetrieveAPIView):
  87. """
  88. Module download endpoint
  89. """
  90. permission_classes = (permissions.IsAuthenticated, ModuleAccess)
  91. model = SPProductMSSModule
  92.  
  93. def get(self, request, *args, **kwargs):
  94. file = self.get_object().file
  95.  
  96. if os.path.exists(file.path):
  97. try:
  98. import mimetypes
  99. mimetype, encoding = mimetypes.guess_type(file.path)
  100. except:
  101. mimetype = "application/force-download"
  102. response = HttpResponse()
  103. response["Content-Disposition"] = "attachment; filename=%s" % os.path.basename(file.path)
  104. response["Content-Type"] = mimetype
  105. response["X-Sendfile"] = file.path
  106. product_downloaded.send(sender=self, user=request.user, product=self.get_object().product)
  107. return response
  108. else:
  109. mail_admins("Package download broken", "URL: %s" % request.build_absolute_uri())
  110. return Response(status=404)
  111.  
  112.  
  113. class LicenseServerException(APIException):
  114. status_code = 503
  115. detail = 'Service temporarily unavailable, try again later.'
  116.  
  117. def __init__(self, status_code=None, detail=None):
  118. if status_code is not None:
  119. self.status_code = status_code
  120. if detail is not None:
  121. self.detail = detail
  122.  
  123.  
  124. class SPProductAppStream(generics.ListAPIView):
  125. """
  126.  
  127. """
  128.  
  129. permission_classes = (permissions.AllowAny,)
  130. def get(self, request, format=None):
  131. """
  132. Return a list of services
  133. """
  134. result = []
  135. category = models.Category
  136. #recupere id category
  137. id_category = category.objects.get(slug='appstream').pk
  138. list_product_appstream = SPProductCategories.objects.filter(category_id=id_category)
  139. #parcoure toutes les produits appstream
  140. for t in list_product_appstream:
  141. produit=SPProduct.objects.filter(pk=t.spproduct_id).filter(mandriva_featured=True)
  142. if produit:
  143. result.append(SPProductappstream(produit, many=True).data)
  144. response = HttpResponse()
  145. response["Content-Type"] = 'application/json'
  146. x = json.dumps(result)
  147. return Response(x)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement