Example #1
0
def flash_editor_errors(form):
    for field, errors in form.errors.items():
        for error in errors:
            flash("В поле '%s'- ошибка: %s" % (
                getattr(form, field).label.text,
                error
            ))
Example #2
0
def edit(OrmForm, model):
    form = OrmForm(request.form, model)
    if form.validate_on_submit():
        form.populate_obj(model)
        model.put()
        flash("MyModel updated")
        return redirect(url_for("index"))
    return render_template("editor.html", form=form)
Example #3
0
def login():
    form = LoginForm()
    if form.validate_on_submit():
        user = User.query.filter_by(email=form.email_or_username.data).first()
        if user is None:
            user = User.query.filter_by(username=form.email_or_username.data).first()
        if user is not None and user.verify_password(form.password.data):
            login_user(user, form.remember_me.data)
            return redirect(request.args.get("next") or url_for("main.index"))
        flash(_("Invalid user name or password"), "d")
    return render_template("auth/login.html", form=form)
Example #4
0
def register():
    form = RegisterForm()
    if form.validate_on_submit():
        user = User(email=form.email.data, username=form.username.data, password=form.password.data)
        profile = User_Profile(id=user.id)
        db.session.add(user)
        db.session.add(profile)
        db.session.commit()
        token = user.generate_confirmation_token()
        send_email(user.email, "Confirm Your Account", "auth/email/confirm", user=user, token=token)
        flash(_("A confirmation email has been sent to you by email."), "s")
        return redirect(url_for("auth.login"))
    return render_template("auth/register.html", form=form)
Example #5
0
def add_olympiad():
    title = "Добавление олимпиады"
    form = EditorOlympiad()

    if form.validate_on_submit():
        new_olympiad = Olympiad(name=form.name.data,
                                max_balls=form.max_balls.data,
                                description=form.description.data)
        db.session.add(new_olympiad)
        db.session.commit()
        flash('Добавлена Олимпиада!')
        return redirect(url_for("hierarchy"))

    flash(flash_editor_errors(form))
    return render_template("editor.html", form=form, action='/add/olympiad', title=title)
Example #6
0
def confirm(token):
    if current_user.confirmed:
        return redirect(url_for("main.index"))
    if current_user.confirm(token):
        flash(_("You have confirmed your account. Thanks!"), "s")
    else:
        flash(
            _(
                "The confirmation link is invalid or has expired."
                + "click <a href='{{url_for('auth.resend_confirmation'}}'>"
                + "here</a>"
                + "to resend the link"
            ),
            "d",
        )
    return redirect(url_for("main.index"))
Example #7
0
def resend_confirmation():
    token = current_user.generate_confirmation_token()
    send_email(current_user.email, "Confirm Your Account", "auth/email/confirm", user=current_user, token=token)
    flash(_("A confirmation email has been sent to you by email."), "s")
    return redirect(request.args.get("next") or url_for("main.index"))
Example #8
0
def logout():
    logout_user()
    flash("You have been logged out.", "s")
    return redirect(request.args.get("next") or url_for("main.index"))
Example #9
0
def server_start():
    p = OptionParser()
    p.add_option('-d', action = "store_true",
                 dest = 'daemonize', help = "Run the server as a daemon")
    p.add_option('-q', '--quiet', action = "store_true",
                 dest = 'quiet', help = "Don't log to console")
    p.add_option('-p', '--pidfile',
                 dest = 'pidfile', default = None,
                 help = "Store the process id in the given file")

    options, args = p.parse_args()

    config = os.path.join(rundir, 'config.ini')

    # Config app
    ca = configApp(config)
    initDb()

    # Start threads
    myCrons = CronJobs(cherrypy.engine, ca, debug)
    myCrons.subscribe()

    # Update script
    myUpdater = Updater(cherrypy.engine)
    myUpdater.subscribe()

    # User config, use own stuff to prevent unexpected results
    cherrypy.config.update({
        'global': {
            'server.thread_pool':               10,
            'server.socket_port':           int(ca.get('global', 'port')),
            'server.socket_host':               ca.get('global', 'host'),
            'server.environment':               ca.get('global', 'server.environment'),
            'engine.autoreload_on':             ca.get('global', 'engine.autoreload_on') and not options.daemonize,
            'tools.mako.collection_size':       500,
            'tools.mako.directories':           os.path.join(path_base, 'app', 'views'),

            'basePath':                         path_base,
            'runPath':                          rundir,
            'cachePath':                        cachedir,
            'debug':                            debug,
            'frozen':                           frozen,

            # Global workers
            'config':                           ca,
            'updater':                          myUpdater,
            'cron':                             myCrons.threads,
            'searchers':                        myCrons.searchers,
            'flash':                            app.flash()
        }
    })

    # Static config
    conf = {
        '/': {
            'request.dispatch': Routes(),
            'tools.sessions.on':  True,
            'tools.sessions.timeout': 240,

            'tools.gzip.on': True,
            'tools.gzip.mime_types': ['text/html', 'text/plain', 'text/css', 'text/javascript', 'application/javascript']
        },
        '/media':{
            'tools.staticdir.on': True,
            'tools.staticdir.root': path_base,
            'tools.staticdir.dir': "media",
            'tools.expires.on': True,
            'tools.expires.secs': 3600 * 24 * 7
        },
        '/cache':{
            'tools.staticdir.on': True,
            'tools.staticdir.root': rundir,
            'tools.staticdir.dir': "cache",
            'tools.expires.on': True,
            'tools.expires.secs': 3600 * 24 * 7
        }
    }

    # Don't use auth when password is empty
    if ca.get('global', 'password') != '':
        conf['/'].update({
            'tools.basic_auth.on': True,
            'tools.basic_auth.realm': 'Awesomeness',
            'tools.basic_auth.users': {ca.get('global', 'username'):ca.get('global', 'password')},
            'tools.basic_auth.encrypt': app.clearAuthText
        })
        cherrypy.tools.mybasic_auth = cherrypy.Tool('on_start_resource', app.basicAuth)

    # I'll do my own logging, thanks!
    cherrypy.log.error_log.propagate = False
    cherrypy.log.access_log.propagate = False

    #No Root controller as we provided all our own.
    cherrypy.tree.mount(root = None, config = conf)

    # Stop logging
    if options.quiet:
        cherrypy.config.update({'log.screen': False})

    # Deamonize
    if options.daemonize:
        cherrypy.config.update({'log.screen': False})
        plugins.Daemonizer(cherrypy.engine).subscribe()

    # PIDfile
    if options.pidfile:
        plugins.PIDFile(cherrypy.engine, options.pidfile).subscribe()

    # Setup the signal handler
    if hasattr(cherrypy.engine, "signal_handler"):
        cherrypy.engine.signal_handler.subscribe()
    if hasattr(cherrypy.engine, "console_control_handler"):
        cherrypy.engine.console_control_handler.subscribe()


    ## start the app
    try:
        cherrypy.engine.start()
    except:
        sys.exit(1)
    else:

        # Launch browser
        if ca.get('global', 'launchbrowser'):
            app.launchBrowser(ca.get('global', 'host'), ca.get('global', 'port'))

        cherrypy.engine.block()