def setup_recommendations_and_likes(self):
        """This method sets up recommendationss to be tested in 
            other test methods.
        """
        r1 = Recommendation(
            title="Fire Doughnuts in Canada",
            content=
            "Ever tried jerk flavored doughnuts?, then you haven't lived!. Marley's yard gotchu!",
            business_name="Marleys Yard",
            business_address="2345 67 Ave NW",
            business_city="Edmonton",
            business_state="Alberta",
            business_country="Canada",
            business_rating=4,
            user_id=self.testuser_id)

        r2 = Recommendation(
            id=365,
            title="Deep Dish Pizza in Edmonton, Canada",
            content=
            "Looking for delicious deep dish pizza?, Chicago 001 has the best on Whyte Ave",
            business_name="Chicago 001",
            business_address="1946 Whyte Ave NW",
            business_city="Edmonton",
            business_state="Alberta",
            business_country="Canada",
            business_rating=5,
            user_id=self.u2id)

        db.session.add_all([r1, r2])
        db.session.commit()

        l1 = Likes(user_id=self.testuser_id, recommendation_id=365)
        db.session.add(l1)
        db.session.commit()
예제 #2
0
    def test_delete_recommend_post(self):
        with self.client as client:
            with client.session_transaction() as sess:
                sess[KEY]=999

            new_user = User(
                id=1002,
                username='******',
                first_name='test',
                last_name='user3',
                password='******',
                email='*****@*****.**',
            )

            db.session.add(new_user)

            new_recommend = Recommendation(recommender_id=1002, recommend_to_user_id=999, drink_id='1')
            rec_id = 9999
            new_recommend.id = rec_id
            db.session.add(new_recommend)

            resp = client.post('/user/recommendations', json={'recId': 9999}, follow_redirects=True)

            self.assertIn("You don\\\'t have any recommendations yet", str(resp.data))
            
            del_new_user = User.query.get(1002)
            db.session.delete(del_new_user)
예제 #3
0
    def test_recommend_add_remove(self):

        new_user = User(
            username='******',
            first_name='rec',
            last_name='user',
            password='******',
            email='*****@*****.**',
        )
        new_user.id = 11114
        db.session.add(new_user)

        new_rec = Recommendation(recommender_id=11114,
                                 recommend_to_user_id=11111,
                                 drink_id=1)
        new_rec.id = 10000
        db.session.add(new_rec)

        rec = Recommendation.query.get(10000)

        self.assertEqual(rec.id, 10000)
        self.assertEqual(rec.recommender_id, 11114)
        self.assertEqual(rec.recommend_to_user_id, 11111)
        self.assertEqual(rec.drink_id, 1)

        db.session.delete(rec)
        db.session.commit()

        check_rec = Recommendation.query.get(10000)

        self.assertEqual(check_rec, None)
예제 #4
0
def _create_recommendation(user, offer, mediation=None):
    recommendation = Recommendation()
    recommendation.user = user

    if offer:
        recommendation.offer = offer
    else:
        offer = offer_queries.find_offer_by_id(mediation.offerId)
        recommendation.offer = offer

    if mediation:
        recommendation.mediation = mediation
    else:
        mediation = Mediation.query \
            .filter(Mediation.offer == offer) \
            .filter(Mediation.isActive) \
            .order_by(func.random()) \
            .first()
        recommendation.mediation = mediation

    if recommendation.mediation:
        recommendation.validUntilDate = datetime.utcnow() + timedelta(days=3)
    else:
        recommendation.validUntilDate = datetime.utcnow() + timedelta(days=1)

    if offer.lastStock and offer.lastStock.bookingLimitDatetime:
        recommendation.validUntilDate = min(
            recommendation.validUntilDate,
            offer.lastStock.bookingLimitDatetime - timedelta(minutes=1))

    PcObject.save(recommendation)
    return recommendation
예제 #5
0
    def get(self):
        """ Returns all Recommendations """

        type_name = request.args.get('type')
        product_id = request.args.get('product_id')
        results = []
        rec_type = None

        if type_name:
            rec_type = RecommendationType.find_by_name(type_name)
            if not rec_type:
                raise NotFound(
                    "Recommendations with type '{}' was not found.".format(
                        type_name))

        if rec_type and product_id:
            recs = Recommendation.find_by_product_id_and_type(
                product_id, rec_type)
        elif rec_type:
            recs = Recommendation.find_by_type(rec_type)
        elif product_id:
            recs = Recommendation.find_by_product_id(product_id)
        else:
            recs = Recommendation.all()

        results = [rec.serialize() for rec in recs if rec is not None]

        return results, status.HTTP_200_OK
예제 #6
0
def recommend(userid, model):
    global model_version
    global canary_version
    if model == 1:
        if userid in predictions.index:
            row = predictions.loc[userid]
            labels = row.nlargest(20).keys().tolist()
            result = ",".join(labels)
            app.logger.info(str(userid) + "->" + result)

            # Store the recommendation in database
            reco = Recommendation(user_id=userid,
                                  movie_ids=labels,
                                  model=model_version,
                                  model_num=1)
            createRecommendation(reco)

            return result
        else:
            app.logger.info("User not indexed. Returning defaults")

            # Store the recommendation in database
            reco = Recommendation(user_id=userid,
                                  movie_ids=getDefaults().split(','),
                                  model=model_version,
                                  model_num=1)
            createRecommendation(reco)

            # Return response
            return getDefaults()

    if model == 2:
        if userid in canary_predictions.index:
            row = predictions.loc[userid]
            labels = row.nlargest(20).keys().tolist()
            result = ",".join(labels)
            app.logger.info(str(userid) + "->" + result)

            # Store the recommendation in database
            reco = CanaryRecommendation(user_id=userid,
                                        movie_ids=labels,
                                        model=canary_version,
                                        model_num=2)
            createCanaryRecommendation(reco)

            return result
        else:
            app.logger.info("User not indexed. Returning defaults")

            # Store the recommendation in database
            reco = CanaryRecommendation(user_id=userid,
                                        movie_ids=getDefaults().split(','),
                                        model=canary_version,
                                        model_num=2)
            createCanaryRecommendation(reco)

            # Return response
            return getDefaults()
예제 #7
0
    def test_list_recommendations(self):
        """This test method tests to confirm that the logged in user can only
        view recommendations in their state and city."""

        L = Location(name="Home",
                     address="False Test Creek SW, Long Beach CA",
                     long=143.12,
                     lat=-234.5,
                     city="Long Beach",
                     state="CA",
                     user_id=self.testuser_id)
        L_id = 124
        L.id = L_id

        db.session.add(L)
        db.session.commit()

        r1 = Recommendation(
            title="Fire Doughnuts in LA",
            content=
            "Bro these mf doughnuts be smacking like gahdamn!. Check out Leonards bro!",
            business_name="Leonards",
            business_address="2345 Rodeo Ave",
            business_city="Long Beach",
            business_state="CA",
            business_country="US",
            business_rating=5,
            user_id=self.testuser_id)

        r2 = Recommendation(
            id=365,
            title="Deep Dish Pizza in Edmonton, Canada",
            content=
            "Looking for delicious deep dish pizza?, Chicago 001 has the best on Whyte Ave",
            business_name="Chicago 001",
            business_address="1946 Whyte Ave NW",
            business_city="Edmonton",
            business_state="Alberta",
            business_country="Canada",
            business_rating=5,
            user_id=self.u2id)

        db.session.add_all([r1, r2])
        db.session.commit()

        with self.client as c:
            with c.session_transaction() as sess:
                sess[CURR_USER_KEY] = self.testuser_id
                sess[CURR_LOCATION] = L_id

            resp = c.get("/recommendations/list")

            self.assertEqual(resp.status_code, 200)
            self.assertIn("Fire Doughnuts in LA", str(resp.data))
            self.assertNotIn("Deep Dish Pizza in Edmonton, Canada",
                             str(resp.data))
예제 #8
0
def create_recommendation(offer=None, user=None, mediation=None, idx=None, date_read=None,
                          valid_until_date=datetime.utcnow() + timedelta(days=7), search=None,
                          is_clicked=False):
    recommendation = Recommendation()
    recommendation.id = idx
    recommendation.offer = offer
    recommendation.user = user
    recommendation.mediation = mediation
    recommendation.dateRead = date_read
    recommendation.validUntilDate = valid_until_date
    recommendation.search = search
    recommendation.isClicked = is_clicked
    return recommendation
예제 #9
0
def create_tuto_mediation_if_non_existent_for_user(user, tuto_mediation):

    already_existing_tuto_recommendation = Recommendation.query\
        .filter_by(mediation=tuto_mediation, user=user)\
        .first()
    if already_existing_tuto_recommendation:
        return

    recommendation = Recommendation()
    recommendation.user = user
    recommendation.mediation = tuto_mediation
    recommendation.validUntilDate = datetime.utcnow() + timedelta(weeks=2)
    PcObject.save(recommendation)
예제 #10
0
def add_recommendation():
    """This view function renders the form to add a new recommendation
       if a GET request is made. 
       If form validates, it adds new recommendation to dateMeet db and redirect to recommendations page.
    """

    if not g.user:
        flash("Access unauthorized.", "danger")
        return redirect("/")

    form = UserRecommendationAddForm()

    if form.validate_on_submit():
        recommendation = Recommendation(
            title=form.title.data,
            content=form.content.data,
            business_name=form.business_name.data,
            business_address=form.business_address.data,
            business_city=form.business_city.data,
            business_state=form.business_state.data,
            business_country=form.business_country.data,
            business_rating=form.business_rating.data)
        g.user.recommendations.append(recommendation)
        db.session.commit()

        return redirect(f"/users/{g.user.id}")

    return render_template('recommendations/new.html', form=form)
 def _format_recommendation_response(
     response: dict, ) -> List[Recommendation]:
     if not response or not response.get('tracks'):
         raise NoContentFound(
             f'Invalid or missing content in response {response}')
     formatted_data = []
     recommendations = response['tracks']
     for recommendation in recommendations:
         formatted_data.append(
             Recommendation(
                 **{
                     'playlist':
                     recommendation['album']['name'],
                     'url':
                     recommendation['external_urls']['spotify'],
                     'artists': [
                         artist['name']
                         for artist in recommendation['artists']
                     ],
                     'total_tracks':
                     round(recommendation['album']['total_tracks']),
                     'duration_seconds':
                     round(recommendation['duration_ms'] / 60, 2),
                 }))
     return formatted_data
    def test_like_recommendation(self):
        """This test method tests to confirm that the 
            like recommendation functionality works.
        """
        r = Recommendation(
            id=3000,
            title="Jamaican Doughnuts in Canada",
            content="Jamaican Doughnuts are the bomb, got to Marley's on 67th!",
            business_name="Marleys Yard",
            business_address="2345 67 Ave NW",
            business_city="Edmonton",
            business_state="Alberta",
            business_country="Canada",
            business_rating=5,
            user_id=self.u2id)

        db.session.add(r)
        db.session.commit()

        with self.client as c:
            with c.session_transaction() as sess:
                sess[CURR_USER_KEY] = self.testuser_id

            resp = c.post("/recommendations/3000/like", follow_redirects=True)
            self.assertEqual(resp.status_code, 200)

            likes = Likes.query.filter(Likes.recommendation_id == 3000).all()
            self.assertEqual(len(likes), 1)
            self.assertEqual(likes[0].user_id, self.testuser_id)
예제 #13
0
    def test_delete_recommendation(self):
        """This test method tests to confirm that a recommendation is deleted when a delete request is sent by a logged in user."""

        r = Recommendation(
            id=419,
            title="Fire Doughnuts in LA",
            content=
            "Bro these mf doughnuts be smacking like gahdamn!. Check out Leonards bro!",
            business_name="Leonards",
            business_address="2345 Rodeo Ave",
            business_city="Long Beach",
            business_state="CA",
            business_country="US",
            business_rating=5,
            user_id=self.testuser_id)

        db.session.add(r)
        db.session.commit()

        with self.client as c:
            with c.session_transaction() as sess:
                sess[CURR_USER_KEY] = self.testuser_id

            resp = c.post("/recommendations/419/delete", follow_redirects=True)
            self.assertEqual(resp.status_code, 200)

            r = Recommendation.query.get(419)
            self.assertIsNone(r)
예제 #14
0
def recommend_song():
    data = request.get_json()

    if not data['to_user']:
        return jsonify({"msg": "Missing to_user parameter"}), 400
    if not data['from_user']:
        return jsonify({"msg": "Missing from_user parameter"}), 400
    if not data['song_id']:
        return jsonify({"msg": "Missing song_id parameter"}), 400

    to_user = data['to_user']
    from_user = data['from_user']
    song_id = data['song_id']
    message = data['message']

    try:
        new_recommendation = Recommendation(to_user=to_user,
                                            from_user=from_user,
                                            song_id=song_id,
                                            message=message)
        db.session.add(new_recommendation)
        db.session.commit()
        return jsonify({"msg": "Recommendation sent!"}), 201
    except Exception as e:
        print(e)
        return jsonify({"msg": "Something went wrong making the rec."}), 400
    def test_recommendation_likes(self):
        """This test method tests recommendation likes model"""

        r1 = Recommendation(
            title="Fire Doughnuts in Canada",
            content=
            "Ever tried jerk flavored doughnuts?, then you haven't lived!. Marley's yard gotchu!",
            business_name="Marleys Yard",
            business_address="2345 67 Ave NW",
            business_city="Edmonton",
            business_state="Alberta",
            business_country="Canada",
            business_rating=4,
            user_id=self.uid)

        u = User.register("User2", "Test", "*****@*****.**", "testuser2",
                          "passWord4u2", None, None)
        uid2 = 2222
        u.id = uid2

        db.session.add(r1)
        db.session.commit()

        u.likes.append(r1)

        db.session.commit()

        l = Likes.query.filter(Likes.user_id == uid2).all()

        # There should be only one liked message and it should be m1

        self.assertEqual(len(l), 1)
        self.assertEqual(l[0].recommendation_id, r1.id)
예제 #16
0
def make_blank_recommendations(user):
    firstrec = Recommendation(
        student_id=user.user_id,
        recommender_id=User.query.filter_by(email=user.rec1email,
                                            role=2).first().user_id)
    secondrec = Recommendation(
        student_id=user.user_id,
        recommender_id=User.query.filter_by(email=user.rec2email,
                                            role=2).first().user_id)
    thirdrec = Recommendation(
        student_id=user.user_id,
        recommender_id=User.query.filter_by(email=user.rec3email,
                                            role=2).first().user_id)
    db.session.add(firstrec)
    db.session.add(secondrec)
    db.session.add(thirdrec)
    db.session.commit()
예제 #17
0
def rec_detail(recommendation_id):
    """ Manage Recommendation Detail"""
    rec = Recommendation.find_by_id(recommendation_id)
    recJSON = rec.serialize()
    return render_template(
        'manage/detail.html',
        detail_id=recJSON["id"],
        product_id=recJSON["product_id"],
        rec_type=recJSON["rec_type"]["id"],
        rec_product_id=recJSON["rec_product_id"],
        weight=recJSON["weight"],
        status=recJSON["rec_type"]["is_active"]), status.HTTP_200_OK
예제 #18
0
def create_recommendations(recommendation_id):
    """ Create Recommendation View """
    data = request.get_json()
    rec = Recommendation()
    rec.deserialize(data)
    rec.save()
    message = rec.serialize()

    return render_template('manage.html', name="Manage",
                           result=message), status.HTTP_201_CREATED
예제 #19
0
def generate_recommendations():
    original = get_user_track_array()
    original_pd = pd.DataFrame(original)
    song_to_song = pd.DataFrame(index=original_pd.columns,
                                columns=original_pd.columns)

    for i in xrange(len(song_to_song.columns)):
        for j in xrange(len(song_to_song.columns)):
            similarity = 1 - cosine(original_pd.ix[:, i], original_pd.ix[:, j])
            song_to_song.ix[i, j] = similarity
            if math.isnan(similarity):
                song_to_song.ix[i, j] = 0

    data_neighbours = pd.DataFrame(index=song_to_song.columns,
                                   columns=[range(1, 11)])

    for i in range(0, len(song_to_song.columns)):
        data_neighbours.ix[i, :10] = song_to_song.ix[0:, i].sort_values(
            ascending=False)[:10].index

    data_sims = pd.DataFrame(index=original_pd.index,
                             columns=original_pd.columns)

    for i in range(0, len(data_sims.index)):
        for j in range(0, len(data_sims.columns)):
            user = data_sims.index[i]
            product = data_sims.columns[j]

            if original_pd.ix[i][j] == 1:
                data_sims.ix[i][j] = 0
            else:
                product_top_names = data_neighbours.ix[product][1:10]
                product_top_sims = song_to_song.ix[product].sort_values(
                    ascending=False)[1:10]
                user_purchases = original_pd.ix[user, product_top_names]

                data_sims.ix[i][j] = getScore(user_purchases, product_top_sims)

    result_dict = {}

    for i in xrange(len(data_sims.index)):
        top_matches = data_sims.ix[i, :].sort_values(
            ascending=False)[0:10].index.values
        result_dict[i + 1] = [x + 1 for x in top_matches]

    for user_id, track_ids in result_dict.items():
        recommendations = []
        for track_id in track_ids:
            recommendations.append(
                Recommendation(user_id=user_id, track_id=track_id))
        db.session.add_all(recommendations)
        db.session.commit()
def recommendations():
    return [
        Recommendation(
            **{
                'playlist': 'album name',
                'url': 'https://open.spotify.com/track/7GhIk7Il098yCjg4BQjzvb',
                'artists': ['artist_1', 'artist_2'],
                'total_tracks': 11,
                'duration_seconds': round(11700 / 60, 2),
            }
        )
        for _ in range(2)
    ]
def sanitize_recommendation(rec: Recommendation):
    description = rec.description
    description = description.replace('\n', ' ')
    tokens = []

    for word in description.split(' '):
        if is_symbol_name(word):
            tokens.append('{term}')
        elif word.endswith('%'):
            tokens.append('{per}')
        elif is_url(word):
            tokens.append(word)
        elif has_path(word):
            tokens.append('{path}')
        elif is_methodname(word):
            tokens.append('{method}')
        elif is_num(word):
            tokens.append('{num}')
        else:
            tokens.append(word)

    sanitized = ' '.join(tokens)
    rec.set_sanitized_descr(sanitized)
예제 #22
0
    def delete(self, recommendation_id):
        """ Delete a Recommendation

        This endpoint will delete a Recommendation based the id specified in the path
        """
        recommendation = Recommendation.find_by_id(recommendation_id)

        if not recommendation:
            raise NotFound("Recommendation with id '{}' was not found.".format(
                recommendation_id))

        recommendation.delete()

        return '', status.HTTP_204_NO_CONTENT
예제 #23
0
    def execute(self, user: User, data) -> Recommendation:
        if user is None:
            raise BadUserException()

        if data is None:
            raise BadArgException

        recommendation = Recommendation()
        recommendation.user_id = user.id
        recommendation.title = data.get('title')
        recommendation.content = data.get('content')
        recommendation.estimated_success_time = data.get(
            'estimated_success_time')
        recommendation.difficulty_level = data.get('difficulty_level')
        recommendation.benefit = data.get('benefit')
        recommendation.type = data.get('type')

        BaseObject.check_and_save(recommendation)

        return recommendation
예제 #24
0
    def put(self, recommendation_id):
        """ Update a Recommendation

        This endpoint will update a Recommendations based the body that is posted
        """
        recommendation = Recommendation.find_by_id(recommendation_id)

        if not recommendation:
            raise NotFound("Recommendation with id '{}' was not found.".format(
                recommendation_id))

        recommendation.deserialize(request.get_json())
        recommendation.id = recommendation_id
        recommendation.save()

        return recommendation.serialize(), status.HTTP_200_OK
예제 #25
0
    def get(self, recommendation_id):
        """ Retrieve a single Recommendation

        This endpoint will return a Recommendations based on it's id
        """
        recommendation = Recommendation.find_by_id(recommendation_id)

        recJSON = ""
        if not recommendation:
            raise NotFound(
                "Recommendations with id '{}' was not found.".format(
                    recommendation_id))
        else:
            recJSON = recommendation.serialize()

        return recJSON, status.HTTP_200_OK
예제 #26
0
    def test_unauthorized_recommendation_delete(self):
        """This test method confirms that an unauthorized user 
           cannot delete a recommendation.
        """

        # We create a separate user first
        u = User.register(first_name="Userrrr",
                          last_name="Test",
                          email="*****@*****.**",
                          username="******",
                          password="******",
                          image_url=None,
                          header_url=None)

        u.id = 589

        # Now we create a recommendation made by testuser

        r = Recommendation(
            id=419,
            title="Fire Doughnuts in LA",
            content=
            "Bro these mf doughnuts be smacking like gahdamn!. Check out Leonards bro!",
            business_name="Leonards",
            business_address="2345 Rodeo Ave",
            business_city="Long Beach",
            business_state="CA",
            business_country="US",
            business_rating=5,
            user_id=self.testuser_id)

        db.session.add_all([u, r])
        db.session.commit()

        with self.client as c:
            with c.session_transaction() as sess:
                sess[CURR_USER_KEY] = 589

            resp = c.post("/recommendations/419/delete", follow_redirects=True)
            self.assertEqual(resp.status_code, 200)
            self.assertIn("Access unauthorized.", str(resp.data))

            r = Recommendation.query.get(419)
            self.assertIsNotNone(r)
예제 #27
0
def recommend_form(drink_id):
    """Show form for posting recommendations, creates new recommendations from POST"""

    if 'user' not in session:
        flash('Please log in or sign up to send recommendations', 'danger')
        return redirect('/')

    form = RecommendForm()

    user_id = session['user']
    user = User.query.get(user_id)
    drink = Drink.query.get(drink_id)

    form = RecommendForm()
    form.recommend_to_name.choices = [
        (user.id, user.username)
        for user in User.query.filter(User.id != user_id).all()
    ]
    form.username.data = user.username
    form.drink.data = drink.name

    if request.method == 'POST':

        username = form.username.data
        recommend_to_id = form.recommend_to_name.data
        drink = form.drink.data

        user = User.query.filter(User.username == username).first()
        drink = Drink.query.filter(Drink.name == drink).first()

        new_recommend = Recommendation(recommender_id=user.id,
                                       recommend_to_user_id=recommend_to_id,
                                       drink_id=drink.id)
        db.session.add(new_recommend)
        db.session.commit()

        return redirect('/user/favorites')

    else:

        return render_template('form.html', form=form, drink_id=drink_id)
예제 #28
0
    def post(self):
        """ Creates a Recommendation

        This endpoint will create a Recommendations based the data in the body that is posted.
        We assume when a user ask for a recommendation they will provide a Product Id
        And a Type in the following format:
            { 'product_id': <int>, 'type': '<[up-sell|accessory|cross-sell]>' }
        """

        #data = request.get_json()
        rec = Recommendation()
        rec.deserialize(api.payload)
        rec.save()

        location_url = api.url_for(RecommendationResource,
                                   recommendation_id=rec.id,
                                   _external=True)

        return rec.serialize(), status.HTTP_201_CREATED, {
            'Location': location_url
        }
    def test_recommendation_model(self):
        """Does the basic recommendation model work?"""

        r = Recommendation(
            title="The best burger in the world",
            content=
            "The best burger in the entire world definitely is Marcos Famous DT Edmonton",
            business_name="Marcos Famous",
            business_address="13425 36 Ave NW",
            business_city="Edmonton",
            business_state="Alberta",
            business_country="Canada",
            business_rating=4,
            user_id=self.uid)

        db.session.add(r)
        db.session.commit()

        # User should have 1 message
        self.assertEqual(self.user.recommendations[0].business_country,
                         "Canada")
        self.assertEqual(self.user.recommendations[0].business_rating, 4)
        self.assertEqual(len(self.user.recommendations), 1)
예제 #30
0
파일: app.py 프로젝트: Eftn/Movie-DataBase
def add_like(username, movie_id):
    """Handle Likes"""
    find_fav(movie_id)
    favorites = find_fav(movie_id)

    #update the form data
    id = favorites['ID']
    title_id = favorites['Title']
    actors = favorites['Actors']
    plot = favorites['Plot']
    username = favorites['Username']

    #Movie Model Blueprint
    movie = Recommendation(id=(id),
                           title_id=(title_id),
                           actors=(actors),
                           plot=(plot),
                           username=(username))

    db.session.add(movie)

    db.session.commit()

    return redirect(f"/users/{username}")