Re: trying not to do any operations in template, but not sure i can avoid it.

2010-05-20 Thread Bill Freeman
On Thu, May 20, 2010 at 4:24 AM, shofty  wrote:
> thanks for the swift response.
>
> On May 19, 10:05 pm, Bill Freeman  wrote:
>> You could put a method on your basket object which calculates the shipping.
>> Then if the basket object is included in the context, you can trivially call 
>> it.
>>
> That seems simple enough for me to do.
>
>
>> Or perhaps better, fix up you all_purchases iterable to append it.
>> Something like:
>>
>>    def add_shipping(purchaces_iterable):
>>       for i, p in enumerate(purchases_iterable):
>>          yield p
>>       yield Purchase(name="Shipping",
>> amount=Decimal(PER_ITEM_SHIPPING)*(i+1)...)
>>
> This one seems a bit trickier. where would that code live? is that in
> the views module for the basket class?

It's flexible.  My though was to define it in views.py, along with its
free referencee PER_ITEM_SHIPPING, though that could obviously
come from a variable or attribute of an object, or out of the settings
module.  Then the view would call it on the iterable of purchases
that you're using now, and pass the result to the template.

But you could also make (something like it) a method on a basket
object, taking only self as an argument, and querying the original
iterable itseld, then pass the basket to the template and let the
template say something like:

   {% for item in basket.with_added_shipping_item %}

>
> one thing i thought about last night was that i should probably be
> doping this a better way. isnt there a way inside django of creating
> an object and including it and everything within it(methods etc) with
> a proper simple statement? a so i could do something like
>
> {% basket %}
>
> and the basket html would be written out. i could also define {%
> basket_total %} and each page would have a summary of the basket?
>
> is this what decorators are for? if not someone tell me the term for
> the funtion i'm describing and i'll go away and do the research. im
>
Be careful that you don't go too far the other way here.  Just as
logic (and calculation) in the template is to be avoided, not being the
province of the designer, presentation in the code is to be avoided because
it *IS* the province of the designer.  Whatever code implements the
iteration over items, you might consider a separate template to render
the individual row, which the code accesses using render_to_string,
for example.

Personally, I don't find iterating over a set of objects each of which
is rendered the same way, simply by accessing attributes, to be
too much logic in the template.

But, yes, you can do such things.

Look for writing your own template tag in the docs.  Consider an
"inclusion" tag, where you really produce the stuff in a separate
template, because writing more general template tags is tricky.
Yes, it would still be being done in the template language, but your
designer doesn't have to see it, they just see {% basket %}.  This
will require a load tag higher up in the template to load your custom
tag library.  Note that your tag will have to presume what the name
of the variable holding the basket is, unless you pass it as an argument,
e.g.; {% basket basket %} (no problem with the names being the same).

Another approach, also using a custom tag library, but writing a filter,
which is easier to get right than a tag, would result in usage like:
{{ basket|basket }}

Yet another way to do something about as good as this is again using
a method on your basket objects.  This would have usage such as
{{ basket.items_list_html }} and wouldn't require loading a tag library.

Note: in all these cases remember to mark your return string as safe
so that the template engine doesn't escape your less than and ampersand
characters.




>
> cheers so far
>
> --
> You received this message because you are subscribed to the Google Groups 
> "Django users" group.
> To post to this group, send email to django-us...@googlegroups.com.
> To unsubscribe from this group, send email to 
> django-users+unsubscr...@googlegroups.com.
> For more options, visit this group at 
> http://groups.google.com/group/django-users?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: trying not to do any operations in template, but not sure i can avoid it.

2010-05-20 Thread shofty
thanks for the swift response.

On May 19, 10:05 pm, Bill Freeman  wrote:
> You could put a method on your basket object which calculates the shipping.
> Then if the basket object is included in the context, you can trivially call 
> it.
>
That seems simple enough for me to do.


> Or perhaps better, fix up you all_purchases iterable to append it.
> Something like:
>
>    def add_shipping(purchaces_iterable):
>       for i, p in enumerate(purchases_iterable):
>          yield p
>       yield Purchase(name="Shipping",
> amount=Decimal(PER_ITEM_SHIPPING)*(i+1)...)
>
This one seems a bit trickier. where would that code live? is that in
the views module for the basket class?

one thing i thought about last night was that i should probably be
doping this a better way. isnt there a way inside django of creating
an object and including it and everything within it(methods etc) with
a proper simple statement? a so i could do something like

{% basket %}

and the basket html would be written out. i could also define {%
basket_total %} and each page would have a summary of the basket?

is this what decorators are for? if not someone tell me the term for
the funtion i'm describing and i'll go away and do the research. im
not asking anyone to write the code for me.

cheers so far

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: trying not to do any operations in template, but not sure i can avoid it.

2010-05-19 Thread Bill Freeman
You could put a method on your basket object which calculates the shipping.
Then if the basket object is included in the context, you can trivially call it.

Or perhaps better, fix up you all_purchases iterable to append it.
Something like:

   def add_shipping(purchaces_iterable):
  for i, p in enumerate(purchases_iterable):
 yield p
  yield Purchase(name="Shipping",
amount=Decimal(PER_ITEM_SHIPPING)*(i+1)...)


On Wed, May 19, 2010 at 4:44 PM, shofty  wrote:
> apologies, i tab spaced before id finished.
>
> the if forloop.last is waiting til its written out all fo the html for
> the items then i was going to add a final item which was shipping to
> the basket. but cant really do that without doing math in the
> template.
>
> would like to know if anyone knows of a better way to do it. have i
> missed the really obvious?
> am i trying to hard to stick to not putting logic into the template?
>
> Matt
>
> --
> You received this message because you are subscribed to the Google Groups 
> "Django users" group.
> To post to this group, send email to django-us...@googlegroups.com.
> To unsubscribe from this group, send email to 
> django-users+unsubscr...@googlegroups.com.
> For more options, visit this group at 
> http://groups.google.com/group/django-users?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: trying not to do any operations in template, but not sure i can avoid it.

2010-05-19 Thread shofty
apologies, i tab spaced before id finished.

the if forloop.last is waiting til its written out all fo the html for
the items then i was going to add a final item which was shipping to
the basket. but cant really do that without doing math in the
template.

would like to know if anyone knows of a better way to do it. have i
missed the really obvious?
am i trying to hard to stick to not putting logic into the template?

Matt

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



trying not to do any operations in template, but not sure i can avoid it.

2010-05-19 Thread shofty
not sure if i've totally missed the point of the correct way to do
this.

because i couldn't get the django-cart project to do what i wanted
(and more because there is a note on the page explaining its got bugs
and that i should have been able to fix them but not detailing said
bugs) i decided to write my own cart. and called it basket ;)

the basket works and is super simple. its built to create html for
submission to google checkout. which works.
the code that generates the html follows.

{% for purchase in all_purchases %}






{% if forloop.last %}
end of loopsss
{% endif %}
{% endfor %}

consider this situation though. the shipping option is a fixed cost
per item, which google doesnt allow you to configure in their options
exactly as the client wants it.

so you think it'd be easy to just wait for the last item in the
basket, then sum the number of items (forloop.counter) and multiply it
by the flat rate. but i dont think im supposed to do maths in the
template am i?

so my alternative is to do the maths based on the number of items in
the basket at creation of the dict in the view.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.