Ejemplo n.º 1
0
 def test_post_favorites2__fail(self):
     user = add_user(USER1, PASS1)
     data = {"data": {"type": "favorite", "attributes": {"item_uuid": ""}}}
     resp = open_with_auth(self.app, API_ENDPOINT.format('favorites/'),
                           'POST', user.email, PASS1, 'application/json',
                           json.dumps(data))
     assert resp.status_code == BAD_REQUEST
     assert Favorite.select().count() == 0
Ejemplo n.º 2
0
 def test_post_favorites__success(self):
     user = add_user(USER1, PASS1)
     item = add_item()
     favorite = json_favorite(str(item.uuid))
     data = format_jsonapi_request('favorite', favorite)
     resp = open_with_auth(self.app, API_ENDPOINT.format('favorites/'),
                           'POST', user.email, PASS1, 'application/json',
                           json.dumps(data))
     assert resp.status_code == CREATED
     assert Favorite.select().count() == 1
Ejemplo n.º 3
0
    def test_delete_favorites__success(self):
        user = add_user(USER1, PASS1)
        item = add_item()
        favorite = add_favorite(user, item)
        user_path = 'favorites/{}'.format(str(favorite.uuid))
        resp = open_with_auth(self.app, API_ENDPOINT.format(user_path),
                              'DELETE', user.email, PASS1, None, None)

        assert resp.status_code == OK
        assert Favorite.select().count() == 0
Ejemplo n.º 4
0
    def test_delete_alien_favorites__fail(self):
        user1 = add_user(USER1, PASS1)
        user2 = add_user(USER2, PASS2)
        item = add_item()
        favorite = add_favorite(user2, item)
        user_path = 'favorites/{}'.format(str(favorite.uuid))
        resp = open_with_auth(self.app, API_ENDPOINT.format(user_path),
                              'DELETE', user1.email, PASS1, None, None)

        assert resp.status_code == NOT_FOUND
        assert Favorite.select().count() == 1
Ejemplo n.º 5
0
def get_one_media(id):

	try:
		media = model_to_dict(Media.get_by_id(id))

		comments = Comment.select().where(Comment.media_id == media['id'])
		comments_dict = [model_to_dict(comment, exclude=[Comment.user_id.password]) for comment in comments]
		media['comments'] = comments_dict

		favorites = Favorite.select().where(Favorite.media_id == media['id'])
		favorites_dict = [model_to_dict(favorite, exclude=[Comment.user_id.password]) for favorite in favorites]
		media['favorites'] = favorites_dict

		return jsonify(data=media, status={"code": 200, "message": "Success"})

	except Media.DoesNotExist:
		return jsonify(data={}, status={"code": 401, "message": "There was an error getting the resource"})
Ejemplo n.º 6
0
 def test_post_favorites__fail(self):
     user = add_user(USER1, PASS1)
     data = {
         "data": {
             "type": "favorite",
             "attributes": {
                 "item_uuid": "2aabf825-40b3-03d5-e686-9eaebd156c0e"
             }
         }
     }
     resp = open_with_auth(self.app, API_ENDPOINT.format('favorites/'),
                           'POST', user.email, PASS1, 'application/json',
                           json.dumps(data))
     assert resp.status_code == NOT_FOUND
     assert Favorite.select().count() == 0
     expected_result = EXPECTED_RESULTS['post_favorites__fail']
     assert_valid_response(resp.data, expected_result)
Ejemplo n.º 7
0
 def has_favorites(self):
     from models import Favorite
     return Favorite.select().where(Favorite.user == self).count() > 0
Ejemplo n.º 8
0
def show_user(id):

    try:

        user = User.get_by_id(id)

        medias = Media.select().where(Media.user_id == id)

        medias_dict = [model_to_dict(media) for media in medias]

        for media in medias_dict:
            comments = Comment.select().where(Comment.media_id == media['id'])
            comments_dict = [
                model_to_dict(comment, exclude=[Comment.user_id.password])
                for comment in comments
            ]
            media['comments'] = comments_dict

            favorites = Favorite.select().where(
                Favorite.media_id == media['id'])
            favorites_dict = [
                model_to_dict(favorite, exclude=[Favorite.user_id.password])
                for favorite in favorites
            ]
            media['favorites'] = favorites_dict

        favorites = Favorite.select().where(Favorite.user_id == id)

        favorites_media = [favorite.media_id for favorite in favorites]

        fav_dict = [model_to_dict(favorite) for favorite in favorites_media]

        for fav in fav_dict:
            comments = Comment.select().where(Comment.media_id == fav['id'])
            comments_dict = [
                model_to_dict(comment, exclude=[Comment.user_id.password])
                for comment in comments
            ]
            fav['comments'] = comments_dict

            favorites = Favorite.select().where(Favorite.media_id == fav['id'])
            favorites_dict = [
                model_to_dict(favorite, exclude=[Favorite.user_id.password])
                for favorite in favorites
            ]
            fav['favorites'] = favorites_dict

        user_dict = model_to_dict(user)

        user_dict['posted_media'] = medias_dict
        user_dict['favorited_media'] = fav_dict

        del user_dict['password']

        # 	#################
        # 	# Need to join with users posted media (with comments and fav counts), plus join to user's favorited media (with comments, fav counts). Also, comments need to join with user to associate username with each comment.
        # 	#################

        return jsonify(data=user_dict,
                       status={
                           'code': 200,
                           'message': 'User found on resource.'
                       })

    except User.DoesNotExist:

        return jsonify(data={},
                       status={
                           'code': 401,
                           'message': 'User not found on resource.'
                       })
Ejemplo n.º 9
0
def get_all_favs():

	favorites = [model_to_dict(favorite) for favorite in Favorite.select()]

	return jsonify(data=favorites, status={'code': 201, 'message': 'Success'})