Esempio n. 1
0
def deletefile(f_id):
    from uliweb.utils.common import log
    
    fileserving = AttachmentsFileServing()
    
    Attachment = functions.get_model('generic_attachment')
    Tables = functions.get_model('tables')
    
    obj = Attachment.get(int(f_id))
    if obj:
        
        #get tablename
        tablename = Tables.get(obj.table_id)
        check_func = settings.Generic_Attachment_Download_Checking.get(tablename)
        if check_func:
            enable = check_func(obj.content_object, request.user, 'delete')
        else:
            enable = True
        
        if enable:
            filename = obj.filepath
            obj.delete()
            try:
                fileserving.delete_filename(filename)
            except Exception as e:
                log.exception(e)
        else:
            raise Forbidden("You have no permission to delete the file.")
        
    return json({'success':True})
Esempio n. 2
0
    def process_response(self, request, response):
        from uliweb import settings, functions, json_dumps
        import base64
        
        #if not debug status it'll quit
        if not settings.get_var('GLOBAL/DEBUG'):
            return response
        
        S = functions.get_model('uliwebrecorderstatus')
        s = S.all().one()
        if not s or s.status == 'E':
            return response
        
        if settings.get_var('ULIWEBRECORDER/response_text'):
            try:
                text = response.data
            except Exception as e:
                text = str(e)
        else:
            text = ''
        
        #test if post_data need to convert base64
        if not request.content_type:
            post_data_is_text = True
        else:
            post_data_is_text = self.test_text(request.content_type)
        if not post_data_is_text:
            post_data = base64.encodestring(request.data)
        else:
            post_data = json_dumps(request.POST.to_dict())

        #test if response.data need to convert base64
        response_data_is_text = self.test_text(response.content_type)
        if not response_data_is_text:
            response_data = base64.encodestring(text)
        else:
            response_data = text

        R = functions.get_model('uliwebrecorder')
        if request.user:
            user_id = request.user.id
        else:
            user_id = None
        max_content_length = settings.get_var('ULIWEBRECORDER/max_content_length')
        if len(response_data) > max_content_length:
            msg = "Content length is great than %d so it will be omitted." % max_content_length
            log.info(msg)
            response_data = msg
            response_data_is_text = True
        recorder = R(method=request.method,
            url=request_url(request),
            post_data_is_text=post_data_is_text,
            post_data=post_data, user=user_id,
            response_data=response_data,
            response_data_is_text=response_data_is_text,
            status_code=response.status_code,
            )
        recorder.save()
        return response
Esempio n. 3
0
 def __init__(self, digital_name, content_name, entity=None):
     self.panel = functions.get_model('dashboardpanel')
     self.layout = functions.get_model('dashboardpanellayout')
     self.digital_name = digital_name
     self.content_name = content_name
     if isinstance(entity, (tuple, list)) or isinstance(entity, orm.Model):
         self.entity = entity
     else:
         self.entity = None
Esempio n. 4
0
    def view_workflow(self, id):
        Job = functions.get_model('cron_job')
        Task = functions.get_model('cron_task')
        job = Job.get(int(id))

        action = request.GET.get('action')
        if action == 'get_tasks':
            return self._do_get_tasks(job)
        else:
            return {'job': job}
Esempio n. 5
0
    def view_workflow(self, id):
        Job = functions.get_model('cron_job')
        Task = functions.get_model('cron_task')
        job = Job.get(int(id))

        action = request.GET.get('action')
        if action == 'get_tasks':
            return self._do_get_tasks(job)
        else:
            return {'job':job}
Esempio n. 6
0
 def __init__(self, digital_name, content_name, entity=None):
     self.panel = functions.get_model('dashboardpanel')
     self.layout = functions.get_model('dashboardpanellayout')
     self.tables = functions.get_model('tables')
     self.digital_name = digital_name
     self.content_name = content_name
     self.entity = None
     self.dashboard_type = None
     if isinstance(entity, (tuple, list)) and len(entity) == 2:
         self.entity = entity
         self.dashboard_type = self.talbes.get_talbe(entity[0]).id
     elif isinstance(entity, orm.Model):
         self.entity = entity
         self.dashboard_type = self.table.get_table(entity.__class__.tablename).id
Esempio n. 7
0
def call(args, options, global_options):
    from uliweb import settings

    signal.signal(signal.SIGINT, handler)
    signal.signal(signal.SIGTERM, handler)

    check_point = settings.get_var('ASYNC_TASKS/check_point')

    log.info('=============================')
    log.info(' Async Task Daemon Starting')
    log.info('=============================')
    log.info('Daemon process %d' % os.getpid())
    log.info('Check point %ds' % check_point)

    Task = functions.get_model('async_tasks')
    query = Task.filter(~Task.c.status.in_([SUCCESS, CANCEL, FAILED])).\
        order_by(Task.c.created_time)

    redis = functions.get_redis()
    while not is_exit:
        Reset()
        for row in query:
            try:
                #log.debug('Ready to process async task {} - {}'.format(row.task_id, row.command_name))
                process_task(row)
            except Exception as e:
                log.exception(e)
        beat = redis.brpop('async_tasks_beat', check_point)
        #to prevent database submit slow than redis
        time.sleep(0.5)
Esempio n. 8
0
def get_jobs():
    global jobs
    jobs = SortedDict()
    Job = functions.get_model('cron_job')

    for row in Job.filter(Job.c.enabled == True):
        jobs[row.id] = row
Esempio n. 9
0
def get_jobs():
    global jobs
    jobs = SortedDict()
    Job = functions.get_model('cron_job')

    for row in Job.filter(Job.c.enabled==True):
        jobs[row.id] = row
Esempio n. 10
0
    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});
Esempio n. 11
0
 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})
Esempio n. 12
0
    def commit(self, session=None):
        Task = functions.get_model('async_tasks')
        if session:
            Task = Task.use(session)
        if self.parent_task and isinstance(self.parent_task, AsyncCommand):
            parent_task_id = self.parent_task.task_id
        else:
            parent_task_id = self.parent_task
        task = Task(task_id=self.task_id,
                    title=self.title,
                    parent_task=parent_task_id,
                    children_count=len(self.tasks),
                    command_name=self.name,
                    command_info=self._make_info(),
                    depend_tasks=self.depend_tasks,
                    startup_time=self.startup_time or date.now(),
                    timeout=self.timeout,
                    max_retry_times=self.max_retry_times,
                    retry_time_interval=self.retry_time_interval,
                    user_id=self.user_id,
                    src_ip=self.src_ip,
                    correlation=self.correlation,
                    correlation_link=self.correlation_link)
        task.update(**self.kwargs)
        task.save()
        self.task = task

        #commit child tasks
        for t in self.tasks.values():
            t.commit()

        log.info('Commit a task %s' % self.task_id)
        return task
Esempio n. 13
0
def get_sequence(key, default=1, step=1, retry_times=None, retry_waittime=None):
    from uliweb.orm import SaveError
    from uliweb import settings
    
    assert step > 0 and default > 0

    Sequence = functions.get_model('sequence')
    i = 0
    waittime = retry_waittime or settings.get_var('SEQUENCE/retry_waittime', 0.05)
    retry_times = retry_times or settings.get_var('SEQUENCE/retry_times', 3)
    while 1:
        try:
            row = Sequence.get(Sequence.c.key==key)
            if row:
                row.value = row.value + step
                row.save(version=True)
            else:
                row = Sequence(key=key, value=(default+step-1))
                row.save()
            break
        except SaveError:
            i += 1
            if i == retry_times:
                raise
            else:
                sleep(waittime)

    return row.value
Esempio n. 14
0
def get_model_columns(model, fields=None, meta="Table"):
    """
    Get fields info according model class, the fields just like ListView fields definition
    :param fields: A list
    :param meta: if no fields, it'll use meta
    """
    from copy import deepcopy

    fields = fields or []
    model = functions.get_model(model)

    if not fields:
        if hasattr(model, meta):
            fields = getattr(model, meta).fields
        else:
            fields = [x for x, y in model._fields_list]

    fields_list = []
    for x in fields:
        if isinstance(x, (str, unicode)):
            f = get_grid_column(model, x)
        elif isinstance(x, dict):
            name = x["name"]
            f = deepcopy(x)
            if "title" not in x:
                f.update(get_grid_column(model, name))
        else:
            raise ValueError("Field should be string or dict type, but {!r} found".format(x))
        fields_list.append(f)
    return fields_list
Esempio n. 15
0
def get_model_columns(model, fields=None, meta='Table'):
    """
    Get fields info according model class, the fields just like ListView fields definition
    :param fields: A list
    :param meta: if no fields, it'll use meta
    """
    from copy import deepcopy

    fields = fields or []
    model = functions.get_model(model)

    if not fields:
        if hasattr(model, meta):
            fields = getattr(model, meta).fields
        else:
            fields = [x for x, y in model._fields_list]

    fields_list = []
    for x in fields:
        if isinstance(x, (str, unicode)):
            f = get_grid_column(model, x)
        elif isinstance(x, dict):
            name = x['name']
            f = deepcopy(x)
            if 'title' not in x:
                f.update(get_grid_column(model, name))
        else:
            raise ValueError(
                "Field should be string or dict type, but {!r} found".format(
                    x))
        fields_list.append(f)
    return fields_list
Esempio n. 16
0
    def _sync_parent(self, parent):
        from sqlalchemy import select, func
        from uliweb.orm import do_

        M = functions.get_model('async_tasks')
        sql = select([M.c.status, func.count('*')], M.c.parent_task==parent.task_id,
                     from_obj=[M.table]).group_by(M.c.status)
        status = {}
        for row in do_(sql):
            status[row[0]] = row[1]

        queued = status.get('0', 0)
        success = status.get('1', 0)
        started = status.get('2', 0)
        error = status.get('E', 0)
        failed = status.get('F', 0)
        cancel = status.get('C', 0)

        if started:
            parent.status = '2'
        elif failed:
            parent.status = 'F'
        elif error:
            parent.status = 'E'
        elif (queued and not filter(None, [success, started, error, failed, cancel])):
            parent.status = '0'
        elif (success and not filter(None, [queued, started, error, failed])):
            parent.status = '1'
        elif (cancel and not filter(None, [queued, success, started, error, failed])):
            parent.status = 'C'
Esempio n. 17
0
 def view(self, id):
     """
     查看某个作业的执行信息
     """
     # Detail = functions.get_model('cron_job_details')
     Task = functions.get_model('async_tasks')
     job = functions.get_object('cron_job', int(id))
     template_data = {'job_id':id, 'job':job}
     # condition = Detail.c.cron_job==int(id)
     fields_convert_map = ['view_details', 'status']
     fields = [
         {'name':'task_id', 'width':250},
         {'name':'startup_time', 'width':150},
         {'name':'started_time', 'width':150},
         {'name':'finished_time', 'width':150},
         {'name':'status', 'width':60},
         {'name':'view_details', 'width':100},
     ]
     return self._list('async_tasks',
                       query=job.instances.fields('id', 'task_id',
                                                  'startup_time', 'started_time',
                                                  'finished_time', 'status'
                                             ),
                       queryview=None,
                       template_data=template_data,
                       fields=fields,
                       # condition=condition,
                       order_by=Task.c.startup_time.desc(),
                       fields_convert_map=fields_convert_map)
Esempio n. 18
0
 def api_add(self):
     ip = request.values.get("ip").strip()
     nickname = request.values.get("nickname").strip()
     if not ip:
         return json({
             "msg": u"IP address should be filled",
             "success": False
         })
     if not nickname:
         return json({
             "msg": u"Nickname should be filled",
             "success": False
         })
     User = functions.get_model("user")
     if User.get(User.c.username == ip):
         return json({
             "msg": u"This IP user %s  exists already" % (ip),
             "success": False
         })
     user = User(username=ip,
                 nickname=nickname,
                 auth_type=settings.AUTH.AUTH_TYPE_IPUSER)
     ret = user.save()
     if ret:
         return json({
             "msg": u"Add user %s(%s) OK!" % (ip, nickname),
             "success": True
         })
     else:
         return json({"msg": u"Fail to save user!", "success": False})
Esempio n. 19
0
 def delete(self):
     model = request.GET.get('model', '')
     ids = request.POST.getlist('ids')
     
     Model = functions.get_model(model)
     Model.filter(Model.c.id.in_(ids)).remove()
     return json({'success':True, 'message':'删除成功'})
Esempio n. 20
0
    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)
Esempio n. 21
0
def teacher(user):
    """
    是否是教师角色
    """
    Teacher = functions.get_model("class_teacher")
    obj = Teacher.get(Teacher.c.teacher == user.id)
    return bool(obj)
Esempio n. 22
0
    def view(self, event_id):
        """
        显示某个活动的详细信息
        """
        Request = functions.get_model('eventrequest')
        
        event = self.model.get(int(event_id))
        if not event:
            error("活动记录不存在")
            
        def _has_register(obj):
            if request.user:
                if Request.filter((Request.c.event==obj.id) & (Request.c.user==request.user.id)).count() > 0:
                    return 'already'
                else:
                    return 'not'
            else:
                return 'need_login'
        
        def _get_users(obj):
            User = functions.get_model('user')
            users = []
            for row in Request.filter((Request.c.event==obj.id) & (Request.c.user==User.c.id)).order_by(Request.c.create_date).values(User.c.username, User.c.id, Request.c.create_date):
                users.append(row)
            return users
        
        template_data = {'has_register':_has_register(event), 'users':_get_users(event)}

        view = functions.DetailView(self.model, obj=event, template_data=template_data)
        return view.run()
Esempio n. 23
0
 def req(self, event_id):
     """
     活动报名处理
     返回值是json = {'success':True|False, 'message':xxxx, 'data':{'counts':number}}
     """
     Request = functions.get_model('eventrequest')
     
     event_id = int(event_id)
     
     event = self.model.get(event_id)
     if not event:
         return json({'success':False, 'message':'活动不存在'})
     
     #todo 检查活动是否已经结束
     
     #检查用户是否已经登录
     if request.user:
         if Request.filter((Request.c.event==event_id) & (Request.c.user==request.user.id)).count() == 0:
             obj = Request(event=event_id, user=request.user.id)
             obj.save()
             event.counts += 1
             event.save()
             return json({'success':True, 'message':'报名成功', 'data':{'counts':event.counts}})
         else:
             message = '你已经报过名了'
     else:
         message = '你还没登录,请登录后再报名'
     return json({'success':False, 'message':message})
Esempio n. 24
0
    def _get_columns(self, options, global_options, args, tables=None):
        from uliweb import functions
        from uliweb.orm import ManyToMany, ModelNotFound
        from uliweb.contrib.orm.commands import get_tables, get_sorted_tables

        if not tables:
            if args:
                apps = args
            else:
                apps = self.get_apps(global_options)
        else:
            apps = None

        tables = get_sorted_tables(get_tables(global_options.apps_dir, apps, tables=tables,
            engine_name=options.engine,
            settings_file=global_options.settings,
            local_settings_file=global_options.local_settings))

        for tablename, table in tables:
            try:
                t = functions.get_model(tablename)
            except ModelNotFound:
                continue
            for name, f in t._fields_list:
                if not isinstance(f, ManyToMany):
                    yield tablename, f.to_column_info()
Esempio n. 25
0
def call(args, options, global_options):
    from uliweb import settings

    signal.signal(signal.SIGINT, handler)
    signal.signal(signal.SIGTERM, handler)

    check_point = settings.get_var('ASYNC_TASKS/check_point')

    log.info('=============================')
    log.info(' Async Task Daemon Starting')
    log.info('=============================')
    log.info('Daemon process %d' % os.getpid())
    log.info('Check point %ds' % check_point)

    Task = functions.get_model('async_tasks')
    query = Task.filter(~Task.c.status.in_([SUCCESS, CANCEL, FAILED])).\
        order_by(Task.c.created_time)

    redis = functions.get_redis()
    while not is_exit:
        Reset()
        for row in query:
            try:
                #log.debug('Ready to process async task {} - {}'.format(row.task_id, row.command_name))
                process_task(row)
            except Exception as e:
                log.exception(e)
        beat = redis.brpop('async_tasks_beat', check_point)
        #to prevent database submit slow than redis
        time.sleep(0.5)
Esempio n. 26
0
def get_sequence(key,
                 default=1,
                 step=1,
                 retry_times=None,
                 retry_waittime=None):
    from uliweb.orm import SaveError
    from uliweb import settings

    assert step > 0 and default > 0

    Sequence = functions.get_model('sequence')
    i = 0
    waittime = retry_waittime or settings.get_var('SEQUENCE/retry_waittime',
                                                  0.05)
    retry_times = retry_times or settings.get_var('SEQUENCE/retry_times', 3)
    while 1:
        try:
            row = Sequence.get(Sequence.c.key == key)
            if row:
                row.value = row.value + step
                row.save(version=True)
            else:
                row = Sequence(key=key, value=(default + step - 1))
                row.save()
            break
        except SaveError:
            i += 1
            if i == retry_times:
                raise
            else:
                sleep(waittime)

    return row.value
Esempio n. 27
0
    def _get_columns(self, options, global_options, args, tables=None):
        from uliweb import functions
        from uliweb.orm import ManyToMany, ModelNotFound
        from uliweb.contrib.orm.commands import get_tables, get_sorted_tables

        if not tables:
            if args:
                apps = args
            else:
                apps = self.get_apps(global_options)
        else:
            apps = None

        tables = get_sorted_tables(
            get_tables(global_options.apps_dir,
                       apps,
                       tables=tables,
                       engine_name=options.engine,
                       settings_file=global_options.settings,
                       local_settings_file=global_options.local_settings))

        for tablename, table in tables:
            try:
                t = functions.get_model(tablename)
            except ModelNotFound:
                continue
            for name, f in t._fields_list:
                if not isinstance(f, ManyToMany):
                    yield tablename, f.to_column_info()
Esempio n. 28
0
def downloadfile(f_id):
    from uliweb import request
    import urllib2
    
    fileserving = AttachmentsFileServing()
    
    Attachment = functions.get_model('generic_attachment')
    Tables = functions.get_models('tables')

    obj = Attachment.get(int(f_id))
    if not obj:
        error(_("Can't find the file record of {0}").format(f_id))
        
    #get tablename
    tablename = Tables.get_tablename(obj.table_id)
    check_func = settings.Generic_Attachment_Download_Checking.get(tablename)
    if check_func:
        enable = check_func(obj.content_object, request.user, 'download')
    else:
        enable = True
    if enable:
        alt_filename = urllib2.unquote(obj.filename)
        _filename = fileserving.get_filename(filename, False, convert=False)
        x_filename = filename
        return fileserving.download(alt_filename, real_filename=_filename, x_filename=x_filename)
    else:
        raise Forbidden("You have no permission to download the file.")
Esempio n. 29
0
def teacher(user):
    """
    是否是教师角色
    """
    Teacher = functions.get_model('class_teacher')
    obj = Teacher.get(Teacher.c.teacher == user.id)
    return bool(obj)
Esempio n. 30
0
def del_comment(id):
    Note = functions.get_model('note')
    n = Note.get(int(id))
    if n:
        n.delete()
        return redirect(url_for(index))
    else:
        error("No such record [%s] existed" % id)
Esempio n. 31
0
def get_attachments(slug_or_obj):
    Attachments = functions.get_model('generic_attachment')
        
    if isinstance(slug_or_obj, (str, unicode)):
        query = Attachments.filter(Attachments.c.slug==slug_or_obj)
    else:
        query = Attachments.content_object.filter(slug_or_obj)
    return query
Esempio n. 32
0
def del_comment(id):
    Note = functions.get_model('note')
    n = Note.get(int(id))
    if n:
        n.delete()
        return redirect(url_for(index))
    else:
        error("No such record [%s] existed" % id)
Esempio n. 33
0
def get_table(tablename):
    Tables = functions.get_model('tables')
    
    if tablename not in __tables__:
        table = Tables.get_table(tablename)
        __tables__[tablename] = table
        return table
    else:
        return __tables__[tablename]
Esempio n. 34
0
    def api_remove(self):
        user_id = int(request.values.get("id"))
        User = functions.get_model("user")
        user = User.get(user_id)
        if not user:
            return json({"msg":u"This IP user %s not found"%(ip),"success":False})

        user.delete()
        return json({"msg":u"Remove user %s(%s) OK!"%(user.username,user.nickname),"success":True})
Esempio n. 35
0
    def handle(self, options, global_options, *args):
        from uliweb import functions
        from uliweb.orm import Commit

        self.get_application(global_options)

        Recorder = functions.get_model('uliwebrecorder')
        Recorder.all().clear()
        Commit()
Esempio n. 36
0
def test_manual_rollback():
    from uliweb.orm import Begin, Commit, Rollback
    
    Begin()
    Blog = functions.get_model('blog')
    b = Blog(title='test_add', content='test_add')
    b.save()
    Rollback()
    return 'ok'
Esempio n. 37
0
    def handle(self, options, global_options, *args):
        from uliweb.utils.common import pkg
        from uliweb import functions
        from uliweb.core.template import template_file
        from uliweb.orm import true
        import time

        self.get_application(global_options)

        Recorder = functions.get_model('uliwebrecorder')

        if args:
            if os.path.exists(args[0]):
                message = "Ths file %s is already exists, do you want to overwrite it?" % args[
                    0]
                ans = 'Y' if global_options.yes else get_answer(message)
                if ans != 'Y':
                    return

            out = open(args[0], 'w')
            relpath = os.path.normpath(
                os.path.relpath(os.path.dirname(args[0]) or './',
                                '.')).replace('\\', '/')
        else:
            out = sys.stdout
            relpath = '.'

        condition = true()
        if options.begin_time:
            condition = (Recorder.c.begin_datetime >=
                         options.begin_time) & condition
        if options.id:
            condition = (Recorder.c.id >= int(options.id)) & condition

        path = pkg.resource_filename('uliweb.contrib.recorder',
                                     'template_files')
        tplfile = os.path.join(path, options.template).replace('\\', '/')
        row_tplfile = os.path.join(path,
                                   options.template_row).replace('\\', '/')

        out.write('#coding=utf8\n')
        if global_options.verbose:
            print('#recorder template is "%s"' % tplfile)
            print('#recorder row template is "%s"' % row_tplfile)

        begin = time.time()
        rows = []
        for row in Recorder.filter(condition):
            rows.append(template_file(row_tplfile, {'row': row}).rstrip())

        out.write(
            template_file(tplfile, {
                'project_dir': relpath,
                'rows': rows
            }))
        out.write('\n#total %d records output, time used %ds\n' %
                  (len(rows), time.time() - begin))
Esempio n. 38
0
    def handle(self, options, global_options, *args):
        from uliweb import functions
        from uliweb.orm import Commit

        self.get_application(global_options)

        Recorder = functions.get_model('uliwebrecorder')
        Recorder.all().clear()
        Commit()
Esempio n. 39
0
def get_table(tablename):
    Tables = functions.get_model('tables')

    if tablename not in __tables__:
        table = Tables.get_table(tablename)
        __tables__[tablename] = table
        return table
    else:
        return __tables__[tablename]
Esempio n. 40
0
def process_task(row):
    from uliweb import settings

    Task = functions.get_model('async_tasks')

    now = date.now()
    #check started status
    if row.status == STARTED:
        #check timeout
        if row.started_time + datetime.timedelta(
                milliseconds=row.timeout) > now:
            return
        else:
            #检查任务是否还在执行
            keys_queue = settings.get_var('ASYNC_TASKS/tasks_keys_queue')
            redis = functions.get_redis()
            if redis.sismember(keys_queue, row.task_id):
                return
            row.startup_time = now

    handler = row.get_handler(log=log)
    #check depend task
    depend_task = row.check_depend_tasks(log)
    if depend_task:
        #依赖任务或父任务为取消时,当前任务取消
        if depend_task.status == CANCEL or depend_task.current_status == CANCEL:
            msg = "Depend task {} has been cancelled.".format(
                depend_task.task_id)
            row.cancel(msg, log=log)
            log.debug(
                "Current task {} cancelled because depend task {} status is CANCEL"
                .format(row.task_id, depend_task.task_id))
        #如果依赖任务失败,当前任务也置为失败
        elif depend_task.status == FAILED:
            msg = "Current task {} FAILED because depend task {} status is FAILED".format(
                row.task_id, depend_task.task_id)
            handler.save(status=FAILED, finished_time=date.now(), message=msg)
            log.info(msg)
        return
    if row.retry_times >= row.max_retry_times:
        msg = ('Async task {} reaches max retry times, '
               'status changes to FAILED').format(row.task_id)
        handler.save(status=FAILED, finished_time=date.now(), message=msg)
        log.info(msg)
        return
    if row.startup_time and row.startup_time <= now or not row.startup_time:
        #处理父结点,如果current_status为成功,则不执行
        if row.children_count > 0 and row.current_status == SUCCESS:
            return
        handler.save(status=STARTED,
                     started_time=date.now(),
                     retry_times=row.retry_times + 1)
        log.info('Async task {0} [{1}({2!r})] started, retry_times={3}'.format(
            row.task_id, row.command_name,
            row.command_info.get('parameters', ''), row.retry_times))
        run_command(row)
Esempio n. 41
0
def process_task(row):
    from uliweb import settings

    Task = functions.get_model('async_tasks')

    now = date.now()
    #check started status
    if row.status == STARTED:
        #check timeout
        if row.started_time + datetime.timedelta(milliseconds=row.timeout) > now:
            return
        else:
            #检查任务是否还在执行
            keys_queue = settings.get_var('ASYNC_TASKS/tasks_keys_queue')
            redis = functions.get_redis()
            if redis.sismember(keys_queue, row.task_id):
                return
            row.startup_time = now

    handler = row.get_handler(log=log)
    #check depend task
    depend_task = row.check_depend_tasks(log)
    if depend_task:
        #依赖任务或父任务为取消时,当前任务取消
        if depend_task.status == CANCEL or depend_task.current_status == CANCEL:
            msg = "Depend task {} has been cancelled.".format(depend_task.task_id)
            row.cancel(msg, log=log)
            log.debug("Current task {} cancelled because depend task {} status is CANCEL".format(
                row.task_id, depend_task.task_id
            ))
        #如果依赖任务失败,当前任务也置为失败
        elif depend_task.status == FAILED:
            msg = "Current task {} FAILED because depend task {} status is FAILED".format(
                row.task_id, depend_task.task_id
            )
            handler.save(status=FAILED, finished_time=date.now(), message=msg)
            log.info(msg)
        return
    if row.retry_times >= row.max_retry_times:
        msg = ('Async task {} reaches max retry times, '
                      'status changes to FAILED').format(row.task_id)
        handler.save(status=FAILED, finished_time=date.now(), message=msg)
        log.info(msg)
        return
    if row.startup_time and row.startup_time<=now or not row.startup_time:
        #处理父结点,如果current_status为成功,则不执行
        if row.children_count > 0 and row.current_status == SUCCESS:
            return
        handler.save(status=STARTED, started_time=date.now(),
                     retry_times=row.retry_times+1)
        log.info('Async task {0} [{1}({2!r})] started, retry_times={3}'.format(
            row.task_id,
            row.command_name,
            row.command_info.get('parameters', ''),
            row.retry_times))
        run_command(row)
Esempio n. 42
0
def get_mc():
    global __mc__

    if not __mc__:
        MC = functions.get_model('model_config', signal=False)
        engine = MC.get_engine().engine
        if not MC.table.exists(engine):
            MC.table.create(engine)
        __mc__ = MC
    return __mc__
def import_mock_data():
    panel = functions.get_model('dashboardpanel')
    panellayout = functions.get_model('dashboardpanellayout')

    for data in settings.PANEL_MOCKDATA.panels:
        obj = panel(**data)
        obj.save()

    for data in settings.PANEL_DASHBOARD_MOCKDATA.layout:
        board, panel_layout, pane, row, col = data
        layout = {}

        layout['dashboard_name'] = board
        layout['panel'] = panel.get(panel.c.name == pane)
        layout['row'] = row
        layout['col'] = col
        layout['layout'] = panel_layout
        obj = panellayout(**layout)
        obj.save()
Esempio n. 44
0
def get_mc():
    global __mc__

    if not __mc__:
        MC = functions.get_model('model_config', signal=False)
        engine = MC.get_engine().engine
        if not MC.table.exists(engine):
            MC.table.create(engine)
        __mc__ = MC
    return __mc__
Esempio n. 45
0
def start_job(job, now=None):
    from uliweb.utils import date

    now = now or date.now()

    log.info(u'Starting job [{title} -- {time}] at {now}'.format(
        title=job.title, now=now, time=job.time))
    Task = functions.get_model('cron_task')

    Begin()
    try:
        parent = functions.AsyncCommand(
            '',
            title=job.title,
            startup_time=now,
            category='cron',
            message_source='l',
            timeout=job.timeout,
            #todo add correlation to job
        )
        commands = []
        ids = {}

        #process task
        for row in Task.filter(Task.c.cron_job == job.id):
            #process async task
            d = parse_command(row)

            c = functions.AsyncCommand(
                title=row.label,
                category='cron',
                message_source='l',
                correlation=job.id,
                queue=(row.queue or 'default').split(','),
                timeout=row.timeout or None,
                # correlation_link='/async_task/view/{0}?next=/cron/{1}'.format(job.id, obj.id)
                **d)

            commands.append((c, row.depend_tasks))
            ids[row.id] = c.task_id

            parent.add_child(c)

        #fix depends
        for c, depends in commands:
            _d = [ids[x] for x in depends]
            c.depend_tasks = _d

        functions.call_async(parent)
        job.instances.add(parent.task_id)
        Commit()
    except Exception as e:
        Rollback()
        log.exception(e)
        raise
    def test(self):
        from uliweb import functions

        Workflow = functions.get_workflow()
        WORKFLOW_SPEC_NAME = "TestWorkflow1"
        #create some workflows
        n = 5
        for i in range(0, n):
            workflow = Workflow.create(WORKFLOW_SPEC_NAME)
            workflow.ref_unique_id = "wf%s" % (i + 1)
            workflow.start()

            tasks = workflow.get_active_tasks()
            print "Create workflow %s" % workflow.ref_unique_id

        TaskDB = functions.get_model('workflow_task')
        while True:
            cond = TaskDB.c.state == '1'  # ACTIVE
            query = TaskDB.filter(cond)
            print "loop -------------------------------------"
            ids = [task_obj.id for task_obj in query]

            if ids:
                print "ids: %s" % ids
                for aid in ids:
                    task_obj = TaskDB.get(aid)
                    #print "task_obj.id -- %d" % task_obj.id
                    wf_id = task_obj._workflow_
                    workflow = Workflow.load(wf_id)
                    #print "deliver workflow %s" % workflow.ref_unique_id

                    tasks = workflow.get_active_tasks()

                    if len(tasks) == 1:

                        task_id = tasks[0].get_unique_id()
                        task_name = tasks[0].get_name()
                        next_tasks = tasks[0].get_next_tasks()

                        # print "tasks - %s" %task_name

                        if len(next_tasks) > 1:
                            to_tasks = next_tasks[0][0]
                            workflow.deliver(task_id,
                                             message="Auto",
                                             next_tasks=[to_tasks],
                                             async=False)
                        else:
                            workflow.deliver(task_id,
                                             message="Auto",
                                             async=False)
            else:
                break

        print "Done"
Esempio n. 47
0
 def edit(self):
     errmsg = ""
     User = functions.get_model("user")
     user_id = request.values.get("id")
     if user_id:
         user = User.get(int(user_id))
     else:
         user = None
     if not user:
         errmsg = "User not found"
     return {"errmsg": errmsg, "user": user}
Esempio n. 48
0
def start_job(job, now=None):
    from uliweb.utils import date

    now =  now or date.now()

    log.info(u'Starting job [{title} -- {time}] at {now}'.format(title=job.title, now=now,
                                                           time=job.time))
    Task = functions.get_model('cron_task')

    Begin()
    try:
        parent = functions.AsyncCommand('', title=job.title, startup_time=now,
                                       category='cron', message_source='l',
                                       timeout=job.timeout,
                                       #todo add correlation to job
                                       )
        commands = []
        ids = {}

        #process task
        for row in Task.filter(Task.c.cron_job==job.id):
            #process async task
            d = parse_command(row)

            c = functions.AsyncCommand(
                                title=row.label,
                                category='cron',
                                message_source='l',
                                correlation=job.id,
                                queue=(row.queue or 'default').split(','),
                                timeout=row.timeout or None,
                                # correlation_link='/async_task/view/{0}?next=/cron/{1}'.format(job.id, obj.id)
                                **d
                                )

            commands.append((c, row.depend_tasks))
            ids[row.id] = c.task_id


            parent.add_child(c)

        #fix depends
        for c, depends in commands:
            _d = [ids[x] for x in depends]
            c.depend_tasks = _d


        functions.call_async(parent)
        job.instances.add(parent.task_id)
        Commit()
    except Exception as e:
        Rollback()
        log.exception(e)
        raise
Esempio n. 49
0
def login():
    from uliweb.contrib.auth import login

    text = ""
    username = request.params.get("username", "")

    form = LoginForm()

    if request.user:
        next = request.GET.get('next')
        if next:
            return redirect(next)

    if request.method == 'GET':
        form.next.data = request.GET.get('next', request.referrer or add_prefix('/'))
        return {'form':form, 'msg':'', 'username': username}
    if request.method == 'POST':
        login_failed_history = functions.get_model("login_failed_history")
        login_failed_user = login_failed_history.get(login_failed_history.c.username == username)

        if login_failed_user and login_failed_user.failed_times >= settings.VERIFICATIONCODE.FAILED_TIMES:
            form = CheckLoginForm()

        flag = form.validate(request.params)
        try:
            if flag:
                if not login_failed_user:
                    login_failed_user = login_failed_history(username = username, failed_times = 0, verification_code = text)
                    login_failed_user.save()

                f, d = functions.authenticate(username = form.username.data, password = form.password.data)
                if f:
                    request.session.remember = form.rememberme.data
                    login(form.username.data)
                    login_failed_user.update(failed_times = 0).save()
                    next = urllib.unquote(request.POST.get('next', add_prefix('/')))
                    return redirect(next)
                form.errors.update(d)
                current_failed_times = login_failed_user.failed_times
                current_failed_times = current_failed_times + 1
                if current_failed_times >= settings.VERIFICATIONCODE.FAILED_TIMES:
                    text = gene_code(username)
                    if not isinstance(form, CheckLoginForm):
                        form = CheckLoginForm()
                login_failed_user.update(failed_times = current_failed_times, verification_code = text).save()
            else:
                if login_failed_user and login_failed_user.failed_times >= settings.VERIFICATIONCODE.FAILED_TIMES:
                    text = gene_code(username)
                    login_failed_user.update(verification_code = text).save()
        except Exception, e:
            logger.error("count user: [%s] login failed times error" % (username))
            logger.error("-- error message: %s" % (e))
        msg = form.errors.get('_', '') or _('Login failed!')
        return {'form':form, 'msg':str(msg), 'username': username}
Esempio n. 50
0
 def edit(self):
     errmsg = ""
     User = functions.get_model("user")
     user_id = request.values.get("id")
     if user_id:
         user = User.get(int(user_id))
     else:
         user = None
     if not user:
         errmsg = "User not found"
     return {"errmsg":errmsg,"user":user}
Esempio n. 51
0
    def handle(self, options, global_options, *args):
        from uliweb import functions

        self.get_application(global_options)

        S = functions.get_model('uliwebrecorderstatus')
        s = S.all().one()
        if s:
            status = s.status
        else:
            status = 'E'
        print 'Recorder status is', S.status.get_display_value(status).upper()
Esempio n. 52
0
def set_sequence(key, value):
    assert value > 0

    Sequence = functions.get_model('sequence')
    row = Sequence.get(Sequence.c.key == key)
    if row:
        row.value = value
        row.save(version=True)
    else:
        row = Sequence(key=key, value=value)
        row.save()
    return row
Esempio n. 53
0
 def validate(self, data, all_data=None):
     username = all_data.get("username")
     if not data:
         return False, "This field is required."
     else:
         login_failed_history = functions.get_model("login_failed_history")
         login_failed_user = login_failed_history.get(
             login_failed_history.c.username == username)
         if login_failed_user:
             if data.lower() != login_failed_user.verification_code.lower():
                 return False, "verification code does not match"
         return True, data
Esempio n. 54
0
    def handle(self, options, global_options, *args):
        from uliweb import functions
        from uliweb.orm import Commit

        self.get_application(global_options)

        S = functions.get_model('uliwebrecorderstatus')
        s = S.all().one()
        if s:
            s.status = 'E'
            s.save()
            Commit()
Esempio n. 55
0
    def handle(self, options, global_options, *args):
        from uliweb import functions

        self.get_application(global_options)

        S = functions.get_model('uliwebrecorderstatus')
        s = S.all().one()
        if s:
            status = s.status
        else:
            status = 'E'
        print('Recorder status is', S.status.get_display_value(status).upper())
Esempio n. 56
0
def refresh_code():
    username = request.params.get("username", "")
    text = gene_code(username)
    login_failed_history = functions.get_model("login_failed_history")
    login_failed_user = login_failed_history.get(login_failed_history.c.username == username)
    if login_failed_user and login_failed_user.failed_times >= settings.VERIFICATIONCODE.FAILED_TIMES:
        try:
            login_failed_user.update(verification_code = text).save()
        except Exception, e:
            logger.error("store refreshed verification code for user: [%s] failed" % (username))
            logger.error("-- error message: %s" % (e))
        return json({"result": "success"})