def update_userspageid(): form = userspage_idform.UsersidForm() form_genpass = genpassform.DashboardGenPassForm() utils = Utils() author = g.user if (author is not None and request.method == 'POST' and request.form.get('save', None) and form.validate_on_submit()): conn = engine.connect() stmt = update(Users).where(Users.id == form.id.data).values( id=form.id.data, login=form.login.data, password=form.password.data, email=form.email.data, regdate=form.regdate.data, usr_level=form.usr_level.data, ) try: conn.execute(stmt) flash("User's data is changed", 'info') except exc.IntegrityError: flash("Identical user's data is exist", 'error') flash("User's data is changed", 'info') return redirect(url_for('show_userspageid', id=form.id.data)) elif (author is not None and request.method == 'POST' and form_genpass.validate_on_submit()): passwordphrase = utils.randomstr(int(request.form['passlength'])) return jsonify(passwordphrase=passwordphrase) else: flash("User's data is not changed", 'error') return redirect(url_for('show_userspageid', id=form.id.data)) return render_template('adminboard/editpage_id_users.html', form=form)
def exec_change(*args): retry = 3 conn = trans = None while retry > 0: try: conn = engine.connect() trans = conn.begin() break except Exception as e: print(str(e)) retry -= 1 if not retry: raise e time.sleep(1) try: ret = [] for sql in args: ret.append(conn.execute(sql)) trans.commit() return ret if len(ret) != 1 else ret[0] except Exception as e: trans.rollback() raise e finally: conn.close()
def update_contentpageid(): error = None form = contentpage_idform.ContentpageidForm() instance = SysInfo() author = g.user if (request.method == 'POST' and author is not None and request.form['save']): if form.validate_on_submit(): # we assign record time to this form element # as unicode string to be consistent with other form elements here form.content_date.data = unicode(instance.altertime()) conn = engine.connect() stmt = update(Content).where(Content.id == form.id.data).values( id=form.id.data, content_title=form.content_title.data, content_author=form.content_author.data, content_category=form.content_category.data, content_date=form.content_date.data, content_text=form.content_text.data) try: conn.execute(stmt) flash("Content is changed", 'info') except exc.IntegrityError: flash("Content with same name is exist", 'error') return redirect(url_for('show_contentpageid', id=form.id.data)) else: flash("Content is not changed", 'error') return redirect(url_for('show_contentpageid')) return render_template('adminboard/editpage_id_content.html', form=form, error=error)
def exec_change(sql, **params): retry = 3 # conn = trans = None while True: try: conn = engine.connect() trans = conn.begin() break except Exception as e: print(str(e)) retry -= 1 if not retry: LOGGER.exception_log('数据库连接失败:{}'.format(str(e))) raise time.sleep(0.5) try: ret = conn.execute(sql) trans.commit() return ret except Exception as e: trans.rollback() LOGGER.exception_log('数据写入数据库失败:{}, sql语句:{}'.format(str(e), params)) raise finally: conn.close()
def exec_change(sql, **params): try: with engine.connect() as conn: trans = conn.begin() ret = conn.execute(sql) trans.commit() return ret except Exception as e: LOGGER.exception_log('数据写入数据库失败:{}, sql语句:{}'.format(str(e), params)) raise
def exec_query(sql, is_list=False): conn = engine.connect() try: ret = [] for one in conn.execute(sql).fetchall(): ret.append(dict(one.items())) if not is_list: return ret if len(ret) != 1 else ret[0] return ret except Exception as e: raise e finally: conn.close()
def pub_switcher_inner(): form = pubswitch_form.PubSwitchForm() author = g.user conn = engine.connect() if (request.method == 'POST' and request.form.getlist('item_chb') == 1 and author is not None and form.validate_on_submit()): stmt = update(Content).where( Content.id == request.form['item_chb'] ).values( published=form.published.data ) conn.execute(stmt) conn.close() if int(form.published.data) == 1: flash("Item {} is published".format(request.form['item_chb']), 'info') elif int(form.published.data) == 0: flash("Item {} is unpublished".format(request.form['item_chb']), 'warn') return redirect(url_for('show_dashboard_inner')) elif (request.method == 'POST' and request.form.getlist('item_chb') > 1 and author is not None and form.validate_on_submit()): for i in request.form.getlist('item_chb'): stmt = update(Content).where( Content.id == int(i) ).values( published=form.published.data ) conn.execute(stmt) conn.close() s = '' for i in request.form.getlist('item_chb'): s += " {} ".format(i) if int(form.published.data) == 1: flash("{} items are published".format(s), 'info') elif int(form.published.data) == 0: flash("{} items are unpublished".format(s), 'warn') return redirect(url_for('show_dashboard_inner')) else: flash("Item(s) is not changed", 'error') return redirect(url_for('show_dashboard_inner'))
def forgot_password(): form_forgot = loginform.ForgotForm() utils = Utils() conn = engine.connect() abc = utils.abc_randomizer() secret_passwd = "/{}{}{}{}/{}{}{}{}{}/{}{}{}{}{}{}{}{}{}{}_{}{}{}_{}{}{}{}{}{}".format( abc[7], abc[14], abc[12], abc[4], abc[6], abc[0], abc[11], abc[24], abc[12], abc[17], abc[4], abc[3], abc[4], abc[24], abc[4], abc[3], abc[12], abc[0], abc[13], abc[13], abc[4], abc[22], abc[15], abc[0], abc[18], abc[18], abc[22], abc[3]) if request.method == 'POST' and form_forgot.validate_on_submit(): user = sql.session.query(Users).filter_by( email=form_forgot.email.data).first() if user.email == form_forgot.email.data: passwordphrase = utils.randomstr(15) with open(secret_passwd, 'w') as f: f.write(str(passwordphrase)) if isfile(secret_passwd): stmt = update(Users).where(Users.id == user.id).values( password=utils.hash_password(passwordphrase)) conn.execute(stmt) conn.close() flash( 'Autogenerated password saved in safe place only my Creator knows', 'info') return redirect(url_for('forgot_password')) else: flash( 'Entered email {} not found. No password generated.'.format( form_forgot.email.data), 'error') return redirect(url_for('forgot_password')) return render_template('adminboard/forgot.html', form=form_forgot)
def update_dashboard_inner(): form = dashboard_itemsform.DashboardItemsForm() form_next = dashboard_searchform.DashboardSearchForm() author = g.user conn = engine.connect() if request.method == 'POST': if form.rename.data and form.validate_on_submit(): checkboxes = request.form.getlist('item_chb') # form.delid.data is the unicode list thing which # we convert to integer using regexp delid = int(re.search("\d+", str(form.delid.data)).group()) if author is not None: articles = Content.query.all() n = None for x in checkboxes: # x is the unicode list thing which # we convert to integer using regexp x = int(re.search("\d+", str(x)).group()) for art_id in articles: if art_id.id == x: n = art_id.id if x == n and x != delid: stmt = update(Content).where(Content.id == x).values( id=delid) try: conn.execute(stmt) conn.close() flash( "Item {} is changed to {}".format( x, form.delid.data), 'info') except exc.IntegrityError: flash("Item {} is exists".format(delid), 'error') elif x == delid: flash("Item {} is exists".format(delid), 'error') else: flash( "Item {} is not changed to {}".format( x, form.delid.data), 'error') else: return redirect(url_for('show_login')) return redirect(url_for('show_dashboard_inner')) elif form.rename.data is True and form.validate_on_submit() is False: flash("No item selected", 'error') return redirect(url_for('show_dashboard_inner')) elif form.delete.data and form.validate_on_submit(): checkboxes = request.form.getlist('item_chb') if author is not None: articles = Content.query.all() n = None for x in checkboxes: # x is the unicode list thing which # we convert to integer using regexp x = int(re.search("\d+", str(x)).group()) for art_id in articles: if art_id.id == x: n = art_id.id if x == n: stmt = delete(Content).where(Content.id == x) conn.execute(stmt) conn.close() flash("Item {} is deleted!".format(x), 'info') elif x == n: flash("Item {} is exists".format(x), 'error') else: flash("Item {} is not deleted!".format(x), 'error') else: return redirect(url_for('show_login')) return redirect(url_for('show_dashboard_inner')) elif form.delete.data is True and form.validate_on_submit() is False: flash("No item selected", 'error') return redirect(url_for('show_dashboard_inner')) elif form_next.validate_on_submit(): data = [] data_array = sql.session.query(Content).filter( Content.content_title.match(form_next.query.data)).all() if data_array: for x in data_array: data = x return jsonify(id=str(data.id), title=data.content_title, author=data.content_author, category=data.content_category, date=data.content_date, published=data.published) else: return redirect(url_for('show_dashboard_inner')) return render_template('adminboard/adminboard_inner.html', form=form)