Example #1
0
def log_in_user(user):
    """
    Log in the given user.
    """

    session_id = uniqid()
    redis.setex(session_id, 2 * 7 * 24 * 60 * 60, user["id"])
    return session_id
Example #2
0
def log_in_user(user):
    """
    Log in the given user.
    """

    session_id = uniqid()
    redis.setex(
        session_id,
        2 * 7 * 24 * 60 * 60,
        user['id'],
    )
    return session_id
Example #3
0
def set_learning_context(user, **d):
    """
    Update the learning context of the user.

    Keys: `card`, `unit`, `subject`
        `next`: `method` and `path`
    """

    context = get_learning_context(user)
    d = pick(d, ('card', 'unit', 'subject', 'next'))
    context.update(d)
    context = compact_dict(context)
    key = 'learning_context_{id}'.format(id=user['id'])
    redis.setex(key, 10 * 60, json.dumps(context, default=json_serial))
    return context
Example #4
0
def set_learning_context(user, **d):
    """
    Update the learning context of the user.

    Keys: `card`, `unit`, `set`
        `next`: `method` and `path`
    """

    context = get_learning_context(user)
    d = pick(d, ('card', 'unit', 'set', 'next'))
    context.update(d)
    context = compact_dict(context)
    key = 'learning_context_{id}'.format(id=user['id'])
    redis.setex(key, 10 * 60, json.dumps(context, default=json_serial))
    return context
Example #5
0
    def set_learning_context(self, **d):
        """
        Update the learning context of the user.

        Keys: `card`, `unit`, `set`
            `next`: `method` and `path`
        """

        context = self.get_learning_context()
        d = pick(d, ("card", "unit", "set", "next"))
        context.update(d)
        context = compact_dict(context)
        key = "learning_context_{id}".format(id=self["id"])
        redis.setex(key, 10 * 60, json.dumps(context, default=json_serial))
        return context
Example #6
0
def get_email_token(user, send_email=True):
    """
    Create an email token for the user to reset their password.
    """

    token = uniqid()
    redis.setex(
        'user_password_token_{id}'.format(id=user['id']),  # key
        60 * 10,  # time
        bcrypt.encrypt(user['id'] + token)  # value
    )
    if send_email:
        send_mail(subject='Sagefy - Reset Password',
                  recipient=user['email'],
                  body=c('change_password_url').replace(
                      '{url}', '%spassword?id=%s&token=%s' %
                      ('https://sagefy.org/', user['id'], token)))
    return token
Example #7
0
    def get_email_token(self, send_email=True):
        """
        Create an email token for the user to reset their password.
        """

        token = uniqid()
        redis.setex(
            "user_password_token_{id}".format(id=self["id"]),  # key
            60 * 10,  # time
            bcrypt.encrypt(self["id"] + token),  # value
        )
        if send_email:
            send_mail(
                subject="Sagefy - Reset Password",
                recipient=self["email"],
                body=c("change_password_url").replace(
                    "{url}", "%spassword?id=%s&token=%s" % ("https://sagefy.org/", self["id"], token)
                ),
            )
        return token
Example #8
0
def get_email_token(user, send_email=True):
    """
    Create an email token for the user to reset their password.
    """

    token = uniqid()
    redis.setex(
        'user_password_token_{id}'.format(id=user['id']),  # key
        60 * 10,  # time
        bcrypt.encrypt(user['id'] + token)  # value
    )
    if send_email:
        send_mail(
            subject='Sagefy - Reset Password',
            recipient=user['email'],
            body=c('change_password_url').replace(
                '{url}',
                '%spassword?id=%s&token=%s' %
                ('https://sagefy.org/', user['id'], token)
            )
        )
    return token
Example #9
0
def memoize_redis(key, fn, time=24 * 60 * 60, *args, **kwargs):
    """
    Memoize the results of a function into Redis.
    """

    data = redis.get(key)
    if isinstance(data, bytes):
        try:
            data = json.loads(data.decode())
        except:
            pass

    if data:
        return data

    data = fn(*args, **kwargs)

    redis.setex(key, time, json.dumps(
        data,
        default=json_serial,
        ensure_ascii=False))

    return data