euscanwww: Added watch/unwatch button to scan world

Signed-off-by: volpino <fox91@anche.no>
This commit is contained in:
volpino 2012-08-10 17:45:43 +02:00
parent 58d6ecc490
commit f04f28aacc
7 changed files with 84 additions and 4 deletions

View File

@ -54,6 +54,7 @@
</p> </p>
<script type="text/javascript"> <script type="text/javascript">
$(document).ready(function () {
$(".favourite-button").click(function() { $(".favourite-button").click(function() {
$.post($(this).data("url"), function() { $.post($(this).data("url"), function() {
$(".unfavourite-button").removeClass("hide"); $(".unfavourite-button").removeClass("hide");
@ -67,6 +68,7 @@
$(".unfavourite-button").addClass("hide"); $(".unfavourite-button").addClass("hide");
}); });
}); });
});
</script> </script>
{% endblock %} {% endblock %}

View File

@ -52,6 +52,7 @@
</p> </p>
<script type="text/javascript"> <script type="text/javascript">
$(document).ready(function () {
$(".favourite-button").click(function() { $(".favourite-button").click(function() {
$.post($(this).data("url"), function() { $.post($(this).data("url"), function() {
$(".unfavourite-button").removeClass("hide"); $(".unfavourite-button").removeClass("hide");
@ -65,6 +66,7 @@
$(".unfavourite-button").addClass("hide"); $(".unfavourite-button").addClass("hide");
}); });
}); });
});
</script> </script>
{% endblock %} {% endblock %}

View File

@ -53,6 +53,7 @@
</p> </p>
<script type="text/javascript"> <script type="text/javascript">
$(document).ready(function () {
$(".favourite-button").click(function() { $(".favourite-button").click(function() {
$.post($(this).data("url"), function() { $.post($(this).data("url"), function() {
$(".unfavourite-button").removeClass("hide"); $(".unfavourite-button").removeClass("hide");
@ -66,6 +67,7 @@
$(".unfavourite-button").addClass("hide"); $(".unfavourite-button").addClass("hide");
}); });
}); });
});
</script> </script>
{% endblock %} {% endblock %}

View File

@ -26,6 +26,7 @@
{% packages packages %} {% packages packages %}
<script type="text/javascript"> <script type="text/javascript">
$(document).ready(function () {
$(".favourite-button").click(function() { $(".favourite-button").click(function() {
$.post($(this).data("url"), function() { $.post($(this).data("url"), function() {
$(".unfavourite-button").removeClass("hide"); $(".unfavourite-button").removeClass("hide");
@ -39,6 +40,7 @@
$(".unfavourite-button").addClass("hide"); $(".unfavourite-button").addClass("hide");
}); });
}); });
});
</script> </script>
{% endblock %} {% endblock %}

View File

@ -1,5 +1,6 @@
{% extends "euscan/_datatable.html" %} {% extends "euscan/_datatable.html" %}
{% load url from future %}
{% load djeuscan_helpers %} {% load djeuscan_helpers %}
{% block title %} {% block title %}
@ -7,6 +8,40 @@
{% endblock %} {% endblock %}
{% block content %} {% block content %}
<h2>World scan:</h2> <h2>
World scan:
{% if user.is_authenticated %}
<span class="pull-right">
<button class="btn favourite-button {% if favourited %}hide{% endif %}" data-url="{% url "favourite_world" %}">
<img src="{{ STATIC_URL}}/img/watch-icon.png" alt="Watch" >
</button>
<button class="btn unfavourite-button {% if not favourited %}hide{% endif %}" data-url="{% url "unfavourite_world" %}">
<img src="{{ STATIC_URL}}/img/unwatch-icon.png" alt="Unwatch" >
</button>
</span>
{% endif %}
</h2>
{% packages packages %} {% packages packages %}
<script type="text/javascript">
$(document).ready(function () {
var packages = [{{packages_ids|join:","}}];
$(".favourite-button").click(function() {
$.post($(this).data("url"), {packages: packages}, function() {
$(".unfavourite-button").removeClass("hide");
$(".favourite-button").addClass("hide");
});
});
$(".unfavourite-button").click(function() {
$.post($(this).data("url"), {packages: packages}, function() {
$(".favourite-button").removeClass("hide");
$(".unfavourite-button").addClass("hide");
});
});
});
</script>
{% endblock %} {% endblock %}

View File

@ -115,6 +115,9 @@ urlpatterns = patterns('djeuscan.views',
url(r'^statistics/charts/(?P<chart>[\w\-]+).png$', 'chart', name="chart"), url(r'^statistics/charts/(?P<chart>[\w\-]+).png$', 'chart', name="chart"),
url(r'^world/$', 'world', name="world"), url(r'^world/$', 'world', name="world"),
url(r'^world/scan/$', 'world_scan', name="world_scan"), url(r'^world/scan/$', 'world_scan', name="world_scan"),
url(r'^world/favourite/$', 'favourite_world', name="favourite_world"),
url(r'^world/unfavourite/$', 'unfavourite_world',
name="unfavourite_world"),
# Real data # Real data
url(r'^categories/', include(categories_patterns)), url(r'^categories/', include(categories_patterns)),

View File

@ -271,7 +271,6 @@ def world(request):
@render_to('euscan/world_scan.html') @render_to('euscan/world_scan.html')
def world_scan(request): def world_scan(request):
if 'world' in request.FILES: if 'world' in request.FILES:
data = request.FILES['world'].read() data = request.FILES['world'].read()
elif 'packages' in request.POST: elif 'packages' in request.POST:
@ -280,8 +279,19 @@ def world_scan(request):
data = "" data = ""
packages = packages_from_names(data) packages = packages_from_names(data)
packages_ids = [p.pk for p in packages]
return {'packages': packages} favourited = False
if request.user.is_authenticated():
profile = get_profile(request.user)
if len(packages) == len(profile.packages.filter(id__in=packages_ids)):
favourited = True
return {
'packages': packages,
'packages_ids': packages_ids,
'favourited': favourited
}
@render_to("euscan/about.html") @render_to("euscan/about.html")
@ -328,7 +338,7 @@ def chart(request, **kwargs):
elif chart == 'versions': elif chart == 'versions':
path = charts.versions(**kwargs) path = charts.versions(**kwargs)
else: else:
raise Http404() raise Http404
return serve(request, path, document_root=charts.CHARTS_ROOT) return serve(request, path, document_root=charts.CHARTS_ROOT)
@ -568,3 +578,27 @@ def unfavourite_overlay(request, overlay):
obj = Overlay.objects.get(name=overlay) obj = Overlay.objects.get(name=overlay)
get_profile(request.user).overlays.remove(obj) get_profile(request.user).overlays.remove(obj)
return {"success": True} return {"success": True}
@login_required
@require_POST
@ajax_request
def favourite_world(request):
if not "packages[]" in request.POST:
return {"success": False}
packages = request.POST.getlist("packages[]")
objs = Package.objects.filter(id__in=packages)
get_profile(request.user).packages.add(*objs)
return {"success": True}
@login_required
@require_POST
@ajax_request
def unfavourite_world(request):
if not "packages[]" in request.POST:
return {"success": False}
packages = request.POST.getlist("packages[]")
objs = Package.objects.filter(id__in=packages)
get_profile(request.user).packages.remove(*objs)
return {"success": True}