def user_favorite_add(request): cat_favorite_form = CatFavoriteForm(request.data) profile = request.get_user().catprofile content = { 'error': 2 } if cat_favorite_form.is_valid(): try: cat_user_favorite = CatUserFavorite( album=cat_favorite_form.cleaned_data['album'], photo=cat_favorite_form.cleaned_data['photo'], profile_id=profile.id ) cat_user_favorite.save() content = _get_user_data(request, add_favorite=cat_user_favorite) except IntegrityError: content = _get_user_data(request) content['error'] = 0 content['favoriteCount'] = CatUserFavorite.objects.filter(album=cat_favorite_form.cleaned_data['album'], photo=cat_favorite_form.cleaned_data['photo']).count() return Response(content)
def user_favorite_remove(request): cat_favorite_form = CatFavoriteForm(request.data) profile = request.get_user().catprofile content = { 'error': 2 } if cat_favorite_form.is_valid(): try: cat_user_favorite = CatUserFavorite.objects.get( album=cat_favorite_form.cleaned_data['album'], photo=cat_favorite_form.cleaned_data['photo'], profile_id=profile.id ) user_favorite_id = deepcopy(cat_user_favorite.id) cat_user_favorite.delete() content = _get_user_data(request, remove_favorite=user_favorite_id) except ObjectDoesNotExist: content = _get_user_data(request) content['error'] = 2 content['favoriteCount'] = CatUserFavorite.objects.filter(album=cat_favorite_form.cleaned_data['album'], photo=cat_favorite_form.cleaned_data['photo']).count() return Response(content)