def fill_state_choices(self): db = get_db() cur = db.cursor() cur.execute("SELECT * FROM state", ) self.state.choices = [('all', 'All')] + list( map(lambda s: (s.slug, s.name), cur.fetchall())) cur.close()
def create_post(): db = get_db() cur = db.cursor() form = CreateUserForm(request.form) fill_form_org_and_perms(form) if form.validate(): cur.execute('select * from users where username=%s', (form.username.data, )) user = cur.fetchone() if user: form.username.errors.append( f'Username {form.username.data} already exists') return render_template('user/create.html', form=form) cur.execute( 'insert into users (username, password, org) values (%s, %s, %s)', (form.username.data, generate_password_hash( form.password.data), form.org.data)) db.commit() cur.close() flash('User created') return redirect(url_for('user.index')) cur.close() return render_template('user/create.html', form=form)
def login(): if request.method == 'POST': db = get_db() cur = db.cursor() form = LoginForm(request.form) if not form.validate(): return render_template('auth/login.html', form=form) cur.execute('select * from users where username = %s', (form.username.data, )) user = cur.fetchone() user = Model(User, user) if not user: form.username.errors.append('invalid login') return render_template('auth/login.html', form=form) if not check_password_hash(user.password, form.password.data): form.password.errors.append('invalid login') return render_template('auth/login.html', form=form) session.clear() session['username'] = user.username return redirect(url_for('home.index')) return render_template('auth/login.html', form=LoginForm())
def index(): db = get_db() cur = db.cursor() pages = [ { 'name': 'Organizations', 'url': url_for('org.index') }, { 'name': 'Patients', 'url': url_for('patient.index') }, { 'name': 'User', 'url': url_for('user.index') }, { 'name': 'Reports', 'url': url_for('report.index') }, { 'name': 'Reservations', 'url': url_for('res.index') }, ] cur.execute( ''' select report.id, p.name, r.start_t from report join patient p on p.id = report.patient join res r on r.id = report.res where r.start_t > timestamp %s limit 5''', (datetime.now(), ), ) reports_upcoming = cur.fetchall() cur.execute( ''' select report.id, p.name, r.start_t from report join patient p on p.id = report.patient join res r on r.id = report.res where r.start_t > timestamp %s and report.creator_user in (select username from users where org = %s) order by r.start_t desc limit 5 ''', (datetime.now(), g.user.org), ) reports_org = cur.fetchall() cur.close() return render_template('home/index.html', pages=pages, reports=reports_upcoming, reports_org=reports_org)
def index(): db = get_db() cur = db.cursor() cur.execute('select * from users', ) users = cur.fetchall() users = list(map(lambda row: Model(User, row), users)) cur.close() return render_template('user/index.html', users=users)
def index(): db = get_db() cur = db.cursor() cur.execute('select * from patient', ) patients = cur.fetchall() patients = list(map(lambda row: Model(Patient, row), patients)) cur.close() return render_template('patient/index.html', patients=patients)
def index(): db = get_db() cur = db.cursor() cur.execute( 'select res.*, count(r.id) as occupied_cnt from res left join report r on res.id = r.res group by res.id', ) reses = cur.fetchall() cur.close() return render_template('res/index.html', reses=reses)
def load_logged_in_user(): username = session.get('username') if username is None: g.user = None else: conn = get_db() cur = conn.cursor() cur.execute('select * from users where username = %s', (username, )) g.user = cur.fetchone() cur.close()
def create_user_command(username, password, org_slug): """Creates a new user with the passed or prompted arguments""" from patients.db import get_db db = get_db() cur = db.cursor() password_hash = generate_password_hash(password) cur.execute( 'INSERT INTO users (username, password, org) VALUES (%s, %s, %s)', (username, password_hash, org_slug)) db.commit() cur.close() click.echo('User created.')
def fill_form_org_and_perms(form: CreateUserForm): db = get_db() cur = db.cursor(cursor_factory=psycopg2.extras.NamedTupleCursor) cur.execute('select * from org', ) orgs = cur.fetchall() cur.execute('select * from perm', ) perms = cur.fetchall() orgs_list = list(map(lambda o: (o.slug, o.slug + ' - ' + o.name), orgs)) form.org.choices = orgs_list perms_list = list(map(lambda o: (o.slug, o.slug + ' - ' + o.name), perms)) form.perms.choices = perms_list cur.close()
def create(): db = get_db() cur = db.cursor() form = CreatePatientForm(request.form) if request.method == 'POST' and form.validate(): cur.execute( 'insert into patient (phone, name, gender, creator_user) values (%s,%s,%s,%s)', (form.phone.data, form.name.data, form.gender.data, g.user.username), ) db.commit() cur.close() return redirect(url_for('patient.index')) cur.close() return render_template('patient/create.html', form=form)
def details(pk: int): db = get_db() cur = db.cursor() cur.execute( 'select * from res where id = %s', (pk, ), ) res = cur.fetchone() cur.execute( 'select p.id as pid, p.name, r.* from report r join patient p on p.id = r.patient where r.res = %s', (pk, ), ) reports = cur.fetchall() cur.close() return render_template('res/details.html', res=res, reports=reports)
def edit_post(username: str): db = get_db() cur = db.cursor() err = None cur.execute('select * from users where username=%s', (username, )) user = cur.fetchone() if not user: err = f'User {username} does not exists' new_password = request.form['password'] cur.execute('update users set password = %s where username=%s', (generate_password_hash(new_password), username)) db.commit() cur.close() flash(err) return redirect(url_for('user.index'))
def fill_form_choices(form: CreateReportForm): db = get_db() cur = db.cursor() cur.execute('SELECT * FROM patient', ) form.patient.choices = list(map(lambda p: (p.id, p.name), cur.fetchall())) cur.close() cur = db.cursor() cur.execute( "SELECT * FROM res where start_t > timestamp %s", (datetime.now().strftime('%Y-%m-%d %H:%M:%S'), ), ) form.res.choices = list( map(lambda r: (r.id, f'{r.start_t} - {r.end_t}'), cur.fetchall())) cur.close() cur = db.cursor() cur.execute("SELECT * FROM state", ) form.state.choices = list(map(lambda s: (s.slug, s.name), cur.fetchall())) cur.close()
def create(): db = get_db() cur = db.cursor() form = CreateReportForm(request.form) fill_form_choices(form) form.state.data = 'not-attached' if request.method == 'POST' and form.validate(): cur.execute( 'insert into report (patient, creator_user, res, state) values (%s, %s, %s, %s)', (form.patient.data, g.user.username, form.res.data, form.state.data), ) db.commit() cur.close() return redirect(url_for('report.index')) cur.close() return render_template('report/create.html', form=form)
def edit(username: str): db = get_db() cur = db.cursor() cur.execute('SELECT * FROM users WHERE username=%s', (username, )) user = cur.fetchone() user = Model(User, user) if not user: abort(404) form = CreateUserForm(request.form) fill_form_org_and_perms(form) form.username.data = user['username'] form.password.data = '*' * 8 form.org.data = user['org'] form.org.default = user['org'] form.perms.default = [] cur.close() return render_template('user/edit.html', form=form)
def create(): db = get_db() cur = db.cursor() form = CreateResForm(request.form) fill_form_choices(form) if request.method == 'POST' and form.validate(): d_start = datetime.strptime(form.date.data, '%Y-%m-%d') d_start += timedelta(hours=int(form.start_time.data)) d_end = datetime.strptime(form.date.data, '%Y-%m-%d') d_end += timedelta(hours=int(form.end_time.data)) cur.execute( 'insert into res (start_t, end_t, cap) values (%s, %s, %s)', (d_start, d_end, form.cap.data), ) db.commit() cur.close() return redirect(url_for('res.index')) cur.close() return render_template('res/create.html', form=form)
def index(): db = get_db() cur = db.cursor() state = request.args.get('state', None) if state and state != 'all': cur.execute( 'select report.id, p.name from report join patient p on p.id = report.patient where report.state = %s', (state, ), ) else: cur.execute( 'select report.id, p.name from report join patient p on p.id = report.patient', ) reports = cur.fetchall() cur.close() state_picker = StatePickerForm() state_picker.fill_state_choices() return render_template('report/index.html', reports=reports, state_picker=state_picker)
def edit(pk: int): db = get_db() cur = db.cursor() cur.execute( 'select * from patient where id=%s', (pk, ), ) patient = cur.fetchone() if not patient: abort(404) patient = Model(Patient, patient) if request.method == 'POST': form = CreatePatientForm(request.form) if form.validate(): cur.execute( 'update patient set phone = %s, name = %s, gender = %s where id=%s', (form.phone.data, form.name.data, form.gender.data, pk), ) db.commit() cur.close() return redirect(url_for('patient.index')) cur.close() return render_template('patient/edit.html', form=form) if request.method == 'GET': form = CreatePatientForm() form.name.data = patient.name form.phone.data = patient.phone form.gender.data = patient.gender cur.close() return render_template('patient/edit.html', form=form)
def edit(pk: int): db = get_db() cur = db.cursor() cur.execute( 'select * from report where id=%s', (pk, ), ) report = cur.fetchone() if not report: abort(404) if request.method == 'POST': form = CreateReportForm(request.form) fill_form_choices(form) if form.validate(): cur.execute( 'update report set patient = %s, res = %s, state = %s where id=%s', (form.patient.data, form.res.data, form.state.data, pk), ) db.commit() cur.close() return redirect(url_for('report.index')) cur.close() return render_template('report/edit.html', form=form) if request.method == 'GET': form = CreateReportForm() fill_form_choices(form) form.patient.data = str(report.patient) form.res.data = report.res form.state.data = report.state cur.close() return render_template('report/edit.html', form=form)