def run_daily_activities(self): # If gengosystem is disabled, we don't want to do anything. if not waffle.switch_is_active('gengosystem'): return gengo_api = FjordGengo() if not gengo_api.is_configured(): # If Gengo isn't configured, then we drop out here rather # than raise a GengoConfig error. return balance = gengo_api.get_balance() threshold = settings.GENGO_ACCOUNT_BALANCE_THRESHOLD if threshold < balance < (2 * threshold): mail_admins( subject='Warning: Gengo account balance {0} < {1}'.format( balance, 2 * threshold), message=wrap_with_paragraphs( 'Dear mom,' '\n\n' 'Translations are the fab. Running low on funds. Send ' 'more money when you get a chance.' '\n\n' 'Love,' '\n\n' 'Fjord McGengo' ) )
def run_daily_activities(self): # If gengosystem is disabled, we don't want to do anything. if not waffle.switch_is_active('gengosystem'): return gengo_api = FjordGengo() if not gengo_api.is_configured(): # If Gengo isn't configured, then we drop out here rather # than raise a GengoConfig error. return balance = gengo_api.get_balance() threshold = settings.GENGO_ACCOUNT_BALANCE_THRESHOLD if threshold < balance < (2 * threshold): mail_admins( subject='Warning: Gengo account balance {0} < {1}'.format( balance, 2 * threshold), message=wrap_with_paragraphs( 'Dear mom,' '\n\n' 'Translations are the fab. Running low on funds. Send ' 'more money when you get a chance.' '\n\n' 'Love,' '\n\n' 'Fjord McGengo' ) )
def pull_translations(self): # If gengosystem is disabled, we just return immediately. We # can backfill later. if not waffle.switch_is_active('gengosystem'): return gengo_api = FjordGengo() if not gengo_api.is_configured(): # If Gengo isn't configured, then we drop out here rather # than raise a GengoConfig error. return # Get all the orders that are in progress orders = GengoOrder.objects.filter(status=STATUS_IN_PROGRESS) for order in orders: # Get the list of all completed jobs completed = gengo_api.completed_jobs_for_order(order.order_id) # If there are no completed jobs, then we don't need to # bother doing any additional processing for this order if not completed: continue # For each complete job we haven't seen before, pull it # from the db, save the translated text and update all the # bookkeeping. for comp in completed: id_ = GengoJob.unique_id_to_id(comp['custom_data']) job = GengoJob.objects.get(pk=id_) if job.status == STATUS_COMPLETE: continue instance = job.content_object setattr(instance, job.dst_field, comp['body_tgt']) instance.save() job.mark_complete() # Check to see if there are still outstanding jobs for # this order. If there aren't, close the order out. outstanding = (GengoJob.objects .filter(order=order, status=STATUS_IN_PROGRESS) .count()) if outstanding == 0: order.mark_complete()
def pull_translations(self): # If gengosystem is disabled, we just return immediately. We # can backfill later. if not waffle.switch_is_active('gengosystem'): return gengo_api = FjordGengo() if not gengo_api.is_configured(): # If Gengo isn't configured, then we drop out here rather # than raise a GengoConfig error. return # Get all the orders that are in progress orders = GengoOrder.objects.filter(status=STATUS_IN_PROGRESS) for order in orders: # Get the list of all completed jobs completed = gengo_api.completed_jobs_for_order(order.order_id) # If there are no completed jobs, then we don't need to # bother doing any additional processing for this order if not completed: continue # For each complete job we haven't seen before, pull it # from the db, save the translated text and update all the # bookkeeping. for comp in completed: id_ = GengoJob.unique_id_to_id(comp['custom_data']) job = GengoJob.objects.get(pk=id_) if job.status == STATUS_COMPLETE: continue instance = job.content_object setattr(instance, job.dst_field, comp['body_tgt']) instance.save() job.mark_complete() # Check to see if there are still outstanding jobs for # this order. If there aren't, close the order out. outstanding = (GengoJob.objects .filter(order=order, status=STATUS_IN_PROGRESS) .count()) if outstanding == 0: order.mark_complete()
def run_daily_activities(self): # If gengosystem is disabled, we don't want to do anything. if not waffle.switch_is_active('gengosystem'): return gengo_api = FjordGengo() if not gengo_api.is_configured(): # If Gengo isn't configured, then we drop out here rather # than raise a GengoConfig error. return balance = gengo_api.get_balance() threshold = settings.GENGO_ACCOUNT_BALANCE_THRESHOLD if threshold < balance < (2 * threshold): recipients = get_recipients('gengo_balance') subject = 'Warning: Gengo account balance {0} < {1}'.format( balance, 2 * threshold) body = wrap_with_paragraphs(dedent("""\ Dear mom, Translations are the fab. Running low on funds. Send more money when you get a chance. Love, Fjord McGengo """)) if recipients: send_mail( subject=subject, message=body, recipient_list=recipients, from_email=settings.SERVER_EMAIL ) else: log.info('No recipients for "%s"\n%s\n%s' % ( 'gengo_balance', subject, body))
def run_daily_activities(self): # If gengosystem is disabled, we don't want to do anything. if not waffle.switch_is_active('gengosystem'): return gengo_api = FjordGengo() if not gengo_api.is_configured(): # If Gengo isn't configured, then we drop out here rather # than raise a GengoConfig error. return balance = gengo_api.get_balance() threshold = settings.GENGO_ACCOUNT_BALANCE_THRESHOLD if threshold < balance < (2 * threshold): recipients = get_recipients('gengo_balance') subject = 'Warning: Gengo account balance {0} < {1}'.format( balance, 2 * threshold) body = wrap_with_paragraphs( dedent("""\ Dear mom, Translations are the fab. Running low on funds. Send more money when you get a chance. Love, Fjord McGengo """)) if recipients: send_mail(subject=subject, message=body, recipient_list=recipients, from_email=settings.SERVER_EMAIL) else: log.info('No recipients for "%s"\n%s\n%s' % ('gengo_balance', subject, body))
def push_translations(self): # If gengosystem is disabled, we just return immediately. We # can backfill later. if not waffle.switch_is_active('gengosystem'): return gengo_api = FjordGengo() if not gengo_api.is_configured(): # If Gengo isn't configured, then we drop out here rather # than raise a GengoConfig error. return if self.gengo_watch_balance: balance = gengo_api.get_balance() threshold = settings.GENGO_ACCOUNT_BALANCE_THRESHOLD # statsd the balance so we can track it with graphite. statsd.gauge('translation.gengo.balance', balance) if not self.balance_good_to_continue(balance, threshold): # If we don't have enough balance, stop. return # Create language buckets for the jobs for this translator. # We bucket by language because this makes it easier for a # single Gengo translator to translate all the jobs in an # order. jobs = GengoJob.objects.filter( tier=self.gengo_tier, status=STATUS_CREATED) lang_buckets = {} for job in jobs: lang_buckets.setdefault(job.src_lang, []).append(job) # For each bucket, assemble an order and post it. for lang, jobs in lang_buckets.items(): batch = [] for job in jobs: batch.append({ 'id': job.id, 'lc_src': job.src_lang, 'lc_dst': job.dst_lang, 'tier': self.gengo_tier, 'text': getattr(job.content_object, job.src_field), 'unique_id': job.unique_id }) try: resp = gengo_api.translate_bulk(batch) except GengoError as exc: self.log_error( instance=None, action='push-translations', msg=unicode(exc), metadata={ 'batch': batch }) continue # We should have an `order_id` at this point, so we create a # GengoOrder with it. order = GengoOrder(order_id=resp['order_id']) order.save() order.log('created', metadata={'response': resp}) # Update all the jobs in the order. for job in jobs: job.assign_to_order(order) if self.gengo_watch_balance: # Update the balance and see if we're below the threshold. balance = balance - float(resp['credits_used']) if not self.balance_good_to_continue(balance, threshold): # If we don't have enough balance, stop. return
def push_translations(self): # If gengosystem is disabled, we just return immediately. We # can backfill later. if not waffle.switch_is_active('gengosystem'): return gengo_api = FjordGengo() if not gengo_api.is_configured(): # If Gengo isn't configured, then we drop out here rather # than raise a GengoConfig error. return if self.gengo_watch_balance: balance = gengo_api.get_balance() threshold = settings.GENGO_ACCOUNT_BALANCE_THRESHOLD # statsd the balance so we can track it with graphite. statsd.gauge('translation.gengo.balance', balance) if not self.balance_good_to_continue(balance, threshold): # If we don't have enough balance, stop. return # Create language buckets for the jobs for this translator. # We bucket by language because this makes it easier for a # single Gengo translator to translate all the jobs in an # order. jobs = GengoJob.objects.filter(tier=self.gengo_tier, status=STATUS_CREATED) lang_buckets = {} for job in jobs: lang_buckets.setdefault(job.src_lang, []).append(job) # For each bucket, assemble an order and post it. for lang, jobs in lang_buckets.items(): batch = [] for job in jobs: batch.append({ 'id': job.id, 'lc_src': job.src_lang, 'lc_dst': job.dst_lang, 'tier': self.gengo_tier, 'text': getattr(job.content_object, job.src_field), 'unique_id': job.unique_id }) try: resp = gengo_api.translate_bulk(batch) except GengoError as exc: self.log_error(instance=None, action='push-translations', msg=unicode(exc), metadata={'batch': batch}) continue # We should have an `order_id` at this point, so we create a # GengoOrder with it. order = GengoOrder(order_id=resp['order_id']) order.save() order.log('created', metadata={'response': resp}) # Update all the jobs in the order. for job in jobs: job.assign_to_order(order) if self.gengo_watch_balance: # Update the balance and see if we're below the threshold. balance = balance - float(resp['credits_used']) if not self.balance_good_to_continue(balance, threshold): # If we don't have enough balance, stop. return