Ejemplo n.º 1
0
def make_reco(request):
    reco_request = json.loads(request.body.decode())
    reco_type = reco_request["type"]

    if request.user.is_authenticated() and reco_type == "general":
        source = UserSource(user=request.user)
    else:
        source = JsonSource(data=reco_request["prefs"], user=request.user)

    # Save pseudo preflist for anonymouse users. They can improve
    # recommendations for other users.
    if not request.user.is_authenticated():
        preflist = PrefList.objects.create()
        for movie in source.get_data():
            preflist.add(movie["id"], movie["rating"])

    rengine = RecoManager(source=source, engine=SlopeOne())
    reco = rengine.make_reco()

    if len(reco) > 0:
        reco_final = [
            movie for movie in reco if movie["rating"] > RECO_MIN_RATING
            and movie["freq"] > RECO_MIN_FREQ
        ]
        reco_final = sorted(reco_final,
                            key=lambda x: x["rating"],
                            reverse=True)
        reco_final = reco_final[0:min(len(reco_final), RECO_MAX_MOVIES)]

        reco_ids = [movie["id"] for movie in reco_final]
        movies = list(Movie.objects.values("id", "title")\
            .filter(id__in = reco_ids))
        movies = {movie["id"]: movie["title"] for movie in movies}

        for movie in reco_final:
            movie["title"] = movies.get(movie["id"])
            del movie["freq"]
    else:
        reco_final = []

    # substitute orginal reco with preparated by this view
    reco_db = rengine.save_reco(reco=reco_final)

    return JsonResponse(
        {
            "status": "OK",
            "movies": reco_final,
            "id": reco_db.id,
            "title": reco_db.title
        },
        safe=False)
Ejemplo n.º 2
0
    def test_get_reco_returns_reco_object(self, mock_engine):
        mock_engine.return_value.make_reco.return_value = self.reco
        for movie in self.reco + self.preferences:
            Movie.objects.create(id=movie["id"], title=movie["title"])

        reco = RecoManager(self.source).get_reco()
        reco_db = Reco.objects.first()
        self.assertEqual(reco, reco_db)
Ejemplo n.º 3
0
    def test_get_reco_calls_create_new_with_base_and_reco(
            self, mock_engine, mock_reco):
        mock_create = Mock()
        mock_reco.create_new = mock_create
        mock_engine.return_value.make_reco.return_value = self.reco

        RecoManager(self.source).get_reco()

        mock_create.assert_called_once_with(base=self.preferences,
                                            reco=self.reco,
                                            user=None)
Ejemplo n.º 4
0
    def test_get_reco_accepts_none_user(self, mock_reco):
        mock_create = Mock()
        mock_reco.create_new = mock_create

        RecoManager(self.source).get_reco()
Ejemplo n.º 5
0
    def test_get_reco_accepts_user_as_argument(self, mock_reco):
        mock_create = Mock()
        mock_reco.create_new = mock_create

        RecoManager(self.source).get_reco(User.objects.create())
Ejemplo n.º 6
0
 def test_raises_error_when_source_is_empty(self):
     rengine = RecoManager(JsonSource([]))
     with self.assertRaises(RecoSourceError):
         rengine.get_reco()
Ejemplo n.º 7
0
 def test_creates_base_for_reco_in_constructor(self):
     rengine = RecoManager(self.source)
     self.assertEqual(rengine.base, self.preferences)
Ejemplo n.º 8
0
    def test_make_reco_returns_list_of_movies_dict(self, mock_engine):
        mock_engine.return_value.make_reco.return_value = self.reco

        reco = RecoManager(self.source).make_reco()
        self.assertEqual(reco, self.reco)