Commits

Federico Hlawaczek committed 21cdd19

New module and improvements to paginator and cart

  • Participants
  • Parent commits 2229952

Comments (0)

Files changed (9)

e42_toolbox/utils/simple_paginator.py

 from django.conf import settings
 from django.utils.safestring import mark_safe
 
-PAGE_VAR = getattr(settings, "SIMPLE_PAGINATOR_PAGE_VAR", "pg")
-ITEMS_PER_PAGE = getattr(settings, "SIMPLE_PAGINATOR_ITEMS_PER_PAGE", 25)
+PAGE_VAR = "pg"
+ITEMS_PER_PAGE = 25
+PAGE_NUM_FORMAT = "03d"
+
+if hasattr(settings, 'SIMPLE_PAGINATOR'):
+    PAGE_VAR = settings.SIMPLE_PAGINATOR.get('PAGE_VAR', PAGE_VAR)
+    ITEMS_PER_PAGE = settings.SIMPLE_PAGINATOR.get('ITEMS_PER_PAGE', ITEMS_PER_PAGE)
+    PAGE_NUM_FORMAT = settings.SIMPLE_PAGINATOR.get('PAGE_NUM_FORMAT', PAGE_NUM_FORMAT)
 
 class SimplePaginator(object):
     def __init__(self, request, params, qs, items_per_page=None):
         for p in self.paginator.page_range:
             pl_params[PAGE_VAR] = p
             if not p == self.cur_page:
-                links.append(u"<a href=\"?{0}\">{1:03d}</a> ".format(urlencode(pl_params, doseq=True), p))
+                links.append((u"<a href=\"?{0}\">{1:" + PAGE_NUM_FORMAT + u"}</a> ").format(urlencode(pl_params, doseq=True), p))
             else:
-                links.append(u"<a href=\"#\" class=\"this-page\">{0:03d}</a> ".format(p))
+                links.append((u"<a href=\"#\" class=\"this-page\">{0:" + PAGE_NUM_FORMAT + u"}</a> ").format(p))
         return mark_safe(u" ".join(links))
     
     def page_navigator(self):
         page_range = self.paginator.page_range
         if self.cur_page > 3:
             pl_params[PAGE_VAR] = 1
-            links.append(u"<a href=\"?{0}\">{1:03d}</a> ".format(urlencode(pl_params, doseq=True), 1))
+            links.append((u"<a href=\"?{0}\">{1:" + PAGE_NUM_FORMAT + u"}</a> ").format(urlencode(pl_params, doseq=True), 1))
             links.append(u"...")
         for p in range(self.cur_page - 2, self.cur_page + 3):
             if p in page_range:
                 pl_params[PAGE_VAR] = p
                 if not p == self.cur_page:
-                    links.append(u"<a href=\"?{0}\">{1:03d}</a> ".format(urlencode(pl_params, doseq=True), p))
+                    links.append((u"<a href=\"?{0}\">{1:" + PAGE_NUM_FORMAT + u"}</a> ").format(urlencode(pl_params, doseq=True), p))
                 else:
-                    links.append(u"<a href=\"#\" class=\"this-page\">{0:03d}</a> ".format(p))
+                    links.append((u"<a href=\"#\" class=\"this-page\">{0:" + PAGE_NUM_FORMAT + u"}</a> ").format(p))
         if self.cur_page < self.paginator.num_pages - 2:
             pl_params[PAGE_VAR] = self.paginator.num_pages
             links.append(u"...")
-            links.append(u"<a href=\"?{0}\">{1:03d}</a> ".format(urlencode(pl_params, doseq=True), self.paginator.num_pages))
+            links.append((u"<a href=\"?{0}\">{1:" + PAGE_NUM_FORMAT + u"}</a> ").format(urlencode(pl_params, doseq=True), self.paginator.num_pages))
         return mark_safe(u" ".join(links))
     
     def showing_from(self):

simple_cart/templates/simple_cart/tag/cart_change_submit.html

 <input name="{{ action_field_name }}" value="{{ action_value }}" type="hidden">
-<input type="submit" value="{{ submit_value }}">
+<input type="submit" name="{{ submit_name }}" value="{{ submit_value }}"{% if tag_class %} class={{ tag_class }}{% endif %}>

simple_cart/templatetags/simple_cart.py

 from django.utils.translation import ugettext as _
 
 from .. import APP_NAME
-from ..views import ACTION_ADD, ACTION_REMOVE, ACTION_VAR, ACTION_CHANGE, \
-     PRODUCT_ID_VAR, QUANTITY_VAR, \
-     get_item_quantity_field_name, get_item_notes_field_name
+from ..views import (ACTION_ADD, ACTION_REMOVE, ACTION_VAR, ACTION_CHANGE, 
+                     SUBMIT_CHECKOUT, SUBMIT_ONLY, 
+                     PRODUCT_ID_VAR, QUANTITY_VAR, 
+                     get_item_quantity_field_name, get_item_notes_field_name
+                     )
 
 register = template.Library()
 
     }
 item_notes_field = register.inclusion_tag("simple_cart/tag/item_notes_field.html")(item_notes_field)
 
-def cart_change_submit(cart):
+def cart_change_submit(cart, tag_class=None, legend=None):
     return {
         'action_field_name': ACTION_VAR, 
         'action_value': ACTION_CHANGE, 
-        'submit_value': _(u"Guardar cambios")
+        'submit_value': legend or _(u"Guardar"), 
+        'tag_class': tag_class, 
+        'submit_name': SUBMIT_ONLY, 
     }
-cart_change_submit = register.inclusion_tag("simple_cart/tag/cart_change_submit.html")(cart_change_submit)
+cart_change_submit = register.inclusion_tag("simple_cart/tag/cart_change_submit.html")(cart_change_submit)
+
+def cart_change_submit_checkout(cart, tag_class=None, legend=None):
+    return {
+        'action_field_name': ACTION_VAR, 
+        'action_value': ACTION_CHANGE, 
+        'submit_value': legend or _(u"Enviar pedido"), 
+        'tag_class': tag_class, 
+        'submit_name': SUBMIT_CHECKOUT, 
+    }
+cart_change_submit_checkout = register.inclusion_tag("simple_cart/tag/cart_change_submit.html")(cart_change_submit_checkout)

simple_cart/views.py

 ACTION_ADD = 'add'
 ACTION_REMOVE = 'del'
 ACTION_CHANGE = 'change'
+SUBMIT_CHECKOUT = "_checkout"
+SUBMIT_ONLY = "_save"
 CART_SESSION_KEY = APP_NAME + "_CART"
 SENTCART_SESSION_KEY = APP_NAME + "_SENTCART"
 
                     except (KeyError, ValueError):
                         continue
                     cart.change_notes(cartitem.product, item_n)
-                    
             request.session[CART_SESSION_KEY] = cart
-            return HttpResponseRedirect("./")
+            if request.REQUEST.has_key(SUBMIT_CHECKOUT):
+                return HttpResponseRedirect(reverse("{0}:checkout".format(APP_NAME)))
+            else:
+                return HttpResponseRedirect("./")
         
         context = {
             'cart': cart, 

simple_gallery/__init__.py

+import os
+
+APP_NAME = os.path.split(os.path.dirname(__file__))[1]

simple_gallery/admin.py

+# coding: utf8
+
+from django.contrib.admin import ModelAdmin
+
+class PhotoGroupAdmin(ModelAdmin):
+    # Change list settings
+    actions = None
+    list_display = ('title', )
+    # Change Form settings
+    fields = ('title', )
+
+class PhotoAdmin(ModelAdmin):
+    # 
+    # Change list settings
+    # 
+    actions = None
+    list_display = ('group', 'title', )
+    list_display_links = ('title', )
+    list_filter = ('group', )    
+    # 
+    # Change Form settings
+    # 
+    fields = ('group', 'title', 'image', )

simple_gallery/models.py

+# coding: utf8
+
+from django.conf import settings
+from django.db import models
+from django.core.exceptions import ObjectDoesNotExist
+from django.utils.translation import ugettext as _
+
+from e42_toolbox.validators import ImageValidator, IMAGE_VALIDATOR_EXACT, IMAGE_VALIDATOR_MAX, IMAGE_VALIDATOR_MIN
+
+from . import APP_NAME
+
+if (hasattr(settings, 'SIMPLE_GALLERY') and
+    'PICTURE_WIDTH' in settings.SIMPLE_GALLERY and
+    'PICTURE_HEIGHT' in settings.SIMPLE_GALLERY
+    ):
+    pic_width = int(settings.SIMPLE_GALLERY['PICTURE_WIDTH'])
+    pic_height = int(settings.SIMPLE_GALLERY['PICTURE_HEIGHT'])
+    pic_field_helptext = _(u"Debe ser un archivo png o jpg de {0} x {1} px.").format(pic_width, pic_height)
+    pic_field = models.ImageField(_(u"Imágen"), blank=True, null=True, 
+                                  validators=[ImageValidator(width=pic_width, height=pic_height, allowed_types=('.png', '.jpg', )), ], 
+                                  upload_to=(APP_NAME + "/simple_gallery"), 
+                                  help_text=pic_field_helptext, 
+                                  )
+else:
+    pic_field = models.ImageField(_(u"Imágen"), blank=True, null=True, 
+                                  upload_to=(APP_NAME + "/simple_gallery"), 
+                                  )
+
+class PhotoGroup(models.Model):
+    title = models.CharField(_(u"Título"), max_length=70, unique=True, )
+    
+    class Meta:
+        verbose_name = _(u"Grupo")
+        verbose_name_plural = _(u"Grupos")
+    
+    def __unicode__(self):
+        return self.title
+
+class PhotoManager(models.Manager):
+    def get_photos_for_group_title(self, title):
+        try:
+            group = PhotoGroup.objects.get(title=title)
+        except ObjectDoesNotExist:
+            group = None
+        if group:
+            return self.filter(group=group)
+        else:
+            return self.none()
+            
+
+class Photo(models.Model):
+    objects = PhotoManager()
+    title = models.CharField(_(u"Título"), max_length=70, )
+    group = models.ForeignKey(PhotoGroup, verbose_name=_(u"Grupo"))
+    image = pic_field
+    
+    class Meta:
+        verbose_name = _(u"Foto")
+        verbose_name_plural = _(u"Fotos")

simple_gallery/tests.py

+"""
+This file demonstrates writing tests using the unittest module. These will pass
+when you run "manage.py test".
+
+Replace this with more appropriate tests for your application.
+"""
+
+from django.test import TestCase
+
+
+class SimpleTest(TestCase):
+    def test_basic_addition(self):
+        """
+        Tests that 1 + 1 always equals 2.
+        """
+        self.assertEqual(1 + 1, 2)

simple_gallery/views.py

+# Create your views here.