Esempio n. 1
0
File: web.py Progetto: imgtl/imgtl
def upload():
    if current_user.is_authenticated():
        user = current_user
    else:
        user = None
    expire = None
    try:
        exp = int(request.form['expire'])
        if exp != -1:
            if exp != 0:
                expire = int(exp)
            else:
                expire = int(request.form['expire-custom']) * int(request.form['expire-custom-unit'])
            if expire > 525600:
                flash(i18n('invalidexpiretime-toolong'), 'error')
                return redirect(url_for('index'))
            expire = imgtl.lib.calc_expire_time(expire)
    except ValueError:
        flash(i18n('invalidexpiretime'), 'error')
        return redirect(url_for('index'))
    upload = do_upload_image(user, request.files['image'], request.form.get('desc'),
                             request.form.get('nsfw') == 'on', request.form.get('anonymous') == 'on', request.form.get('private') == 'on',
                             request.form.get('keep-exif') == 'on', expire, request.form.get('expire-behavior'))
    if isinstance(upload, str):
        flash(i18n(upload), 'error')
        return redirect(url_for('index'))
    else:
        if current_user.is_anonymous():
            if 'anon_uploads' not in session:
                session['anon_uploads'] = []
            session['anon_uploads'].append(upload.id)
        flash(i18n('uploadsuccess'), 'success')
        return redirect(url_for('show', url=upload.url))
Esempio n. 2
0
def oauth_disconnect():
    if current_user.password:
        current_user.oauth_uid = None
        db.session.commit()
        flash(i18n('oauth-disconnected'), 'success')
    else:
        flash(i18n('nopassword'), 'error')
    return redirect(url_for('settings'))
Esempio n. 3
0
File: web.py Progetto: imgtl/imgtl
def oauth_disconnect():
    if current_user.password:
        current_user.oauth_uid = None
        db.session.commit()
        flash(i18n('oauth-disconnected'), 'success')
    else:
        flash(i18n('nopassword'), 'error')
    return redirect(url_for('settings'))
Esempio n. 4
0
File: web.py Progetto: imgtl/imgtl
def login():
    if request.method == 'GET':
        return render_imgtl_template('login.html')
    elif request.method == 'POST':
        emailusername = request.form['emailusername']
        user = User.query.filter((User.email == emailusername) | (User.name == emailusername)).first()
        if user and user.password and imgtl.lib.pw_verify(user.password, request.form['password']):
            login_user(user, remember=True)
            return redirect(request.args.get('next') or url_for('index'))
        else:
            flash(i18n('loginfailed' if not user or user.password else 'loginfailed-oauthuser'), 'error')
            return redirect(url_for('login'))
Esempio n. 5
0
def upload():
    if current_user.is_authenticated():
        user = current_user
    else:
        user = None
    expire = None
    try:
        exp = int(request.form['expire'])
        if exp != -1:
            if exp != 0:
                expire = int(exp)
            else:
                expire = int(request.form['expire-custom']) * int(
                    request.form['expire-custom-unit'])
            if expire > 525600:
                flash(i18n('invalidexpiretime-toolong'), 'error')
                return redirect(url_for('index'))
            expire = imgtl.lib.calc_expire_time(expire)
    except ValueError:
        flash(i18n('invalidexpiretime'), 'error')
        return redirect(url_for('index'))
    upload = do_upload_image(user, request.files['image'],
                             request.form.get('desc'),
                             request.form.get('nsfw') == 'on',
                             request.form.get('anonymous') == 'on',
                             request.form.get('private') == 'on',
                             request.form.get('keep-exif') == 'on', expire,
                             request.form.get('expire-behavior'))
    if isinstance(upload, str):
        flash(i18n(upload), 'error')
        return redirect(url_for('index'))
    else:
        if current_user.is_anonymous():
            if 'anon_uploads' not in session:
                session['anon_uploads'] = []
            session['anon_uploads'].append(upload.id)
        flash(i18n('uploadsuccess'), 'success')
        return redirect(url_for('show', url=upload.url))
Esempio n. 6
0
def login():
    if request.method == 'GET':
        return render_imgtl_template('login.html')
    elif request.method == 'POST':
        emailusername = request.form['emailusername']
        user = User.query.filter((User.email == emailusername)
                                 | (User.name == emailusername)).first()
        if user and user.password and imgtl.lib.pw_verify(
                user.password, request.form['password']):
            login_user(user, remember=True)
            return redirect(request.args.get('next') or url_for('index'))
        else:
            flash(
                i18n('loginfailed' if not user or user.password else
                     'loginfailed-oauthuser'), 'error')
            return redirect(url_for('login'))
Esempio n. 7
0
File: web.py Progetto: imgtl/imgtl
def show(url):
    if request.method == 'DELETE':
        res = do_delete_image(current_user, url)
        if res == 'success':
            flash(i18n('deletesuccess'), 'success')
        return jsonify({'res': res})
    elif request.method == 'PUT':
        res = do_update_image(current_user, url, request.form.get('nsfw') == 'true', request.form.get('anonymous') == 'true', request.form.get('private') == 'true')
        return jsonify({'res': res})
    elif request.method == 'GET':
        upload = get_upload(current_user, url)
        if isinstance(upload, int):
            abort(upload)
        obj = upload.object
        if isinstance(obj, Image):
            return render_imgtl_template('show/image.html', upload=upload)
Esempio n. 8
0
def show(url):
    if request.method == 'DELETE':
        res = do_delete_image(current_user, url)
        if res == 'success':
            flash(i18n('deletesuccess'), 'success')
        return jsonify({'res': res})
    elif request.method == 'PUT':
        res = do_update_image(current_user, url,
                              request.form.get('nsfw') == 'true',
                              request.form.get('anonymous') == 'true',
                              request.form.get('private') == 'true')
        return jsonify({'res': res})
    elif request.method == 'GET':
        upload = get_upload(current_user, url)
        if isinstance(upload, int):
            abort(upload)
        obj = upload.object
        if isinstance(obj, Image):
            return render_imgtl_template('show/image.html', upload=upload)
Esempio n. 9
0
def signup():
    if request.method == 'GET':
        return redirect(url_for('login'))
    elif request.method == 'POST':
        if not imgtl.validator.email(request.form['email']):
            flash(i18n('invalidemail'), 'error')
            return redirect(url_for('signup'))
        if not imgtl.validator.username(request.form['username']):
            flash(i18n('invalidusername'), 'error')
            return redirect(url_for('signup'))
        if not imgtl.validator.password(request.form['password']):
            flash(i18n('invalidpassword'), 'error')
            return redirect(url_for('signup'))
        if request.form['password'] != request.form['passwordconfirm']:
            flash(i18n('passwordmismatch'), 'error')
            return redirect(url_for('signup'))
        user = User.query.filter((User.email == request.form['email']) | (
            User.name == request.form['username'])).first()
        if user:
            if user.email == request.form['email']:
                flash(i18n('alreadyexistemail'), 'error')
                return redirect(url_for('signup'))
            elif user.name == request.form['username']:
                flash(i18n('alreadyexistname'), 'error')
                return redirect(url_for('signup'))
        user = User(email=request.form['email'],
                    name=request.form['username'],
                    password=imgtl.lib.pw_hash(request.form['password']))
        while 1:
            try:
                user.token = imgtl.lib.make_token()
                db.session.commit()
            except IntegrityError:
                db.session.rollback()
                continue
            else:
                break
        db.session.add(user)
        db.session.commit()
        login_user(user, remember=True)
        do_log('web', 'signup', user.id)
        flash(i18n('signupsuccess'), 'success')
        return redirect(url_for('index'))
Esempio n. 10
0
File: web.py Progetto: imgtl/imgtl
def signup():
    if request.method == 'GET':
        return redirect(url_for('login'))
    elif request.method == 'POST':
        if not imgtl.validator.email(request.form['email']):
            flash(i18n('invalidemail'), 'error')
            return redirect(url_for('signup'))
        if not imgtl.validator.username(request.form['username']):
            flash(i18n('invalidusername'), 'error')
            return redirect(url_for('signup'))
        if not imgtl.validator.password(request.form['password']):
            flash(i18n('invalidpassword'), 'error')
            return redirect(url_for('signup'))
        if request.form['password'] != request.form['passwordconfirm']:
            flash(i18n('passwordmismatch'), 'error')
            return redirect(url_for('signup'))
        user = User.query.filter((User.email == request.form['email']) | (User.name == request.form['username'])).first()
        if user:
            if user.email == request.form['email']:
                flash(i18n('alreadyexistemail'), 'error')
                return redirect(url_for('signup'))
            elif user.name == request.form['username']:
                flash(i18n('alreadyexistname'), 'error')
                return redirect(url_for('signup'))
        user = User(email=request.form['email'], name=request.form['username'], password=imgtl.lib.pw_hash(request.form['password']))
        while 1:
            try:
                user.token = imgtl.lib.make_token()
                db.session.commit()
            except IntegrityError:
                db.session.rollback()
                continue
            else:
                break
        db.session.add(user)
        db.session.commit()
        login_user(user, remember=True)
        do_log('web', 'signup', user.id)
        flash(i18n('signupsuccess'), 'success')
        return redirect(url_for('index'))
Esempio n. 11
0
def oauth_authorized(resp):
    next_url = request.args.get('next') or url_for('index')
    if resp is None:
        return redirect(next_url)
    user = User.query.filter_by(oauth_uid=resp['user_id']).first()
    session['twitter_token'] = (
        resp['oauth_token'],
        resp['oauth_token_secret'],
    )
    if user:
        del session['twitter_token']
        login_user(user, remember=True)
        return redirect(next_url)
    else:
        if 'oauth-connect-pre' in session:
            current_user.oauth_uid = resp['user_id']
            db.session.commit()
            flash(i18n('oauth-connected'), 'success')
        else:
            session['oauth-signup'] = {
                'name': resp['screen_name'],
                'oauth_uid': resp['user_id'],
            }
        return redirect(url_for('settings'))
Esempio n. 12
0
File: web.py Progetto: imgtl/imgtl
def oauth_authorized(resp):
    next_url = request.args.get('next') or url_for('index')
    if resp is None:
        return redirect(next_url)
    user = User.query.filter_by(oauth_uid=resp['user_id']).first()
    session['twitter_token'] = (
        resp['oauth_token'],
        resp['oauth_token_secret'],
    )
    if user:
        del session['twitter_token']
        login_user(user, remember=True)
        return redirect(next_url)
    else:
        if 'oauth-connect-pre' in session:
            current_user.oauth_uid = resp['user_id']
            db.session.commit()
            flash(i18n('oauth-connected'), 'success')
        else:
            session['oauth-signup'] = {
                'name': resp['screen_name'],
                'oauth_uid': resp['user_id'],
            }
        return redirect(url_for('settings'))
Esempio n. 13
0
def settings():
    if not (current_user.is_authenticated() or
            (request.method == 'GET' and 'oauth-signup' in session) or
            (request.method == 'POST' and 'oauth-signup-post' in session)):
        return redirect(url_for('login', next=url_for('settings')))
    if request.method == 'GET':
        if 'oauth-signup' in session:
            user = User(email='',
                        name=session['oauth-signup']['name'],
                        oauth_uid=session['oauth-signup']['oauth_uid'])
            session['oauth-signup-post'] = session['oauth-signup']
            del session['oauth-signup']
            return render_template('settings.html', user=user)
        else:
            data = None
            if current_user.oauth_uid is not None:
                resp = twitter.get('users/show.json',
                                   data={'user_id': current_user.oauth_uid},
                                   token='')
                if resp.status == 200:
                    data = resp.data
            return render_imgtl_template('settings.html', twitter=data)
    elif request.method == 'POST':
        if request.form['what'] == 'token':
            while 1:
                try:
                    current_user.token = imgtl.lib.make_token()
                    db.session.commit()
                except IntegrityError:
                    db.session.rollback()
                    continue
                else:
                    break
            return jsonify({'token': current_user.token})
        elif request.form['what'] == 'update':
            if 'oauth-signup-post' in session:
                user = User(
                    email='',
                    name=session['oauth-signup-post']['name'],
                    oauth_uid=session['oauth-signup-post']['oauth_uid'])
            else:
                user = current_user
            if request.form.get('password', '') != '':
                if not imgtl.validator.password(request.form['password']):
                    flash(i18n('invalidpassowrd'), 'error')
                    return redirect(url_for('settings'))
                elif request.form['password'] != request.form[
                        'passwordconfirm']:
                    flash(i18n('passwordmismatch'), 'error')
                    return redirect(url_for('settings'))
                else:
                    user.password = imgtl.lib.pw_hash(request.form['password'])
            new_email = request.form['email']
            new_username = request.form['username']
            if not imgtl.validator.email(new_email):
                flash(i18n('invalidemail'), 'error')
                return redirect(url_for('settings'))
            if not imgtl.validator.username(new_username):
                flash(i18n('invalidusername'), 'error')
                return redirect(url_for('settings'))
            if user.email != new_email:
                if User.query.filter_by(email=new_email).first():
                    flash(i18n('alreadyexistemail'), 'error')
                    return redirect(url_for('settings'))
            if user.name != new_username:
                if User.query.filter_by(name=new_username).first():
                    flash(i18n('alreadyexistname'), 'error')
                    return redirect(url_for('settings'))
            user.email = new_email
            user.name = new_username
            db.session.add(user)
            db.session.commit()
            if 'oauth-signup-post' in session:
                while 1:
                    try:
                        user.token = imgtl.lib.make_token()
                        db.session.commit()
                    except IntegrityError:
                        db.session.rollback()
                        continue
                    else:
                        break
                login_user(user, remember=True)
                do_log('web', 'signup_by_oauth', user.id)
                del session['oauth-signup-post']
                flash(i18n('signupsuccess'), 'success')
                return redirect(url_for('index'))
            else:
                flash(i18n('accupdatesuccess'), 'success')
                return redirect(url_for('settings'))
Esempio n. 14
0
    app.config.from_pyfile('.imgtl.tests.cfg')
else:
    app.config.from_pyfile('imgtl.cfg')
app.jinja_env.trim_blocks = True
app.jinja_env.lstrip_blocks = True
app.jinja_env.filters['nl2br'] = jinja2_filter_nl2br
app.jinja_env.filters['dt'] = jinja2_filter_dt

db.init_app(app)
db.app = app
log_db.init_app(app)
log_db.app = app

login_manager = LoginManager()
login_manager.login_view = 'login'
login_manager.login_message = i18n('youmustlogin')
login_manager.init_app(app)

oauth = OAuth(app)
twitter = twitter_factory.register_to(oauth)


def render_imgtl_template(*args, **kwargs):
    kwargs['user'] = current_user
    return render_template(*args, **kwargs)


@login_manager.user_loader
def load_user(user_id):
    return User.query.get(user_id)
Esempio n. 15
0
File: web.py Progetto: imgtl/imgtl
def settings():
    if not (current_user.is_authenticated() or (request.method == 'GET' and 'oauth-signup' in session) or (request.method == 'POST' and 'oauth-signup-post' in session)):
        return redirect(url_for('login', next=url_for('settings')))
    if request.method == 'GET':
        if 'oauth-signup' in session:
            user = User(email='', name=session['oauth-signup']['name'], oauth_uid=session['oauth-signup']['oauth_uid'])
            session['oauth-signup-post'] = session['oauth-signup']
            del session['oauth-signup']
            return render_template('settings.html', user=user)
        else:
            data = None
            if current_user.oauth_uid is not None:
                resp = twitter.get('users/show.json', data={'user_id': current_user.oauth_uid}, token='')
                if resp.status == 200:
                    data = resp.data
            return render_imgtl_template('settings.html', twitter=data)
    elif request.method == 'POST':
        if request.form['what'] == 'token':
            while 1:
                try:
                    current_user.token = imgtl.lib.make_token()
                    db.session.commit()
                except IntegrityError:
                    db.session.rollback()
                    continue
                else:
                    break
            return jsonify({'token': current_user.token})
        elif request.form['what'] == 'update':
            if 'oauth-signup-post' in session:
                user = User(email='', name=session['oauth-signup-post']['name'], oauth_uid=session['oauth-signup-post']['oauth_uid'])
            else:
                user = current_user
            if request.form.get('password', '') != '':
                if not imgtl.validator.password(request.form['password']):
                    flash(i18n('invalidpassowrd'), 'error')
                    return redirect(url_for('settings'))
                elif request.form['password'] != request.form['passwordconfirm']:
                    flash(i18n('passwordmismatch'), 'error')
                    return redirect(url_for('settings'))
                else:
                    user.password = imgtl.lib.pw_hash(request.form['password'])
            new_email = request.form['email']
            new_username = request.form['username']
            if not imgtl.validator.email(new_email):
                flash(i18n('invalidemail'), 'error')
                return redirect(url_for('settings'))
            if not imgtl.validator.username(new_username):
                flash(i18n('invalidusername'), 'error')
                return redirect(url_for('settings'))
            if user.email != new_email:
                if User.query.filter_by(email=new_email).first():
                    flash(i18n('alreadyexistemail'), 'error')
                    return redirect(url_for('settings'))
            if user.name != new_username:
                if User.query.filter_by(name=new_username).first():
                    flash(i18n('alreadyexistname'), 'error')
                    return redirect(url_for('settings'))
            user.email = new_email
            user.name = new_username
            db.session.add(user)
            db.session.commit()
            if 'oauth-signup-post' in session:
                while 1:
                    try:
                        user.token = imgtl.lib.make_token()
                        db.session.commit()
                    except IntegrityError:
                        db.session.rollback()
                        continue
                    else:
                        break
                login_user(user, remember=True)
                do_log('web', 'signup_by_oauth', user.id)
                del session['oauth-signup-post']
                flash(i18n('signupsuccess'), 'success')
                return redirect(url_for('index'))
            else:
                flash(i18n('accupdatesuccess'), 'success')
                return redirect(url_for('settings'))
Esempio n. 16
0
File: web.py Progetto: imgtl/imgtl
    app.config.from_pyfile('.imgtl.tests.cfg')
else:
    app.config.from_pyfile('imgtl.cfg')
app.jinja_env.trim_blocks = True
app.jinja_env.lstrip_blocks = True
app.jinja_env.filters['nl2br'] = jinja2_filter_nl2br
app.jinja_env.filters['dt'] = jinja2_filter_dt

db.init_app(app)
db.app = app
log_db.init_app(app)
log_db.app = app

login_manager = LoginManager()
login_manager.login_view = 'login'
login_manager.login_message = i18n('youmustlogin')
login_manager.init_app(app)

oauth = OAuth(app)
twitter = twitter_factory.register_to(oauth)


def render_imgtl_template(*args, **kwargs):
    kwargs['user'] = current_user
    return render_template(*args, **kwargs)

@login_manager.user_loader
def load_user(user_id):
    return User.query.get(user_id)

@app.route('/')