Пример #1
0
def favorite_move(request, object_id):
    """Renders a formular to move a favorite to another folder. If the action is successful the user
    is redirect using :func:`favorites.views._get_next`.

    :template favorites/favorite_move.html: - ``favorite`` :class:`favorites.models.Favorite` instance target of the operation.
                                            - ``next`` value returned by :func:`favorites.views._get_next`.
                                            - ``form`` :class:`favorites.forms.UserFolderChoicesForm` instance.
    """
    favorite = get_object_or_404(Favorite, pk=object_id)
    # check credentials
    if not favorite.user == request.user:
        return HttpResponseForbidden()
    else:
        # init folder choices for form
        folder_choices = Folder.objects.filter(user=request.user).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 == '':
                    folder = None
                else:
                    folder = get_object_or_404(Folder, pk=folder_id)
                favorite.folder = folder
                favorite.save()
                return redirect(_get_next(request))
        else:
            folder_id = favorite.folder.pk if favorite.folder else ''
            form = UserFolderChoicesForm(choices=folder_choices, initial={'folder_id': folder_id})
        # form is not valid or it's a GET request
        ctx = {'favorite': favorite, 'next': _get_next(request), 'form': form}
        return render(request, 'favorites/favorite_move.html', ctx)
Пример #2
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)
Пример #3
0
def favorite_move(request, object_id):
    """Renders a formular to move a favorite to another folder. If the action is successful the user
    is redirect using :func:`favorites.views._get_next`.

    :template favorites/favorite_move.html: - ``favorite`` :class:`favorites.models.Favorite` instance target of the operation.
                                            - ``next`` value returned by :func:`favorites.views._get_next`.
                                            - ``form`` :class:`favorites.forms.UserFolderChoicesForm` instance.
    """
    favorite = get_object_or_404(Favorite, pk=object_id)
    # check credentials
    if not favorite.user == request.user:
        return HttpResponseForbidden()
    else:
        # init folder choices for form
        folder_choices = Folder.objects.filter(
            user=request.user).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 == '':
                    folder = None
                else:
                    folder = get_object_or_404(Folder, pk=folder_id)
                favorite.folder = folder
                favorite.save()
                return redirect(_get_next(request))
        else:
            folder_id = favorite.folder.pk if favorite.folder else ''
            form = UserFolderChoicesForm(choices=folder_choices,
                                         initial={'folder_id': folder_id})
        # form is not valid or it's a GET request
        ctx = {'favorite': favorite, 'next': _get_next(request), 'form': form}
        return render(request, 'favorites/favorite_move.html', ctx)
Пример #4
0
def favorite_move_widget(context, favorite):
    user = context['request'].user
    path = context['request'].path
    choices = Folder.objects.filter(user=user).order_by('name').values_list(
        'pk', 'name')
    folder_id = favorite.folder.pk if favorite.folder else 0
    form = UserFolderChoicesForm(choices=choices,
                                 initial={
                                     'folder_id': folder_id,
                                     'object_id': favorite.pk
                                 })
    return {
        'form': form,
        'favorite': favorite,
        'next': path,
        'path': context['request'].path
    }
Пример #5
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)