Commits

Alex Ulianytskyi  committed eb56bce

Сохранение валюты в куках и отображение ее соответственно в результатах поиска.

  • Participants
  • Parent commits baaa876

Comments (0)

Files changed (4)

File catalogue/utils.py

         defaults['choices'] = choices
     defaults.update(override)
     field = field_class(** defaults)
-    return field
+    return field
+
+def set_currency(response, currency):
+    return response.set_cookie('botanic_currency', currency, 7*24*60*60)
+def get_currency(request):
+    return request.COOKIES.get('botanic_currency', 'uah')
+def delete_currency(response):
+    return response.delete_cookie('botanic_currency')

File catalogue/views.py

 from catalogue import forms
 from catalogue import models
 from catalogue import utils
+from catalogue.utils import get_currency
+from catalogue.utils import set_currency
 from django.shortcuts import get_object_or_404
 from django.shortcuts import redirect
 from django.views.generic.list_detail import object_list
 from django_utils.functions import compact_dict
+from django.http import QueryDict
 
 #@render_to('catalogue/index.html')
 def index(request):
+    currency = get_currency(request)
     apartment_list = models.Apartment.objects.all()
-    search_form = forms.SearchForm()
+    search_form = forms.SearchForm({'currency': currency})
     find_by_id_form = forms.FindByIdForm()
     context = {
         'search_form': search_form,
-        'find_by_id_form': find_by_id_form
+        'find_by_id_form': find_by_id_form,
+        'currency': currency,
     }
     return object_list(request,
                        queryset=apartment_list,
 
 @render_to('catalogue/apartment.html')
 def show(request, id=None, code=None):
+    currency = get_currency(request)
     if id:
         apartment = get_object_or_404(models.Apartment, id=id)
     elif code:
         apartment = get_object_or_404(models.Apartment, code=code)
-    search_form = forms.SearchForm()
+    search_form = forms.SearchForm({'currency': currency})
     find_by_id_form = forms.FindByIdForm()
     reservation_form = forms.ReservationForm({'apartment': apartment.id}, empty_permitted=True)
     reservation_form.quiet = True
         'search_form': search_form,
         'find_by_id_form': find_by_id_form,
         'reservation_form': reservation_form,
+        'currency': currency,
         }
 
 def search(request):
     context = {}
     search_results = models.Apartment.objects.none()
+    currency = get_currency(request)
+    form_data = QueryDict('currency=%s' % currency, True)
+    form_data.update(request.POST)
     if request.method == 'POST':
-        search_form = forms.SearchForm(request.POST)
+        search_form = forms.SearchForm(form_data)
         if search_form.is_valid():
             search_results = utils.search_apartment(compact_dict(search_form.cleaned_data), search_form)
+            _c = search_form.cleaned_data.get('currency', None)
+            if _c is not None:
+                currency = _c
     else:
         search_form = forms.SearchForm()
     find_by_id_form = forms.FindByIdForm()
     context.update({
                    'search_form': search_form,
-                   'find_by_id_form': find_by_id_form
+                   'find_by_id_form': find_by_id_form,
+                   'currency': currency,
                    })
-    return object_list(request,
-                       queryset=search_results,
-                       template_name='catalogue/search_results.json',
-                       template_object_name='apartment',
-                       allow_empty=True,
-                       paginate_by=25,
-                       extra_context=context,
-                       mimetype='application/json'
-                       )
+    response  = object_list(request,
+                            queryset=search_results,
+                            template_name='catalogue/search_results.json',
+                            template_object_name='apartment',
+                            allow_empty=True,
+                            paginate_by=25,
+                            extra_context=context,
+                            mimetype='application/json'
+                            )
+    set_currency(response, currency)
+    return response
 
 #@render_to('catalogue/search.html')
 def rooms(request, rooms):
+    currency = get_currency(request)
     if rooms == u'vip':
         filter = {'level': 3}
     else:
         filter = {'rooms': rooms, 'level__lt': 3}
+    filter.update({'currency': currency})
     search_results = utils.search_apartment(filter)
     search_form = forms.SearchForm(filter)
     find_by_id_form = forms.FindByIdForm()
     context = {
         'search_form': search_form,
-        'find_by_id_form': find_by_id_form
+        'find_by_id_form': find_by_id_form,
+        'currency': currency,
     }
     return object_list(request,
                        queryset=search_results,
 
 @render_to('catalogue/find_by_id.html')    
 def find_by_id(request):
+    currency = get_currency(request)
     if request.method == 'POST':
         find_by_id_form = forms.FindByIdForm(request.POST)
     else:
         find_by_id_form = forms.FindByIdForm()
-    search_form = forms.SearchForm()
+    search_form = forms.SearchForm({'currency': currency})
     find_by_id_form_valid = find_by_id_form.is_valid()
     if find_by_id_form_valid:
         id = find_by_id_form.cleaned_data.get('id', None)
         'find_by_id_form': find_by_id_form,
         'search_form': search_form,
         'find_by_id_form_valid': find_by_id_form_valid,
-        'find_by_id_query': find_by_id_form.data.get('id', '')
+        'find_by_id_query': find_by_id_form.data.get('id', ''),
+        'currency': currency,
         }
 
 @render_to('catalogue/reserve.html')
 def reserve(request):
-    search_form = forms.SearchForm()
+    currency = get_currency(request)
+    search_form = forms.SearchForm({'currency': currency})
     find_by_id_form = forms.FindByIdForm()
     context = {
         'search_form': search_form,
-        'find_by_id_form': find_by_id_form
+        'find_by_id_form': find_by_id_form,
+        'currency': currency,
     }
     return context
 
 @render_to()
 def validate_reservation_form(request):
+    currency = get_currency(request)
     context = {}
     if request.method == 'POST':
         reservation_form = forms.ReservationForm(request.POST)
         reservation_form = forms.ReservationForm()
     context.update({'reservation_form': reservation_form})
     context.update({'TEMPLATE': 'partials/reservation_form.html'})
+    context.update({'currency': currency})
     return context

File db/development.sqlite

Binary file modified.

File templates/partials/apartment.html

         <!-- p class="apartment_value">{% blocktrans count apartment.rooms as rooms %}one room{% plural %}{{ rooms }} rooms{% endblocktrans %}</p -->
         <p class="apartment_value">{{ apartment.description_any|default_if_none:""|truncatewords:10 }}</p>
         <p class="apartment_value">
-        {% trans 'Price per day'%}: 
-                    {% if apartment.price_usd %}<span class="apartment-value">{{ apartment.price_usd }} $</span>
-                    {% endif %}
-                    {% if apartment.price_euro %}<span class="apartment-value">{{ apartment.price_euro }} &euro;</span>
-                    {% endif %}
-                    {% if apartment.price_uah %}<span class="apartment-value">{{ apartment.price_uah }} грн.</span>
-                    {% endif %}
+        {% trans 'Price per day'%}:
+        {% ifequal currency 'usd' %}
+            <span class="apartment-value">{{ apartment.price_usd|default_if_none:"??" }}$</span>
+        {% endifequal %}
+        {% ifequal currency 'euro' %}
+            <span class="apartment-value">{{ apartment.price_euro|default_if_none:"??" }}&euro;</span>
+        {% endifequal %}
+        {% ifequal currency 'uah' %}
+            <span class="apartment-value">{{ apartment.price_uah|default_if_none:"??" }} грн.<!--&#8372;--></span>
+        {% endifequal %}
+        {% ifequal currency 'rur' %}
+            <span class="apartment-value">{{ apartment.price_rur|default_if_none:"??" }} руб.</span>
+        {% endifequal %}
         </p>
     </div>
     {% endif %}