def api_get_ladp_userdata(self): if request.data: data = json_.loads(request.data) else: data = {} rows = [] User = functions.get_model("user") username = data.get("username", "").strip() if username: try: litems = functions.ldap_search_user(username) for dn, ldap_dict in litems: if dn is not None: cnt = User.filter( User.c.username == ldap_dict['name']).count() if cnt: ldap_dict["isIn"] = "1" rows.append(ldap_dict) # print "%s:%s" % (ldap_dict['name'], cnt) except ldap.INVALID_CREDENTIALS as err: return json({ "desc": "invalid credentials, please contact administrator" }) except Exception as err: return json(err.message) cntall = len(rows) # print rows return json({"total": cntall, "rows": rows})
def addbatchuser(self): User = functions.get_model('user') UserGroupMd = self.UserGroupMd user_ids = request.POST.get('user_ids') group_id = request.POST.get('group_id') user_ids = user_ids.replace('\n', ',').replace('\r', '').replace(u',', ',') user_ids = user_ids.split(',') error_users = [] for user_id in user_ids: if user_id != '': user = User.get(User.c.username == user_id) if not user: error_users.append(user_id) if len(error_users) > 0: return json({'success':False, 'msg': u"下列人员帐号找不到: %s, 请仔细检查后再次增加。" % ','.join(error_users)}) else: userGroup = UserGroupMd.get(int(group_id)) for user_id in user_ids: user = User.get(User.c.username == user_id) if not userGroup.users.has(user): userGroup.users.add(user) msg = "批量增加人员成功" # flash(msg, category='success') return json({'success':True, 'msg': msg});
def normalupload(): # 形成form print "-------normalupload action invoked" ufile = request.files.get('file', None) if not ufile: return json({'msg': 'No file had been upload'}, status=400) #----keep the file---- try: rf = ResumeFile(fileobj=ufile, fileName=ufile.filename, identify=uuid.uuid1().hex, username=request.user.username) rf.keepFile() except Exception as e: return json({'msg': 'keep file error'}, status=400) return json( { 'msg': 'success uploaded file', 'status': 'success', 'files': [{ 'name': rf.fileName }] }, status=200)
def resumeupload(): """ breakpoint resume file uploading """ resumableChunkNumber = int(request.params['resumableChunkNumber']) resumableChunkSize = int(request.params['resumableChunkSize']) resumableIdentifier = request.params['resumableIdentifier'] resumableTotalChunks = int(request.params['resumableTotalChunks']) ruModel = get_model('resumableUpload') resumableUploadObj = ruModel.get(ruModel.c.guid == resumableIdentifier) if request.method == 'GET': #----check if the chunk has uploaded if resumableUploadObj is not None and int(resumableUploadObj.currentChunk) >= resumableChunkNumber: return json({'status':'success'}, status=200) else: return json({'status':'404'}, status=404) else: try: resumableFilename = request.params['resumableFilename'] # basePath = os.path.join(application_path(settings.TRANSFER.BASEUPLAOD), request.user.username, resumableIdentifier) ufile = request.files.get('file', None) if not ufile: return json({'status':'err', 'msg':'No file had been upload'}, status=400) rf = ResumeFile(fileobj=ufile, identify=resumableIdentifier, fileName=resumableFilename, username=request.user.username, totalChunk=resumableTotalChunks, currentChunk=resumableChunkNumber) rf.keepChunk(resumableChunkNumber) if resumableUploadObj is None: ruModel(guid=resumableIdentifier, currentChunk=resumableChunkNumber, totalChunk=resumableTotalChunks, state=0).save() else: ruModel.filter(ruModel.c.guid == resumableIdentifier).update(currentChunk=resumableChunkNumber) return json({'status':'ok'}, status=200) except Exception as e: return json({'msg':'keep file error'}, status=400)
def start_task(self, id): from .daemon import start_task try: c = start_task(id) return json({'success':True, 'message':'启动命令成功', 'id':c.task_id}) except: return json({'success':False, 'message':'启动命令失败'})
def checkFileChunkStartOffset(): identify = request.params['identify'] if identify is None or identify == '': return json({'error':'parms error'}, 400); print "----identify=%s" % identify ruModel = get_model('resumableUpload') resumableUploadObj = ruModel.get(ruModel.c.guid == identify) return json({'offset':resumableUploadObj.currentChunk if resumableUploadObj else 0}, status=200)
def api_remove(self): group_id = int(request.values.get("id")) UserGroupMd = self.UserGroupMd userGroup = UserGroupMd.get(group_id) cnt = userGroup.users.count() if cnt > 0: return json({"msg":u"The UserGroup has users.Please delete firstly", "success":False}) if not userGroup: return json({"msg":u"This UserGroup not found", "success":False}) userGroup.delete() return json({"msg":u"Remove UserGroup (%s) OK!" % userGroup.name, "success":True})
def start_task(self, id): from .daemon import start_task try: c = start_task(id) return json({ 'success': True, 'message': '启动命令成功', 'id': c.task_id }) except: return json({'success': False, 'message': '启动命令失败'})
def checkFileChunkStartOffset(): identify = request.params['identify'] if identify is None or identify == '': return json({'error': 'parms error'}, 400) print "----identify=%s" % identify ruModel = get_model('resumableUpload') resumableUploadObj = ruModel.get(ruModel.c.guid == identify) return json( { 'offset': resumableUploadObj.currentChunk if resumableUploadObj else 0 }, status=200)
def generic_list(model=None, get_url=get_url, layout='layout.html', template=None, key_field='id', add_button_text=None, view=None, data=None, json_result=True, pagination=True, rows=10): from uliweb.utils.generic import ListView from uliweb import request, response from uliweb.core.html import Tag if not view: def key(value, obj): url = get_url('view', id=obj.id) return str(Tag('a', value, href="%s" % url)) if not model or not get_model(model): error("Can't find model [%s], please check it" % model) pageno = int(request.GET.get('pageno', 0)) rows_per_page=rows if json_result: pageno = int(request.values.get('page', 1)) - 1 rows_per_page = int(request.values.get('rows', rows)) fields_convert_map = {key_field:key} _id = '%s_table' % model view = ListView(model, rows_per_page=rows_per_page, pageno=pageno, id=_id, fields_convert_map=fields_convert_map, pagination=pagination) else: _id = view.id if 'data' in request.values: if json_result: return json(view.json()) else: result = view.run(head=False, body=True) return json(result) else: result = view.run(head=True, body=False) if isinstance(result, dict): template = template or 'generic_list.html' data = data or {} result['layout'] = layout result['get_url'] = get_url result['add_button_text'] = add_button_text or _('New') if json_result: result['table'] = view result.update(data) return render_template(template, layout, result) else: return result
def normalupload(): # 形成form print "-------normalupload action invoked" ufile = request.files.get('file', None) if not ufile: return json({'msg':'No file had been upload'}, status=400) #----keep the file---- try: rf = ResumeFile(fileobj=ufile, fileName=ufile.filename, identify=uuid.uuid1().hex, username=request.user.username) rf.keepFile() except Exception as e: return json({'msg':'keep file error'}, status=400) return json({'msg':'success uploaded file', 'status':'success', 'files':[{'name':rf.fileName}]}, status=200)
def _wiki_update_editor(self, pagename): """ 更新当前页面的编辑用户及时间 """ wiki = self.model.get(self.model.c.name == pagename) if wiki: #check read permission self._check_permission('edit', wiki, page=wiki) wiki.cur_user = request.user.id wiki.start_time = date.now() wiki.save() return json({'success':True}) else: return json({'success':False, 'message':'页面不存在'})
def api_get_usergroupdata(self): if request.data: data = json_.loads(request.data) else: data = {} UserGroupMd = self.UserGroupMd sc_groupname = data.get("sc_groupname", "").strip() sc_grouptype = data.get("sc_grouptype", "").strip() sort = data.get("sort") order = data.get("order") limit = data.get("limit") offset = data.get("offset") grouplist = UserGroupMd.all() if sc_groupname: grouplist.filter(UserGroupMd.c.name.like('%%%s%%' % sc_groupname)) if sc_grouptype == settings.USERGROUP.AUTHTYPE_LDAP: grouplist.filter(UserGroupMd.c.auth_type == settings.USERGROUP.AUTHTYPE_LDAP) elif sc_grouptype == settings.USERGROUP.AUTHTYPE_LOCAL: grouplist.filter(UserGroupMd.c.auth_type != settings.USERGROUP.AUTHTYPE_LDAP) if sort: sort_key = getattr(UserGroupMd.c, sort) if order: sort_key = getattr(sort_key, order)() grouplist.order_by(sort_key) if limit: grouplist.limit(limit) if offset: grouplist.offset(offset) return json({"total":grouplist.count(), "rows": [i.to_dict() for i in grouplist]})
def _list(self, model, queryview=None, **kwargs): from uliweb import request, json, CONTENT_TYPE_JSON from sqlalchemy import and_ if queryview: queryview.run() condition = queryview.get_condition() else: condition = None if 'condition' in kwargs: condition = and_(condition, kwargs['condition']) kwargs['condition'] = condition else: kwargs['condition'] = condition #get list view view = self._list_view(model=model, **kwargs) if 'data' in request.values: return json(view.json(), content_type=CONTENT_TYPE_JSON) elif 'download' in request.GET: filename = download_filename or 'download.xls' kwargs.setdefault('action', 'download') kwargs.setdefault('timeout', 0) return view.download(filename, **kwargs) else: result = view.run() if queryview: result.update({'query_form':queryview.form}) else: result.update({'query_form':''}) result.update({'table':view}) return result
def _select_list(self, queryview=None, download_filename=None, **kwargs): from uliweb import request, json if queryview: queryview.run() condition = queryview.get_condition() else: condition = None if 'condition' in kwargs: condition = and_(condition, kwargs['condition']) kwargs['condition'] = condition else: kwargs['condition'] = condition view = functions.SelectListView(**kwargs) if 'data' in request.values: return json(view.json()) elif 'download' in request.GET: filename = download_filename or 'download.xls' kwargs.setdefault('action', 'download') kwargs.setdefault('timeout', 0) return view.download(filename, **kwargs) else: result = view.run() if queryview: result.update({'query_form':queryview.form}) else: result.update({'query_form':''}) result.update({'table':view}) return result
def api_restore(self): user_id = request.values.get("id", 0).strip() User = functions.get_model("user") if user_id: user = User.get(int(user_id)) user.update(deleted=False) ret = user.save() if ret: return json({"msg": u"user restore!", "success": True}) else: return json({"msg": u"Fail to restore user", "success": False}) else: return json({ "msg": u"para userid does not exist!", "success": False })
def _search(self, model, condition=None, search_field='name', value_field='id', label_field=None): """ Default search function :param search_field: Used for search field, default is 'name' :param value_field: Used for id field, default is id :param label_field: Used for label field, default is None, then it'll use unicode() function """ from uliweb import json, request name = request.GET.get('term', '') M = functions.get_model(model) def _v(label_field): if label_field: return lambda x: getattr(x, label_field) else: return lambda x: unicode(x) v_field = request.values.get('label', 'title') if name: if condition is None: condition = M.c[search_field].like('%' + name + '%') result = [{ 'id': getattr(obj, value_field), v_field: _v(label_field) } for obj in M.filter(condition)] else: result = [] return json(result)
def _select_list(self, queryview=None, download_filename=None, **kwargs): from uliweb import request, json if queryview: queryview.run() condition = queryview.get_condition() else: condition = None if "condition" in kwargs: condition = and_(condition, kwargs["condition"]) kwargs["condition"] = condition else: kwargs["condition"] = condition view = functions.SelectListView(**kwargs) if "data" in request.values: return json(view.json()) elif "download" in request.GET: filename = download_filename or "download.xls" kwargs.setdefault("action", "download") kwargs.setdefault("timeout", 0) return view.download(filename, **kwargs) else: result = view.run() if queryview: result.update({"query_form": queryview.form}) else: result.update({"query_form": ""}) result.update({"table": view}) return result
def _list(self, model, queryview=None, **kwargs): from uliweb import request, json from sqlalchemy import and_ if queryview: queryview.run() condition = queryview.get_condition() else: condition = None if 'condition' in kwargs: condition = and_(condition, kwargs['condition']) kwargs['condition'] = condition else: kwargs['condition'] = condition #get list view view = self._list_view(model=model, **kwargs) if 'data' in request.values: return json(view.json()) else: result = view.run() if queryview: result.update({'query_form':queryview.form}) else: result.update({'query_form':''}) result.update({'table':view}) return result
def _list(self, model=None, query_parameters=None, query_config='QueryConfig', list_parameters=None, list_config='ListConfig', post_condition=None): from uliweb import request, json #get model model = self._get_model(model) #get query instance and condition query_inst, _cond = self._default_query(model, query_parameters, query_config) #get list view view = self._get_list_view(model=model, condition=_cond, parameters=list_parameters, config=list_config) if 'data' in request.values: return json(view.json()) else: result = view.run() if query_inst: result.update({'query_form':query_inst.form}) else: result.update({'query_form':''}) result.update({'table':view}) return result
def resumeupload(): """ breakpoint resume file uploading """ resumableChunkNumber = int(request.params['resumableChunkNumber']) resumableChunkSize = int(request.params['resumableChunkSize']) resumableIdentifier = request.params['resumableIdentifier'] resumableTotalChunks = int(request.params['resumableTotalChunks']) ruModel = get_model('resumableUpload') resumableUploadObj = ruModel.get(ruModel.c.guid == resumableIdentifier) if request.method == 'GET': #----check if the chunk has uploaded if resumableUploadObj is not None and int( resumableUploadObj.currentChunk) >= resumableChunkNumber: return json({'status': 'success'}, status=200) else: return json({'status': '404'}, status=404) else: try: resumableFilename = request.params['resumableFilename'] # basePath = os.path.join(application_path(settings.TRANSFER.BASEUPLAOD), request.user.username, resumableIdentifier) ufile = request.files.get('file', None) if not ufile: return json({ 'status': 'err', 'msg': 'No file had been upload' }, status=400) rf = ResumeFile(fileobj=ufile, identify=resumableIdentifier, fileName=resumableFilename, username=request.user.username, totalChunk=resumableTotalChunks, currentChunk=resumableChunkNumber) rf.keepChunk(resumableChunkNumber) if resumableUploadObj is None: ruModel(guid=resumableIdentifier, currentChunk=resumableChunkNumber, totalChunk=resumableTotalChunks, state=0).save() else: ruModel.filter(ruModel.c.guid == resumableIdentifier).update( currentChunk=resumableChunkNumber) return json({'status': 'ok'}, status=200) except Exception as e: return json({'msg': 'keep file error'}, status=400)
def _select_list(self, model=None, queryview=None, queryform=None, auto_condition=True, post_view=None, post_run=None, **kwargs): """ SelectListView wrap method :param auto_condition: if using queryview to create condition """ from uliweb import request, json from uliweb.utils.generic import get_sort_field import copy condition = None if queryview and auto_condition: queryview.run() if hasattr(queryview, 'get_condition'): condition = queryview.get_condition() if 'condition' in kwargs: condition = kwargs['condition'] & condition kwargs['condition'] = condition #process order if 'order_by' not in kwargs: order_by = get_sort_field(model) if order_by is not None: kwargs['order_by'] = order_by _fields = copy.copy(kwargs.get('fields_convert_map', [])) self._process_fields_convert_map(kwargs) downloads = self._parse_download_args(kwargs, _fields) self._process_fields_convert_map(downloads, download=True) view = self._select_list_view(model, **kwargs) if post_view: post_view(view) if 'data' in request.values: return json(view.json()) elif 'download' in request.GET: return view.download(**downloads) else: result = view.run() if queryform or queryview: result.update({'query_form': queryform or queryview.form}) else: result.update({'query_form': ''}) result.update({'table': view}) if post_run: post_run(view, result) return result
def ret_json_response(): commentlist = load_data() result_dict = CommentListMapper({"data": commentlist}).as_dict() print result_dict json_response = json(result_dict, content_type='text/json; charset=utf-8') return json_response
def _list(self, model, queryview=None, queryform=None, auto_condition=True, post_view=None, post_run=None, **kwargs): from uliweb import request, json, CONTENT_TYPE_JSON from sqlalchemy import and_ from uliweb.utils.generic import get_sort_field import copy condition = None if queryview and auto_condition: queryview.run() if hasattr(queryview, 'get_condition'): condition = queryview.get_condition() if 'condition' in kwargs: condition = and_(condition, kwargs['condition']) kwargs['condition'] = condition #process order if 'order_by' not in kwargs: order_by = get_sort_field(model) if order_by is not None: kwargs['order_by'] = order_by _fields = copy.copy(kwargs.get('fields_convert_map', [])) self._process_fields_convert_map(kwargs) downloads = self._parse_download_args(kwargs, _fields) self._process_fields_convert_map(downloads, download=True) #get list view view = self._list_view(model=model, **kwargs) if post_view: post_view(view) if 'data' in request.values: return json(view.json(), content_type=CONTENT_TYPE_JSON) elif 'download' in request.GET: return view.download(**downloads) else: result = view.run() if queryview: result.update({'query_form': queryform or queryview.form}) else: result.update({'query_form': ''}) result.update({'table': view}) if post_run: post_run(view, result) return result
def api_sync_user(self): username = request.values.get("username", "").strip() errmsg = "" if username: user = functions.get_user(username) else: user = None if not user: errmsg = "User not found" return json({"errmsg": errmsg, "user": user})
def _wiki_get_revision(self, pagename): from uliweb import request, json page = int(request.GET.get('page', 1)) num = settings.WIKI.WIKI_REVISION_NUM wiki = self.model.get(self.model.c.name == pagename) if wiki: count = wiki.changeset.all().count() changeset = wiki.changeset.all().fields('wiki', 'revision', 'editor', 'modified_time').order_by(self.changeset.c.revision.desc()).limit(num).offset((page-1)*num) result = [] for row in changeset: d = row.to_dict(fields=['wiki', 'revision', 'editor', 'modified_time']) d['editor'] = unicode(row.editor) result.append(d) return json({'success':True, 'changeset':result, 'next':(page)*num<count}) else: return json({'success':False, 'message':unicode(_("The page %s is not existed, please check!")) % pagename})
def api_add(self): if request.data: data = json_.loads(request.data) else: data = {} data["groupname"] = data.get("name").strip() data["order"] = data.get("order").strip() data["auth_type"] = settings.USERGROUP.AUTHTYPE_LOCAL UserGroupMd = self.UserGroupMd if not data["groupname"]: return json({"msg":u"GroupName must be filled", "success":False}) cnt = UserGroupMd.filter(UserGroupMd.c.name == data["groupname"]).count() if cnt > 0: return json({"msg":u"GroupName (%s) existed" % data["groupname"], "success":False}) ret = UserGroupMd(**data).save() if ret: return json({"msg":u"Add GroupUser (%s) OK!" % data["groupname"], "success":True}) else: return json({"msg":u"Fail to save UserGroup!", "success":False})
def deluser(self): User = functions.get_model('user') UserGroupMd = self.UserGroupMd user_id = request.POST.get('user_id') group_id = request.POST.get('group_id') user = User.get(int(user_id)) userGroup = UserGroupMd.get(int(group_id)) if not user: return json({'success':False, 'msg':"Can't find the user id %s" % user_id}) if not userGroup: return json({'success':False, 'msg':"Can't find the UserGroup id %s" % group_id}) if userGroup.auth_type == settings.USERGROUP.AUTHTYPE_LDAP: return json({'success':False, 'msg':"The 'ldap' UserGroup does not allow to del.id is %s" % group_id}) if userGroup.users.has(user): userGroup.users.remove(user) return json({'success':True, 'msg':"The user %s has been delete from UserGroup(%s) successfully." % (user.username, userGroup.name)}) else: return json({'success':False, 'msg':"The user %s is not existed in UserGroup %s ." % (user.username, userGroup.name)})
def adduser(self): User = functions.get_model('user') UserGroupMd = self.UserGroupMd user_id = request.POST.get('user_id') group_id = request.POST.get('group_id') user = User.get(int(user_id)) userGroup = UserGroupMd.get(int(group_id)) if not user: return json({'success':False, 'msg':"Can't find the user id %s" % user_id}) if not userGroup: return json({'success':False, 'msg':"Can't find the UserGroup id %s" % userGroup}) if userGroup.users.has(user): return json({'success':False, 'msg':"The user %s has already existed in UserGroup %s" % (user.username, userGroup.name)}) else: userGroup.users.add(user) userdata = {'username':user.username, 'id':user.id} if hasattr(user, 'nickname'): userdata['nickname'] = user.nickname return json({'success':True, 'data':userdata, 'msg':"The user %s added to UserGroup(%s) successfully" % (user.username, userGroup.name)})
def _select_list(self, model=None, queryview=None, queryform=None, auto_condition=True, post_view=None, post_run=None, **kwargs): """ SelectListView wrap method :param auto_condition: if using queryview to create condition """ from uliweb import request, json from uliweb.utils.generic import get_sort_field import copy condition = None if queryview and auto_condition: queryview.run() if hasattr(queryview, 'get_condition'): condition = queryview.get_condition() if 'condition' in kwargs: condition = kwargs['condition'] & condition kwargs['condition'] = condition #process order if 'order_by' not in kwargs: order_by = get_sort_field(model) if order_by is not None: kwargs['order_by'] = order_by _fields = copy.copy(kwargs.get('fields_convert_map', [])) self._process_fields_convert_map(kwargs) downloads = self._parse_download_args(kwargs, _fields) self._process_fields_convert_map(downloads, download=True) view = self._select_list_view(model, **kwargs) if post_view: post_view(view) if 'data' in request.values: return json(view.json()) elif 'download' in request.GET: return view.download(**downloads) else: result = view.run() if queryform or queryview: result.update({'query_form':queryform or queryview.form}) else: result.update({'query_form':''}) result.update({'table':view}) if post_run: post_run(view, result) return result
def add(self): if request.method == "GET": images = [] for i in self.client.images(): images.append(i["RepoTags"][0]) return {"images": sorted(images)} elif request.method == "POST": image = request.params["image"] name = request.params["name"] command = request.params["command"] ip = request.params["ip"] if len(name) <= 1: return json({"success": False, "message": "name长度不能小于一个字符"}) containerId = self.client.create_container( image=image, name=name, command=command, stdin_open=True, tty=True, detach=True )["Id"] print containerId self.client.start(container=containerId) cmd = "" if self.client.inspect_container(container=containerId)["State"]["Running"] and ip.strip(): cmd = "pipework " + settings["DOCKER"]["NETWORK"] + " " + name + " " + ip + "/24" else: cmd = "pipework " + settings["DOCKER"]["NETWORK"] + " " + name + " dhcp" print cmd # print "docker exec "+containerId + """ bash -c "echo 'root:""" + command + """' | chpasswd" """ print os.system(cmd) # ip = str(commands.getoutput("docker exec "+ containerId +" ifconfig eth1|sed -n 2p|awk '{ print $2 }'|awk -F : '{ print $2 }'")).lower() # print ip # if ip.find("error") ==-1: # #con = functions.get_model(Container) # c = con( # containerid = containerId, # name = name, # ip = ip # ) # c.save() print json({"success": True, "container": containerId}) return json({"success": True, "container": containerId}) else: return redirect(url_for(Containers.add))
def _list(self, model, queryview=None, queryform=None, auto_condition=True, post_view=None, post_run=None, **kwargs): from uliweb import request, json, CONTENT_TYPE_JSON from sqlalchemy import and_ from uliweb.utils.generic import get_sort_field import copy condition = None if queryview and auto_condition: queryview.run() if hasattr(queryview, 'get_condition'): condition = queryview.get_condition() if 'condition' in kwargs: condition = and_(condition, kwargs['condition']) kwargs['condition'] = condition #process order if 'order_by' not in kwargs: order_by = get_sort_field(model) if order_by is not None: kwargs['order_by'] = order_by _fields = copy.copy(kwargs.get('fields_convert_map', [])) self._process_fields_convert_map(kwargs) downloads = self._parse_download_args(kwargs, _fields) self._process_fields_convert_map(downloads, download=True) #get list view view = self._list_view(model=model, **kwargs) if post_view: post_view(view) if 'data' in request.values: return json(view.json(), content_type=CONTENT_TYPE_JSON) elif 'download' in request.GET: return view.download(**downloads) else: result = view.run() if queryview: result.update({'query_form':queryform or queryview.form}) else: result.update({'query_form':''}) result.update({'table':view}) if post_run: post_run(view, result) return result
def _list(self, model, queryview=None, queryform=None, **kwargs): from uliweb import request, json, CONTENT_TYPE_JSON from sqlalchemy import and_ from uliweb.utils.generic import get_sort_field if queryview: queryview.run() condition = queryview.get_condition() else: condition = None if 'condition' in kwargs: condition = and_(condition, kwargs['condition']) kwargs['condition'] = condition else: kwargs['condition'] = condition #process order if 'order_by' not in kwargs: order_by = get_sort_field(model) if order_by is not None: kwargs['order_by'] = order_by self._process_fields_convert_map(kwargs) downloads = {} downloads['filename'] = kwargs.pop('download_filename', 'download.xlsx') downloads['action'] = kwargs.pop('download_action', 'download') downloads['fields_convert_map'] = kwargs.pop( 'download_fields_convert_map', kwargs.get('fields_convert_map')) downloads['domain'] = kwargs.pop('download_domain', '') downloads['timeout'] = 0 downloads.update(kwargs.pop('download_kwargs', {})) self._process_fields_convert_map(downloads) #get list view view = self._list_view(model=model, **kwargs) if 'data' in request.values: return json(view.json(), content_type=CONTENT_TYPE_JSON) elif 'download' in request.GET: return view.download(**downloads) else: result = view.run() if queryview: result.update({'query_form': queryform or queryview.form}) else: result.update({'query_form': ''}) result.update({'table': view}) return result
def api_update(self): if request.data: data = json_.loads(request.data) else: data = {} data["name"] = data.get("name").strip() data["order"] = data.get("order").strip() data["auth_type"] = "local" UserGroupMd = self.UserGroupMd userGroup = UserGroupMd.get(int(data.get("id"))) if not data["name"]: return json({"msg":u"GroupName must be filled", "success":False}) cnt = UserGroupMd.filter(UserGroupMd.c.name == data["name"]).count() if userGroup.name != data["name"] and cnt > 0: return json({"msg":u"GroupName (%s) existed" % data["name"], "success":False}) if userGroup.auth_type == settings.USERGROUP.AUTHTYPE_LDAP: return json({"msg":u"AuthTyppe(%s) does not allow to update" % userGroup.auth_type, "success":False}) ret = userGroup.update(**data).save() if ret: return json({"msg":u"Update UserGroup (%s) OK!" % data["name"], "success":True}) else: return json({"msg":u"Fail to update UserGroup!", "success":False})
def _wiki_test_acl(self, pagename): from uliweb import request User = functions.get_model('user') user = User.get(User.c.username == request.POST.get('username')) wiki = self.model.get(self.model.c.name == pagename) wiki.acl = request.POST.get('acl') p = ['read', 'write', 'revert', 'delete', 'admin'] result = self._find_tree_permissions(p, wiki, user) x = [(y, result.get(y)) for y in p] return json({'success':True, 'data':x})
def _list(self, model, queryview=None, queryform=None, **kwargs): from uliweb import request, json, CONTENT_TYPE_JSON from sqlalchemy import and_ from uliweb.utils.generic import get_sort_field if queryview: queryview.run() condition = queryview.get_condition() else: condition = None if "condition" in kwargs: condition = and_(condition, kwargs["condition"]) kwargs["condition"] = condition else: kwargs["condition"] = condition # process order if "order_by" not in kwargs: order_by = get_sort_field(model) if order_by is not None: kwargs["order_by"] = order_by self._process_fields_convert_map(kwargs) downloads = {} downloads["filename"] = kwargs.pop("download_filename", "download.xlsx") downloads["action"] = kwargs.pop("download_action", "download") downloads["fields_convert_map"] = kwargs.pop("download_fields_convert_map", kwargs.get("fields_convert_map")) downloads["domain"] = kwargs.pop("download_domain", "") downloads["timeout"] = 0 downloads.update(kwargs.pop("download_kwargs", {})) self._process_fields_convert_map(downloads) # get list view view = self._list_view(model=model, **kwargs) if "data" in request.values and request.is_xhr: return json(view.json(), content_type=CONTENT_TYPE_JSON) elif "download" in request.GET: return view.download(**downloads) else: result = view.run() if queryview: result.update({"query_form": queryform or queryview.form}) else: result.update({"query_form": ""}) result.update({"table": view}) return result
def api_addbatch(self): ldapusernames = request.values.get("ldapusers", 0).strip() usernames = ldapusernames.split(",") succlist = [] faillist = [] for name in usernames: user = functions.get_user(name) if user: succlist.append(user.to_dict()) else: faillist.append(name) return json({ "success": True, "succlist": succlist, "faillist": faillist })
def jsonql(): from . import query from uliweb import json print request.data d = request.json() try: result = query(d) result['status'] = '200' result['message'] = 'Success' except Exception as e: log.exception(e) result = {} result['status'] = '500' result['message'] = 'Error' return json(result)
def save(self, relative_model=None): """ 保存category,同时根据relative_model删除对应的分类下的记录 """ import json as json_lib from uliweb import request from uliweb.utils import date data = json_lib.loads(request.POST['data']) ids = {0: 0} objs = [] #记录更新的记录 #先将所有记录置为删除状态 self.model.all().update(deleted=True) for row in data: d = { 'name': row['name'], 'parent': ids[row['parent']], 'order': row['order'], 'deleted': False, 'modified_time': date.now(), 'modified_user': request.user.id } flag = '' if not row['id'].isdigit(): #新记录 flag = 'new' else: obj = self.model.get(int(row['id'])) if not obj: flag = 'new' if flag == 'new': obj = self.model(**d) else: obj.update(**d) obj.save() objs.append(obj.to_dict()) ids[row['id']] = obj.id self.model.filter(self.model.c.deleted == True).remove() if relative_model: R = functions.get_model(relative_model) for row in self.model.filter(self.model.c.deleted == True): R.filter(R.c.category == row.id).update(category=0) row.remove() return json({'success': True, 'message': '保存成功!', 'data': objs})
def _search(self, model, condition=None, search_field='name', value_field='id', label_field=None, pagination=True): """ Default search function :param search_field: Used for search field, default is 'name' :param value_field: Used for id field, default is id :param label_field: Used for label field, default is None, then it'll use unicode() function """ from uliweb import json, request name = request.GET.get('term', '') M = functions.get_model(model) def _v(label_field): if label_field: return lambda x: getattr(x, label_field) else: return lambda x: unicode(x) v_field = request.values.get('label', 'title') page = int(request.values.get('page') or 1) limit = int(request.values.get('limit') or 10) v_func = _v(label_field) if name: if condition is None: condition = M.c[search_field].like('%' + name + '%') if pagination: query = M.filter(condition) total = query.count() rows = [{ 'id': getattr(obj, value_field), v_field: v_func(obj) } for obj in query.limit(limit).offset((page - 1) * limit)] result = {'total': total, 'rows': rows} else: result = [{ 'id': getattr(obj, value_field), v_field: v_func(obj) } for obj in M.filter(condition)] else: result = [] return json(result)
def _do_get_tasks(self, job): from uliweb import json tasks = [] for t in job.tasks: d = {'id':str(t.id), 'command':t.command, 'title':t.command, 'label':t.label, 'work_directory':t.work_directory, 'depend_tasks':t.depend_tasks, 'queue':t.queue.split(','), 'timeout':t.timeout/60/1000, # 'change':False, } tasks.append(d) return json({'tasks':tasks})
def _do_get_tasks(self, job): from uliweb import json tasks = [] for t in job.tasks: d = { 'id': str(t.id), 'command': t.command, 'title': t.command, 'label': t.label, 'work_directory': t.work_directory, 'depend_tasks': t.depend_tasks, 'queue': t.queue.split(','), 'timeout': t.timeout / 60 / 1000, # 'change':False, } tasks.append(d) return json({'tasks': tasks})
def _do_save(self, job): import json as _json from uliweb import request, json from uliweb.utils.common import expand_path Task = functions.get_model('cron_task') nodes = _json.loads(request.POST.get('nodes')) timeout = 0 #对已有结点进行遍历,不存在的删除,已存在的更新,将依赖和子结点数清空 for task in job.tasks: data = nodes.pop(task.id, None) #将分钟转为毫秒 data['timeout'] = int(data['timeout']) * 60 * 1000 if data['queue']: data['queue'] = ','.join(data['queue']) else: data['queue'] = 'default' if not data: task.delete() else: task.update(cron_job=job.id, modified_user=request.user.id, **data) task.save() timeout += task.timeout #nodes中剩余的就是新增的 for _id, data in nodes.items(): #将分钟转为毫秒 data['timeout'] = int(data['timeout']) * 60 * 1000 if data['queue']: data['queue'] = ','.join(data['queue']) else: data['queue'] = 'default' task = Task(cron_job=job.id, modified_user=request.user.id, **data) task.save() timeout += task.timeout #计算整个job的超时 job.timeout = timeout job.save() return json({'success': True})
def _do_save(self, job): import json as _json from uliweb import request, json from uliweb.utils.common import expand_path Task = functions.get_model('cron_task') nodes = _json.loads(request.POST.get('nodes')) timeout = 0 #对已有结点进行遍历,不存在的删除,已存在的更新,将依赖和子结点数清空 for task in job.tasks: data = nodes.pop(task.id, None) #将分钟转为毫秒 data['timeout'] = int(data['timeout']) * 60 * 1000 if data['queue']: data['queue'] = ','.join(data['queue']) else: data['queue'] = 'default' if not data: task.delete() else: task.update(cron_job=job.id, modified_user=request.user.id, **data) task.save() timeout += task.timeout #nodes中剩余的就是新增的 for _id, data in nodes.items(): #将分钟转为毫秒 data['timeout'] = int(data['timeout']) * 60 * 1000 if data['queue']: data['queue'] = ','.join(data['queue']) else: data['queue'] = 'default' task = Task(cron_job=job.id, modified_user=request.user.id, **data) task.save() timeout += task.timeout #计算整个job的超时 job.timeout = timeout job.save() return json({'success':True})
def save(self, relative_model=None): """ 保存category,同时根据relative_model删除对应的分类下的记录 """ import json as json_lib from uliweb import request from uliweb.utils import date data = json_lib.loads(request.POST['data']) ids = {0:0} objs = [] #记录更新的记录 #先将所有记录置为删除状态 self.model.all().update(deleted=True) for row in data: d = {'name':row['name'], 'parent':ids[row['parent']], 'order':row['order'], 'deleted':False, 'modified_time':date.now(), 'modified_user':request.user.id} flag = '' if not row['id'].isdigit(): #新记录 flag = 'new' else: obj = self.model.get(int(row['id'])) if not obj: flag = 'new' if flag == 'new': obj = self.model(**d) else: obj.update(**d) obj.save() objs.append(obj.to_dict()) ids[row['id']] = obj.id self.model.filter(self.model.c.deleted==True).remove() if relative_model: R = functions.get_model(relative_model) for row in self.model.filter(self.model.c.deleted==True): R.filter(R.c.category==row.id).update(category=0) row.remove() return json({'success':True, 'message':'保存成功!', 'data':objs})
def _list(self, model, queryview=None, queryform=None, **kwargs): from uliweb import request, json, CONTENT_TYPE_JSON from sqlalchemy import and_ if queryview: queryview.run() condition = queryview.get_condition() else: condition = None if 'condition' in kwargs: condition = and_(condition, kwargs['condition']) kwargs['condition'] = condition else: kwargs['condition'] = condition self._process_fields_convert_map(kwargs) #get list view view = self._list_view(model=model, **kwargs) if 'data' in request.values: return json(view.json(), content_type=CONTENT_TYPE_JSON) elif 'download' in request.GET: filename = 'download.xlsx' kw = {} kw['filename'] = kwargs.get('download_filename', filename) kw['action'] = kwargs.get('download_action', 'download') kw['timeout'] = 0 kw['query'] = kwargs.get('download_query', kwargs.get('query')) kw['fields_convert_map'] = kwargs.get('download_fields_convert_map', kwargs.get('fields_convert_map')) kw['domain'] = kwargs.get('download_domain') return view.download(**kw) else: result = view.run() if queryview: result.update({'query_form':queryform or queryview.form}) else: result.update({'query_form':''}) result.update({'table':view}) return result
def list(self): query_view = self._create_query_view() if 'data' in request.values: rows_data = [ {'c1': '大分类一', 'c2': '小分类1', 'c3': '数据', 'c4': '数据', 'c5': '数据'}, {'c1': '大分类一', 'c2': '小分类2', 'c3': '数据', 'c4': '数据', 'c5': '数据'}, {'c1': '大分类一', 'c2': '小分类2', 'c3': '数据', 'c4': '数据', 'c5': '数据'}, {'c1': '大分类二', 'c2': '小分类2', 'c3': '数据', 'c4': '数据', 'c5': '数据'}, {'c1': '大分类二', 'c2': '小分类3', 'c3': '数据', 'c4': '数据', 'c5': '数据'}, {'c1': '大分类二', 'c2': '小分类3', 'c3': '数据', 'c4': '数据', 'c5': '数据'} ] grid_data = { 'page_rows': 10, 'rows': rows_data, 'limit': 10, 'pageno': 1, 'table_id': 'test_table', 'total': '10', 'page': '1' } return json(grid_data) else: fields_list = [ {'name': 'c1', 'title': '字段1', 'width': 200, 'frozen': True}, {'name': 'c2', 'title': '字段2', 'width': 200, 'frozen': True}, {'name': 'c3', 'title': '字段3', 'width': 200}, {'name': 'c4', 'title': '字段4', 'width': 200}, {'name': 'c5', 'title': '字段5', 'width': 200}, ] result = { 'query_form': query_view.get_json(), 'table': { 'data_url': query_string(request_url(), data=1), 'table_info': { 'fields_list': fields_list } } } return result
def _list(self, model, queryview=None, **kwargs): from uliweb import request, json, CONTENT_TYPE_JSON from sqlalchemy import and_ if queryview: queryview.run() condition = queryview.get_condition() else: condition = None if 'condition' in kwargs: condition = and_(condition, kwargs['condition']) kwargs['condition'] = condition else: kwargs['condition'] = condition #get list view view = self._list_view(model=model, **kwargs) if 'data' in request.values: return json(view.json(), content_type=CONTENT_TYPE_JSON) elif 'download' in request.GET: filename = 'download.xls' kw = {} kw['action'] = kwargs.get('download_action', 'download') kw['timeout'] = 0 kw['query'] = kwargs.get('download_query', kwargs.get('query')) kw['fields_convert_map'] = kwargs.get('download_fields_convert_map', kwargs.get('fields_convert_map')) kw['domain'] = kwargs.get('download_domain') return view.download(filename, **kw) else: result = view.run() if queryview: result.update({'query_form':queryview.form}) else: result.update({'query_form':''}) result.update({'table':view}) return result
def _search(self, model, condition=None, search_field="name", value_field="id", label_field=None, pagination=True): """ Default search function :param search_field: Used for search field, default is 'name' :param value_field: Used for id field, default is id :param label_field: Used for label field, default is None, then it'll use unicode() function """ from uliweb import json, request name = request.GET.get("term", "") M = functions.get_model(model) def _v(label_field): if label_field: return lambda x: getattr(x, label_field) else: return lambda x: unicode(x) v_field = request.values.get("label", "title") page = int(request.values.get("page") or 1) limit = int(request.values.get("limit") or 10) v_func = _v(label_field) if name: if condition is None: condition = M.c[search_field].like("%" + name + "%") if pagination: query = M.filter(condition) total = query.count() rows = [ {"id": getattr(obj, value_field), v_field: v_func(obj)} for obj in query.limit(limit).offset((page - 1) * limit) ] result = {"total": total, "rows": rows} else: result = [{"id": getattr(obj, value_field), v_field: v_func(obj)} for obj in M.filter(condition)] else: result = [] return json(result)
def _search(self, model, condition=None, search_field='name', value_field='id', label_field=None, pagination=True): """ Default search function :param search_field: Used for search field, default is 'name' :param value_field: Used for id field, default is id :param label_field: Used for label field, default is None, then it'll use unicode() function """ from uliweb import json, request name = request.GET.get('term', '') M = functions.get_model(model) def _v(label_field): if label_field: return lambda x: getattr(x, label_field) else: return lambda x: unicode(x) v_field = request.values.get('label', 'title') page = int(request.values.get('page') or 1) limit = int(request.values.get('limit') or 10) v_func = _v(label_field) if name: if condition is None: condition = M.c[search_field].like('%' + name + '%') if pagination: query = M.filter(condition) total = query.count() rows = [{'id': getattr(obj, value_field), v_field: v_func(obj)} for obj in query.limit(limit).offset((page-1)*limit)] result = {'total':total, 'rows':rows} else: result = [{'id': getattr(obj, value_field), v_field: v_func(obj)} for obj in M.filter(condition)] else: result = [] return json(result)
def api_get_database_userdata(self): if request.data: data = json_.loads(request.data) else: data = {} User = functions.get_model("user") l = User.filter(User.c.auth_type == settings.AUTH.AUTH_TYPE_LDAP) username = data.get("username", "").strip() sort = data.get("sort") order = data.get("order") limit = data.get("limit") offset = data.get("offset") if username: l.filter(User.c.username.like('%%%s%%' % username)) if sort: sort_key = getattr(User.c, sort) if order: sort_key = getattr(sort_key, order)() l = l.order_by(sort_key) if limit: l = l.limit(limit) if offset: l = l.offset(offset) return json({"total": l.count(), "rows": [i.to_dict() for i in l]})
def list(self): result = [] for row in self.model.filter(self.model.c.deleted==False): d = row.to_dict() result.append(d) return json(result)
def _wiki_preview(self, pagename): from uliweb import request, json content, kwargs = self._get_page_html(request.POST.get('content') or 'No conent') return json({'success':True, 'data':content, 'page_args':kwargs})
def ajaxPOST(): var1 = request.POST.get("var1") var2 = request.POST.get("var2") print "var1=%s\r\nvar2=%s"%(var1,var2) return json({'success': True, 'msg': "", 'content': "<h1>AJAX POST</h1>"})
def list(self): query_view = self._create_query_view() if 'data' in request.values: rows_data = [{ 'c1': '大分类一', 'c2': '小分类1', 'c3': '数据', 'c4': '数据', 'c5': '数据' }, { 'c1': '大分类一', 'c2': '小分类2', 'c3': '数据', 'c4': '数据', 'c5': '数据' }, { 'c1': '大分类一', 'c2': '小分类2', 'c3': '数据', 'c4': '数据', 'c5': '数据' }, { 'c1': '大分类二', 'c2': '小分类2', 'c3': '数据', 'c4': '数据', 'c5': '数据' }, { 'c1': '大分类二', 'c2': '小分类3', 'c3': '数据', 'c4': '数据', 'c5': '数据' }, { 'c1': '大分类二', 'c2': '小分类3', 'c3': '数据', 'c4': '数据', 'c5': '数据' }] grid_data = { 'page_rows': 10, 'rows': rows_data, 'limit': 10, 'pageno': 1, 'table_id': 'test_table', 'total': '10', 'page': '1' } return json(grid_data) else: fields_list = [ { 'name': 'c1', 'title': '字段1', 'width': 200, 'frozen': True }, { 'name': 'c2', 'title': '字段2', 'width': 200, 'frozen': True }, { 'name': 'c3', 'title': '字段3', 'width': 200 }, { 'name': 'c4', 'title': '字段4', 'width': 200 }, { 'name': 'c5', 'title': '字段5', 'width': 200 }, ] result = { 'query_form': query_view.get_json(), 'table': { 'data_url': query_string(request_url(), data=1), 'table_info': { 'fields_list': fields_list } } } return result
def generic_list(model=None, get_url=get_url, layout=None, template=None, key_field='id', add_button_text=None, view=None, data=None, json_result=True, pagination=True, rows=10): from uliweb.utils.generic import ListView from uliweb import request, response from uliweb.core.html import Tag if not view: def key(value, obj): url = get_url('view', id=obj.id) return str(Tag('a', value, href="%s" % url)) if not model or not get_model(model): error("Can't find model [%s], please check it" % model) pageno = int(request.GET.get('pageno', 0)) rows_per_page = rows if json_result: pageno = int(request.values.get('page', 1)) - 1 rows_per_page = int(request.values.get('rows', rows)) fields_convert_map = {key_field: key} _id = '%s_table' % model view = ListView(model, rows_per_page=rows_per_page, pageno=pageno, id=_id, fields_convert_map=fields_convert_map, pagination=pagination) else: _id = view.id if 'data' in request.values: if json_result: return json(view.json()) else: result = view.run(head=False, body=True) return json(result) else: result = view.run(head=True, body=False) if isinstance(result, dict): layout = layout or 'layout.html' template = template or 'generic_list.html' response.template = template data = data or {} result['layout'] = layout result['table_id'] = _id result['get_url'] = get_url result['add_button_text'] = add_button_text or _('New') if json_result: result['table'] = view result.update(data) return result