home_info_ajax.py 2.21 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
# -*- coding: utf-8
from __future__ import unicode_literals

from django.conf import settings
from django.http import Http404
from django.http import HttpResponse
from django.urls import reverse
from django.utils.html import format_html
from django.utils.safestring import mark_safe
from django.views.decorators.cache import never_cache
from django.views.decorators.http import require_GET

from repanier.const import PERMANENCE_OPENED
from repanier.models import Permanence


@never_cache
@require_GET
def home_info_ajax(request):
    if request.is_ajax():
        result = []
        for permanence in Permanence.objects.filter(
                status=PERMANENCE_OPENED) \
                .only("id", "permanence_date", "with_delivery_point") \
                .order_by('permanence_date'):
            result.append(
                format_html(
                    '<div class="panel-heading"><h4 class="panel-title"><a href="{}">{}</a></h4></div>',
                    reverse('order_view', args=(permanence.id,)),
                    permanence.get_permanence_customer_display()
                )
            )
            if permanence.offer_description_on_home_page and permanence.offer_description:
                if permanence.picture:
                    result.append(
                        format_html(
Patrick's avatar
Patrick committed
37
                            '<div class="panel-body"><div class="col-xs-12"><img class="img-rounded" style="float: left; margin: 5px;" alt="{}" title="{}" src="{}{}"/>{}</div></div>',
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
                            permanence.get_permanence_customer_display(),
                            permanence.get_permanence_customer_display(),
                            settings.MEDIA_URL,
                            permanence.picture,
                            mark_safe(permanence.offer_description)
                        )
                    )
                else:
                    result.append(
                        format_html(
                            '<div class="panel-body"><div class="col-xs-12">{}</div></div>',
                            mark_safe(permanence.offer_description)
                        )
                    )
        if len(result) > 0:
            return HttpResponse("".join(result))
    raise Http404