On 6/24/06, Elver Loho <[EMAIL PROTECTED]> wrote:
>
> I have an SQLObject question. I'm hoping that someone here has perhaps
> encountered a similar problem and is willing to share some experiences
> :)
>
> Anyway...
>
> What I have is a table where every row is a visit record.
>
> class View(SQLObject):
>         ip = UnicodeCol(length = 50)
>         time = DateTimeCol(default = datetime.datetime.now)
>         site = ForeignKey("Page")
>
> What I need is, given a page ID, a data structure holding, for the
> last n days, the date and the number of views on that date.
>

You'd need to do something similar to the following:

from datetime import datetime, timedelta

# 5 days by default
n = 5

# the site id
id = 1

now = datetime.now()
daystart = datetime(now.year, now.month, now.day)
dayend = daystart + timedelta(1)

data = list()
for day in range(n):
    view_count = View.select(AND(View.q.siteID==id,
         View.q.time >= daystart,
         View.q.time < dayend)).count()

    data.append(dict(date=daystart, views=view_count))
    daystart = daystart - timedelta(1)
    dayend = dayend - timedelta(1)


This was all written in gmail and isn't tested but, assuming I haven't
done something really silly there, that should get you what you need.

Hope that helps,

Lee

-- 
Lee McFadden

blog: http://www.splee.co.uk
work: http://fireflisystems.com

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"TurboGears" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/turbogears
-~----------~----~----~----~------~----~------~--~---

Reply via email to