[odrs-web] Use the component duplicates when getting app reviews or ratings
- From: Richard Hughes <rhughes src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [odrs-web] Use the component duplicates when getting app reviews or ratings
- Date: Fri, 5 Jul 2019 16:26:43 +0000 (UTC)
commit ca2b7a9e5eb6bed5152f6371942b49bb68bd6927
Author: Richard Hughes <richard hughsie com>
Date: Fri Jul 5 17:24:03 2019 +0100
Use the component duplicates when getting app reviews or ratings
app_data/cron.py | 12 +++++-------
app_data/odrs/models.py | 11 +++++++++++
app_data/odrs/util.py | 4 ++--
app_data/odrs/views_api.py | 39 ++++++++++++++++++++-------------------
4 files changed, 38 insertions(+), 28 deletions(-)
---
diff --git a/app_data/cron.py b/app_data/cron.py
index 1d1483f..891edbb 100755
--- a/app_data/cron.py
+++ b/app_data/cron.py
@@ -18,7 +18,7 @@ from lxml import etree as ET
from odrs import db
from odrs.models import Review, Taboo, Component
-from odrs.util import _get_rating_for_app_id, _get_taboos_for_locale
+from odrs.util import _get_rating_for_component, _get_taboos_for_locale
def _auto_delete(days=31):
@@ -38,14 +38,12 @@ def _auto_delete(days=31):
def _regenerate_ratings(fn):
item = {}
-
- app_ids = [res[0] for res in db.session.query(Component.app_id).\
- order_by(Component.app_id.asc()).all()]
- for app_id in app_ids:
- ratings = _get_rating_for_app_id(app_id, 2)
+ for component in db.session.query(Component).\
+ order_by(Component.app_id.asc()).all():
+ ratings = _get_rating_for_component(component, 2)
if len(ratings) == 0:
continue
- item[app_id] = ratings
+ item[component.app_id] = ratings
# dump to file
with open(fn, 'w') as outfd:
diff --git a/app_data/odrs/models.py b/app_data/odrs/models.py
index de7f994..4406dc2 100644
--- a/app_data/odrs/models.py
+++ b/app_data/odrs/models.py
@@ -173,6 +173,17 @@ class Component(db.Model):
return adopted
+ @property
+ def app_ids(self):
+ app_ids = [self.app_id]
+ if self.parent:
+ if self.parent.app_id not in app_ids:
+ app_ids.append(self.parent.app_id)
+ for child in self.children:
+ if child.app_id not in app_ids:
+ app_ids.append(child.app_id)
+ return app_ids
+
def __repr__(self):
return 'Component object %s' % self.component_id
diff --git a/app_data/odrs/util.py b/app_data/odrs/util.py
index 34d1ea7..858de2d 100644
--- a/app_data/odrs/util.py
+++ b/app_data/odrs/util.py
@@ -59,7 +59,7 @@ def _eventlog_add(user_addr=None,
db.session.add(Event(user_addr, user_id, app_id, message, important))
db.session.commit()
-def _get_rating_for_app_id(app_id, min_total=1):
+def _get_rating_for_component(component, min_total=1):
""" Gets the ratings information for the application """
from odrs import db
from odrs.models import Review, Component
@@ -68,7 +68,7 @@ def _get_rating_for_app_id(app_id, min_total=1):
array = [0] * 6
for rating in db.session.query(Review.rating).\
join(Component).\
- filter(Component.app_id == app_id).all():
+ filter(Component.app_id.in_(component.app_ids)).all():
idx = int(rating[0] / 20)
if idx > 5:
continue
diff --git a/app_data/odrs/views_api.py b/app_data/odrs/views_api.py
index 201fc68..6edd8c9 100644
--- a/app_data/odrs/views_api.py
+++ b/app_data/odrs/views_api.py
@@ -23,7 +23,7 @@ from odrs import app, db
from .models import Review, User, Vote, Analytic, Taboo, Component
from .models import _vote_exists
from .util import json_success, json_error, _locale_is_compatible, _eventlog_add, _get_user_key,
_get_datestr_from_dt
-from .util import _sanitised_version, _sanitised_summary, _sanitised_description, _get_rating_for_app_id
+from .util import _sanitised_version, _sanitised_summary, _sanitised_description, _get_rating_for_component
from .util import _get_taboos_for_locale
ODRS_REPORTED_CNT = 2
@@ -162,11 +162,14 @@ def api_show_app(app_id, user_hash=None):
"""
Return details about an application.
"""
- reviews = db.session.query(Review).\
- join(Component).\
- filter(Component.app_id == app_id).\
- filter(Review.reported < ODRS_REPORTED_CNT).\
- order_by(Review.date_created.desc()).all()
+ reviews = []
+ component = db.session.query(Component).filter(Component.app_id == app_id).first()
+ if component:
+ reviews = db.session.query(Review).\
+ join(Component).\
+ filter(Component.app_id.in_(component.app_ids)).\
+ filter(Review.reported < ODRS_REPORTED_CNT).\
+ order_by(Review.date_created.desc()).all()
items = [review.asdict(user_hash) for review in reviews]
dat = json.dumps(items, sort_keys=True, indent=4, separators=(',', ': '))
return Response(response=dat,
@@ -216,12 +219,9 @@ def api_fetch():
if 'compat_ids' in item:
app_ids.extend(item['compat_ids'])
if component:
- if component.parent:
- if component.parent.app_id not in app_ids:
- app_ids.append(component.parent.app_id)
- for child in component.children:
- if child.app_id not in app_ids:
- app_ids.append(child.app_id)
+ for app_id in component.app_ids:
+ if app_id not in app_ids:
+ app_ids.append(app_id)
reviews = db.session.query(Review).\
join(Component).\
filter(Component.app_id.in_(app_ids)).\
@@ -464,7 +464,10 @@ def api_rating_for_id(app_id):
"""
Get the star ratings for a specific application.
"""
- ratings = _get_rating_for_app_id(app_id)
+ ratings = []
+ component = db.session.query(Component).filter(Component.app_id == app_id).first()
+ if component:
+ ratings = _get_rating_for_component(component)
dat = json.dumps(ratings, ensure_ascii=False, sort_keys=True, indent=4, separators=(',', ': '))
return Response(response=dat,
status=200, \
@@ -491,14 +494,12 @@ def api_ratings():
Get the star ratings for all known applications.
"""
item = {}
- app_ids = [res[0] for res in db.session.query(Component.app_id).\
- order_by(Component.app_id.asc()).\
- distinct(Component.app_id).all()]
- for app_id in app_ids:
- ratings = _get_rating_for_app_id(app_id, 2)
+ for component in db.session.query(Component).\
+ order_by(Component.app_id.asc()).all():
+ ratings = _get_rating_for_component(component, 2)
if len(ratings) == 0:
continue
- item[app_id] = ratings
+ item[component.app_id] = ratings
dat = json.dumps(item, sort_keys=True, indent=4, separators=(',', ': '))
return Response(response=dat,
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]