Explorar el Código

Add confirmation view for removing products from wish lists

We can't always POST to the view, so an optional confirmation view was
added.

Fixes #1121
master
Maik Hoepfel hace 12 años
padre
commit
52821db323

+ 42
- 19
oscar/apps/customer/wishlists/views.py Ver fichero

@@ -250,39 +250,62 @@ class LineMixin(object):
250 250
     * product_pk: The primary key of the product
251 251
     """
252 252
 
253
-    def dispatch(self, request, *args, **kwargs):
254
-        self.wishlist = get_object_or_404(
255
-            WishList, owner=request.user, key=kwargs['key'])
256
-        try:
257
-            if 'line_pk' in kwargs:
258
-                self.line = self.wishlist.lines.get(pk=kwargs['line_pk'])
259
-            elif 'product_pk' in kwargs:
260
-                self.line = self.wishlist.lines.get(
261
-                    product_id=kwargs['product_pk'])
262
-        except (ObjectDoesNotExist, MultipleObjectsReturned):
263
-            raise Http404
253
+    def fetch_line(self, user, wishlist_key, line_pk=None, product_pk=None):
254
+        self.wishlist = WishList._default_manager.get(
255
+            owner=user, key=wishlist_key)
256
+        if line_pk is not None:
257
+            self.line = self.wishlist.lines.get(pk=line_pk)
258
+        else:
259
+            self.line = self.wishlist.lines.get(product_id=product_pk)
264 260
         self.product = self.line.product
265
-        return super(LineMixin, self).dispatch(request, *args, **kwargs)
266 261
 
267 262
 
268
-class WishListRemoveProduct(LineMixin, View):
263
+class WishListRemoveProduct(LineMixin, PageTitleMixin, DeleteView):
264
+    template_name = 'customer/wishlists/wishlists_delete_product.html'
265
+    active_tab = "wishlists"
269 266
 
270
-    def post(self, request, *args, **kwargs):
271
-        self.line.delete()
267
+    def get_page_title(self):
268
+        return _(u'Remove %s') % self.object.product.get_title()
269
+
270
+    def get_object(self, queryset=None):
271
+        self.fetch_line(
272
+            self.request.user, self.kwargs['key'],
273
+            self.kwargs.get('line_pk'), self.kwargs.get('product_pk'))
274
+        return self.line
275
+
276
+    def get_context_data(self, **kwargs):
277
+        ctx = super(WishListRemoveProduct, self).get_context_data(**kwargs)
278
+        ctx['wishlist'] = self.wishlist
279
+        ctx['product'] = self.product
280
+        return ctx
272 281
 
282
+    def get_success_url(self):
273 283
         msg = _("'%(title)s' was removed from your '%(name)s' wish list") % {
274 284
             'title': self.product.get_title(),
275 285
             'name': self.wishlist.name}
276 286
         messages.success(self.request, msg)
277 287
 
278
-        default_url = reverse(
279
-            'customer:wishlists-detail', kwargs={'key': self.wishlist.key})
280
-        return HttpResponseRedirect(self.request.META.get(
281
-            'HTTP_REFERER', default_url))
288
+        # We post directly to this view on product pages; and should send the
289
+        # user back there if that was the case
290
+        referrer = self.request.META.get('HTTP_REFERER', '')
291
+        if self.product.get_absolute_url() in referrer:
292
+            return referrer
293
+        else:
294
+            return reverse(
295
+                'customer:wishlists-detail', kwargs={'key': self.wishlist.key})
282 296
 
283 297
 
284 298
 class WishListMoveProductToAnotherWishList(LineMixin, View):
285 299
 
300
+    def dispatch(self, request, *args, **kwargs):
301
+        try:
302
+            self.fetch_line(request.user, kwargs['key'],
303
+                            product_pk=kwargs['product_pk'])
304
+        except (ObjectDoesNotExist, MultipleObjectsReturned):
305
+            raise Http404
306
+        return super(WishListMoveProductToAnotherWishList, self).dispatch(
307
+            request, *args, **kwargs)
308
+
286 309
     def get(self, request, *args, **kwargs):
287 310
         to_wishlist = get_object_or_404(
288 311
             WishList, owner=request.user, key=kwargs['to_key'])

+ 41
- 0
oscar/templates/oscar/customer/wishlists/wishlists_delete_product.html Ver fichero

@@ -0,0 +1,41 @@
1
+{% extends "customer/baseaccountpage.html" %}
2
+{% load url from future %}
3
+{% load i18n %}
4
+
5
+{% block breadcrumbs %}
6
+    <ul class="breadcrumb">
7
+        <li>
8
+            <a href="{% url 'promotions:home' %}">{% trans 'Home' %}</a>
9
+            <span class="divider">/</span>
10
+        </li>
11
+        <li>
12
+            <a href="{% url 'customer:summary' %}">{% trans 'Account' %}</a>
13
+            <span class="divider">/</span>
14
+        </li>
15
+        <li>
16
+            <a href="{% url 'customer:wishlists-list' %}">{% trans 'Wish Lists' %}</a>
17
+            <span class="divider">/</span>
18
+        </li>
19
+        <li>
20
+            <a href="{% url 'customer:wishlists-detail' key=wishlist.key %}">{{ wishlist.name }}</a>
21
+            <span class="divider">/</span>
22
+        </li>
23
+        <li class="active">{% trans "Remove product" %}</li>
24
+    </ul>
25
+{% endblock %}
26
+
27
+{% block tabcontent %}
28
+    <form method="post">
29
+        {% csrf_token %}
30
+        <p>
31
+            {% blocktrans with product_name=object.get_title wishlist_name=wishlist.name %}
32
+                Are you sure you want to delete {{ product_name }} from wish list {{ wishlist_name }}?
33
+            {% endblocktrans %}
34
+        </p>
35
+
36
+        <div class="form-actions">
37
+            <button type="submit" class="btn btn-large">{% trans 'Remove' %}</button> {% trans 'or' %} <a href="{{ wishlist.get_absolute_url }}">{% trans 'cancel' %}</a>
38
+        </div>
39
+    </form>
40
+{% endblock tabcontent %}
41
+

Loading…
Cancelar
Guardar