def create_playlist(submission_period): if not submission_period or not submission_period.all_tracks or not is_deployed(): return from musicleague.bot import get_botify bot_id, botify = get_botify() playlist_name = submission_period.name description = submission_period.description or '' description = description.replace('\n', ' ').replace('\r', ' ') tracks = submission_period.all_tracks try: app.logger.info("Creating new playlist: %s", playlist_name) playlist = botify.user_playlist_create( bot_id, playlist_name, description=description) app.logger.info( "Created new playlist: %s (%s)", playlist_name, playlist.get('id')) except Exception as e: app.logger.error("Error while creating playlist with params: %s %s %s", bot_id, playlist_name, json.dumps(description)) raise try: app.logger.info("Adding tracks to new playlist: %s", tracks) botify.user_playlist_add_tracks(bot_id, playlist.get('id'), tracks) except Exception as e: app.logger.error("Error while adding tracks: %s", json.dumps(tracks)) raise return playlist
def _send_email(to, subject, text, html, bcc_list=None, additional_data=None): if not is_deployed(): app.logger.info(text) return try: ses = boto3.client('ses', region_name='us-east-1') response = ses.send_email( Destination={ 'ToAddresses': [to], 'BccAddresses': bcc_list if bcc_list else [], }, Message={ 'Body': { 'Html': { 'Charset': 'UTF-8', 'Data': html, }, 'Text': { 'Charset': 'UTF-8', 'Data': text, } }, 'Subject': { 'Charset': 'UTF-8', 'Data': subject, } }, Source=get_setting(NOTIFICATION_SENDER), ) except Exception as e: app.logger.exception(u'Mail send failed w/ exception: %s', str(e)) return
def update_playlist(submission_period): if not submission_period or not submission_period.playlist_url: return if not is_deployed(): app.logger.warn("Not deployed - skipping playlist update") return from musicleague.bot import get_botify bot_id, botify = get_botify() tracks = submission_period.all_tracks # TODO Reference submission period's url so we don't have to return this try: app.logger.info("Replacing existing tracks with: %s", tracks) playlist_id = to_playlist_id(submission_period.playlist_url) playlist = botify.playlist(playlist_id) playlist_bot_id = playlist.get('owner').get('id') if playlist_bot_id != bot_id: bot_id, botify = get_botify(bot_id=playlist_bot_id) botify.user_playlist_replace_tracks(bot_id, playlist_id, tracks) except Exception as e: app.logger.error("Error updating tracks: %s", json.dumps(tracks)) raise return playlist
def schedule_new_round_notification(submission_period): if not is_deployed(): return notify_time = datetime.now() + timedelta(minutes=5) job_id = '%s_%s' % (submission_period.id, TYPES.NOTIFY_SUBMISSION_PERIOD) try: # If job has been previously scheduled, reschedule job = Job.fetch(job_id, connection=redis_conn) scheduler.change_execution_time(job, notify_time) logging.info('New round notification job updated', extra={ 'execute': notify_time, 'job': job.id }) except (NoSuchJobError, ValueError): # If job has not been previously scheduled or is no longer in queue, enqueue job = scheduler.enqueue_at(notify_time, notify_new_round, submission_period.id, job_id=job_id) logging.info('New round notification enqueued', extra={ 'execute': notify_time, 'job': job.id })
def create_or_update_playlist(submission_period): if not submission_period or not is_deployed(): return if not submission_period.playlist_created: # Create new playlist and link to this submission period app.logger.info("Creating playlist for round: %s", submission_period.id) playlist = create_playlist(submission_period) if not playlist: if submission_period.all_tracks: app.logger.error('There was a problem creating the playlist with tracks: %s', submission_period.all_tracks) else: app.logger.warn('Playlist creation w/ 0 tracks prevented') return playlist_url = playlist['external_urls']['spotify'] submission_period.playlist_url = playlist_url upsert_round(submission_period) user_playlist_created_notification(submission_period) else: # Update existing playlist for this submission period app.logger.info("Updating playlist for round: %s", submission_period.id) playlist = update_playlist(submission_period) # TODO user_playlist_updated_notification(submission_period) return playlist
def schedule_playlist_creation(submission_period): if not is_deployed(): return creation_time = submission_period.submission_due_date job_id = '%s_%s' % (submission_period.id, TYPES.CREATE_PLAYLIST) try: # If job has been previously scheduled, reschedule job = Job.fetch(job_id, connection=redis_conn) scheduler.change_execution_time(job, creation_time) logging.info('Playlist creation job updated', extra={ 'execute': creation_time, 'job': job.id }) except (NoSuchJobError, ValueError): # If job has not been previously scheduled or is no longer in queue, enqueue job = scheduler.enqueue_at(creation_time, complete_submission_process, submission_period.id, job_id=job_id) logging.info('Playlist creation enqueued', extra={ 'execute': creation_time, 'job': job.id })
def schedule_round_completion(submission_period): if not is_deployed(): return completion_time = submission_period.vote_due_date job_id = '%s_%s' % (submission_period.id, TYPES.COMPLETE_SUBMISSION_PERIOD) try: # If job has been previously scheduled, reschedule job = Job.fetch(job_id, connection=redis_conn) scheduler.change_execution_time(job, completion_time) logging.info('Round completion job updated', extra={ 'execute': completion_time, 'job': job.id }) except (NoSuchJobError, ValueError): # If job has not been previously scheduled or is no longer in queue, enqueue job = scheduler.enqueue_at(completion_time, complete_submission_period, submission_period.id, job_id=job_id) logging.info('Round completion enqueued', extra={ 'execute': completion_time, 'job': job.id })
def cancel_round_completion(submission_period): if not is_deployed(): return job_id = '%s_%s' % (submission_period.id, TYPES.COMPLETE_SUBMISSION_PERIOD) cancel_pending_task(job_id) app.logger.info('Completion canceled for %s', job_id)
def cancel_vote_reminders(submission_period): if not is_deployed(): return job_id = '%s_%s' % (submission_period.id, TYPES.SEND_VOTE_REMINDERS) cancel_pending_task(job_id) app.logger.info('Vote reminders canceled for %s.', job_id)
def cancel_playlist_creation(submission_period): if not is_deployed(): return job_id = '%s_%s' % (submission_period.id, TYPES.CREATE_PLAYLIST) cancel_pending_task(job_id) app.logger.info('Playlist creation canceled for %s.', job_id)
def schedule_vote_reminders(submission_period): if not is_deployed(): return # TODO Select preference instead of entire league league = select_league(submission_period.league_id) diff = league.preferences.vote_reminder_time notify_time = submission_period.vote_due_date - timedelta(hours=diff) job_id = '%s_%s' % (submission_period.id, TYPES.SEND_VOTE_REMINDERS) # If notification time would be in the past, cancel # any enqueued job instead of scheduling if notify_time < utc.localize(datetime.now()): logging.info('Not rescheduling vote reminder - datetime has passed', extra={ 'execute': notify_time, 'round': submission_period.id }) scheduler.cancel(job_id) return try: # If job has been previously scheduled, reschedule job = Job.fetch(job_id, connection=redis_conn) scheduler.change_execution_time(job, notify_time) logging.info('Vote reminder job updated', extra={ 'execute': notify_time, 'job': job.id }) except (NoSuchJobError, ValueError): # If job has not been previously scheduled or is no longer in queue, enqueue job = scheduler.enqueue_at(notify_time, send_vote_reminders, submission_period.id, job_id=job_id) logging.info('Vote reminder enqueued', extra={ 'execute': notify_time, 'job': job.id })
from rq_scheduler import Scheduler from musicleague.environment import get_redis_url from musicleague.environment import get_secret_key from musicleague.environment import get_server_name from musicleague.environment import is_deployed # Initialize Flask app app = Flask(__name__) moment = Moment(app) app.secret_key = get_secret_key() handler = logging.StreamHandler() handler.setFormatter(logmatic.JsonFormatter()) del app.logger.handlers[:] app.logger.addHandler(handler) app.logger.setLevel(logging.INFO) if is_deployed(): server_name = get_server_name() if server_name: app.config['SERVER_NAME'] = server_name redis_conn = Redis.from_url(get_redis_url()) queue = Queue(connection=redis_conn) scheduler = Scheduler(connection=redis_conn) from musicleague import routes # noqa
def test_not_deployed(self): self.assertFalse(is_deployed())
def test_deployed(self): self.assertTrue(is_deployed())
def wrapper(*args, **kwargs): if not g.user.is_admin and is_deployed(): flash_error('You must be an admin to access that page') return redirect(url_for('hello')) return func(*args, **kwargs)