Пример #1
0
Файл: app.py Проект: SamR1/moa
def index():
    mform = MastodonIDForm()
    settings = Settings()
    enabled = True
    found_settings = False

    if 'twitter' in session and 'mastodon' in session:
        # look up settings
        bridge = db.session.query(Bridge).filter_by(
            mastodon_user=session['mastodon']['username'],
            twitter_handle=session['twitter']['screen_name'],
        ).first()

        if bridge:
            found_settings = True
            settings = bridge.settings
            enabled = bridge.enabled
            g.bridge = bridge
            app.logger.debug(
                f"Existing settings found: {enabled} {settings.__dict__}")

    form = SettingsForm(obj=settings)

    return render_template('index.html.j2',
                           form=form,
                           mform=mform,
                           enabled=enabled,
                           found_settings=found_settings)
Пример #2
0
Файл: app.py Проект: uetchy/moa
def options():

    if 'bridge_id' in session:
        bridge = db.session.query(Bridge).filter_by(
            id=session['bridge_id']).first()
    else:
        flash('ERROR: Please log in to an account')
        return redirect(url_for('index'))

    form = SettingsForm()

    if not bridge.mastodon_access_code or not bridge.twitter_oauth_token:
        form.remove_masto_and_twitter_fields()

    if form.validate_on_submit():

        app.logger.debug("Existing settings found")
        form.populate_obj(bridge.t_settings)

        bridge.enabled = form.enabled.data
        bridge.updated = datetime.now()

        catch_up_twitter(bridge)
        catch_up_mastodon(bridge)

        app.logger.debug("Saving new settings")

        flash("Settings Saved.")
        db.session.commit()
    else:
        for e in form.errors.items():
            flash(e[1][0])
        return redirect(url_for('index'))

    return redirect(url_for('index'))
Пример #3
0
def index():
    if app.config['MAINTENANCE_MODE']:
        return render_template('maintenance.html.j2')

    mform = MastodonIDForm()
    settings = TSettings()
    enabled = True
    form = SettingsForm(obj=settings)

    if 'bridge_id' in session:
        bridge = db.session.query(Bridge).filter_by(
            id=session['bridge_id']).first()

        if bridge:
            g.bridge = bridge
            settings = bridge.t_settings
            app.logger.debug(
                f"Existing settings found: {enabled} {settings.__dict__}")

            form = SettingsForm(obj=settings)

            if not bridge.mastodon_access_code or not bridge.twitter_oauth_token:
                form.remove_masto_and_twitter_fields()

    return render_template(
        'index.html.j2',
        form=form,
        mform=mform,
    )
Пример #4
0
def options():
    form = SettingsForm()

    if form.validate_on_submit():

        bridge = db.session.query(Bridge).filter_by(
            mastodon_user=session['mastodon']['username'],
            twitter_handle=session['twitter']['screen_name'],
        ).first()

        if bridge:
            app.logger.debug("Existing settings found")
            form.populate_obj(bridge.t_settings)

        else:
            bridge = Bridge()
            settings = TSettings()
            form.populate_obj(settings)

        bridge.enabled = form.enabled.data
        # bridge.t_settings = settings
        bridge.updated = datetime.now()
        bridge.twitter_oauth_token = session['twitter']['oauth_token']
        bridge.twitter_oauth_secret = session['twitter']['oauth_token_secret']
        bridge.twitter_handle = session['twitter']['screen_name']
        bridge.mastodon_access_code = session['mastodon']['access_code']
        bridge.mastodon_user = session['mastodon']['username']
        bridge.mastodon_host = get_or_create_host(session['mastodon']['host'])

        if not bridge.mastodon_host:
            flash(
                f"There was a problem connecting to {session['mastodon']['host']}"
            )
            return redirect(url_for('index'))

        catch_up_twitter(bridge)
        catch_up_mastodon(bridge)

        app.logger.debug("Saving new settings")

        flash("Settings Saved.")
        db.session.commit()
    else:
        for e in form.errors.items():
            flash(e[1][0])
        return redirect(url_for('index'))

    return redirect(url_for('index'))
Пример #5
0
Файл: app.py Проект: SamR1/moa
def options():
    form = SettingsForm()

    if form.validate_on_submit():

        settings = Settings()

        form.populate_obj(settings)

        bridge_found = False

        bridge = db.session.query(Bridge).filter_by(
            mastodon_user=session['mastodon']['username'],
            twitter_handle=session['twitter']['screen_name'],
        ).first()

        if bridge:
            bridge_found = True
            app.logger.debug("Existing settings found")
        else:
            bridge = Bridge()

        bridge.enabled = form.enabled.data
        bridge.settings = settings
        bridge.updated = datetime.now()
        bridge.twitter_oauth_token = session['twitter']['oauth_token']
        bridge.twitter_oauth_secret = session['twitter']['oauth_token_secret']
        bridge.twitter_handle = session['twitter']['screen_name']
        bridge.mastodon_access_code = session['mastodon']['access_code']
        bridge.mastodon_user = session['mastodon']['username']
        bridge.mastodon_host = get_or_create_host(session['mastodon']['host'])

        if not bridge.mastodon_host:
            flash(
                f"There was a problem connecting to {session['mastodon']['host']}"
            )
            return redirect(url_for('index'))

        # get twitter ID
        twitter_api = twitter.Api(
            consumer_key=app.config['TWITTER_CONSUMER_KEY'],
            consumer_secret=app.config['TWITTER_CONSUMER_SECRET'],
            access_token_key=session['twitter']['oauth_token'],
            access_token_secret=session['twitter']['oauth_token_secret'],
            tweet_mode='extended'  # Allow tweets longer than 140 raw characters
        )

        if bridge.twitter_last_id == 0:
            tl = twitter_api.GetUserTimeline()
            if len(tl) > 0:
                bridge.twitter_last_id = tl[0].id
            else:
                bridge.twitter_last_id = 0

        if bridge.mastodon_last_id == 0:

            # get mastodon ID
            api = mastodon_api(session['mastodon']['host'],
                               access_code=session['mastodon']['access_code'])

            bridge.mastodon_account_id = api.account_verify_credentials()["id"]

            try:
                statuses = api.account_statuses(bridge.mastodon_account_id)
                if len(statuses) > 0:
                    bridge.mastodon_last_id = statuses[0]["id"]
                else:
                    bridge.mastodon_last_id = 0

            except MastodonAPIError:
                bridge.mastodon_last_id = 0

        app.logger.debug("Saving new settings")

        flash("Settings Saved.")
        db.session.commit()
    else:
        for e in form.errors.items():
            flash(e[1][0])
        return redirect(url_for('index'))

    return redirect(url_for('index'))