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, )
def index(): if app.config['MAINTENANCE_MODE']: return render_template('maintenance.html.j2') mform = MastodonIDForm() settings = TSettings() 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.t_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)
def setUp(self): moa_config = os.environ.get('MOA_CONFIG', 'DevelopmentConfig') self.c = getattr(importlib.import_module('config'), moa_config) self.settings = TSettings() FORMAT = '%(asctime)-15s %(message)s' logging.basicConfig(format=FORMAT) self.l = logging.getLogger() self.l.setLevel(logging.DEBUG)
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'))
def get_or_create_bridge(bridge_id=None): if bridge_id: bridge = db.session.query(Bridge).filter_by(id=bridge_id).first() else: bridge = Bridge() bridge.enabled = True bridge.t_settings = TSettings() db.session.add(bridge.t_settings) db.session.add(bridge) db.session.commit() session['bridge_id'] = bridge.id return bridge
def setUp(self): moa_config = os.environ.get('MOA_CONFIG', 'TestingConfig') self.c = getattr(importlib.import_module('config'), moa_config) self.settings = TSettings() FORMAT = '%(asctime)-15s %(message)s' logging.basicConfig(format=FORMAT) self.l = logging.getLogger() self.l.setLevel(logging.INFO) self.api = twitter.Api( consumer_key=self.c.TWITTER_CONSUMER_KEY, consumer_secret=self.c.TWITTER_CONSUMER_SECRET, access_token_key=self.c.TWITTER_OAUTH_TOKEN, access_token_secret=self.c.TWITTER_OAUTH_SECRET, tweet_mode='extended' # Allow tweets longer than 140 raw characters )
def get_or_create_bridge(bridge_id=None): if bridge_id: bridge = db.session.query(Bridge).filter_by(id=bridge_id).first() if not bridge.md: bridge.md = BridgeMetadata() else: bridge = Bridge() bridge.enabled = True bridge.t_settings = TSettings() bridge.md = BridgeMetadata() bridge.md.worker_id = random.randint(1, app.config['WORKER_JOBS']) bridge.updated = datetime.now() db.session.add(bridge.t_settings) db.session.add(bridge) db.session.commit() session['bridge_id'] = bridge.id return bridge
def mastodon_oauthorized(): authorization_code = request.args.get('code') if authorization_code is None: flash('You denied the request to sign in to Mastodon.') else: host = session.get('mastodon_host', None) app.logger.info(f"Authorization code {authorization_code} for {host}") if not host: flash( 'There was an error. Please ensure you allow this site to use cookies.' ) return redirect(url_for('index')) session.pop('mastodon_host', None) api = mastodon_api(host) try: access_code = api.log_in(code=authorization_code, scopes=["read", "write"], redirect_uri=url_for( "mastodon_oauthorized", _external=True)) except MastodonIllegalArgumentError as e: flash( f"There was a problem connecting to the mastodon server. The error was {e}" ) return redirect(url_for('index')) # app.logger.info(f"Access code {access_code}") api.access_code = access_code try: session['mastodon'] = { 'host': host, 'access_code': access_code, 'username': api.account_verify_credentials()["username"] } except MastodonUnauthorizedError as e: flash( f"There was a problem connecting to the mastodon server. The error was {e}" ) return redirect(url_for('index')) 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") else: bridge = Bridge() bridge.enabled = True bridge.t_settings = TSettings() 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']) db.session.add(bridge.t_settings) db.session.add(bridge) db.session.commit() catch_up_mastodon(bridge) catch_up_twitter(bridge) if app.config.get('MAIL_SERVER', None): # Fetch twitter follower count twitter_api = twitter.Api( consumer_key=app.config['TWITTER_CONSUMER_KEY'], consumer_secret=app.config['TWITTER_CONSUMER_SECRET'], access_token_key=bridge.twitter_oauth_token, access_token_secret=bridge.twitter_oauth_secret) follower_list = twitter_api.GetFollowerIDs() follower_count = len(follower_list) body = render_template('new_user_email.txt.j2', bridge=bridge, follower_count=follower_count) msg = Message(subject="New moa.party user", body=body, recipients=[app.config.get('MAIL_TO', None)]) try: mail.send(msg) except Exception as e: app.logger.error(e) return redirect(url_for('index'))
from sqlalchemy import create_engine from sqlalchemy.orm import Session from pprint import pprint as pp from moa.models import Bridge, TSettings moa_config = os.environ.get('MOA_CONFIG', 'DevelopmentConfig') config = getattr(importlib.import_module('config'), moa_config) engine = create_engine(config.SQLALCHEMY_DATABASE_URI) engine.connect() session = Session(engine) if len(sys.argv) > 1: bridge = session.query(Bridge).filter_by(id=sys.argv[1]).first() new_settings = TSettings() new_settings.import_settings(bridge.settings) session.add(new_settings) session.commit() bridge.t_settings = new_settings session.commit() else: bridges = session.query(Bridge).all() for bridge in bridges: print(bridge.id) new_settings = TSettings() new_settings.import_settings(bridge.settings) session.add(new_settings)