Exemple #1
0
 def testSearchId(self):
     logging.debug('Testing different searches on ids...')
     containers = search_id('report1bis')
     self.assertEqual(len(containers), 1)
     self.assertEqual(containers.get(content__rec_id='search_test_1_other').id, self.ids[1])
     containers = search_id('bundle1', exact=True)
     self.assertEqual(len(containers), 0)
     containers = search_id('alice')
     self.assertEqual(len(containers), 2)
     self.assertEqual(containers.get(content__rec_id='search_test_1').id, self.ids[0])
Exemple #2
0
 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())
Exemple #3
0
def list_bundles(request):
    if request.user.is_anonymous():
        user = User.objects.get(id=ANONYMOUS_USER_ID)
    else:
        user = request.user
    bundles = None
    choice = 0
    if request.method == 'POST':
        form = SearchForm(request.POST)
        if form.is_valid():
            choice = form.cleaned_data['choice']
            if choice == 'name':
                result = search_name(form.cleaned_data['name'])
                choice = 0
            elif choice == 'id':
                result = search_id(form.cleaned_data['id'])
                choice = 1
            elif choice == 'type':
                result = search_literal(form.cleaned_data['literal']+'prov#type', form.cleaned_data['value'])
                choice = 2
            elif choice == 'time':
                start_date = form.cleaned_data['start_time_date']
                start_time = form.cleaned_data['start_time_time']
                if start_date:
                    start = str(start_date) + 'T' + str(start_time)
                else:
                    start = None
                end_date = form.cleaned_data['end_time_date']
                end_time = form.cleaned_data['end_time_time']
                if end_date:
                    end = str(end_date) + 'T' + str(end_time)
                else:
                    end = None
                result = search_timeframe(start, end)
                choice = 3
            elif form.cleaned_data['choice'] == 'any':
                result = search_any_text_field(form.cleaned_data['any'])
                choice = 4
#            if result:
#            result = result.values_list('id', flat=True)
            '''Filter the result by the user permissions '''
            bundles = get_objects_for_user(user=user, perms = ['view_container'],
                                           klass=Container, any_perm=True).filter(id__in=result).\
                                           order_by('-id')
#            else:
#                bundles = []
#            cache.set(request.user.username+'_s', bundles)
            page = 1
        else:
            pass
            #bundles = cache.get(request.user.username+'_s')
    else:
        form = SearchForm()
        page = request.GET.get('page', 1)
#        if page:
#            pass
#            bundles = cache.get(request.user.username+'_s', Container.objects.none())
#        else:
#            pass
#            cache.delete(request.user.username+'_s')    
#        if not bundles:
        bundle_list = get_objects_for_user(user=user, perms = ['view_container'],
                                       klass=Container, use_groups=True,any_perm=True).\
                                       order_by('-id').select_related('content__rec_id', 'owner')
            #cache.set(request.user.username+'_s', bundles)
            
    paginator = Paginator(bundle_list, PAGINATION_THRESHOLD)
    
    ''' Change 'bundles to the actual page object'''
    try:
        bundles = paginator.page(page)
    except PageNotAnInteger:
        bundles = paginator.page(1)
        page = 1
    except EmptyPage:
        bundles = paginator.page(paginator.num_pages)
        page = paginator.num_pages
    return render_to_response('server/list_bundles.html', 
                                  {'bundles': bundles, 'page_list': _pagination(paginator, page),
                                   'form': form, 'choice': choice},
                                  context_instance=RequestContext(request))