Skip to content

Commit

Permalink
Updates based on review.
Browse files Browse the repository at this point in the history
  • Loading branch information
tfnribeiro committed Oct 7, 2024
1 parent 30e56d9 commit 5df374a
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 44 deletions.
14 changes: 7 additions & 7 deletions tools/migrations/24-04-22-5-new_topic_user_feedback.sql
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
CREATE TABLE `zeeguu_test`.`new_topic_user_feedback` (
CREATE TABLE `zeeguu_test`.`article_topic_user_feedback` (
`id` INT NOT NULL AUTO_INCREMENT,
`article_id` INT NOT NULL,
`new_topic_id` INT NOT NULL,
`user_id` INT NOT NULL,
`feedback` VARCHAR(50),
PRIMARY KEY (`id`),
INDEX `new_topic_user_feedback_ibfk_1_idx` (`article_id` ASC) VISIBLE,
INDEX `new_topic_user_feedback_ibfk_2_idx` (`new_topic_id` ASC) VISIBLE,
INDEX `new_topic_user_feedback_ibfk_3_idx` (`user_id` ASC) VISIBLE,
CONSTRAINT `new_topic_user_feedback_ibfk_1` FOREIGN KEY (`article_id`) REFERENCES `zeeguu_test`.`article` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `new_topic_user_feedback_ibfk_2` FOREIGN KEY (`new_topic_id`) REFERENCES `zeeguu_test`.`new_topic` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `new_topic_user_feedback_ibfk_3` FOREIGN KEY (`user_id`) REFERENCES `zeeguu_test`.`user` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
INDEX `article_topic_user_feedback_ibfk_1_idx` (`article_id` ASC) VISIBLE,
INDEX `article_topic_user_feedback_ibfk_2_idx` (`new_topic_id` ASC) VISIBLE,
INDEX `article_topic_user_feedback_ibfk_3_idx` (`user_id` ASC) VISIBLE,
CONSTRAINT `article_topic_user_feedback_ibfk_1` FOREIGN KEY (`article_id`) REFERENCES `zeeguu_test`.`article` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `article_topic_user_feedback_ibfk_2` FOREIGN KEY (`new_topic_id`) REFERENCES `zeeguu_test`.`new_topic` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `article_topic_user_feedback_ibfk_3` FOREIGN KEY (`user_id`) REFERENCES `zeeguu_test`.`user` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
);
20 changes: 14 additions & 6 deletions zeeguu/api/endpoints/article.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import flask
from flask import request
from zeeguu.core.model import Article, Language, User, NewTopic
from zeeguu.core.model.new_topic_user_feedback import NewTopicUserFeedback
from zeeguu.core.model.new_topic_user_feedback import ArticleTopicUserFeedback
from zeeguu.api.utils import json_result
from zeeguu.core.model.personal_copy import PersonalCopy
from sqlalchemy.orm.exc import NoResultFound
Expand Down Expand Up @@ -112,25 +112,33 @@ def is_article_language_supported():
except:
return "NO"


@api.route("/remove_ml_suggestion", methods=["POST"])
@cross_domain
@requires_session
def remove_ml_suggestion():
"""
Saves a user feedback to remove a ML prediction
of the new topics. Can indicate that the prediciton
isn't correct.
Saves a user feedback to remove a ML prediction
of the new topics. Can indicate that the prediciton
isn't correct.
"""
user = User.find_by_id(flask.g.user_id)
article_id = request.form.get("article_id", "")
new_topic = request.form.get("new_topic", "")
article = Article.find_by_id(article_id)
new_topic = NewTopic.find(new_topic)
try:
NewTopicUserFeedback.find_or_create(db_session, article, user, new_topic, NewTopicUserFeedback.DO_NOT_SHOW_FEEDBACK)
ArticleTopicUserFeedback.find_or_create(
db_session,
article,
user,
new_topic,
ArticleTopicUserFeedback.DO_NOT_SHOW_FEEDBACK,
)
return "OK"
except Exception as e:
from sentry_sdk import capture_exception

capture_exception(e)
print(e)
return "Something went wrong!"
return "Something went wrong!"
8 changes: 3 additions & 5 deletions zeeguu/api/endpoints/topics.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def subscribe_to_new_topic_with_id():
topic_object = NewTopic.find_by_id(new_topic_id)
user = User.find_by_id(flask.g.user_id)
NewTopicSubscription.find_or_create(db_session, user, topic_object)

db_session.commit()
return "OK"


Expand Down Expand Up @@ -242,18 +242,16 @@ def get_available_new_topics():
"""
topic_data = []
user = User.find_by_id(flask.g.user_id)
already_filtered = [each.new_topic for each in NewTopicFilter.all_for_user(user)]
already_subscribed = [
each.new_topic for each in NewTopicSubscription.all_for_user(user)
]

topics = NewTopic.get_all_topics()

for topic in topics:
if (topic not in already_filtered) and (topic not in already_subscribed):
if topic not in already_subscribed:
topic_data.append(topic.as_dictionary())
print(already_filtered)
print(already_subscribed)

return json_result(topic_data)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,20 @@

from zeeguu.core.model import db

class NewTopicUserFeedback(db.Model):
"""

A topic subscription is created when
the user subscribed to a particular topic.
This is then taken into account in the
mixed recomemmder, when retrieving articles.
class ArticleTopicUserFeedback(db.Model):
"""
This table allows for users to provide feedback to a specific Topic assigned to an
article. Currently, this is only used for users to remove Inferred topics in the
frontend. We can then have a process to review these and improve our method to
infer topics.
Eventually, this could be extended to support other types of feedback.
"""

__table_args__ = {"mysql_collate": "utf8_bin"}
__tablename__ = "new_topic_user_feedback"
__tablename__ = "article_topic_user_feedback"

DO_NOT_SHOW_FEEDBACK = "DO_NOT_SHOW"

id = db.Column(db.Integer, primary_key=True)
Expand Down Expand Up @@ -51,29 +52,39 @@ def __str__(self):
def find_or_create(cls, session, article, user, topic, feedback):
try:
return (
cls.query.filter(cls.article == article).filter(cls.user == user).filter(cls.new_topic == topic).filter(cls.article == article).filter(cls.feedback == feedback).one()
cls.query.filter(cls.article == article)
.filter(cls.user == user)
.filter(cls.new_topic == topic)
.filter(cls.article == article)
.filter(cls.feedback == feedback)
.one()
)
except sqlalchemy.orm.exc.NoResultFound:
new = cls(article, user, topic, feedback)
session.add(new)
session.commit()
print("Commitned new row: ", new, " to new_topic_user_feedback")
print("Commitned new row: ", new, " to article_topic_user_feedback")
return new

@classmethod
def find_given_user_article(cls, article:Article, user:User):
def find_given_user_article(cls, article: Article, user: User):
try:
return cls.query.filter_by(user=user, article=article).all()
return cls.query.filter_by(user=user, article=article).all()
except sqlalchemy.orm.exc.NoResultFound:
return None

@classmethod
def all_for_user(cls, user):
return cls.query.filter(cls.user == user).all()

@classmethod
def all_for_user_as_list_w_feedback(cls, user, feedback):
return [user_feedback for user_feedback in cls.query.filter(cls.user == user).filter(cls.feedback == feedback).all()]
return [
user_feedback
for user_feedback in cls.query.filter(cls.user == user)
.filter(cls.feedback == feedback)
.all()
]

@classmethod
def with_id(cls, i):
Expand Down
1 change: 0 additions & 1 deletion zeeguu/core/model/new_topic_subscription.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ def find_or_create(cls, session, user, topic):
except sqlalchemy.orm.exc.NoResultFound:
new = cls(user, topic)
session.add(new)
session.commit()
return new

@classmethod
Expand Down
27 changes: 17 additions & 10 deletions zeeguu/core/model/user_article.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from sqlalchemy.orm.exc import NoResultFound

from zeeguu.core.model import Article, User
from zeeguu.core.model.new_topic_user_feedback import NewTopicUserFeedback
from zeeguu.core.model.new_topic_user_feedback import ArticleTopicUserFeedback
from zeeguu.core.model.article_difficulty_feedback import ArticleDifficultyFeedback
from zeeguu.core.model.personal_copy import PersonalCopy
from zeeguu.core.util.encoding import datetime_to_json
Expand Down Expand Up @@ -170,12 +170,11 @@ def all_liked_articles_of_user(cls, user):
return (
cls.query.filter_by(user=user).filter(UserArticle.liked.isnot(False)).all()
)

@classmethod
def all_liked_articles_of_user_by_id(cls, user_id):
return (
cls.query
.filter(UserArticle.user_id == user_id)
cls.query.filter(UserArticle.user_id == user_id)
.filter(UserArticle.liked == True)
.all()
)
Expand All @@ -194,7 +193,6 @@ def all_starred_or_liked_articles_of_user(cls, user, limit=30):

@classmethod
def all_starred_articles_of_user_info(cls, user):

"""
prepares info as it is promised by /get_starred_articles
Expand Down Expand Up @@ -222,7 +220,6 @@ def all_starred_articles_of_user_info(cls, user):

@classmethod
def all_starred_and_liked_articles_of_user_info(cls, user):

"""
prepares info as it is promised by /get_starred_articles
Expand Down Expand Up @@ -261,12 +258,20 @@ def user_article_info(

user_diff_feedback = ArticleDifficultyFeedback.find(user, article)

user_new_topics_feedback = NewTopicUserFeedback.find_given_user_article(article, user)

user_new_topics_feedback = ArticleTopicUserFeedback.find_given_user_article(
article, user
)

if user_new_topics_feedback:
article_topic_list = returned_info["new_topics_list"]
new_topic_list = []
topics_to_remove = set([untf.new_topic.title for untf in user_new_topics_feedback if untf.feedback == NewTopicUserFeedback.DO_NOT_SHOW_FEEDBACK])
topics_to_remove = set(
[
untf.new_topic.title
for untf in user_new_topics_feedback
if untf.feedback == ArticleTopicUserFeedback.DO_NOT_SHOW_FEEDBACK
]
)
for each in article_topic_list:
title, _ = each
if title not in topics_to_remove:
Expand All @@ -290,7 +295,9 @@ def user_article_info(
)

if user_diff_feedback is not None:
returned_info["relative_difficulty"] = user_diff_feedback.difficulty_feedback
returned_info["relative_difficulty"] = (
user_diff_feedback.difficulty_feedback
)

if with_translations:
translations = Bookmark.find_all_for_user_and_article(user, article)
Expand Down

0 comments on commit 5df374a

Please sign in to comment.