Пример #1
0
    def post(self):
        username = self.get_argument('username', None)
        password = self.get_argument('password', None)
        email = self.get_argument('email', None)
        avatar = self.default_avatar_url

        if username is None or password is None or email is None:
            raise exceptions.EmptyFields()
        else:
            user = yield gen.maybe_future(User.get_by_name(username))
            if user is not None:
                raise exceptions.UsernameAlreadyExists()
            user = yield gen.maybe_future(User.get_by_email(email))
            if user is not None:
                raise exceptions.EmailAlreadyExists()
            password = encrypt_password(password)
            user = yield gen.maybe_future(
                User.create(username=username, password=password,
                            email=email, avatar=avatar))

            # Update permission after xxx seconds.
            seconds = Level['time'][Roles.Comment]
            wait = datetime.now(get_localzone()) + timedelta(seconds=seconds)
            update_permission.apply_async((user, Roles.Comment), eta=wait)

            # Register success, then login.
            token = set_cookie_session(self, username, 1)
            raise gen.Return({'username': username, 'token': token})
Пример #2
0
def initdb():
    """Initialize MySQL databse."""
    from app.libs.db import init_db
    from app.models import Permission, User, Topic
    from app.base.roles import Roles
    from app.settings import Admins, Topics
    from app.libs.utils import encrypt_password

    click.echo('[2L] {0}..'.format(initdb.__doc__))
    init_db()

    click.echo('\n\n[2L] init permisions...')
    for attr, role in Roles.__dict__.items():
        if (not attr.startswith('__') and '{0}' not in role and
                role != 'root'):
            click.echo(' -> {0}'.format(role))
            Permission.create(role)

    click.echo('\n\n[2L] init master chief...')
    bit_sum = Permission.root_permission()
    for admin in Admins:
        click.echo(' -> {0}'.format(admin))
        if admin['role'] == 'root':
            admin['role'] = bit_sum
        else:
            admin['role'] = (Permission.get_by_role(admin['role']).bit |
                             Permission.get_by_role('comment').bit |
                             Permission.get_by_role('vote').bit)
        admin['password'] = encrypt_password(admin['password'])
        User.create(**admin)

    click.echo('\n\n[2L] create default topics...')
    for topic in Topics:
        click.echo(' -> {0}'.format(topic))
        Topic.create(**topic)
Пример #3
0
    def post(self):
        username = self.get_argument('username', None)
        password = self.get_argument('password', None)
        expire = int(self.get_argument('expire', 0))

        if username is None or password is None:
            raise exceptions.EmptyFields()
        else:
            user = yield gen.maybe_future(User.get_by_name(username))
            if user is None:
                raise exceptions.UsernameDoesNotExists()
            if encrypt_password(password) != user.password:
                raise exceptions.PasswordWrong()

            token = set_cookie_session(self, username, expire)
            raise gen.Return({'username': username, 'token': token})