home_info_ajax.py 3.63 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
# -*- 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

Patrick's avatar
Patrick committed
13
from repanier.const import PERMANENCE_OPENED, EMPTY_STRING
14 15 16 17 18 19 20
from repanier.models import Permanence


@never_cache
@require_GET
def home_info_ajax(request):
    if request.is_ajax():
Patrick's avatar
Patrick committed
21 22 23
        from repanier.apps import REPANIER_SETTINGS_CONFIG
        permanences = []
        home_info = EMPTY_STRING
24 25 26
        for permanence in Permanence.objects.filter(
                status=PERMANENCE_OPENED) \
                .only("id", "permanence_date", "with_delivery_point") \
Patrick's avatar
Patrick committed
27 28
                .order_by('-permanence_date', '-id'):
            permanences.append(
29 30 31 32 33 34 35 36
                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:
Patrick's avatar
Patrick committed
37
                    permanences.append(
38
                        format_html(
Patrick's avatar
Patrick committed
39
                            '<div class="panel-body"><div class="col-xs-12"><img class="img-responsive img-rounded " style="float: left; margin: 5px;" alt="{}" title="{}" src="{}{}"/><div class="clearfix visible-xs-block visible-sm-block"></div>{}</div></div>',
40 41 42 43 44 45 46 47
                            permanence.get_permanence_customer_display(),
                            permanence.get_permanence_customer_display(),
                            settings.MEDIA_URL,
                            permanence.picture,
                            mark_safe(permanence.offer_description)
                        )
                    )
                else:
Patrick's avatar
Patrick committed
48
                    permanences.append(
49 50 51 52 53
                        format_html(
                            '<div class="panel-body"><div class="col-xs-12">{}</div></div>',
                            mark_safe(permanence.offer_description)
                        )
                    )
Patrick's avatar
Patrick committed
54 55 56 57 58 59
        if len(permanences) > 0:
            home_info = """
            <div class="container">
                <div class="row">
                    <div class="panel-group">
                        <div class="panel panel-default">
Patrick's avatar
Patrick committed
60
                            {permanences}
Patrick's avatar
Patrick committed
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
                        </div>
                    </div>
                </div>
            </div>
            """.format(
                permanences="".join(permanences)
            )
        if REPANIER_SETTINGS_CONFIG.notification:
            if REPANIER_SETTINGS_CONFIG.notification_is_public or request.user.is_authenticated:
                home_info = """
                <div class="container">
                    <div class="row">
                        <div class="panel-group">
                            <div class="panel panel-default">
                                <div class="panel-body">
                                    {notification}
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                {home_info}
                """.format(
                    notification=REPANIER_SETTINGS_CONFIG.notification,
                    home_info=home_info
                )

        return HttpResponse(home_info)
89
    raise Http404