Ejemplo n.º 1
0
def scriptdetail(aid=None):
    user = request.user
    if request.method == 'GET':
        if aid is None:
            article = {'_id':'', 'name':'', 'clsname':'', 'desc':1, 'filepath':'', 'digest':0, 'flows':[], 'own':True}
        else:
            article = Article.queryOne(user, {'_id':aid}, projection={'name':1, 'clsname':1, 'desc':1, 'filepath':1, 'digest':1, 'creator':1})
            article['own'] = str(article['creator']) == user['_id']
            article['flows'] = list(Flow.queryAll(user, {'aid':aid}, projection={'name':1, '_id':1}))

        projection = {'_id':1, 'username':1}
        article['creators'] = [{'text':one['username'], 'value':one['_id']} for one in Creator.queryAll(user, {'_id':{'$ne':user['_id']}}, projection=projection, limit=None)]
        article['select_updators'] = ','.join([str(one['cid']) for one in Permit.queryAll({'cid':{'$ne':user['_id']}, 'creator':user['_id'], 'oid':aid, 'otype':'Article', 'authority':{'$in':[2,3,6,7,10,11,14,15]}}, projection={'cid':1}, limit=None)])
        article['select_queryers'] = ','.join([str(one['cid']) for one in Permit.queryAll({'cid':{'$ne':user['_id']}, 'creator':user['_id'], 'oid':aid, 'otype':'Article', 'authority':{'$mod':[2, 1]}}, projection={'cid':1}, limit=None)])

        result = {"appname":g.appname, "user":user, "script":article}
        result = json.dumps({'code':1, 'msg':'', 'res':result}, ensure_ascii=False, sort_keys=True, indent=4).encode('utf8')
        return result
    elif request.method == 'POST':
        name = request.form.get('name')
        desc = request.form.get('desc')
        select_updators = request.form.get('select_updators')
        unselect_updators = request.form.get('unselect_updators')
        select_queryers = request.form.get('select_queryers')
        unselect_queryers = request.form.get('unselect_queryers')
        result = {'code':1, 'msg':'', 'res':{"appname":g.appname, "user":user}}
        if aid is None:
            result['msg'] = 'Not create article.'
        else:
            Article.update(user, {'_id':aid}, {'$set':{'name':name, 'desc':desc}})
            result['msg'] = 'Update article successfully.'
            unselect(unselect_queryers, 'Article', aid, user['_id'])
            unselect(unselect_updators, 'Article', aid, user['_id'])
            select(select_updators, 'Article', aid, user['_id'], 'update')
            select(select_queryers, 'Article', aid, user['_id'], 'query')
        result = json.dumps(result, ensure_ascii=False, sort_keys=True, indent=4).encode('utf8')
        return result
    else:
        pass
Ejemplo n.º 2
0
def sectiondetail(sid=None):
    user = request.user
    sid = baseorm.IdField.verify(sid)
    if request.method == 'GET':
        projection = {'aid':1, 'next_id':1, 'name':1, 'desc':1, 'flow':1, 'index':1, 'retry':1, 'timelimit':1, 'store':1, 'additions':1, 'creator':1}
        section = Section.queryOne(user, {'_id':sid}, projection=projection)
        next = Section.queryOne(user, {'_id':section['next_id']}, projection=projection) if section['next_id'] else None
        if next is None:
            section['next'] = ''
        else:
            section['next'] = next['name']
        del section['creator']
        author = {}
        for one in Permit.queryAll({'otype':'Section', 'oid':sid}, projection={'cid':1, '_id':0}):
            author[str(one['cid'])] = ''
        section['author'] = urllib.quote(json.dumps(author).encode('utf8'))
        result = {"appname":g.appname, "user":user, "step":section}
        result = json.dumps({'code':1, 'msg':'', 'res':result}, ensure_ascii=False, sort_keys=True, indent=4).encode('utf8')
        return result
    elif request.method == 'POST':
        desc = request.form.get('desc')
        retry = request.form.get('retry')
        timelimit = request.form.get('timelimit')
        store = request.form.get('store')
        additions = request.form.get('additions')
        addcid = request.form.get('addcid', '').split(',')
        delcid = request.form.get('delcid', '').split(',')

        Section.update(user, {'_id':sid}, {'$set':{'desc':desc, 'retry':retry, 'timelimit':timelimit, 'store':store, 'additions':additions}})
        for cid in addcid:
            if cid == '':
                continue
            # cid = baseorm.IdField.verify(cid)
            # if Permit.queryOne({'cid':cid, 'otype':'Section', 'oid':sid}) is None:
            #     permit = Permit(cid=cid, otype='Section', oid=baseorm.IdField.verify(sid), authority=1, desc='---q', status=1, creator=user['_id'], updator=user['_id'], create_time=datetime.datetime.now())
            #     Permit.insert(permit)
        for cid in delcid:
            if cid == '':
                continue
            # Permit.delete({'cid':cid, 'otype':'Section', 'oid':sid})
        result = {"appname":g.appname, "user":user, "step":{}}
        result = json.dumps({'code':1, 'msg':'', 'res':result}, ensure_ascii=False, sort_keys=True, indent=4).encode('utf8')
        return result
    else:
        pass
        
Ejemplo n.º 3
0
def taskdetail(tid=None):
    user = request.user
    tid = baseorm.IdField.verify(tid) if tid else tid
    if request.method == 'GET':
        user = request.user
        if tid is None:
            tid = request.args.get('tid')
        if tid is None:
            task = {'_id':'', 'aid':'', 'sid':'', 'name':'', 'extra':'', 'type':'ONCE', 'period':0, 'fid':'', 'params':'', 'worknum':6, 'queuetype':'M', 'worktype':'THREAD', 'timeout':30, 'category':'', 'push_url':'', 'pull_url':'', 'tag':'', 'own':True}
        else:
            projection = {'aid':1, 'sid':1, 'name':1, 'extra':1, 'type':1, 'period':1, 'fid':1, 'params':1, 'worknum':1, 'queuetype':1, 'worktype':1, 'timeout':1, 'category':1, 'push_url':1, 'tag':1, 'creator':1}
            task = Task.queryOne(user, {'_id':tid}, projection=projection)

            projection = {'name':1}
            task['article'] = {
                'label':'article',
                'key':'aid',
                'val':task['aid'],
                'url':'task/article',
                'options':[{'text':one['name'], 'value':one['_id']} for one in Article.queryAll(user, {}, projection=projection, limit=None)]
            }

            projection = {'name':1}
            task['flow'] = {
                'label':'flow',
                'key':'fid',
                'val':task['fid'],
                'url':'task/flow',
                'options':[{'text':one['name'], 'value':one['_id']} for one in Flow.queryAll(user, {'aid':task['aid']}, projection=projection)]
            }

            projection = {'name':1}
            task['section'] = {
                'label':'section',
                'key':'sid',
                'val':task['sid'],
                'url':'task/section',
                'options':[{'text':one['name'], 'value':one['_id']} for one in Section.queryAll(user, {'fid':task['fid']}, projection=projection)]
            }

            task['own'] = str(task['creator']) == user['_id']
            task['pull_url'] = 'http://%s/gdc/api/data/%s' % (request.host, str(task['_id']))
            del task['creator']
        task['queuetype'] = 'M' # QUEUETYPE.get(task['queuetype'], '')
        task['queuetype_options'] = [{'text':'local', 'value':'P'},
                                    {'text':'beanstalkd', 'value':'B'},
                                    {'text':'redis', 'value':'R'},
                                    {'text':'mongo', 'value':'M'}]
        task['worktype'] = 'THREAD' # WORKTYPE.get(task['worktype'], '')
        task['worktype_options'] = [{'text':'多线程', 'value':'THREAD'},
                                    {'text':'协程', 'value':'GEVENT'}]
        task['type_options'] = [{'text':'临时任务', 'value':'ONCE'},
                                {'text':'周期任务', 'value':'FOREVER'}]
        author = {}
        # for one in Permit.queryAll({'otype':'Task', 'oid':tid}, projection={'cid':1, '_id':0}):
        #     author[str(one['cid'])] = ''
        task['author'] = urllib.quote(json.dumps(author).encode('utf8'))

        projection = {'_id':1, 'username':1}
        task['creators'] = [{'text':one['username'], 'value':one['_id']} for one in Creator.queryAll(user, {'_id':{'$ne':user['_id']}}, projection=projection, limit=None)]
        task['select_updators'] = ','.join([str(one['cid']) for one in Permit.queryAll({'cid':{'$ne':user['_id']}, 'creator':user['_id'], 'oid':tid, 'otype':'Task', 'authority':{'$in':[2,3,6,7,10,11,14,15]}}, projection={'cid':1}, limit=None)])
        task['select_queryers'] = ','.join([str(one['cid']) for one in Permit.queryAll({'cid':{'$ne':user['_id']}, 'creator':user['_id'], 'oid':tid, 'otype':'Task', 'authority':{'$mod':[2, 1]}}, projection={'cid':1}, limit=None)])

        result = {"appname":g.appname, "user":user, "task":task}
        result = json.dumps({'code':1, 'msg':'', 'res':result}, ensure_ascii=False, sort_keys=True, indent=4).encode('utf8')
        return result
    elif request.method == 'POST':
        user = request.user
        name = request.form.get('name')
        extra = request.form.get('extra')
        category = request.form.get('category')
        tag = request.form.get('tag')
        period = request.form.get('period')
        push_url = request.form.get('push_url')
        aid = request.form.get('aid')
        fid = request.form.get('fid')
        sid = request.form.get('sid')
        params = request.form.get('params')
        timeout = request.form.get('timeout', 30)
        worknum = request.form.get('worknum', 6)
        queuetype = request.form.get('queuetype', 'P')
        worktype = 'THREAD'

        select_updators = request.form.get('select_updators')
        unselect_updators = request.form.get('unselect_updators')
        select_queryers = request.form.get('select_queryers')
        unselect_queryers = request.form.get('unselect_queryers')

        aid = baseorm.IdField.verify(aid) if aid is not None else aid
        fid = baseorm.IdField.verify(fid) if fid is not None else fid
        sid = baseorm.IdField.verify(sid) if sid is not None else sid

        if request.form.get('type') == 'ONCE':
            period = 0
            queuetype = 'P'
        else:
            queuetype = 'R'
        section = Section.queryOne(user, {'_id':sid})
        result = {'stat':1, 'desc':'success', 'data':{}}
        if tid is None:
            task = Task(name=name,
                extra=extra,
                category=category,
                tag=tag,
                type=request.form.get('type'),
                period=period,
                push_url=push_url,
                aid=aid,
                fid=fid,
                sid=sid,
                params=params,
                timeout=timeout,
                worknum=worknum,
                state=0,
                status=1,
                queuetype=queuetype,
                worktype=worktype,
                creator=user['_id'],
                updator=user['_id'],
                create_time=datetime.datetime.now())
            task['_id'] = Task.insert(user, task)
        else:
            task = {'name':name,
                'extra':extra,
                'category':category,
                'tag':tag,
                'type':request.form.get('type'),
                'period':period,
                'push_url':push_url,
                'params':params,
                'timeout':timeout,
                'worknum':worknum,
                'queuetype':queuetype,
                'worktype':worktype,
                'updator':user['_id'],
                'update_time':datetime.datetime.now()
            }
            if aid is not None:
                task['aid'] = aid
            if fid is not None:
                task['fid'] = fid
            if sid is not None:
                task['sid'] = sid
            Task.update(user, {'_id':tid}, {'$set':task})
            task['_id'] = tid
        unselect(unselect_queryers, 'Task', tid, user['_id'])
        unselect(unselect_updators, 'Task', tid, user['_id'])
        select(select_updators, 'Task', tid, user['_id'], 'update')
        select(select_queryers, 'Task', tid, user['_id'], 'query')
        result = {"appname":g.appname, "user":user, "task":task}
        result = json.dumps({'code':1, 'msg':'', 'res':result}, ensure_ascii=False, sort_keys=True, indent=4).encode('utf8')
        return result
    elif request.method == 'DELETE':
        if tid:
            Task.update(user, {'_id':tid}, {'$set':{'status':0}})
        else:
            ids = request.form.get('ids')
            for tid in ids.split(','):
                tid = baseorm.IdField.verify(tid)
                Task.update(user, {'_id':tid}, {'$set':{'status':0}})
        result = {"appname":g.appname, "user":user, "task":{}}
        result = json.dumps({'code':1, 'msg':'Delete it successfully.', 'res':result}, ensure_ascii=False, sort_keys=True, indent=4).encode('utf8')
        return result
    else:
        pass
Ejemplo n.º 4
0
def sectiondetail(sid=None):
    user = request.user
    sid = baseorm.IdField.verify(sid)
    if request.method == 'GET':
        projection = {
            'aid': 1,
            'next_id': 1,
            'name': 1,
            'desc': 1,
            'flow': 1,
            'index': 1,
            'retry': 1,
            'timelimit': 1,
            'store': 1,
            'additions': 1,
            'creator': 1
        }
        section = Section.queryOne(user, {'_id': sid}, projection=projection)
        next = Section.queryOne(
            user, {'_id': section['next_id']},
            projection=projection) if section['next_id'] else None
        if next is None:
            section['next'] = ''
        else:
            section['next'] = next['name']
        del section['creator']
        author = {}
        for one in Permit.queryAll({
                'otype': 'Section',
                'oid': sid
        },
                                   projection={
                                       'cid': 1,
                                       '_id': 0
                                   }):
            author[str(one['cid'])] = ''
        section['author'] = urllib.quote(json.dumps(author).encode('utf8'))
        result = {"appname": g.appname, "user": user, "step": section}
        result = json.dumps({
            'code': 1,
            'msg': '',
            'res': result
        },
                            ensure_ascii=False,
                            sort_keys=True,
                            indent=4).encode('utf8')
        return result
    elif request.method == 'POST':
        desc = request.form.get('desc')
        retry = request.form.get('retry')
        timelimit = request.form.get('timelimit')
        store = request.form.get('store')
        additions = request.form.get('additions')
        addcid = request.form.get('addcid', '').split(',')
        delcid = request.form.get('delcid', '').split(',')

        Section.update(user, {'_id': sid}, {
            '$set': {
                'desc': desc,
                'retry': retry,
                'timelimit': timelimit,
                'store': store,
                'additions': additions
            }
        })
        for cid in addcid:
            if cid == '':
                continue
            # cid = baseorm.IdField.verify(cid)
            # if Permit.queryOne({'cid':cid, 'otype':'Section', 'oid':sid}) is None:
            #     permit = Permit(cid=cid, otype='Section', oid=baseorm.IdField.verify(sid), authority=1, desc='---q', status=1, creator=user['_id'], updator=user['_id'], create_time=datetime.datetime.now())
            #     Permit.insert(permit)
        for cid in delcid:
            if cid == '':
                continue
            # Permit.delete({'cid':cid, 'otype':'Section', 'oid':sid})
        result = {"appname": g.appname, "user": user, "step": {}}
        result = json.dumps({
            'code': 1,
            'msg': '',
            'res': result
        },
                            ensure_ascii=False,
                            sort_keys=True,
                            indent=4).encode('utf8')
        return result
    else:
        pass