def profile_cust_messages(): welcome_note = "Customers" try: page = int(request.args.get("page", 1)) pages = request.args.get("pages") search_q = request.args.get("q", None) except: abort(404) request_args = utils.copy_dict(request.args, {}) query = Message.query.filter(Message.seller_id == current_user.id).order_by(desc(Message.date_created)) results = query.paginate(page, 20, False) if results.has_next: # build next page query parameters request_args["page"] = results.next_num results.next_page = "%s%s" % ("?", urllib.urlencode(request_args)) if results.has_prev: # build previous page query parameters request_args["page"] = results.prev_num results.previous_page = "%s%s" % ("?", urllib.urlencode(request.args)) return render_template("public/profile/cust_messages.html", **locals())
def products(): page_title = "Products" try: page = int(request.args.get("page", 1)) pages = request.args.get("pages") search_q = request.args.get("q", None) except: abort(404) request_args = utils.copy_dict(request.args, {}) query = Product.query.order_by(desc(Product.date_created)) results = query.paginate(page, 20, False) if results.has_next: # build next page query parameters request_args["page"] = results.next_num results.next_page = "%s%s" % ("?", urllib.urlencode(request_args)) if results.has_prev: # build previous page query parameters request_args["page"] = results.prev_num results.previous_page = "%s%s" % ("?", urllib.urlencode(request.args)) return render_template('admin/messages.html', **locals())
def search_results(): page_title = "Search results" """ handles the search by composing the filters and query to make the results. Currently searching products/shops """ try: pages = request.args.get("pages") page = int(request.args.get("page", 1)) size = abs(int(request.args.get("size", 30))) search_q = request.args.get("q", None) except: abort(404) products = site_services.fetch_top_view_products() universities = site_services.fetch_universities() form = SearchForm() form.product_type.choices = [(0, "--- Select One ---")] + [(i.id, i.name) for i in ProductType.query.all()] form.university_id.choices = [(0, "--- Select One ---")] + [(i.id, i.name) for i in universities] form.sec_id.choices = [(0, "--- Select One ---")] + [(i.id, i.name) for i in Section.query.all()] form.cat_id.choices = [(0, "--- Select One ---")] + [(i.id, i.name) for i in Category.query.all()] request_args = utils.copy_dict(form.data, {}) logger.info(request_args) # form = PageFilterForm(request_args, csrf_enabled=False) # search_data = prepare_search_data(form) # page = search_data['page'] # order_by = search_data['order_by'] # tag_id = search_data['tag_id'] # search_q = search_data['q'] query = Product.query.filter(Product.is_enabled == True).order_by( desc(Product.date_created)) if search_q: queries = [Product.name.ilike("%%%s%%" % search_q)] query = query.filter(or_(*queries)) results = query.paginate(page, 20, False) if results.has_next: # build next page query parameters request_args["page"] = results.next_num results.next_page = "%s%s" % ("?", urllib.urlencode(request.args)) if results.has_prev: # build previous page query parameters request_args["page"] = results.prev_num results.previous_page = "%s%s" % ("?", urllib.urlencode(request.args)) return render_template("public/search-results.html", **locals())
def profile_message_response(id): obj = AdminMessage.query.get(id) if not obj: abort(404) try: page = int(request.args.get("page", 1)) pages = request.args.get("pages") search_q = request.args.get("q", None) except: abort(404) request_args = utils.copy_dict(request.args, {}) is_cust = False query = AdminMessage.query.filter(AdminMessage.user_id == current_user.id).order_by(desc(AdminMessage.date_created)) results = query.paginate(page, 20, False) if results.has_next: # build next page query parameters request_args["page"] = results.next_num results.next_page = "%s%s" % ("?", urllib.urlencode(request_args)) if results.has_prev: # build previous page query parameters request_args["page"] = results.prev_num results.previous_page = "%s%s" % ("?", urllib.urlencode(request.args)) reply_form = ReplyForm() if reply_form.validate_on_submit(): data = reply_form.data data["admin_message_id"]=id data["user_id"] = current_user.id reply = operations.AdminMessageResponseService.create(**data) return redirect(url_for('.profile_message_response',id=id)) return render_template("public/profile/message_response.html", **locals())
def execute_return(self, resource_name, query, service_class, filter_args={}, search_args={}, resource_fields=[], operator_args={}, from_cache=True, **kwargs): resp = {"endpoint": resource_name} search_q = search_args.get("query") search_id = search_args.get("id") op = operator_args.get("op") # TODO implement permission check here # self.is_permitted() # execute limit query: query = self.limit_query(query) # apply the query filters for name, value in filter_args.items(): query = operator_func(query, service_class, op, name, value) if self.first_result_only: res = query.first() if not res: _data_ = {"status":"Result not Found","message":"Requested Information does not exist"} _data_ = json.dumps(_data_) resp = make_response(_data_, 404) resp.headers['Content-Type'] = "application/json" return resp output_fields = self.output_fields output_fields.update(self.resource_fields) return marshal(res, output_fields), 200 # apply searching based on searchable fields if search_q and self.searchable_fields: like_queries = [(getattr(service_class.model_class, name).ilike("%%%s%%" % search_q)) for name in self.searchable_fields] query = query.filter(or_(*like_queries)) # execute the query and include paging resp["op"] = op resp["pagers"] = self.pagers resp["filters"] = self.filters resp["sorters"] = self.sorters # extract the request args and modify them for paging request_args = copy_dict(request.args, {}) # if paging.has_next: # # build next page query parameters # request_args["page"] = paging.next_num # resp["next"] = paging.next_num # resp["next_page"] = "%s%s" % ("?", urllib.urlencode(request_args)) # # if paging.has_prev: # # build previous page query parameters # request_args["page"] = paging.prev_num # resp["prev"] = paging.prev_num # resp["prev_page"] = "%s%s" % ("?", urllib.urlencode(request_args)) _models = [] resp['results'] = {} # for c in resource_fields: # resp['results'].update({c+"s":{}}) # for c in resource_fields: # model_class = getattr(models, c.capitalize(), None) # o = getattr(self, "%s_%ss_resource_fields"%(resource_name.lower(),c), None) # o.update(self.output_fields) # for obj in query: # if isinstance(obj, model_class): # getattr(self, "%s_%ss"%(resource_name.lower(), c), None).append(obj) # print getattr(self, "%s_%ss"%(resource_name.lower(), c), None), "im collated" # else: pass # resp["results"][c+"s"] = marshal(getattr(self, "%s_%ss"%(resource_name.lower(), c), None), o) # # from operator import itemgetter # # newlist = sorted(resp["results"][c+"s"], key=itemgetter('id'), reverse=True) # # resp["results"][c+"s"] = newlist # _models.append(model_class) for key, value in query.items(): out = getattr(self, "%s_%s_resource_fields"%(resource_name.lower(),key), None) try: out.update(self.output_fields) except: pass resp["results"][key] = marshal(value, out) # TODO: Figure out how to handle exceptions so that it works out well return resp, 200
def execute_query(self, resource_name, query, service_class, filter_args={}, sort_args={}, search_args={}, paging_args={}, resource_fields={}, operator_args={}, from_cache=True, **kwargs): resp = {"endpoint": resource_name} order_by, asc_desc = sort_args.get("order_by"), sort_args.get("asc_desc") page, per_page, error_out = paging_args.get("page"), paging_args.get("per_page"), paging_args.get("error_out") search_q = search_args.get("query") search_id = search_args.get("id") op = operator_args.get("op") # TODO implement permission check here # self.is_permitted() # execute limit query: query = self.limit_query(query) # apply the query filters for name, value in filter_args.items(): query = operator_func(query, service_class, op, name, value) # apply sorting _sort = sort_func(asc_desc) # Extracts which sorting direction is required query = query.order_by(_sort(getattr(service_class.model_class, order_by))) if self.first_result_only: res = query.first() if not res: _data_ = {"status":"Result not Found","message":"Requested Information does not exist"} _data_ = json.dumps(_data_) resp = make_response(_data_, 404) resp.headers['Content-Type'] = "application/json" return resp output_fields = self.output_fields output_fields.update(self.resource_fields) return marshal(res, output_fields), 200 # apply searching based on searchable fields if search_q and self.searchable_fields: like_queries = [(getattr(service_class.model_class, name).ilike("%%%s%%" % search_q)) for name in self.searchable_fields] query = query.filter(or_(*like_queries)) # execute the query and include paging paging = query.paginate(page, per_page, error_out) resp["order_by"] = order_by resp["asc_desc"] = asc_desc resp["page"] = paging.page resp["total"] = paging.total resp["pages"] = paging.pages resp["per_page"] = per_page resp["op"] = op resp["pagers"] = self.pagers resp["filters"] = self.filters resp["sorters"] = self.sorters # extract the request args and modify them for paging request_args = copy_dict(request.args, {}) if paging.has_next: # build next page query parameters request_args["page"] = paging.next_num resp["next"] = paging.next_num resp["next_page"] = "%s%s" % ("?", urllib.urlencode(request_args)) if paging.has_prev: # build previous page query parameters request_args["page"] = paging.prev_num resp["prev"] = paging.prev_num resp["prev_page"] = "%s%s" % ("?", urllib.urlencode(request_args)) output_fields = self.output_fields _resource_fields = resource_fields or self.resource_fields output_fields.update(_resource_fields) resp["results"] = marshal(paging.items, output_fields) # TODO: Figure out how to handle exceptions so that it works out well return resp, 200