def search_results(lang, page=1, sortargs=None, query_string=None, ajax=False, **kwargs): table_headers = ( {'name': 'Name', 'field': 'name', 'value': '{labels}{name}', 'sort': 'files_file.name'}, {'name': 'Score', 'field': 'score', 'value': '{score}', 'sort': 'files_file.ratings_score', 'desc': True, 'class': 'tc'}, {'name': 'Downloads', 'field': 'downloads', 'value': '{downloads}', 'sort': 'files_file.downloads_total', 'style': 'width: 70px;', 'desc': True, 'class': 'tc'}, {'name': 'Last Update', 'field': 'date', 'value': '{last_post_date}', 'desc': True, 'sort': 'files_file.last_post_date', 'style': 'width: 80px;', 'class': 'tr'}, ) vgtable = VersionGame._meta.db_table vtable = Version._meta.db_table fdtable = FileData._meta.db_table listobj = ListView('files_search', File, table_headers) if ajax: listobj.set_query_string(listobj.filter_sajax(query_string)) else: listobj.set_query_string(query_string) listobj.set_sort_args(*sortargs) listobj.set_filter_args(**kwargs) listobj.set_default_sort('name', '-date') listobj.set_page(page) listobj.show_filters = False if stub_slug: listobj.add_filter('category__slug__exact', stub_slug) elif cat_slug: listobj.add_filter('category__parent__slug__exact', cat_slug) from operator import or_ for k,v in kwargs.iteritems(): if k == 'q' and v[0]: qs = v[0].split(',') q_objects = [] if kwargs.get('q_descs', 0) == 1: pass listobj.add_extra('tables', fdtable) listobj.add_extra('where', "`%s`.`file_id` = `files_file`.`id`" % fdtable) listobj.add_extra('where', "`%s`.`lang` = '%s'" % (fdtable, lang)) for q in qs: q_type = kwargs.get('q_type', 0) if q_type == 3: q_objects.append(Q(name__endswith=q)) elif q_type == 2: q_objects.append(Q(name__startswith=q)) elif q_type == 1: q_objects.append(Q(name__exact=q)) else: q_objects.append(Q(name__icontains=q)) if kwargs.get('q_labels', 0) == 1: q_objects.append(Q(labels__icontains=q)) if kwargs.get('q_descs', 0) == 1: pass #q_objects.append(Q( if len(q_objects): listobj.add_cfilter(reduce(or_, q_objects)) elif k == 'label' and len(v) > 0: for label in v: if label.strip(): listobj.add_filter('labels__icontains', label) elif k == 'game' and len(v) > 0: listobj.add_extra('tables', vtable) listobj.add_extra('tables', vgtable) listobj.add_extra('where', "`%s`.`file_id` = `files_file`.`id`" % vtable) listobj.add_extra('where', "`%s`.`version_id` = `files_version`.`id`" % vgtable) string = [] for game in v: if game: args = game.split(':') if len(args) == 2: string.append("`%s`.`game_version_id` = '%s'" % (vgtable, args[1])) else: string.append("`%s`.`game_id` = '%s'" % (vgtable, args[0])) if len(string): string = ') | ('.join(string) listobj.add_extra('where', string) elif k == 'cat' and len(v) > 0: q_objects = [] for cat in v: if cat: args = cat.split(':') if len(args) == 2: q_objects.append(Q(category__exact=args[1])) else: q_objects.append(Q(category__parent__exact=args[0])) if len(string): listobj.add_cfilter(reduce(or_, q_objects)) cfilter = get_related_filters_for_versions(settings.SITE_ID) if cfilter: listobj.add_cfilter(cfilter) listobj.add_filter('visible', True, True) listobj.get_results() if not listobj.is_compiled: nlist = [] for obj in listobj.list: if obj.labels: labels = '%s' % ', '.join(obj.get_label_names()) else: labels = '' nlist.append({ 'id': obj.id, 'name': obj.name, 'labels': labels, 'url': obj.get_absolute_url(), 'score': obj.get_score(), 'downloads': obj.downloads_total, 'last_post_date': obj.last_post_date.strftime('%m/%d/%Y') }) listobj.list = nlist listobj.compile() # these variables are required to be sent context = listobj.get_context() # things that arent used in page generation (such as filter lists) should not be sent w/ the ajax call if not ajax: context.update({ 'page_name': 'Files', 'path': '/%s/files/' % lang, 'table_js': listobj.dump_js(table_headers), 'sort_listjs': listobj.dump_js(context['sort_list']), 'table_headers': table_headers, }) if cat_slug: context['path'] += '%s/' % cat_slug if stub_slug: context['path'] += '%s/' % stub_slug if ajax: return listobj.dump_js(context) return context