def process(self, version): """Process a single version, figuring out if it should be auto-approved and calling the approval code if necessary.""" already_locked = AutoApprovalSummary.check_is_locked(version) if not already_locked: # Lock the addon for ourselves if possible. Even though # AutoApprovalSummary.create_summary_for_version() will do # call check_is_locked() again later when calculating the verdict, # we have to do it now to prevent overwriting an existing lock with # our own. set_reviewing_cache(version.addon.pk, settings.TASK_USER_ID) try: log.info('Processing %s version %s...', unicode(version.addon.name), unicode(version.version)) summary, info = AutoApprovalSummary.create_summary_for_version( version, dry_run=self.dry_run) log.info('Auto Approval for %s version %s: %s', unicode(version.addon.name), unicode(version.version), summary.get_verdict_display()) self.stats.update({k: int(v) for k, v in info.items()}) if summary.verdict == self.successful_verdict: self.stats['auto_approved'] += 1 if summary.verdict == amo.AUTO_APPROVED: self.approve(version) except (AutoApprovalNotEnoughFilesError, AutoApprovalNoValidationResultError): log.info( 'Version %s was skipped either because it had no ' 'file or because it had no validation attached.', version) self.stats['error'] += 1 finally: # Always clear our own lock no matter what happens (but only ours). if not already_locked: clear_reviewing_cache(version.addon.pk)
def test_check_is_locked(self): assert AutoApprovalSummary.check_is_locked(self.version) is False set_reviewing_cache(self.version.addon.pk, settings.TASK_USER_ID) assert AutoApprovalSummary.check_is_locked(self.version) is False set_reviewing_cache(self.version.addon.pk, settings.TASK_USER_ID + 42) assert AutoApprovalSummary.check_is_locked(self.version) is True
def review_viewing(request): if 'addon_id' not in request.POST: return {} addon_id = request.POST['addon_id'] user_id = request.user.id current_name = '' is_user = 0 key = get_reviewing_cache_key(addon_id) user_key = '%s:review_viewing_user:%s' % (settings.CACHE_PREFIX, user_id) interval = amo.EDITOR_VIEWING_INTERVAL # Check who is viewing. currently_viewing = get_reviewing_cache(addon_id) # If nobody is viewing or current user is, set current user as viewing if not currently_viewing or currently_viewing == user_id: # Get a list of all the reviews this user is locked on. review_locks = cache.get_many(cache.get(user_key, {})) can_lock_more_reviews = ( len(review_locks) < amo.EDITOR_REVIEW_LOCK_LIMIT or acl.action_allowed(request, amo.permissions.REVIEWER_ADMIN_TOOLS_VIEW)) if can_lock_more_reviews or currently_viewing == user_id: set_reviewing_cache(addon_id, user_id) # Give it double expiry just to be safe. cache.set(user_key, set(review_locks) | {key}, interval * 4) currently_viewing = user_id current_name = request.user.name is_user = 1 else: currently_viewing = settings.TASK_USER_ID current_name = _('Review lock limit reached') is_user = 2 else: current_name = UserProfile.objects.get(pk=currently_viewing).name return { 'current': currently_viewing, 'current_name': current_name, 'is_user': is_user, 'interval_seconds': interval }
def process(self, version): """Process a single version, figuring out if it should be auto-approved and calling the approval code if necessary.""" already_locked = AutoApprovalSummary.check_is_locked(version) if not already_locked: # Lock the addon for ourselves if possible. Even though # AutoApprovalSummary.create_summary_for_version() will do # call check_is_locked() again later when calculating the verdict, # we have to do it now to prevent overwriting an existing lock with # our own. set_reviewing_cache(version.addon.pk, settings.TASK_USER_ID) try: log.info('Processing %s version %s...', unicode(version.addon.name), unicode(version.version)) summary, info = AutoApprovalSummary.create_summary_for_version( version, max_average_daily_users=self.max_average_daily_users, min_approved_updates=self.min_approved_updates, dry_run=self.dry_run, post_review=waffle.switch_is_active('post-review')) log.info('Auto Approval for %s version %s: %s', unicode(version.addon.name), unicode(version.version), summary.get_verdict_display()) self.stats.update({k: int(v) for k, v in info.items()}) if summary.verdict == self.successful_verdict: self.stats['auto_approved'] += 1 if summary.verdict == amo.AUTO_APPROVED: self.approve(version) except (AutoApprovalNotEnoughFilesError, AutoApprovalNoValidationResultError): log.info( 'Version %s was skipped either because it had no ' 'file or because it had no validation attached.', version) self.stats['error'] += 1 finally: # Always clear our own lock no matter what happens (but only ours). if not already_locked: clear_reviewing_cache(version.addon.pk)
def review_viewing(request): if 'addon_id' not in request.POST: return {} addon_id = request.POST['addon_id'] user_id = request.user.id current_name = '' is_user = 0 key = get_reviewing_cache_key(addon_id) user_key = '%s:review_viewing_user:%s' % (settings.CACHE_PREFIX, user_id) interval = amo.EDITOR_VIEWING_INTERVAL # Check who is viewing. currently_viewing = get_reviewing_cache(addon_id) # If nobody is viewing or current user is, set current user as viewing if not currently_viewing or currently_viewing == user_id: # Get a list of all the reviews this user is locked on. review_locks = cache.get_many(cache.get(user_key, {})) can_lock_more_reviews = ( len(review_locks) < amo.EDITOR_REVIEW_LOCK_LIMIT or acl.action_allowed(request, amo.permissions.REVIEWER_ADMIN_TOOLS_VIEW)) if can_lock_more_reviews or currently_viewing == user_id: set_reviewing_cache(addon_id, user_id) # Give it double expiry just to be safe. cache.set(user_key, set(review_locks) | {key}, interval * 4) currently_viewing = user_id current_name = request.user.name is_user = 1 else: currently_viewing = settings.TASK_USER_ID current_name = ugettext('Review lock limit reached') is_user = 2 else: current_name = UserProfile.objects.get(pk=currently_viewing).name return {'current': currently_viewing, 'current_name': current_name, 'is_user': is_user, 'interval_seconds': interval}