def hydrate(self, bundle, request=None): # check not empty if bundle.data['name'].strip() == '': raise ORBAPIBadRequest(ERROR_CODE_TAG_EMPTY) # check not all non-word chars regex = re.compile('[,\.!?"\']') if regex.sub('', bundle.data['name'].strip()) == '': raise ORBAPIBadRequest(ERROR_CODE_TAG_EMPTY) # check tag doesn't already exist try: tag = Tag.objects.get(name=bundle.data['name']) tr = TagResource() bundle = tr.build_bundle(obj=tag, request=request) raise ORBAPIBadRequest(ERROR_CODE_TAG_EXISTS) except Tag.DoesNotExist: pass category = Category.objects.get(slug='other') bundle.obj.create_user_id = bundle.request.user.id bundle.obj.update_user_id = bundle.request.user.id bundle.obj.category_id = category.id return bundle
def hydrate(self, bundle, request=None): bundle.obj.create_user_id = bundle.request.user.id bundle.obj.update_user_id = bundle.request.user.id if 'status' in bundle.data: del bundle.obj.status del bundle.data['status'] # check required fields if 'title' not in bundle.data or bundle.data['title'].strip() == '': raise ORBAPIBadRequest(ERROR_CODE_RESOURCE_NO_TITLE) if 'description' not in bundle.data or bundle.data[ 'description'].strip() == '': raise ORBAPIBadRequest(ERROR_CODE_RESOURCE_NO_DESCRIPTION) no_words = len(strip_tags(bundle.data['description']).split(' ')) if no_words > settings.ORB_RESOURCE_DESCRIPTION_MAX_WORDS: raise ORBAPIBadRequest(ERROR_CODE_RESOURCE_DESCRIPTION_TOO_LONG) request_method = bundle.request.META['REQUEST_METHOD'] # check that resource doesn't already exist for this user if request_method.lower() != 'put': try: resource = Resource.objects.get( create_user=bundle.request.user, title=bundle.data['title']) raise ORBAPIBadRequest(ERROR_CODE_RESOURCE_EXISTS, pk=resource.id) except Resource.DoesNotExist: pass return bundle
def hydrate(self, bundle, request=None): if 'resource_id' not in bundle.data or not bundle.data['resource_id']: raise ORBAPIBadRequest(ERROR_CODE_RESOURCETAG_NO_RESOURCE) if 'tag_id' not in bundle.data or not bundle.data['tag_id']: raise ORBAPIBadRequest(ERROR_CODE_RESOURCETAG_NO_TAG) # check resource exists try: resource = Resource.objects.get(pk=bundle.data['resource_id']) except Resource.DoesNotExist: raise ORBAPIBadRequest(ERROR_CODE_RESOURCE_DOES_NOT_EXIST) # check tag exists try: tag = Tag.objects.get(pk=bundle.data['tag_id']) except Tag.DoesNotExist: raise ORBAPIBadRequest(ERROR_CODE_TAG_DOES_NOT_EXIST) # check that user has permissions on the resource user = User.objects.get(pk=bundle.request.user.id) if not resource_can_edit(resource, user): raise Unauthorized("You do not have edit access for this resource") # check that tag not already added to resource resource_tags = ResourceTag.objects.filter(resource=resource, tag=tag).count() if resource_tags != 0: raise ORBAPIBadRequest(ERROR_CODE_RESOURCETAG_EXISTS) bundle.obj.create_user_id = bundle.request.user.id bundle.obj.resource_id = bundle.data['resource_id'] bundle.obj.tag_id = bundle.data['tag_id'] return bundle
def get_search(self, request, **kwargs): self.method_check(request, allowed=['get']) self.is_authenticated(request) self.throttle_check(request) q = request.GET.get('q', '') page_no = int(request.GET.get('page', 1)) if q == '': raise ORBAPIBadRequest(ERROR_CODE_SEARCH_NO_QUERY) # Allow basic search without Solr based on local configuration if getattr(settings, 'SEARCH_DB', False): sqs = Resource.resources.approved().text_search(q) else: sqs = SearchQuerySet().models(Resource).load_all().auto_query(q) paginator = Paginator(sqs, 20) try: page = paginator.page(page_no) except InvalidPage: raise Http404("Sorry, no results on that page.") objects = [] for result in page.object_list: if result: if getattr(settings, 'SEARCH_DB', False): # Search performed directly against database bundle = self.build_bundle(obj=result, request=request) else: # Search performed against search engine bundle = self.build_bundle(obj=result.object, request=request) bundle = self.full_dehydrate(bundle) objects.append(bundle) object_list = { 'objects': objects, } search.send(sender=sqs, query=q, no_results=sqs.count(), request=request, page=page_no, type=SearchTracker.SEARCH_API) self.log_throttled_access(request) return self.create_response(request, object_list)