def disable_req(request): if request.POST: if 'disable' in request.POST: mcid = normalize_mcid(request.POST['mcid']) user = User.objects.get(mcid=mcid) sql_execute('DELETE FROM groupmembers WHERE memberaccid=%s', user.mcid) user.sha1 = None user.acctype = 'DISABLED' user.save() return HttpResponseRedirect('.') mcid = normalize_mcid(request.GET['mcid']) user = User.objects.get(mcid=mcid) ts = datetime.fromtimestamp(user.ccrlogupdatetime) where = [ 'groupmembers.memberaccid = %s' % mcid, 'groupmembers.groupinstanceid = groupinstances.groupinstanceid' ] member_of = Group.objects.extra(where=where, tables=['groupmembers']) return render_to_response( 'users/disable.html', default_context(request, mcuser=user, ccrlogupdatetime=ts, member_of=member_of))
def create_group(name, mcid, url_root): """Create a group and a related practice. name is the name of the group and practice mcid is the account id of the owner url_root is the base URL of the for 'https://host' """ group_mcid = mcid_generator.next_mcid() g = Group() g.name = name g.grouptypeid = 0 g.accid_id = group_mcid g.save() p = Practice() p.providergroupid = g p.practicename = name p.accid_id = group_mcid p.save() p.practiceRlsUrl = url_root + '/acct/ws/R.php?pid=%d' % p.practiceid p.save() g.parentid = p.practiceid sql_execute("INSERT INTO groupmembers (groupinstanceid, memberaccid) " + \ "VALUES (%s, %s)", int(g.groupinstanceid), mcid) sql_execute("INSERT INTO groupadmins (groupinstanceid, adminaccid, comment) " + \ "VALUES (%s, %s, %s)", int(g.groupinstanceid), mcid, "") return g
def upload_req(request): from base64 import b64decode if request.FILES and 'csv' in request.FILES: s = request.FILES['csv']['content'] elif 'url' in request.REQUEST: s = urlopen(request.REQUEST['url']).read() else: return render_to_response('idps/upload.html', default_context(request)) r = reader(StringIO(s)) # okay to remove first column, the 'id' field columns = r.next() assert columns[0] == 'id' if 'png16x16' in columns: image_row = columns.index('png16x16') else: image_row = None columns = columns[1:] sql = "INSERT INTO identity_providers (" + ', '.join(columns) + \ ") VALUES (" + ', '.join(['%s'] * len(columns)) + ')' for row in r: if image_row is not None and row[image_row]: row[image_row] = b64decode(row[image_row]) sql_execute(sql, *row[1:]) return HttpResponseRedirect('.')
def add_to_group_req(request): mcid = normalize_mcid(request.GET["mcid"]) group = request.GET["group"] sql_execute("INSERT INTO groupmembers (groupinstanceid, memberaccid) " + "VALUES (%s, %s);", int(group), mcid) return HttpResponseRedirect("user?mcid=" + mcid)
def remove_from_group_req(request): mcid = normalize_mcid(request.GET["mcid"]) group = request.GET["group"] sql_execute("DELETE FROM groupmembers " + "WHERE groupinstanceid=%s AND memberaccid=%s;", int(group), mcid) return HttpResponseRedirect("user?mcid=" + mcid)
def disable_req(request): if request.POST: if "disable" in request.POST: mcid = normalize_mcid(request.POST["mcid"]) user = User.objects.get(mcid=mcid) sql_execute("DELETE FROM groupmembers WHERE memberaccid=%s", user.mcid) user.sha1 = None user.acctype = "DISABLED" user.save() return HttpResponseRedirect(".") mcid = normalize_mcid(request.GET["mcid"]) user = User.objects.get(mcid=mcid) ts = datetime.fromtimestamp(user.ccrlogupdatetime) where = ["groupmembers.memberaccid = %s" % mcid, "groupmembers.groupinstanceid = groupinstances.groupinstanceid"] member_of = Group.objects.extra(where=where, tables=["groupmembers"]) return render_to_response( "users/disable.html", default_context(request, mcuser=user, ccrlogupdatetime=ts, member_of=member_of) )
def my_uploads(): """我的上传记录页面""" nid = session['id'] sql = "select id, lines_num,user,upload_time from codes_upload where user=%d" % nid ret = sql_execute(sql) categories = [] for item in ret: categories.append(str(item[3])) import json categories = json.dumps(categories) print('categories>>>>>>>>>>>>>>', categories, type(categories)) lines_count = [] for item in ret: lines_count.append(item[1]) sql1 = 'select id,nick_name from user_profile where id=%s' % nid ret1 = sql_execute(sql1) ret = { 'user': ret1, 'data': ret, 'categories': categories, 'lines_count': lines_count } return render_template('codes/my_codes.html', data=ret)
def login(): # 拿到用户信息 if request.method == 'POST': username = request.form.get('username', '') password = request.form.get('password', '') print(username, password) # 进入数据库验证用户信息 sql = 'select id, nick_name from user_profile where name = "%s" and password = "******"' % ( username, password) rets = sql_execute(sql) ret = {'user': username, "data": rets} if ret: session['user'] = username session['id'] = ret['data'][0][0] session['is_login'] = True # 取出当前用户的所有权限url列表 sql = '''select url from permmision where id in (select permission_id from role_permission where role_id in (select role from user_profile_role u_r where u_r.user =%d))''' % ret[ 'data'][0][0] ret = sql_execute(sql) ret = [item[0] for item in ret if item] print('urls>>>', ret) session['permission_urls'] = ret return redirect('/users') return render_template('account/login.html')
def add_to_group_req(request): mcid = normalize_mcid(request.GET['mcid']) group = request.GET['group'] sql_execute("INSERT INTO groupmembers (groupinstanceid, memberaccid) " + \ "VALUES (%s, %s);", int(group), mcid) return HttpResponseRedirect("user?mcid=" + mcid)
def remove_from_group_req(request): mcid = normalize_mcid(request.GET['mcid']) group = request.GET['group'] sql_execute("DELETE FROM groupmembers " + \ "WHERE groupinstanceid=%s AND memberaccid=%s;", int(group), mcid) return HttpResponseRedirect("user?mcid=" + mcid)
def update_logo(id, file): i = Image.open(StringIO(file['content'])) i.thumbnail((16, 16), Image.ANTIALIAS) o = StringIO() i.save(o, format='PNG') sql_execute("UPDATE identity_providers SET png16x16 = %s WHERE id = %s", o.getvalue(), id)
def unlink_user(request, redirect): mcid = normalize_mcid(request.POST['mcid']) idp = request.POST['idp'] username = request.POST['username'] sql_execute("DELETE FROM external_users " + \ "WHERE provider_id = %s AND mcid = %s AND username = %s", int(idp), mcid, username) return HttpResponseRedirect(redirect % locals())
def remove_from_group_req(request): mcid = request.REQUEST['mcid'] group = request.REQUEST['group'] if 'delete' in request.POST or 'delete.x' in request.POST: sql_execute("DELETE FROM groupmembers " + \ "WHERE groupinstanceid=%s AND memberaccid=%s;", int(group), mcid) return HttpResponseRedirect("group?id=" + group)
def upload_code(): error = None if request.method == 'POST': # 1、上传的文件不能为空 if 'file' not in request.files: error = '需要上传文件' return render_template('codes/upload.html', error=error) file = request.files['file'] print(file.filename) # 文件名称 print(file.stream) # 文件内容 # 2、上传的文件格式必须符号要求 if file and allowed_file(file.filename): # 3、判断当前用户是否是首次上传 today_str = str(datetime.datetime.now())[:10] sql = 'select * from codes_upload where user=%s and upload_time=%s' % ( int(session.get('id', '')), "'" + today_str + "'") rets = sql_execute(sql) # print('查询结果:', rets) if not rets: # 4、接收用户上传文件并解压到指定的目录,生成随机文件夹名称,防止重名覆盖 # 注意必须确保上传的文件是以zip方式压缩的,单纯的修改zip后缀仍然不算zip格式 target_path = os.path.join(DevelopmentConfig.UPLOAD_FOLDER, str(uuid.uuid4())) shutil._unpack_zipfile(file.stream, target_path) # 6、遍历目录下的所有文件,并汇总出解压后的所有的文件的行数 total_num = 0 for root, dirs, files in os.walk(target_path): # print(dirs) # print(root) # 文件夹的绝对路径 for filename in files: line_count = 0 file_path = os.path.join(root, filename) with open(file_path, 'rb') as f: for line in f: line = line.strip() if not line or line.startswith(b'#'): continue line_count += 1 total_num += line_count print(total_num) add_sql = 'insert into codes_upload (user,lines_num,upload_time) values (%s, %s, %s)' % ( session.get('id', ''), total_num, "'" + today_str + "'") rets = sql_execute(add_sql, type='insert') return redirect( url_for('co.my_uploads', nid=int(session.get('id')))) error = '今天代码已上传' else: error = '文件必须为zip格式' return render_template('codes/upload.html', error=error)
def delete_user(mcid): for ga in Groupadmin.objects.filter(adminaccid=mcid): g = Group.objects.get(groupinstanceid=ga.groupinstanceid) delete_group(g) ga.delete() sql_execute("DELETE FROM ccrlog WHERE accid = %s", mcid) sql_execute("DELETE FROM todir WHERE accid = %s", mcid) sql_execute("DELETE FROM groupmembers WHERE memberaccid = %s", mcid) sql_execute("DELETE FROM document_type WHERE dt_account_id = %s", mcid) sql_execute("DELETE FROM users WHERE mcid = %s", mcid)
def delete_user(mcid): for ga in Groupadmin.objects.filter(adminaccid = mcid): g = Group.objects.get(groupinstanceid = ga.groupinstanceid) delete_group(g) ga.delete() sql_execute("DELETE FROM ccrlog WHERE accid = %s", mcid) sql_execute("DELETE FROM todir WHERE accid = %s", mcid) sql_execute("DELETE FROM groupmembers WHERE memberaccid = %s", mcid) sql_execute("DELETE FROM document_type WHERE dt_account_id = %s", mcid) sql_execute("DELETE FROM users WHERE mcid = %s", mcid)
def delete_data(): properties = get_properties() delete_user(properties['DemoDoctor']) for first_name, last_name, email, ccr, reason, ccrs in PATIENTS: users = User.objects.filter(first_name=first_name, last_name=last_name, email=email) for user in users: delete_user(user.mcid) sql_execute("DELETE FROM mcproperties WHERE property='acDemoDoctor'")
def delete_data(): properties = get_properties() delete_user(properties['DemoDoctor']) for first_name, last_name, email, ccr, reason, ccrs in PATIENTS: users = User.objects.filter(first_name = first_name, last_name = last_name, email = email) for user in users: delete_user(user.mcid) sql_execute("DELETE FROM mcproperties WHERE property='acDemoDoctor'")
def delete_wl_entry_req(request): pid = int(request.REQUEST['pid']) practice = Practice.objects.get(practiceid = pid) if request.POST: guid = request.POST['guid'] if 'delete' in request.POST or 'delete.x' in request.POST: sql_execute("DELETE FROM practiceccrevents WHERE Guid=%s AND practiceid=%s", guid, pid) return HttpResponseRedirect('group?id=%d' % practice.providergroupid.groupinstanceid) guid = request.GET['guid'] event = Practiceccrevent.objects.get(Guid=guid, practiceid=pid) return render_to_response('groups/delete_wl_entry.html', default_context(request, entry=entry, practice=practice))
def user_list(): # 进入数据库验证用户信息 sql = 'select * from codes_upload' ret = sql_execute(sql) # 1、查询用户的所有上传总量 sql1 = "select u.name, sum(c.lines_num) as lines_count from codes_upload c inner join user_profile u on c.user = u.id group by c.user" ret_total = sql_execute(sql1) print('ret_total', ret_total) users = [] total_counts = [] import json for item in ret_total: users.append(item[0]) total_counts.append(int(item[1])) users = json.dumps(users) # 2、查询本周的所有用户的上传总量 sql2 = 'select u.name, sum(c.lines_num) as lines_count from codes_upload c inner join user_profile u on c.user = u.id where YEARWEEK(upload_time)=YEARWEEK(now()) group by c.user' ret_week = sql_execute(sql2) print('ret_week', ret_week) week_count = [] import json for item in ret_week: week_count.append(int(item[1])) # 3、查询本月的数据 sql3 = 'select u.name, sum(c.lines_num) as lines_count from codes_upload c inner join user_profile u on c.user = u.id where DATE_FORMAT(upload_time, "%Y-%m")= DATE_FORMAT(now(), "%Y-%m") group by c.user' ret_month = sql_execute(sql3) print('ret_month', ret_month) month_count = [] for item in ret_month: month_count.append(int(item[1])) ret = { 'total': total_counts, 'week': week_count, 'month': month_count, 'users': users, 'ret': ret } return render_template('codes/users.html', data=ret)
def delete_wl_entry_req(request): pid = int(request.REQUEST['pid']) practice = Practice.objects.get(practiceid=pid) if request.POST: guid = request.POST['guid'] if 'delete' in request.POST or 'delete.x' in request.POST: sql_execute( "DELETE FROM practiceccrevents WHERE Guid=%s AND practiceid=%s", guid, pid) return HttpResponseRedirect('group?id=%d' % practice.providergroupid.groupinstanceid) guid = request.GET['guid'] event = Practiceccrevent.objects.get(Guid=guid, practiceid=pid) return render_to_response( 'groups/delete_wl_entry.html', default_context(request, entry=entry, practice=practice))
def delete_req(request): id = request.REQUEST['id'].strip() object = IdentityProvider.objects.get(id = id) where = ['external_users.mcid = users.mcid', 'external_users.provider_id = %s' % id] qs = User.objects.extra(where = where, select = {'username': '******'}, tables = ['external_users']) if request.POST: if 'delete' in request.POST: sql_execute("DELETE FROM external_users WHERE provider_id = %s", id) object.delete() return HttpResponseRedirect('.') else: return render_to_response('idps/delete.html', default_context(request, object = object, count = qs.count()))
def add_users(request, this_action, next_action, template): id = request.REQUEST['id'].strip() group = Group.objects.get(groupinstanceid=int(id)) context = default_context(request, action=this_action, group=group, id=id) if 'q' in request.REQUEST: q = request.REQUEST['q'] qs = search_query_set(q) context['q'] = q else: qs = all_query_set() if 'mcid' in request.POST: mcid = normalize_mcid(request.POST['mcid']) if 'remove.x' in request.POST: sql_execute("DELETE FROM groupmembers " + \ "WHERE groupinstanceid = %s AND memberaccid = %s", int(id), mcid) elif 'add.x' in request.POST: # Only one group per user sql_execute("DELETE FROM groupmembers " + \ "wHERE memberaccid = %s", mcid) sql_execute("INSERT INTO groupmembers " + \ "(groupinstanceid, memberaccid) " + \ "VALUES (%s, %s)", int(id), mcid) return HttpResponseRedirect(this_action) if 'prev' in request.POST: return HttpResponseRedirect('wiz_name?id=%s' % id) if 'finish' in request.POST: return HttpResponseRedirect(next_action) where = [ 'groupmembers.memberaccid = users.mcid', 'groupmembers.groupinstanceid = %s' % id ] context['members'] = User.objects.extra(where=where, tables=['groupmembers']) where = [ 'mcid NOT IN (SELECT memberaccid FROM groupmembers WHERE groupinstanceid = %s)' % id ] select = { 'groupname': 'SELECT DISTINCT(name) FROM groupinstances, groupmembers WHERE groupmembers.groupinstanceid = groupinstances.groupinstanceid AND groupmembers.memberaccid = users.mcid' } return object_list(request, queryset=qs.extra(where=where, select=select), template_name=template, paginate_by=20, extra_context=context)
def merge_req(request): id = int(request.REQUEST['id'].strip()) object = Appliance.objects.get(id = id) augment(object) objects = Appliance.objects.all() same_objects = [] diff_objects = [] # divide out the appliances: those with the same IP address and those # with different IP addresses for o in objects: augment(o) if o.id == object.id: continue if o.ipaddr == object.ipaddr: same_objects.append(o) else: diff_objects.append(o) if request.POST: to_id = int(request.REQUEST['to'].strip()) if to_id != id: sql_execute("UPDATE alloc_log SET appliance_id = %s WHERE appliance_id = %s", to_id, id) object.delete() return HttpResponseRedirect('.') return render_to_response('appliances/merge.html', default_context(request, object=object, same_objects=same_objects, diff_objects=diff_objects))
def delete_req(request): id = request.REQUEST['id'].strip() object = IdentityProvider.objects.get(id=id) where = [ 'external_users.mcid = users.mcid', 'external_users.provider_id = %s' % id ] qs = User.objects.extra(where=where, select={'username': '******'}, tables=['external_users']) if request.POST: if 'delete' in request.POST: sql_execute("DELETE FROM external_users WHERE provider_id = %s", id) object.delete() return HttpResponseRedirect('.') else: return render_to_response( 'idps/delete.html', default_context(request, object=object, count=qs.count()))
def add_users(request, this_action, next_action, template): id = request.REQUEST['id'].strip() group = Group.objects.get(groupinstanceid=int(id)) context = default_context(request, action=this_action, group=group, id=id) if 'q' in request.REQUEST: q = request.REQUEST['q'] qs = search_query_set(q) context['q'] = q else: qs = all_query_set() if 'mcid' in request.POST: mcid = normalize_mcid(request.POST['mcid']) if 'remove.x' in request.POST: sql_execute("DELETE FROM groupmembers " + \ "WHERE groupinstanceid = %s AND memberaccid = %s", int(id), mcid) elif 'add.x' in request.POST: # Only one group per user sql_execute("DELETE FROM groupmembers " + \ "wHERE memberaccid = %s", mcid) sql_execute("INSERT INTO groupmembers " + \ "(groupinstanceid, memberaccid) " + \ "VALUES (%s, %s)", int(id), mcid) return HttpResponseRedirect(this_action) if 'prev' in request.POST: return HttpResponseRedirect('wiz_name?id=%s' % id) if 'finish' in request.POST: return HttpResponseRedirect(next_action) where = ['groupmembers.memberaccid = users.mcid', 'groupmembers.groupinstanceid = %s' % id] context['members'] = User.objects.extra(where = where, tables = ['groupmembers']) where = ['mcid NOT IN (SELECT memberaccid FROM groupmembers WHERE groupinstanceid = %s)' % id] select = {'groupname': 'SELECT DISTINCT(name) FROM groupinstances, groupmembers WHERE groupmembers.groupinstanceid = groupinstances.groupinstanceid AND groupmembers.memberaccid = users.mcid'} return object_list(request, queryset = qs.extra(where = where, select = select), template_name = template, paginate_by = 20, extra_context = context)
def add_to_group(g, mcid): sql_execute("""INSERT INTO groupmembers (groupinstanceid, memberaccid) VALUES (%s, %s)""", g.groupinstanceid, mcid)
def delete_group(group): i = group.groupinstanceid sql_execute("DELETE FROM groupmembers WHERE groupinstanceid = %s", i) sql_execute("DELETE FROM practice WHERE providergroupid = %s", i) group.delete()
def create_data(url_root): gateway = urljoin(url_root, 'router/') transaction.enter_transaction_management() doctor = User() doctor.mcid = mcid_generator.next_mcid() doctor.first_name = 'Demo' doctor.last_name = 'Doctor' doctor.email = '*****@*****.**' doctor.updatetime = 0 doctor.ccrlogupdatetime = 0 doctor.save() physician = User() physician.mcid = mcid_generator.next_mcid() physician.first_name = 'Demo' physician.last_name = 'Physician' physician.email = '*****@*****.**' physician.updatetime = 0 physician.ccrlogupdatetime = 0 physician.save() g, p = create_group('Demo Group Worklist', '*****@*****.**', url_root, doctor.mcid) add_to_group(g, physician.mcid) patients = [] for first_name, last_name, email, currentccr, reason, ccrs in PATIENTS: user = User() user.first_name = first_name user.last_name = last_name user.email = email user.mcid = mcid_generator.next_mcid() user.acctype = 'USER' user.rolehack = 'ccrlhm' user.updatetime = 0 user.ccrlogupdatetime = 0 user.save() patients.append(user) # Set worklist sql_execute(ACCOUNT_RLS_SQL, user.mcid, p.practiceRlsUrl) if not currentccr: continue sql_execute(DOCUMENT_TYPE_SQL, user.mcid, currentccr) ev = Practiceccrevent() ev.practiceid = p ev.PatientGivenName = user.first_name ev.PatientFamilyName = user.last_name ev.PatientIdentifier = user.mcid ev.PatientIdentifierSource = 'Patient Medcommons ID' ev.Guid = currentccr ev.Purpose = reason ev.SenderProviderId = 'idp' ev.ReceiverProviderId = 'idp' ev.DOB = '16 Jan 1968 05:00:00 GMT' ev.CXPServerURL = '' ev.CXPServerVendor = 'Medcommons' ev.ViewerURL = urljoin(gateway, 'access?g=%s' % currentccr) ev.Comment = '\n 3D Imaging Consult\n ' ev.CreationDateTime = 1162365858 ev.ConfirmationCode = tn_generator.next_tracking_number() ev.RegistrySecret = '' ev.PatientSex = 'Female' ev.PatientAge = '' ev.Status = 'New' ev.ViewStatus = 'Visible' ev.save() sql_execute(CCRLOG_SQL, user.mcid, currentccr, 'CCR', ev.ConfirmationCode) for ccr in ccrs: sql_execute(CCRLOG_SQL, user.mcid, ccr, 'CCR', tn_generator.next_tracking_number()) sql_execute(TODIR_SQL, g.groupinstanceid, doctor.email, doctor.email, doctor.mcid) sql_execute(TODIR_SQL, g.groupinstanceid, physician.email, physician.email, physician.mcid) demoCCR = 'fdfbbb9cf53f8577b420ed72567cd2104589fb0d' sql_execute(CCRLOG_SQL, doctor.mcid, demoCCR, 'DICOM Import', tn_generator.next_tracking_number()) sql_execute(CCRLOG_SQL, patients[0].mcid, demoCCR, 'DICOM Import', tn_generator.next_tracking_number()) sql_execute(CCRLOG_SQL, patients[0].mcid, PATIENTS[0][3], 'DICOM Import', tn_generator.next_tracking_number()) # Secondary group if 0: g2, p2 = create_group('Healthy Doctors', '*****@*****.**', url_root, doctor.mcid) p = MCProperty() p.property = 'acDemoDoctor' p.value = doctor.mcid p.save() transaction.leave_transaction_management()
def add_to_group(g, mcid): sql_execute( """INSERT INTO groupmembers (groupinstanceid, memberaccid) VALUES (%s, %s)""", g.groupinstanceid, mcid)