From 9f5402f8960dc51158711ecb929fa2a356c875c5 Mon Sep 17 00:00:00 2001 From: volpino Date: Thu, 8 Nov 2012 18:57:29 +0100 Subject: [PATCH] euscanwww: Favourite/Unfavourite template code refactored * Now is more DRY, the JS code and the HTML code are in one place * It works even without JS Signed-off-by: volpino --- .../djeuscan/templates/euscan/category.html | 31 +++------------- euscanwww/djeuscan/templates/euscan/herd.html | 31 +++------------- .../djeuscan/templates/euscan/maintainer.html | 31 +++------------- .../djeuscan/templates/euscan/overlay.html | 29 +++------------ .../djeuscan/templates/euscan/package.html | 32 +++++------------ .../djeuscan/templates/euscan/world_scan.html | 30 ++++------------ euscanwww/djeuscan/views.py | 2 +- .../templates/euscan/_favourite.html | 23 ++++++++++++ .../euscan_accounts/templatetags/__init__.py | 0 .../templatetags/euscan_accounts_helpers.py | 11 ++++++ euscanwww/euscan_accounts/views.py | 35 ++++++++++++++++--- euscanwww/htdocs/css/style.css | 4 +++ euscanwww/htdocs/js/favourite.js | 23 ++++++++++++ 13 files changed, 125 insertions(+), 157 deletions(-) create mode 100644 euscanwww/euscan_accounts/templates/euscan/_favourite.html create mode 100644 euscanwww/euscan_accounts/templatetags/__init__.py create mode 100644 euscanwww/euscan_accounts/templatetags/euscan_accounts_helpers.py create mode 100644 euscanwww/htdocs/js/favourite.js diff --git a/euscanwww/djeuscan/templates/euscan/category.html b/euscanwww/djeuscan/templates/euscan/category.html index 85935bf..55d82c6 100644 --- a/euscanwww/djeuscan/templates/euscan/category.html +++ b/euscanwww/djeuscan/templates/euscan/category.html @@ -1,7 +1,8 @@ {% extends "euscan/_datatable.html" %} -{% load djeuscan_helpers %} {% load url from future %} +{% load djeuscan_helpers %} +{% load euscan_accounts_helpers %} {% block title %} {{ block.super }} - Category: {{ category }} @@ -22,13 +23,8 @@ Category: {{ category }} {% if user.is_authenticated %} - - - + + {% favourite_buttons "category" category %} {% endif %} @@ -53,24 +49,7 @@ packages monthly

- - + {% endblock %} {% block menus %} diff --git a/euscanwww/djeuscan/templates/euscan/herd.html b/euscanwww/djeuscan/templates/euscan/herd.html index add323c..afc187d 100644 --- a/euscanwww/djeuscan/templates/euscan/herd.html +++ b/euscanwww/djeuscan/templates/euscan/herd.html @@ -1,7 +1,8 @@ {% extends "euscan/_datatable.html" %} -{% load djeuscan_helpers %} {% load url from future %} +{% load djeuscan_helpers %} +{% load euscan_accounts_helpers %} {% block title %} {{ block.super }} - Herd: {{ herd.herd }} @@ -21,13 +22,8 @@

Herd: {{ herd.herd }} {% if user.is_authenticated %} - - - + + {% favourite_buttons "herd" herd.herd %} {% endif %} @@ -51,24 +47,7 @@ packages monthly

- - + {% endblock %} {% block menus %} diff --git a/euscanwww/djeuscan/templates/euscan/maintainer.html b/euscanwww/djeuscan/templates/euscan/maintainer.html index d73b582..82ed52b 100644 --- a/euscanwww/djeuscan/templates/euscan/maintainer.html +++ b/euscanwww/djeuscan/templates/euscan/maintainer.html @@ -1,7 +1,8 @@ {% extends "euscan/_datatable.html" %} -{% load djeuscan_helpers %} {% load url from future %} +{% load djeuscan_helpers %} +{% load euscan_accounts_helpers %} {% block title %} {{ block.super }} - Maintainer: {{ maintainer.name }} @@ -21,13 +22,8 @@

Maintainer: {{ maintainer.name }} <{{ maintainer.email }}> {% if user.is_authenticated %} - - - + + {% favourite_buttons "maintainer" maintainer.email %} {% endif %}

@@ -52,24 +48,7 @@ packages monthly

- - + {% endblock %} {% block menus %} diff --git a/euscanwww/djeuscan/templates/euscan/overlay.html b/euscanwww/djeuscan/templates/euscan/overlay.html index 740d5f9..cf67f4d 100644 --- a/euscanwww/djeuscan/templates/euscan/overlay.html +++ b/euscanwww/djeuscan/templates/euscan/overlay.html @@ -2,6 +2,7 @@ {% load url from future %} {% load djeuscan_helpers %} +{% load euscan_accounts_helpers %} {% block title %} {{ block.super }} - Overlay: {{ overlay }} @@ -11,13 +12,8 @@

Overlay: {{ overlay }} {% if user.is_authenticated %} - - - + + {% favourite_buttons "overlay" overlay %} {% endif %} @@ -25,22 +21,5 @@ {% packages packages %} - - + {% endblock %} diff --git a/euscanwww/djeuscan/templates/euscan/package.html b/euscanwww/djeuscan/templates/euscan/package.html index bd2e6e8..874ab52 100644 --- a/euscanwww/djeuscan/templates/euscan/package.html +++ b/euscanwww/djeuscan/templates/euscan/package.html @@ -1,7 +1,8 @@ {% extends "euscan/_datatable.html" %} -{% load sub %} {% load url from future %} +{% load sub %} +{% load euscan_accounts_helpers %} {% block meta %} {{ block.super }} @@ -34,7 +35,7 @@ {{ package.category }}/{{ package.name }} {% if user.is_authenticated %} - +
@@ -46,13 +47,9 @@ - - - + + {% favourite_buttons "package" package.category package.name %} +
{% endif %}

@@ -84,22 +81,9 @@ }); return false; }); - - $(".favourite-button").click(function() { - $.post($(this).data("url"), function() { - $(".unfavourite-button").removeClass("hide"); - $(".favourite-button").addClass("hide"); - }); - }); - - $(".unfavourite-button").click(function() { - $.post($(this).data("url"), function() { - $(".favourite-button").removeClass("hide"); - $(".unfavourite-button").addClass("hide"); - }); - }); - }); + + {% endblock %} diff --git a/euscanwww/djeuscan/templates/euscan/world_scan.html b/euscanwww/djeuscan/templates/euscan/world_scan.html index 14dd428..fa6d7b0 100644 --- a/euscanwww/djeuscan/templates/euscan/world_scan.html +++ b/euscanwww/djeuscan/templates/euscan/world_scan.html @@ -2,6 +2,7 @@ {% load url from future %} {% load djeuscan_helpers %} +{% load euscan_accounts_helpers %} {% block title %} {{ block.super }} - World Scan @@ -23,13 +24,8 @@

World scan: {% if user.is_authenticated %} - - - + + {% favourite_buttons "world" %} {% endif %}

@@ -37,23 +33,9 @@ {% packages packages %} + + {% endblock %} diff --git a/euscanwww/djeuscan/views.py b/euscanwww/djeuscan/views.py index 5f453a7..eccffe4 100644 --- a/euscanwww/djeuscan/views.py +++ b/euscanwww/djeuscan/views.py @@ -313,7 +313,7 @@ def world_scan(request): packages_ids = [p.pk for p in packages] favourited = False - if request.user.is_authenticated(): + if len(packages) > 0 and request.user.is_authenticated(): profile = get_profile(request.user) if len(packages) == len(profile.packages.filter(id__in=packages_ids)): favourited = True diff --git a/euscanwww/euscan_accounts/templates/euscan/_favourite.html b/euscanwww/euscan_accounts/templates/euscan/_favourite.html new file mode 100644 index 0000000..ff4686a --- /dev/null +++ b/euscanwww/euscan_accounts/templates/euscan/_favourite.html @@ -0,0 +1,23 @@ + + + + {% for pk in packages_ids %} + + {% endfor %} + + +
+ + + {% for pk in packages_ids %} + + {% endfor %} +
diff --git a/euscanwww/euscan_accounts/templatetags/__init__.py b/euscanwww/euscan_accounts/templatetags/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/euscanwww/euscan_accounts/templatetags/euscan_accounts_helpers.py b/euscanwww/euscan_accounts/templatetags/euscan_accounts_helpers.py new file mode 100644 index 0000000..c664837 --- /dev/null +++ b/euscanwww/euscan_accounts/templatetags/euscan_accounts_helpers.py @@ -0,0 +1,11 @@ +from django import template +from django.core.urlresolvers import reverse + +register = template.Library() + + +@register.inclusion_tag('euscan/_favourite.html', takes_context=True) +def favourite_buttons(context, subj, *args): + context["favourite_url"] = reverse("favourite_%s" % subj, args=args) + context["unfavourite_url"] = reverse("unfavourite_%s" % subj, args=args) + return context diff --git a/euscanwww/euscan_accounts/views.py b/euscanwww/euscan_accounts/views.py index e80bc04..5856cbd 100644 --- a/euscanwww/euscan_accounts/views.py +++ b/euscanwww/euscan_accounts/views.py @@ -1,6 +1,7 @@ from annoying.decorators import render_to, ajax_request -from django.shortcuts import get_object_or_404 +from django.shortcuts import get_object_or_404, redirect +from django.core.urlresolvers import reverse from django.contrib.auth.decorators import login_required from django.views.decorators.http import require_POST @@ -113,6 +114,8 @@ def accounts_overlays(request): def favourite_package(request, category, package): obj = get_object_or_404(Package, category=category, name=package) get_profile(request.user).packages.add(obj) + if "nojs" in request.POST: + return redirect(reverse("package", args=(category, package))) return {"success": True} @@ -120,8 +123,10 @@ def favourite_package(request, category, package): @require_POST @ajax_request def unfavourite_package(request, category, package): - package = get_object_or_404(Package, category=category, name=package) - get_profile(request.user).packages.remove(package) + obj = get_object_or_404(Package, category=category, name=package) + get_profile(request.user).packages.remove(obj) + if "nojs" in request.POST: + return redirect(reverse("package", args=(category, package))) return {"success": True} @@ -131,6 +136,8 @@ def unfavourite_package(request, category, package): def favourite_herd(request, herd): obj = get_object_or_404(Herd, herd=herd) get_profile(request.user).herds.add(obj) + if "nojs" in request.POST: + return redirect(reverse("herd", args=(herd, ))) return {"success": True} @@ -138,8 +145,10 @@ def favourite_herd(request, herd): @require_POST @ajax_request def unfavourite_herd(request, herd): - herd = get_object_or_404(Herd, herd=herd) - get_profile(request.user).herds.remove(herd) + obj = get_object_or_404(Herd, herd=herd) + get_profile(request.user).herds.remove(obj) + if "nojs" in request.POST: + return redirect(reverse("herd", args=(herd, ))) return {"success": True} @@ -149,6 +158,8 @@ def unfavourite_herd(request, herd): def favourite_maintainer(request, maintainer_id=None, maintainer_email=None): obj = get_maintainer_or_404(maintainer_id, maintainer_email) get_profile(request.user).maintainers.add(obj) + if "nojs" in request.POST: + return redirect(reverse("maintainer", args=(obj.email, ))) return {"success": True} @@ -158,6 +169,8 @@ def favourite_maintainer(request, maintainer_id=None, maintainer_email=None): def unfavourite_maintainer(request, maintainer_id=None, maintainer_email=None): obj = get_maintainer_or_404(maintainer_id, maintainer_email) get_profile(request.user).maintainers.remove(obj) + if "nojs" in request.POST: + return redirect(reverse("maintainer", args=(obj.email, ))) return {"success": True} @@ -167,6 +180,8 @@ def unfavourite_maintainer(request, maintainer_id=None, maintainer_email=None): def favourite_category(request, category): obj = Category.objects.get(name=category) get_profile(request.user).categories.add(obj) + if "nojs" in request.POST: + return redirect(reverse("category", args=(category, ))) return {"success": True} @@ -176,6 +191,8 @@ def favourite_category(request, category): def unfavourite_category(request, category): obj = Category.objects.get(name=category) get_profile(request.user).categories.remove(obj) + if "nojs" in request.POST: + return redirect(reverse("category", args=(category, ))) return {"success": True} @@ -185,6 +202,8 @@ def unfavourite_category(request, category): def favourite_overlay(request, overlay): obj = Overlay.objects.get(name=overlay) get_profile(request.user).overlays.add(obj) + if "nojs" in request.POST: + return redirect(reverse("overlay", args=(overlay, ))) return {"success": True} @@ -194,6 +213,8 @@ def favourite_overlay(request, overlay): def unfavourite_overlay(request, overlay): obj = Overlay.objects.get(name=overlay) get_profile(request.user).overlays.remove(obj) + if "nojs" in request.POST: + return redirect(reverse("overlay", args=(overlay, ))) return {"success": True} @@ -206,6 +227,8 @@ def favourite_world(request): packages = request.POST.getlist("packages[]") objs = Package.objects.filter(id__in=packages) get_profile(request.user).packages.add(*objs) + if "nojs" in request.POST: + return redirect(reverse("world")) return {"success": True} @@ -218,4 +241,6 @@ def unfavourite_world(request): packages = request.POST.getlist("packages[]") objs = Package.objects.filter(id__in=packages) get_profile(request.user).packages.remove(*objs) + if "nojs" in request.POST: + return redirect(reverse("world")) return {"success": True} diff --git a/euscanwww/htdocs/css/style.css b/euscanwww/htdocs/css/style.css index 3d8c428..b87877e 100644 --- a/euscanwww/htdocs/css/style.css +++ b/euscanwww/htdocs/css/style.css @@ -238,3 +238,7 @@ textarea { .inline { display: inline; } + +.padding-bottom10 { + padding-bottom: 10px; +} diff --git a/euscanwww/htdocs/js/favourite.js b/euscanwww/htdocs/js/favourite.js new file mode 100644 index 0000000..e8c80b6 --- /dev/null +++ b/euscanwww/htdocs/js/favourite.js @@ -0,0 +1,23 @@ +$(document).ready(function() { + $(".favourite-form").submit(function(e) { + e.preventDefault(); + e.stopPropagation(); + + $.post($(this).attr("action"), {packages: window.packages}, function() { + $(".unfavourite-button").removeClass("hide"); + $(".favourite-button").addClass("hide"); + }); + return false; + }); + + $(".unfavourite-form").submit(function(e) { + e.preventDefault(); + e.stopPropagation(); + + $.post($(this).attr("action"), {packages: window.packages}, function() { + $(".favourite-button").removeClass("hide"); + $(".unfavourite-button").addClass("hide"); + }); + return false; + }); +});