Example #1
0
def favorite_add(request, app_label, object_name, object_id):  #FIXME factor
    """Renders a formular to get confirmation to favorite the
    object represented by `app_label`, `object_name` and `object_id`
    creation. It raise a 400 exception if there is not such object.
    If it's a `POST` creates the favorite object if there isn't
    a favorite already. If validation fails it renders an
    insightful error message. If the validation succeed the favorite is
    added and a redirection is returned. If the object is already a favorite
    renders a message.

    :template favorites/favorite_already_favorite.html: - ``object`` object targeted by the favorite add operation.
                                                        - ``next`` value returned by :func:`favorites.views._get_next`.
                                                        - ``favorite`` :class:`favorites.models.Favorite`.

    :template favorites/favorite_add.html: - ``form`` :class:`favorites.forms.UserFolderChoicesForm` instance
                                           - ``object`` object targeted by the favorite add operation.
                                           - ``next`` value returned by :func:`favorites.views._get_next`.
    """

    # Is it a valid object ?
    instance_or_response = get_object_or_400_response(app_label, object_name, object_id)
    if isinstance(instance_or_response, HttpResponse):
        return instance_or_response  # the object is not found can be unknown
                                     # model or unknown object
    else:
        # it's a known object
        instance = instance_or_response
        favorites = Favorite.objects.favorites_for_object(instance, request.user)
        # is it already favorited by the user
        if favorites:
            # user already has this object as favorite
            favorite = favorites[0]
            ctx = {'object': instance, 'next': _get_next(request), 'favorite': favorite}
            return render(request, 'favorites/favorite_already_favorite.html', ctx)
        else:
            # init folder_choices for UserFolderChoicesForm validation or rendering
            query = Folder.objects.filter(user=request.user)
            folder_choices = query.order_by('name').values_list('pk', 'name')
            if request.method == 'POST':
                form = UserFolderChoicesForm(choices=folder_choices, data=request.POST)
                if form.is_valid():
                    folder_id = form.cleaned_data['folder_id']
                    if folder_id:
                        # form is valid hence the folder exists
                        folder = Folder.objects.get(pk=folder_id)
                    else:
                        folder = None
                    Favorite.objects.create_favorite(instance, request.user, folder)
                    return redirect(_get_next(request))
            else:  # GET
                form = UserFolderChoicesForm(choices=folder_choices)
            # if form is not valid or if it's a GET request
            ctx = {'form': form, 'object': instance, 'next':_get_next(request)}
            return render(request, 'favorites/favorite_add.html', ctx)
Example #2
0
def favorite_delete_for_object(request,
                               app_label,
                               object_name,
                               object_id):
    """Renders a formular to get confirmation to unfavorite the object
    represented by `app_label`, `object_name` and `object_id`. It raise a 404
    exception if there is no such object. If the action is successful the user
    is redirect using :func:`favorites.views._get_next`."""
    # Is it a valid object ?
    instance_or_response = get_object_or_400_response(app_label, object_name, object_id)
    if isinstance(instance_or_response, HttpResponse):
        # the object is not found can be unknown model or unknown object
        return instance_or_response
    else:
        instance = instance_or_response
        favorites = Favorite.objects.favorites_for_object(instance, request.user)
        if not favorites:
            # user has no favorite for this object
            return HttpResponseNotFound()
        else:
            favorite = favorites[0]
            return redirect(reverse('favorites:favorite_delete', kwargs={'object_id': favorite.pk}))
Example #3
0
def favorite_delete_for_object(request, app_label, object_name, object_id):
    """Renders a formular to get confirmation to unfavorite the object
    represented by `app_label`, `object_name` and `object_id`. It raise a 404
    exception if there is no such object. If the action is successful the user
    is redirect using :func:`favorites.views._get_next`."""
    # Is it a valid object ?
    instance_or_response = get_object_or_400_response(app_label, object_name,
                                                      object_id)
    if isinstance(instance_or_response, HttpResponse):
        # the object is not found can be unknown model or unknown object
        return instance_or_response
    else:
        instance = instance_or_response
        favorites = Favorite.objects.favorites_for_object(
            instance, request.user)
        if not favorites:
            # user has no favorite for this object
            return HttpResponseNotFound()
        else:
            favorite = favorites[0]
            return redirect(
                reverse('favorites:favorite_delete',
                        kwargs={'object_id': favorite.pk}))
Example #4
0
def favorite_add(request, app_label, object_name, object_id):  #FIXME factor
    """Renders a formular to get confirmation to favorite the
    object represented by `app_label`, `object_name` and `object_id`
    creation. It raise a 400 exception if there is not such object.
    If it's a `POST` creates the favorite object if there isn't
    a favorite already. If validation fails it renders an
    insightful error message. If the validation succeed the favorite is
    added and a redirection is returned. If the object is already a favorite
    renders a message.

    :template favorites/favorite_already_favorite.html: - ``object`` object targeted by the favorite add operation.
                                                        - ``next`` value returned by :func:`favorites.views._get_next`.
                                                        - ``favorite`` :class:`favorites.models.Favorite`.

    :template favorites/favorite_add.html: - ``form`` :class:`favorites.forms.UserFolderChoicesForm` instance
                                           - ``object`` object targeted by the favorite add operation.
                                           - ``next`` value returned by :func:`favorites.views._get_next`.
    """

    # Is it a valid object ?
    instance_or_response = get_object_or_400_response(app_label, object_name,
                                                      object_id)
    if isinstance(instance_or_response, HttpResponse):
        return instance_or_response  # the object is not found can be unknown
        # model or unknown object
    else:
        # it's a known object
        instance = instance_or_response
        favorites = Favorite.objects.favorites_for_object(
            instance, request.user)
        # is it already favorited by the user
        if favorites:
            # user already has this object as favorite
            favorite = favorites[0]
            ctx = {
                'object': instance,
                'next': _get_next(request),
                'favorite': favorite
            }
            return render(request, 'favorites/favorite_already_favorite.html',
                          ctx)
        else:
            # init folder_choices for UserFolderChoicesForm validation or rendering
            query = Folder.objects.filter(user=request.user)
            folder_choices = query.order_by('name').values_list('pk', 'name')
            if request.method == 'POST':
                form = UserFolderChoicesForm(choices=folder_choices,
                                             data=request.POST)
                if form.is_valid():
                    folder_id = form.cleaned_data['folder_id']
                    if folder_id:
                        # form is valid hence the folder exists
                        folder = Folder.objects.get(pk=folder_id)
                    else:
                        folder = None
                    Favorite.objects.create_favorite(instance, request.user,
                                                     folder)
                    return redirect(_get_next(request))
            else:  # GET
                form = UserFolderChoicesForm(choices=folder_choices)
            # if form is not valid or if it's a GET request
            ctx = {
                'form': form,
                'object': instance,
                'next': _get_next(request)
            }
            return render(request, 'favorites/favorite_add.html', ctx)