本文整理汇总了Python中tastypie.resources.ModelResource类的典型用法代码示例。如果您正苦于以下问题:Python ModelResource类的具体用法?Python ModelResource怎么用?Python ModelResource使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ModelResource类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: bundleItemCollection
def bundleItemCollection(modelResource, request, items):
item_list = []
for item in items:
bundle = ModelResource.build_bundle(modelResource, obj=item, request=request)
bundle = ModelResource.full_dehydrate(modelResource, bundle)
item_list.append(bundle)
return item_list;
示例2: _handle_500
def _handle_500(self, request, exception):
if settings.DEBUG == False:
settings.DEBUG = True
resp = ModelResource._handle_500(self, request, exception)
settings.DEBUG = False
return resp
else:
return ModelResource._handle_500(self, request, exception)
示例3: dispatch_list
def dispatch_list(self, request, **kwargs):
""" Handle the dispatching of GFKs. """
self.method_check(request, allowed=["get", "post"])
self.is_authenticated(request)
self.throttle_check(request)
if "content_type" in kwargs and "object_id" in kwargs and request.method == "POST":
data = json.loads(request.body)
if "profile_id" in data:
profile = get_object_or_404(Profile, pk=data["profile_id"])
else:
profile = request.user.profile
objectprofilelink_item, created = ObjectProfileLink.objects.get_or_create( # NOQA
profile=profile,
content_type=ContentType.objects.get(model=kwargs["content_type"]),
object_id=kwargs["object_id"],
level=data["level"],
detail=data["detail"],
isValidated=data["isValidated"],
)
bundle = self.build_bundle(obj=objectprofilelink_item, request=request)
bundle = self.full_dehydrate(bundle)
bundle = self.alter_detail_data_to_serialize(request, bundle)
return self.create_response(
request, bundle, response_class=http.HttpCreated, location=self.get_resource_uri(bundle)
)
return ModelResource.dispatch_list(self, request, **kwargs)
示例4: full_dehydrate
def full_dehydrate(self, bundle, for_list=False):
bundle = ModelResource.full_dehydrate(self, bundle, for_list=for_list)
bundle.related_obj = self
if for_list is True:
fk = fields.ForeignKey(I4pProjectListResource, attribute="master", full=True)
bundle.data["project"] = fk.dehydrate(bundle)
return bundle
示例5: dispatch_list
def dispatch_list(self, request, **kwargs):
self.method_check(request, allowed=['get', 'post'])
self.is_authenticated(request)
self.throttle_check(request)
if 'content_type' in kwargs and 'object_id' in kwargs and request.method=="POST":
data = json.loads(request.body)
if 'tag' in data:
tag_obj, created = Tag.objects.get_or_create(name=data['tag'], slug=slugify(data['tag']))
params = kwargs.copy()
del params['resource_name']
del params['api_name']
params['tag'] = tag_obj
params['content_type'] = ContentType.objects.get(model=kwargs['content_type'])
tagged_item, created = self._meta.queryset.model.objects.get_or_create(**params)
bundle = self.build_bundle(obj=tagged_item, request=request)
bundle = self.full_dehydrate(bundle)
bundle = self.alter_detail_data_to_serialize(request, bundle)
return self.create_response(request,
bundle,
response_class=http.HttpCreated,
location=self.get_resource_uri(bundle))
return ModelResource.dispatch_list(self, request, **kwargs)
示例6: full_hydrate
def full_hydrate(self, bundle):
"""
Override to just call tastypie's full_hydrate.
django-tastypie-mongoengine does a lot of extra checks that make some
assumptions that break things in their full_hydrate method.
"""
return ModelResource.full_hydrate(self, bundle)
示例7: dehydrate
def dehydrate(self, bundle):
bundle = ModelResource.dehydrate(self, bundle)
bundle = ImageMixin.dehydrate(self, bundle)
if bundle.obj.activity == Spirit.ACTIVITY_WANDER:
if bundle.obj.health_current == 0:
bundle.data["experience_given"] = bundle.obj.get_ladder().xp_given
return bundle
示例8: obj_create
def obj_create(self, bundle, **kwargs):
try:
return ModelResource.obj_create(self, bundle, **kwargs)
except IntegrityError:
# This has the negative side-effect of always making the server
# respond with (201) regardless of whether or not the object was
# just created. It would be better if it could return (200) if the
# object already existed.
allocation_id = simplejson.loads(bundle.request.body)['allocation_id']
fileset = models.Fileset.objects.get(allocation_id=allocation_id)
bundle.obj = fileset
return bundle
示例9: api_field_from_django_field
def api_field_from_django_field(cls, f, default=fields.CharField):
"""
Returns the field type that would likely be associated with each
Django type.
"""
result = default
internal_type = f.get_internal_type()
if internal_type in ('JSONField'):
return JSONApiField
else:
return ModelResource.api_field_from_django_field(f, default)
示例10: full_dehydrate
def full_dehydrate(self, bundle, for_list=False):
bundle = ModelResource.full_dehydrate(self, bundle, for_list)
if bundle.obj.picture:
thumbnailer = get_thumbnailer(bundle.obj.picture)
thumbnail_options = {'size': (ResizeThumbApi.width, ResizeThumbApi.height)}
bundle.data["thumb"] = thumbnailer.get_thumbnail(thumbnail_options).url
else:
bundle.data["thumb"] = None
if for_list is False:
bundle.data["tags"] = [tag.name for tag in Tag.objects.get_for_object(bundle.obj)]
if(bundle.obj.picture):
thumbnail_options = {'size': (ResizeDisplay.width, ResizeDisplay.width)}
bundle.data["image"] = thumbnailer.get_thumbnail(thumbnail_options).url
else:
bundle.data["image"] = None
try:
bundle.data["article"] = Article.get_for_object(bundle.obj).render()
except ArticleForObject.DoesNotExist:
bundle.data["article"] = None
return bundle
示例11: obj_get_list
def obj_get_list(self, bundle, **kwargs):
if bundle.request.GET.get("finder"):
if not bundle.request.location:
raise BadRequest(
"Finder cannot be invoked without a location header"
)
if not bundle.request.user.is_authenticated():
raise BadRequest(
"Finder is only available to authenticated users"
)
try:
return self._finder(bundle.request)
except ValidationError as e:
raise BadRequest(e.messages[0])
else:
return ModelResource.obj_get_list(self, bundle, **kwargs)
示例12: dispatch_list
def dispatch_list(self, request, **kwargs):
self.method_check(request, allowed=['get', 'post'])
self.is_authenticated(request)
self.throttle_check(request)
if 'content_type' in kwargs and 'object_pk' in kwargs and request.method=="POST":
data = json.loads(request.body)
commented_item, created = Comment.objects.get_or_create(comment=data['comment_text'],
user=request.user,
user_name=request.user.username,
content_type=ContentType.objects.get(model=kwargs['content_type']),
object_pk=kwargs['object_pk'],
site_id=settings.SITE_ID,
submit_date=datetime.datetime.now())
bundle = self.build_bundle(obj=commented_item, request=request)
bundle = self.full_dehydrate(bundle)
bundle = self.alter_detail_data_to_serialize(request, bundle)
return self.create_response(request,
bundle,
response_class=http.HttpCreated,
location=self.get_resource_uri(bundle))
return ModelResource.dispatch_list(self, request, **kwargs)
示例13: get_object_list
def get_object_list(self, request):
''' Method to return the list of objects via GET method to the Resource (not concrete).
If the variable 'search_type' is present returns the appropriate bundles
which match the searching query. 'search_type' can have several values:
'name' - accompanied by 'q_str' variable containing the search string
returns all Bundles containing the q_str in their name.
'id' - accompanied by 'q_str' variable containing the search string
returns all Bundles containing a record that contains the q_str in their name.
'type' - accompanied by 'q_str' variable containing the search string
returns all Bundles containing a literal attribute with type prov:type
and value containing q_str.
'time' - accompanied by 'start' and/or 'end' variable containing the times
returns all Bundles with within the time frame [strat:end]
'any' - accompanied by 'q_str' variable containing the search string
returns all Bundles containing anything matching q_str.
'''
search_type = request.GET.get('search_type', None)
if not search_type:
return ModelResource.get_object_list(self, request)
try:
if search_type == 'name':
result = search_name(request.GET.get('q_str', None))
elif search_type == 'id':
result = search_id(request.GET.get('q_str', None))
elif search_type == 'type':
result = search_literal(request.GET.get('literal', None) + 'prov#type', request.GET.get('q_str', None))
elif search_type == 'time':
result = search_timeframe(request.GET.get('start', None), request.GET.get('end', None))
elif search_type == 'any':
result = search_any_text_field(request.GET.get('q_str', None))
else:
raise ImmediateHttpResponse(HttpBadRequest())
return result
except:
raise ImmediateHttpResponse(HttpBadRequest())
示例14: build_filters
def build_filters(self, filters=None):
if 'tags__contains' in filters:
filters['tags__contains'] = filters['tags__contains'].split(',')
return ModelResource.build_filters(self, filters)
示例15: api_field_from_django_field
def api_field_from_django_field(cls, f, **kwargs):
internal_type = f.get_internal_type()
if internal_type == "BigIntegerField":
return fields.IntegerField
return ModelResource.api_field_from_django_field(f, **kwargs)