def _create_full_text_query(self, query): '''Modifies the query for full text search boolean mode. This adds a + and * char to each word. + sets boolean AND search. * activates wildcard search''' fts_query = '' for word in query.split(): fts_query = "{0}+{1}* ".format(fts_query, word) fts_query = fts_query[:-1] return fts_query
def search(query, user, page=0, count=None): """Look in columns specified by self.SEARCH_FIELDS for the given query. Return a queryset with the results.""" qs = Node.objects.mine(user).select_related('todo_state') # Apply keyword searches. def construct_search(field_name): if field_name.startswith('^'): return "%s__istartswith" % field_name[1:] elif field_name.startswith('='): return "%s__iexact" % field_name[1:] elif field_name.startswith('@'): return "%s__search" % field_name[1:] else: return "%s__icontains" % field_name if Node.SEARCH_FIELDS and query: for bit in query.split(): or_queries = [ models.Q(**{construct_search(str(field_name)): bit}) for field_name in Node.SEARCH_FIELDS ] qs = qs.filter(reduce(operator.or_, or_queries)) for field_name in Node.SEARCH_FIELDS: if '__' in field_name: qs = qs.distinct() break # Limit the number of results to the value of `count` total = qs.count() if count: qs = qs[page*count:(page+1)*count] return (qs, total)
def search_by(self, query, column_name=None): if column_name is None: # Global search search_method = getattr(self, 'search', None) if callable(search_method): self.qs = search_method(self.qs, query) else: tokens = [token for token in query.split() if token] search_lookups = [ A(accessor_str).django_lookup + '__icontains' for column in self.columns.values() if column.searchable for accessor_str in ( column.search_in or (column.accessor, )) ] if tokens and search_lookups: global_search_lookup = reduce(and_, [ reduce( or_, [Q((lookup, token)) for lookup in search_lookups]) for token in tokens ]) self.qs = self.qs.filter(global_search_lookup) else: # Column search search_method = getattr(self, 'search_' + column_name, None) if callable(search_method): self.qs = search_method(self.qs, query) else: # TODO: implement the default case pass self.filtered_qs = self.qs
def search(query, user, page=0, count=None): """Look in columns specified by self.SEARCH_FIELDS for the given query. Return a queryset with the results.""" qs = Node.objects.mine(user).select_related('todo_state') # Apply keyword searches. def construct_search(field_name): if field_name.startswith('^'): return "%s__istartswith" % field_name[1:] elif field_name.startswith('='): return "%s__iexact" % field_name[1:] elif field_name.startswith('@'): return "%s__search" % field_name[1:] else: return "%s__icontains" % field_name if Node.SEARCH_FIELDS and query: for bit in query.split(): or_queries = [ models.Q(**{construct_search(str(field_name)): bit}) for field_name in Node.SEARCH_FIELDS ] qs = qs.filter(reduce(operator.or_, or_queries)) for field_name in Node.SEARCH_FIELDS: if '__' in field_name: qs = qs.distinct() break # Limit the number of results to the value of `count` total = qs.count() if count: qs = qs[page * count:(page + 1) * count] return (qs, total)
def search(request): if request.method == 'POST': try: data = get_home() query = request.POST.get('myInput') query = query.split() product_ids = list() for word in query: tags = Tag.objects.filter(name=word) if len(tags) == 0: continue else: for tag in tags: kkdict = json.loads(tag.product_list) product_ids.extend(kkdict['product_id']) product_ids = tuple(set(product_ids)) products = list() for id in product_ids: pd = Product.objects.filter(pk=int(id)) if len(pd) == 0: continue else: products.append(pd[0]) del data['product'] data['product'] = products except Exception as ex: print(f"SEARCH EX : {ex}") return redirect('home') else: print(f"SEARCH SUCCESS : {query}") return render(request, 'front/home.html', data) else: return redirect('home')
def login(request): user = request.user if user.is_authenticated: return redirect('user_home') else: if request.method == 'POST': username = request.POST.get('username') password = request.POST.get('password') user = authenticate(request, username=username, password=password) try: user_obj = User.objects.get(username=username) except User.DoesNotExist: if user is None: uname_error = 'Invalid creditials ! !' context = { 'uname_error': uname_error, } return render(request, 'User/login.html', context) if user_obj.is_active == False: block_error = 'This user was blocked' context = { 'block_error': block_error, } return render(request, 'User/login.html', context) elif user is None: error = "Invalid creditials ! !" context = { 'block_error': error, } return render(request, 'User/login.html', context) elif user is not None: # This try and exception are guest cartItems merge to the user cart try: cart = Cart.objects.get(cart_id=_cart_session_id(request)) exists = CartItem.objects.filter(cart=cart).exists() if exists: cart_item = CartItem.objects.filter(cart=cart) for ct in cart_item: ct.user = user ct.save() except: pass auth_login(request, user) url = request.META.get('HTTP_REFERER') try: query = requests.utils.urlparse(url).query params = dict(x.split('=') for x in query.split('&')) if 'next' in params: next_page = params['next'] return redirect(next_page) except: return redirect('user_home') return render(request, 'User/login.html')
def login(request): if request.method == 'POST': email = request.POST['email'] password = request.POST['password'] user = auth.authenticate(email=email, password=password) if user is not None: try: cart = Cart.objects.get(cart_id=_cart_id(request)) is_cart_item_exist = CartItem.objects.filter( cart=cart).exists() if is_cart_item_exist: cart_item = CartItem.objects.filter(cart=cart) product_variation = [] for item in cart_item: variation = item.variations.all() product_variation.append(list(variation)) cart_item = CartItem.objects.filter(user=user) ex_var_list = [] id = [] for item in cart_item: existing_variation = item.variations.all() ex_var_list.append(list(existing_variation)) id.append(item.id) for pr in product_variation: if pr in ex_var_list: index = ex_var_list.index(pr) item_id = id[index] item = CartItem.objects.get(id=item_id) item.quantity += 1 item.user = user item.save() else: cart_item = CartItem.objects.filter(cart=cart) for item in cart_item: item.user = user item.save() except: pass auth.login(request, user) messages.success(request, 'You Are Now Logged In') url = request.META.get('HTTP_REFERER') try: query = requests.utils.urlparse(url).query params = dict(x.split('=') for x in query.split('&')) if 'next' in params: nextPage = params['next'] return redirect(nextPage) except: return redirect('dashboard') else: messages.error(request, 'Invalid Login Credentials') return redirect('login') return render(request, 'accounts/login.html')