def _insert_ratings(self): for r in self.ratings: rating = Rating(r_project=b'Test Project', r_namespace=0, r_article=r[0]) rating.r_quality = r[1] rating.r_quality_timestamp = GLOBAL_TIMESTAMP_WIKI rating.r_importance = r[2] rating.r_importance_timestamp = GLOBAL_TIMESTAMP_WIKI with self.wp10db.cursor() as cursor: cursor.execute( ''' INSERT INTO ratings (r_project, r_namespace, r_article, r_score, r_quality, r_quality_timestamp, r_importance, r_importance_timestamp) VALUES (%(r_project)s, %(r_namespace)s, %(r_article)s, %(r_score)s, %(r_quality)s, %(r_quality_timestamp)s, %(r_importance)s, %(r_importance_timestamp)s) ''', attr.asdict(rating)) self.wp10db.commit()
def update_project_assessments_by_kind(wikidb, wp10db, project, extra_assessments, kind, old_ratings, seen): if kind not in (AssessmentKind.QUALITY, AssessmentKind.IMPORTANCE): raise ValueError( 'Parameter "kind" was not one of QUALITY or IMPORTANCE') logger.info('Updating project %s assessments for %s', kind, project.p_project) rating_to_category = update_project_categories_by_kind( wikidb, wp10db, project, extra_assessments, kind) n = 0 new_ratings = defaultdict(list) for current_rating, (category, ranking) in rating_to_category.items(): logger.info('Fetching article list for %r' % category.decode('utf-8')) current_rating = current_rating.encode('utf-8') for page in logic_page.get_pages_by_category(wikidb, category): # Talk pages are tagged, we want the NS of the article itself. namespace = page.page_namespace - 1 if not logic_util.is_namespace_acceptable(namespace): logger.debug('Skipping %s with namespace=%s', page.page_title, namespace) continue article_ref = str(namespace).encode( 'utf-8') + b':' + page.page_title seen.add(article_ref) old_rating = old_ratings.get(article_ref) old_rating_value = None if old_rating: rating = Rating(**attr.asdict(old_rating)) if kind == AssessmentKind.QUALITY: old_rating_value = rating.r_quality elif kind == AssessmentKind.IMPORTANCE: old_rating_value = rating.r_importance else: rating = Rating(r_project=project.p_project, r_namespace=namespace, r_article=page.page_title, r_score=0) old_rating_value = NOT_A_CLASS.encode('utf-8') if kind == AssessmentKind.QUALITY: rating.r_quality = current_rating rating.set_quality_timestamp_dt(page.cl_timestamp) elif kind == AssessmentKind.IMPORTANCE: rating.r_importance = current_rating rating.set_importance_timestamp_dt(page.cl_timestamp) new_ratings[article_ref].append((rating, kind, old_rating_value)) n += 1 if n >= MAX_ARTICLES_BEFORE_COMMIT: wp10db.ping() wp10db.commit() logger.info('End, committing db') wp10db.ping() wp10db.commit() return (new_ratings, rating_to_category)