Exemplo n.º 1
0
def set_user_context(uid=None, email=None):
    if email:
        user = User.get(User.email == email.lower())
    else:
        user = User.get(User.id == uid)
    sid = sessionslib.create(user.id, user.groups)
    context.set_context(uid=user.id, sid=sid, groups=user.groups)
Exemplo n.º 2
0
def authenticate(email, password='', _oauthed=False):
    """
    returns session if successful else returns None
    """
    if not validate_email(email):
        raise InvalidEmailError(email)
    email = email.lower()
    user = User.get(User.email == email)
    if not user:
        raise EmailiDoesNotExistError(email)
    if _oauthed:
        return sessionslib.create(user.id,
                                  user.groups,
                                  extras={
                                      'email': email,
                                      'name': user.name
                                  })
    if user.password == encrypt(password, settings.SALT):
        return sessionslib.create(user.id,
                                  user.groups,
                                  extras={
                                      'email': email,
                                      'name': user.name
                                  })
    raise AuthError(email)
Exemplo n.º 3
0
def info(email=None, uid=None):
    if email:
        cond = (User.email == email.lower())
    else:
        cond = (User.id == uid)
    user = User.select(User.id, User.active, User.created, User.groups).where(cond)[0]
    return playhouse.shortcuts.model_to_dict(user)
Exemplo n.º 4
0
def create(email, password=None, groups=None, name=None, connection=None):
    email = email.lower()

    if password:
        validate_password(password)
        if not validate_email(email):
            raise InvalidEmailError(email)
        encpassword = encrypt(password, settings.SALT)
    else:
        encpassword = None

    if uid_by_email(email):
        raise EmailExistsError(email)

    created = datetime.datetime.now()
    user = User.create(name=name,
                       email=email,
                       password=encpassword,
                       created=created,
                       groups=groups or [])
    user.save()

    if groups:
        for gname in groups:
            GroupUser.create(user_id=user.id, group=gname)
    #user_created.send(uid, email)
    if settings.SEND_WELCOME_EMAIL:
        welcome(email)
    return user.id
Exemplo n.º 5
0
def uid_by_email(email):
    """
    -> user or None
    """
    user = User.select(User.id).where(User.email == email.lower()).first()
    if user:
        return user.id
Exemplo n.º 6
0
def info(email=None, uid=None):
    if email:
        cond = (User.email == email.lower())
    else:
        cond = (User.id == uid)
    user = User.select(User.name, User.id, User.active, User.created,
                       User.groups).where(cond)[0]
    return playhouse.shortcuts.model_to_dict(user)
Exemplo n.º 7
0
def uid_by_email(email):
    """
    -> user or None
    """
    #User.get().where(User.email == email.lower())
    try:
        user_id = User.get(User.email == email.lower())
    except User.DoesNotExist:
        user_id = None
    return user_id
Exemplo n.º 8
0
def authenticate(email, password):
    """
    returns session if successful else returns None
    """
    if not validate_email(email):
        raise InvalidEmailError(email)
    user = User.get(User.email == email.lower())
    if not user:
        raise EmailiDoesNotExistError(email)
    if user.password == encrypt(password, settings.SALT):
        return sessionslib.create(user.id, user.groups)
    raise AuthError(email)
Exemplo n.º 9
0
def create(email, password, groups=None, connection=None):
    email = email.lower()

    validate_password(password)
    if not validate_email(email):
        raise InvalidEmailError(email)

    if uid_by_email(email):
        raise EmailExistsError(email)

    encpassword = encrypt(password, settings.SALT)
    created = datetime.datetime.now()
    user = User.create(email=email, password=encpassword, created=created, groups=groups)
    user.save()

    for name in groups:
        GroupUser.create(user_id=user.id, group=name)
    #user_created.send(uid, email)
    if settings.SEND_WELCOME_EMAIL:
        welcome(email)
    return user.id
Exemplo n.º 10
0
def count():
    return User.select(peewee.fn.COUNT(User.id))
Exemplo n.º 11
0
def created_today():
    today = datetime.date.today()
    return User.select([peewee.fn.count(User.id)
                        ]).where(User.created > today)[0]
Exemplo n.º 12
0
def groupby_created(precision='month'):
    month = peewee.func.DATE_TRUNC('month', users.c.created).label('month')
    users = User.select([month, peewee.func.COUNT(User.id)
                         ]).group_by('month').order_by('month')
    return [(dt.strftime('%b %Y'), num) for (dt, num) in users]
Exemplo n.º 13
0
def list_():
    fields = [User.id, User.email, User.active, User.created, User.groups]
    return list(User.select(*fields))
Exemplo n.º 14
0
def disable(uid):
    q = User.update(active=False).where(User.id == uid)
    q.execute()
Exemplo n.º 15
0
def enable(uid):
    q = User.update(active=True).where(User.id == uid)
    q.execute()
Exemplo n.º 16
0
def list_():
    fields = [User.id, User.email, User.active, User.created, User.groups]
    return list(User.select(*fields))
Exemplo n.º 17
0
def disable(uid):
    q = User.update(active=False).where(User.id == uid)
    q.execute()
Exemplo n.º 18
0
def enable(uid):
    q = User.update(active=True).where(User.id == uid)
    q.execute()