def search(): ''' search for tickets. ''' search_kwargs = {} first_time = not bool(request.args.get('page', default=0, type=int)) form = forms.Search_s(session.get('lang')) if first_time else search.form base_template_arguments = dict( form=form, page_title='Tickets search', offices=data.Office.query, tasks=data.Task.query, users=data.User.query, len=len, operators=data.Operators.query, navbar='#snb1', hash='#da1', serial=data.Serial.query.filter(data.Serial.number != 100)) # NOTE: storing the first form submitted as an endpoint attr instead of a global variable if first_time: setattr(search, 'form', form) if form.validate_on_submit() or not first_time: for form_data, keyword_argument in [ (form.number.data, { 'number': form.number.data }), (form.date.data, { 'date': form.date.data and form.date.data.strftime('%Y-%m-%d') }), (form.tl.data, { 'office_id': form.tl.data }) ]: if form_data and str(form_data).strip(): search_kwargs.update(keyword_argument) if not search_kwargs: flash('Error: fault in search parameters', 'danger') return redirect(url_for('manage_app.search')) tickets_found = data.Serial.query.filter(data.Serial.number != 100)\ .filter_by(**search_kwargs) if not tickets_found.first(): flash('Notice: Sorry, no matching results were found ', 'info') return redirect(url_for('manage_app.search')) page = request.args.get('page', 1, type=int) pagination = tickets_found.order_by(data.Serial.timestamp.desc())\ .paginate(page, per_page=10, error_out=False) return render_template('search_r.html', serials=tickets_found, pagination=pagination, serialsp=pagination.items, **base_template_arguments) return render_template('search.html', **base_template_arguments)
def search(): if current_user.role_id == 3: flash(get_lang(17), "danger") return redirect(url_for('core.root')) form = forms.Search_s(session.get('lang')) if form.validate_on_submit() or request.args.get("page"): from sqlalchemy.sql import and_ terms = [] counter = 0 global pll if request.args.get("page"): ll = pll else: ll = [form.number.data, form.date.data, form.tl.data] pll = ll for l in ll: counter += 1 if l is not None: if counter == 1: if len(str(l)) >= 2: terms.append("number=" + str(ll[0])) elif counter == 2: if len(str(l)) >= 2: terms.append( "date='" + ll[1].strftime('%Y-%m-%d') + "'") elif counter == 3: if l != 0: terms.append("office_id=" + str(ll[2])) if len(terms) == 0: flash(get_lang(48), "danger") return redirect(url_for("manage_app.search")) serials = data.Serial.query.filter(and_(*terms)) if serials.first() is None or serials.order_by(data.Serial.id.desc()).first() == 100: flash(get_lang(49), "info") return redirect(url_for("manage_app.search")) page = request.args.get('page', 1, type=int) if page > int(data.Serial.query.filter(and_(*terms)).count() / 10) + 1: flash(get_lang(4), 'danger') return redirect(url_for('manage_app.search')) pagination = data.Serial.query.filter( and_(*terms)).order_by(data.Serial .timestamp .desc()).paginate( page, per_page=10, error_out=False) return render_template("search_r.html", serials=serials, ptitle="Tickets search", offices=data.Office.query, tasks=data.Task.query, users=data.User.query, pagination=pagination, serialsp=pagination.items, operators=data.Operators.query, len=len, navbar="#snb1", hash="#da1", serial=data.Serial.query) return render_template("search.html", form=form, ptitle="Tickets search", offices=data.Office.query, tasks=data.Task.query, operators=data.Operators.query, navbar="#snb1", hash="#da1", serial=data.Serial.query)
def search(): """ to search for tickets """ form = forms.Search_s(session.get('lang')) if form.validate_on_submit() or request.args.get("page"): from sqlalchemy import text from sqlalchemy.sql import and_ terms = [] counter = 0 global pll if request.args.get("page"): ll = pll else: ll = [form.number.data, form.date.data, form.tl.data] pll = ll for l in ll: counter += 1 if l is not None: if counter == 1: if len(str(l)) >= 2: terms.append(text("number=" + str(ll[0]))) elif counter == 2: if len(str(l)) >= 2: terms.append( text("date='" + ll[1].strftime('%Y-%m-%d') + "'")) elif counter == 3: if l != 0: terms.append(text("office_id=" + str(ll[2]))) if len(terms) == 0: flash("Error: fault in search parameters", 'danger') return redirect(url_for("manage_app.search")) serials = data.Serial.query.filter(and_(*terms)) if serials.first() is None or serials.order_by( data.Serial.id.desc()).first() == 100: flash("Notice: Sorry, no matching results were found ", 'info') return redirect(url_for("manage_app.search")) page = request.args.get('page', 1, type=int) pagination = data.Serial.query.filter(and_(*terms), data.Serial.number != 100)\ .order_by(data.Serial.timestamp.desc())\ .paginate(page, per_page=10, error_out=False) return render_template( "search_r.html", serials=serials, page_title="Tickets search", offices=data.Office.query, tasks=data.Task.query, users=data.User.query, pagination=pagination, serialsp=pagination.items, operators=data.Operators.query, len=len, navbar="#snb1", hash="#da1", serial=data.Serial.query.filter(data.Serial.number != 100)) return render_template( "search.html", form=form, page_title="Tickets search", offices=data.Office.query, tasks=data.Task.query, operators=data.Operators.query, navbar="#snb1", hash="#da1", serial=data.Serial.query.filter(data.Serial.number != 100))