def decorated_view(*args, **kwargs):

            if current_app.login_manager._login_disabled:
                return func(*args, **kwargs)

            if not current_user.is_authenticated():
                return current_app.login_manager.unauthorized()

            usuario_role = current_user.get_role()
            if (len(roles) > 0) and (usuario_role not in roles):
                logout_user()
                return current_app.login_manager.unauthorized()
            return func(*args, **kwargs)
Esempio n. 2
0
        def decorated_view(*args, **kwargs):

            if current_app.login_manager._login_disabled:
                return func(*args, **kwargs)

            if not current_user.is_authenticated():
                return current_app.login_manager.unauthorized()

            usuario_role = current_user.get_role()
            if (len(roles) > 0) and (usuario_role not in roles):
                logout_user()
                return current_app.login_manager.unauthorized()
            return func(*args, **kwargs)
Esempio n. 3
0
 def wrapper(*args, **kwargs):
     parser = reqparse.RequestParser()
     parser.add_argument('Meme-Token', location='headers',
                         required=True, dest='token',
                         type=authenticate_netid)
     args = parser.parse_args()
     return func(*args, **kwargs)
Esempio n. 4
0
def now_func():
    """Return current datetime
   
    """
    func = get_now_func()
    dt = func()
    if isinstance(dt, datetime.datetime):
        if dt.tzinfo is None:
            return dt.replace(tzinfo=pytz.utc)
    return dt
Esempio n. 5
0
def now_func():
    """Return current datetime
   
    """
    func = get_now_func()
    dt = func()
    if isinstance(dt, datetime.datetime):
        if dt.tzinfo is None:
            return dt.replace(tzinfo=pytz.utc)
    return dt
Esempio n. 6
0
def todict(o,
           fully_qualified=False,
           fmap=None,
           excludes=None,
           dict_class=dict):
    """Transmogrifies data of record object into dict.

    Inspired by
    http://blog.mitechie.com/2010/04/01/hacking-the-sqlalchemy-base-class/
    Converts only physical table columns. Columns created by e.g.
    relationship() must be handled otherwise.

    :param o: Data to transmogrify
    :param fully_qualified: Whether dict keys should be fully qualified (schema
        + '.' + table + '.' + column) or not (just column name). *CAVEAT* Full
        qualification is only possible if ``o`` has attribute ``__table__``.
        E.g. a KeyedTuple does not.
    :param fmap: Mapping of field names to functions. Each function is called to
        build the value for this field.
    :param excludes: Optional list of column names to exclude
    :rtype: dict
    """
    def convert_datetime(v):
        try:
            return v.strftime("%Y-%m-%d %H:%M:%S")
        except AttributeError:
            # 'NoneType' object has no attribute 'strftime'
            return None

    d = dict_class()
    if excludes is None:
        excludes = []
    if isinstance(o, sa.util.KeyedTuple):
        d = o._asdict()
    else:
        for c in o.__table__.columns:
            if c.name in excludes:
                continue
            if isinstance(c.type, DateTime):
                value = convert_datetime(getattr(o, c.name))
            elif isinstance(c, InstrumentedList):
                value = list(c)
            else:
                value = getattr(o, c.name)
            if fully_qualified:
                k = o.__table__.schema + '.' + o.__table__.name + '.' + c.name
            else:
                k = c.name
            d[k] = value

    if fmap:
        for k, func in fmap.items():
            d[k] = func(o)
    return d
Esempio n. 7
0
def todict(o, fully_qualified=False, fmap=None, excludes=None):
    """Transmogrifies data of record object into dict.

    Inspired by
    http://blog.mitechie.com/2010/04/01/hacking-the-sqlalchemy-base-class/
    Converts only physical table columns. Columns created by e.g.
    relationship() must be handled otherwise.

    :param o: Data to transmogrify
    :param fully_qualified: Whether dict keys should be fully qualified (schema
        + '.' + table + '.' + column) or not (just column name). *CAVEAT* Full
        qualification is only possible if ``o`` has attribute ``__table__``.
        E.g. a KeyedTuple does not.
    :param fmap: Mapping of field names to functions. Each function is called to
        build the value for this field.
    :param excludes: Optional list of column names to exclude
    :rtype: Dict
    """

    def convert_datetime(v):
        try:
            return v.strftime("%Y-%m-%d %H:%M:%S")
        except AttributeError:
            # 'NoneType' object has no attribute 'strftime'
            return None

    d = {}
    if excludes is None:
        excludes = []
    if isinstance(o, sa.util.KeyedTuple):
        d = o._asdict()
    else:
        for c in o.__table__.columns:
            if c.name in excludes:
                continue
            if isinstance(c.type, DateTime):
                value = convert_datetime(getattr(o, c.name))
            elif isinstance(c, InstrumentedList):
                value = list(c)
            else:
                value = getattr(o, c.name)
            if fully_qualified:
                k = o.__table__.schema + "." + o.__table__.name + "." + c.name
            else:
                k = c.name
            d[k] = value

    if fmap:
        for k, func in fmap.items():
            d[k] = func(o)
    return d
Esempio n. 8
0
    def dispatch(self, request, *args, **kwargs):
        self.storage_setup()

        try:
            if request.method in self.methods: 
                func = getattr(self, request.method.lower(), None)
                if func:
                    return func(request, *args, **kwargs)
                else:
                    raise Exception('Unhandled method: %s' % request.method)
            else:
                raise Exception('Unknown method: %s' % request.method)
        except:
            raise
        finally:
            self.storage_teardown()
Esempio n. 9
0
    def dispatch(self, request, *args, **kwargs):
        self.storage_setup()

        try:
            if request.method in self.methods:
                func = getattr(self, request.method.lower(), None)
                if func:
                    return func(request, *args, **kwargs)
                else:
                    raise Exception('Unhandled method: %s' % request.method)
            else:
                raise Exception('Unknown method: %s' % request.method)
        except:
            raise
        finally:
            self.storage_teardown()
Esempio n. 10
0
def ciStringFindMap(string, term, func):
	"""runs func on case insensitive matches of term in the string and returns the string"""
	
	peices = []
	
	f = 0
	l = len(term)
	
	while l < len(string):
		if string[f:l].lower() == term.lower():
			peices.append(string[:f])
			peices.append(func(string[f:l]))
			string = string[l:]
			
			f = 0
			l = len(term)
		else:
			f += 1
			l += 1
	
	return "".join(peices) + string
Esempio n. 11
0
def journal_read(session, func):
    """Read, process and delete (if successful) the oldest journal row.

    The row is locked on read, and remains locked until the processing
    succeeds or gives up.  This is to ensure that (in a multithreaded
    or multiprocess environment) only one worker processes the update,
    which in turn ensures monotonicity.
    """

    # TODO(ijw): start a transaction here

    # Find and lock the oldest journal entry

    # NB this will serialise on locking the oldest record if there are
    # multiple threads running (as there may be if multiple processes
    # are running)

    maybe_more = True

    # TODO(ijw): not true, check with Arvind
    # Set subtransactions to True otherwise this generates a failure
    # in the L3 plugin API. This is invoked from within the greater
    # create/delete router API call and we ideally want both to pass or
    # fail together.

    # Find the lowest ID'd row in the table. We assume that the ID is
    # a time ordering. We are only scanning for it now, not locking yet.
    entry = session.query(models.VppEtcdJournal) \
                   .order_by(models.VppEtcdJournal.id) \
                   .first()

    if not entry:
        return False  # Signal that we nothing more to do
    else:
        with session.begin(subtransactions=True):
            # The plan: get the next record into etcd and delete it from the
            # journal table.  The transaction ensures that no-one else does the
            # same thing (master elections are not perfect) while we're on the
            # job, which can ultimately lead to out-of-order updates.
            try:
                first_id = entry.id
                # Reselect with a lock, but without doing the range check
                # so that the lock is row-specific
                rs = session.query(models.VppEtcdJournal)\
                    .filter(models.VppEtcdJournal.id == first_id)\
                    .with_for_update().all()

                if len(rs) > 0:
                    # The entry is still around, and we are still master
                    entry = rs[0]
                    try:
                        func(entry.k, entry.v)  # do work, could fail
                        session.delete(entry)  # This is now processed
                        LOG.debug('forwarded etcd record %d', first_id)
                    except Exception as e:
                        # For some reason, we can't do the job.
                        # This is uncommon.
                        LOG.warning(
                            "Couldn't forward etcd record %d due to"
                            " exception %s. retrying later", first_id,
                            type(e).__name__)

                        try:
                            # The retry count and time are niceties for admin
                            # debugging. The update can itself fail
                            # (DB connection problems, etc.), but since it's a
                            # nicety it's not critical.
                            entry.retry_count += 1
                            entry.last_retried = sa.func.now()
                            entry.update(entry)
                        except Exception as e:
                            LOG.warning("Couldn't update retry info due to %s",
                                        type(e).__name__)
                            pass
                else:
                    # We cannot find that entry any more, means some other
                    # master kicking around. Should be rare
                    # TODO(ijw): We should re-elect ourselves if we can
                    maybe_more = False
                    LOG.debug(
                        "etcd record %d processed by "
                        "another forwarder", first_id)
            except Exception as e:
                LOG.exception(
                    "forward worker journal read processing hit "
                    "error. Error is: %s", e)

        return maybe_more
Esempio n. 12
0
 def wrap(*args, **kwargs):
     if session.get('inputed_card_number'):
         return func(*args, **kwargs)
     else:
         return redirect(url_for('custom_login_user'))
Esempio n. 13
0
def now_func():
    """Return current datetime
    
    """
    func = get_now_func()
    return func()
Esempio n. 14
0
def journal_read(session, func):
    """Read, process and delete (if successful) the oldest journal row.

    The row is locked on read, and remains locked until the processing
    succeeds or gives up.  This is to ensure that (in a multithreaded
    or multiprocess environment) only one worker processes the update,
    which in turn ensures monotonicity.
    """

    # TODO(ijw): start a transaction here

    # Find and lock the oldest journal entry

    # NB this will serialise on locking the oldest record if there are
    # multiple threads running (as there may be if multiple processes
    # are running)

    maybe_more = True

    # TODO(ijw): not true, check with Arvind
    # Set subtransactions to True otherwise this generates a failure
    # in the L3 plugin API. This is invoked from within the greater
    # create/delete router API call and we ideally want both to pass or
    # fail together.

    # Find the lowest ID'd row in the table. We assume that the ID is
    # a time ordering. We are only scanning for it now, not locking yet.
    entry = session.query(models.VppEtcdJournal) \
                   .order_by(models.VppEtcdJournal.id) \
                   .first()

    if not entry:
        return False  # Signal that we nothing more to do
    else:
        with session.begin(subtransactions=True):
            # The plan: get the next record into etcd and delete it from the
            # journal table.  The transaction ensures that no-one else does the
            # same thing (master elections are not perfect) while we're on the
            # job, which can ultimately lead to out-of-order updates.
            try:
                first_id = entry.id
                # Reselect with a lock, but without doing the range check
                # so that the lock is row-specific
                rs = session.query(models.VppEtcdJournal)\
                    .filter(models.VppEtcdJournal.id == first_id)\
                    .with_for_update().all()

                if len(rs) > 0:
                    # The entry is still around, and we are still master
                    entry = rs[0]
                    try:
                        func(entry.k, entry.v)  # do work, could fail
                        session.delete(entry)  # This is now processed
                        LOG.debug('forwarded etcd record %d', first_id)
                    except Exception as e:
                        # For some reason, we can't do the job.
                        # This is uncommon.
                        LOG.warning("Couldn't forward etcd record %d due to"
                                    " exception %s. retrying later",
                                    first_id, type(e).__name__)

                        try:
                            # The retry count and time are niceties for admin
                            # debugging. The update can itself fail
                            # (DB connection problems, etc.), but since it's a
                            # nicety it's not critical.
                            entry.retry_count += 1
                            entry.last_retried = sa.func.now()
                            entry.update(entry)
                        except Exception as e:
                            LOG.warning("Couldn't update retry info due to %s",
                                        type(e).__name__)
                            pass
                else:
                    # We cannot find that entry any more, means some other
                    # master kicking around. Should be rare
                    # TODO(ijw): We should re-elect ourselves if we can
                    maybe_more = False
                    LOG.debug("etcd record %d processed by "
                              "another forwarder", first_id)
            except Exception as e:
                LOG.exception("forward worker journal read processing hit "
                              "error. Error is: %s", e)

        return maybe_more
def install_repository_dependencies(
    status, session, cwd, repository, notebooks_iter, mode, env,
    notebook_exec_mode, dry_run, out, err
):
    vprint(2, "{}Installing repository dependencies".format(
        "[DRY RUN] " if dry_run >= 2 else ""
    ))
    if dry_run >= 2:
        return None

    install_options = [
        ("setup.py", install_setups, repository.setup_names),
        ("requirements.txt", install_requirements, repository.requirement_names),
        ("Pipfile", install_pipfiles, repository.pipfile_names),
        ("Pipfile.lock", install_pipfiles, repository.pipfile_lock_names),
    ]
    installed = True
    data_ok_list = []
    data_failed_list = []
    data_failed = b""
    for spec, func, names in install_options:
        success, data = func(cwd, names, "work", out, err)
        installed = installed and success
        spec_bytes = spec.encode("utf-8")
        if success:
            data_ok_list.append(spec_bytes)
        else:
            data_failed += b"\n##<<>>##" + spec_bytes + b":\n" + data
            data_failed_list.append(spec_bytes)
    if not installed:
        reason = "<Install Dependency Error>"
        cause = b"Ok: " + b", ".join(data_ok_list)
        cause += b"\n##<<>>##Failed: " + b", ".join(data_failed_list)
        cause += data_failed
        for notebook, repository in notebooks_iter:
            status.skipped += 1
            status.report()
            nmode = notebook_exec_mode(mode, notebook, repository)
            mode_num = exec_to_num(*nmode)
            execution = session.query(Execution).filter(
                Execution.notebook_id == notebook.id,
                Execution.mode == mode_num,
            ).first()
            if execution:
                if execution.processed & consts.E_EXECUTED:
                    continue
                execution.reason = reason
                execution.msg = cause
                execution.cell = None
                execution.count = None
                execution.diff = None
                execution.duration = None
                execution.timeout = None
                execution.diff_count = None
                execution.processed = consts.E_CREATED
            else:
                execution = Execution(
                    notebook_id=notebook.id, mode=mode_num,
                    reason=reason, msg=cause,
                    processed=consts.E_CREATED,
                    repository_id=notebook.repository_id,
                )
            session.add(execution)
            notebook.processed |= nmode.processed
            session.add(notebook)
        session.commit()
        return "Failed to install {}".format(
            b", ".join(data_failed_list).decode("utf-8")
        )
    return None
Esempio n. 16
0
 def decorated_view(*args, **kwargs):
     if current_user and current_user.is_authenticated:
         return redirect(url_for('review'))
     return func(*args, **kwargs)
Esempio n. 17
0
 def worker(defer, func, a, kw):
     try:
         res = func(*a, **kw)
         reactor.callFromThread(defer.callback, res)
     except Exception as e:
         reactor.callFromThread(defer.errback, e)
Esempio n. 18
0
 def worker(defer, func, a, kw):
     try:
         res = func(*a, **kw)
         reactor.callFromThread(defer.callback, res)
     except Exception as e:
         reactor.callFromThread(defer.errback, e)
Esempio n. 19
0
def now_func():
    """Return current datetime

    """
    func = _now_func[0]
    return func()
Esempio n. 20
0
 def inner(*args, **kwargs):
     if not session.get('user'):
         return redirect('/login')
     return func(*args, **kwargs)
Esempio n. 21
0
 def decorated_view(*args, **kwargs):
     if config.config_anonbrowse == 1:
         return func(*args, **kwargs)
     return login_required(func)(*args, **kwargs)
Esempio n. 22
0
def now_func():
    """Return current datetime
    
    """
    func = get_now_func()
    return func()
Esempio n. 23
0
 def decorated(*args, **kwargs):
     if not approve_meme_admin(flask.g.netid):
         return reject_failed_admin()
     logger.info("Authenticated admin request from %s" % flask.g.netid)
     return func(*args, **kwargs)
Esempio n. 24
0
 def wrapper(*args, **kwargs):
     if 'username' not in login_session:
         flash("You need to be logged in to perform this action")
         return redirect('/login')
     return func(*args, **kwargs)
Esempio n. 25
0
 def decorated_view(*args, **kwargs):
     if not current_user.member:
         return redirect('/')
     return func(*args, **kwargs)