Guest User

Untitled

a guest
Jan 23rd, 2018
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.80 KB | None | 0 0
  1. diff --git a/qualitio/api/__init__.py b/qualitio/api/__init__.py
  2. index 8ba4d36..42a464d 100644
  3. --- a/qualitio/api/__init__.py
  4. +++ b/qualitio/api/__init__.py
  5. @@ -22,7 +22,7 @@ Serializer.content_types = {'json': 'application/json'}
  6. class BaseMeta(object):
  7. default_format = 'application/json'
  8. serializer = Serializer(formats=Serializer.formats, content_types=Serializer.content_types)
  9. - authentication = BasicAuthentication()
  10. + # authentication = BasicAuthentication()
  11. authorization = DjangoAuthorization()
  12.  
  13.  
  14. @@ -146,3 +146,8 @@ api.register(WordResource())
  15.  
  16.  
  17. urls = api.urls
  18. +
  19. +
  20. +from pprint import pprint
  21. +for u in urls:
  22. + pprint(u.regex.pattern)
  23. diff --git a/qualitio/core/custommodel/api.py b/qualitio/core/custommodel/api.py
  24. index 9be7018..a2afd21 100644
  25. --- a/qualitio/core/custommodel/api.py
  26. +++ b/qualitio/core/custommodel/api.py
  27. @@ -5,9 +5,14 @@ automatically generated reverse ``customization`` field.
  28. For models that has customizations it will automatically
  29. register customization model and resource.
  30. """
  31. +import re
  32. +from django.conf.urls.defaults import *
  33. +
  34. from tastypie.resources import ModelResource as TastypieModelResource, ModelDeclarativeMetaclass
  35. from tastypie import fields
  36. +from tastypie.utils import trailing_slash
  37. from tastypie.api import Api as TastypieApi
  38. +from tastypie.bundle import Bundle
  39.  
  40.  
  41. def create_customization_model_resource(customization_model):
  42. @@ -38,11 +43,27 @@ class CustomizationModelDeclarativeMetaclass(ModelDeclarativeMetaclass):
  43. return super(CustomizationModelDeclarativeMetaclass, cls).__new__(cls, cls_name, bases, attrs)
  44.  
  45.  
  46. -class ModelResource(TastypieModelResource):
  47. +class ProjectUpdater(object):
  48. + def _update_reverse_url_kwargs(self, kwargs):
  49. + from qualitio.core.middleware import THREAD
  50. + kwargs = kwargs or {}
  51. + kwargs.update(project=getattr(getattr(THREAD, 'project', None), 'slug', None))
  52. + return kwargs
  53. +
  54. +
  55. +class ModelResource(TastypieModelResource, ProjectUpdater):
  56. __metaclass__ = CustomizationModelDeclarativeMetaclass
  57.  
  58. + def _build_reverse_url(self, name, args=None, kwargs=None):
  59. + return super(ModelResource, self)._build_reverse_url(
  60. + name, args=args, kwargs=self._update_reverse_url_kwargs(kwargs))
  61. +
  62. + def obj_get(self, request=None, **kwargs):
  63. + kwargs['project'] = request.project
  64. + return super(ModelResource, self).obj_get(request=request, **kwargs)
  65. +
  66.  
  67. -class Api(TastypieApi):
  68. +class Api(TastypieApi, ProjectUpdater):
  69. def register(self, resource, canonical=True):
  70. super_register = super(Api, self).register
  71. CustomizationModelResource = getattr(resource.__class__, 'CustomizationModelResource', None)
  72. @@ -61,3 +82,26 @@ class Api(TastypieApi):
  73. super_unregister(resource_class.CustomizationModelResource._meta.resource_name)
  74.  
  75. super_unregister(resource_name)
  76. +
  77. + def _build_reverse_url(self, name, args=None, kwargs=None):
  78. + return super(ModelResource, self)._build_reverse_url(
  79. + name, args=args, kwargs=self._update_reverse_url_kwargs(kwargs))
  80. +
  81. + @property
  82. + def urls(self):
  83. + pattern_list = [
  84. + url(r"^project/(?P<project>[\w-]+)/(?P<api_name>%s)%s$" % (self.api_name, trailing_slash()), self.wrap_view('top_level'), name="api_%s_top_level" % self.api_name),
  85. + ]
  86. +
  87. + for name in sorted(self._registry.keys()):
  88. + self._registry[name].api_name = self.api_name
  89. + pattern_list.append((r"^project/(?P<project>[\w-]+)/(?P<api_name>%s)/" % self.api_name, include(self._registry[name].urls)))
  90. +
  91. + urlpatterns = self.override_urls() + patterns('',
  92. + *pattern_list
  93. + )
  94. + return urlpatterns
  95. +
  96. + def top_level(self, request, **kwargs):
  97. + kwargs.pop('project', None)
  98. + return super(Api, self).top_level(request, **kwargs)
Add Comment
Please, Sign In to add comment