Ejemplo n.º 1
0
 def insert_user_info(id_, name, sex=0, jsonstr=''):
     #sql = u'INSERT INTO wordp_user (openid, nickname, sex, groupid, add_time, misc) VALUES ("%s", "%s", %s, %s, %s, \'%s\')' \
     #        % (id_, name, sex, 0, int(time.time()), jsonstr)
     user = User(openid=id_, nickname=name, sex=sex, groupid=0, add_time=int(time.time()), misc=jsonstr)
     session.add(user)
     session.commit()
     return name
Ejemplo n.º 2
0
def set_task_status_99(id_, redo, status=-99):
    '''Set task status to -99 to lock it'''
    result = session.query(Task).filter(Task.id==id_).with_lockmode('update').all()
    status = redo and 0 or -88

    if result:
        status = redo and 0 or result[0].status

        if (redo or (result[0].status < WEIXIN_TRY_TIME and result[0].status >= 0)):
            if not redo and (result[0].status > 0 and result[0].ret == 0):
                # NOTE: Task has been done, and the result is success, so we
                # just exit
                logger.debug('Task has been done, exit task %s' % id_)
                return sys.exit(0)
            # We locked the item
            try:
                session.query(Task).filter(Task.id==id_).update({Task.status: status,})
                session.commit()
            except Exception as e:
                session.rollback()
                logger.error('Can not update task(%s) status msg: %s' % (id_, str(e)))
                return False, status
        else:
            return False, status

    return True, status
Ejemplo n.º 3
0
def set_task_status_99(id_, redo, status=-99):
    '''Set task status to -99 to lock it'''
    result = session.query(Task).filter(
        Task.id == id_).with_lockmode('update').all()
    status = redo and 0 or -88

    if result:
        status = redo and 0 or result[0].status

        if (redo or
            (result[0].status < WEIXIN_TRY_TIME and result[0].status >= 0)):
            if not redo and (result[0].status > 0 and result[0].ret == 0):
                # NOTE: Task has been done, and the result is success, so we
                # just exit
                logger.debug('Task has been done, exit task %s' % id_)
                sys.exit(0)
            # We locked the item
            try:
                session.query(Task).filter(Task.id == id_).update({
                    Task.status:
                    status,
                })
                session.commit()
            except Exception as e:
                session.rollback()
                logger.error('Can not update task(%s) status msg: %s' %
                             (id_, str(e)))
                return False, status
        else:
            return False, status

    return True, status
Ejemplo n.º 4
0
 def insert_user_info(id_, name, sex=0, jsonstr=''):
     #sql = u'INSERT INTO wordp_user (openid, nickname, sex, groupid, add_time, misc) VALUES ("%s", "%s", %s, %s, %s, \'%s\')' \
     #        % (id_, name, sex, 0, int(time.time()), jsonstr)
     user = User(openid=id_,
                 nickname=name,
                 sex=sex,
                 groupid=0,
                 add_time=int(time.time()),
                 misc=jsonstr)
     session.add(user)
     session.commit()
     return name
Ejemplo n.º 5
0
def set_task_ret(id_, status, ret, retmsg):
    result = session.query(Task).filter(Task.id==id_).with_lockmode('update').all()
    if result and result[0].status < WEIXIN_TRY_TIME and result[0].status >= 0:
        # We locked the item
        try:
            session.query(Task).filter(Task.id==id_).update({Task.status: status, Task.ret: ret, Task.retmsg: retmsg})
            session.commit()
        except Exception as e:
            session.rollback()
            logger.error('Can not update task(%s) status msg: %s' % (id_, str(e)))
            return False
    else:
        return False

    return True
Ejemplo n.º 6
0
def set_task_ret(id_, status, ret, retmsg):
    result = session.query(Task).filter(
        Task.id == id_).with_lockmode('update').all()
    if result and result[0].status < WEIXIN_TRY_TIME and result[0].status >= 0:
        # We locked the item
        try:
            session.query(Task).filter(Task.id == id_).update({
                Task.status:
                status,
                Task.ret:
                ret,
                Task.retmsg:
                retmsg
            })
            session.commit()
        except Exception as e:
            session.rollback()
            logger.error('Can not update task(%s) status msg: %s' %
                         (id_, str(e)))
            return False
    else:
        return False

    return True
Ejemplo n.º 7
0
    def assign_content_to_client(user_id, user_groupid, client_name, content):
        # Save url to taks list in database
        # Frist, we should find url in database, which user insert in 7
        # days, if find same url, we reject the request.(There have some
        # `status` and `ret` check)
        #sql = 'SELECT * from wordp_task where param1=\'%s\' and add_time>=%s \
        #and uid=%s' \
        #% (content, int(time.time())-3600*24*7, user_id)
        t_before_7_day = int(time.time())-3600*24*7
        result = session.query(Task).filter(Task.uid==user_id).filter(Task.add_time>=t_before_7_day).filter(Task.param1==content).filter(Task.client_name==client_name).scalar()
        if result:
            def common_resend():
                # NOTE: send signal to clients
                ret = Home.send_task_request(client_name, result.id)
                if ret:
                    return _('Your request have some error(status%s ret%s), \
and we have send the process request') % (result.status, result.ret)
                else:
                    return _('Can not deal with your request, (Something about client manager \
error), please inform administrator %s. Thanks.') % ADMIN_MAIL

            if (result.status == 0):
                return common_resend()

            elif (result.status > 0 and result.status < WEIXIN_RETRY_TIME):
                # Resend the request
                if result.ret != 0:
                    return common_resend()
                else:
                    # process successfully
                    return _('''Do not submit request duplicated(status%s ret%s), your \
request been processed successfully.''') % (result.status, result.ret)
            elif (result.status >= WEIXIN_RETRY_TIME):
                # The task has been processed out of retry time, and we
                # will not process it again
                if result.status == -99:
                    return _('''Your request is being processed, please wait.''')
                if result.ret != 0:
                    return _('''Your request has been processed, but not \
success, is %s, please contact with administrator %s''') % (result.ret, ADMIN_MAIL)
                else:
                    return _('''Do not submit request duplicated(status%s ret%s), your \
request been processed successfully.''') % (result.status, result.ret)

            return _('''ERROR, duplicated URL.''')

        # This is a new URL request, we should insert it into task list.
        #sql = 'INSERT INTO wordp_task (uid, status, param1, param2, param3, \
        #add_time) VALUES (%s, %s, \'%s\', \'%s\', \'%s\', %s)' % (user_id, 0,
        #content, '', '', int(time.time()))
        try:
            task = Task(uid=user_id, status=0, param1=content, param2='', 
                        param3='', add_time=int(time.time()), ret=0, retmsg='', client_name=client_name)
            session.add(task)
            session.commit()
        except Exception as e:
            session.rollback()
            logger.error('Can not insert URL into database %s' % content)
            logger.error(str(e))
            return _('Can not deal with your request, (Something about database \
error), please inform administrator %s. Thanks.') % ADMIN_MAIL
        else:
            # NOTE: send signal to clients
            ret = Home.send_task_request(client_name, task.id)
            if ret:
                return _('We have received your URL request, please wait to processing (taskid%s).') % task.id
            else:
                return _('Can not deal with your request, (Something about client manager \
error), please inform administrator %s. Thanks.') % ADMIN_MAIL
Ejemplo n.º 8
0
    def assign_content_to_client(user_id, user_groupid, client_name, content):
        # Save url to taks list in database
        # Frist, we should find url in database, which user insert in 7
        # days, if find same url, we reject the request.(There have some
        # `status` and `ret` check)
        #sql = 'SELECT * from wordp_task where param1=\'%s\' and add_time>=%s \
        #and uid=%s' \
        #% (content, int(time.time())-3600*24*7, user_id)
        t_before_7_day = int(time.time()) - 3600 * 24 * 7
        result = session.query(Task).filter(Task.uid == user_id).filter(
            Task.add_time >= t_before_7_day).filter(
                Task.param1 == content).filter(
                    Task.client_name == client_name).scalar()
        if result:

            def common_resend():
                # NOTE: send signal to clients
                ret = Home.send_task_request(client_name, result.id)
                if ret:
                    return _('Your request have some error(status%s ret%s), \
and we have send the process request') % (result.status, result.ret)
                else:
                    return _(
                        'Can not deal with your request, (Something about client manager \
error), please inform administrator %s. Thanks.') % ADMIN_MAIL

            if (result.status == 0):
                return common_resend()

            elif (result.status > 0 and result.status < WEIXIN_RETRY_TIME):
                # Resend the request
                if result.ret != 0:
                    return common_resend()
                else:
                    # process successfully
                    return _(
                        '''Do not submit request duplicated(status%s ret%s), your \
request been processed successfully.''') % (result.status, result.ret)
            elif (result.status >= WEIXIN_RETRY_TIME):
                # The task has been processed out of retry time, and we
                # will not process it again
                if result.status == -99:
                    return _(
                        '''Your request is being processed, please wait.''')
                if result.ret != 0:
                    return _('''Your request has been processed, but not \
success, is %s, please contact with administrator %s''') % (result.ret,
                                                            ADMIN_MAIL)
                else:
                    return _(
                        '''Do not submit request duplicated(status%s ret%s), your \
request been processed successfully.''') % (result.status, result.ret)

            return _('''ERROR, duplicated URL.''')

        # This is a new URL request, we should insert it into task list.
        #sql = 'INSERT INTO wordp_task (uid, status, param1, param2, param3, \
        #add_time) VALUES (%s, %s, \'%s\', \'%s\', \'%s\', %s)' % (user_id, 0,
        #content, '', '', int(time.time()))
        try:
            task = Task(uid=user_id,
                        status=0,
                        param1=content,
                        param2='',
                        param3='',
                        add_time=int(time.time()),
                        ret=0,
                        retmsg='',
                        client_name=client_name)
            session.add(task)
            session.commit()
        except Exception as e:
            session.rollback()
            logger.error('Can not insert URL into database %s' % content)
            logger.error(str(e))
            return _(
                'Can not deal with your request, (Something about database \
error), please inform administrator %s. Thanks.') % ADMIN_MAIL
        else:
            # NOTE: send signal to clients
            ret = Home.send_task_request(client_name, task.id)
            if ret:
                return _(
                    'We have received your URL request, please wait to processing (taskid%s).'
                ) % task.id
            else:
                return _(
                    'Can not deal with your request, (Something about client manager \
error), please inform administrator %s. Thanks.') % ADMIN_MAIL