3

I am using piston to write a JSON api for an application I am writing which handles recurring calendar events.

My API was working for regular events, when I attempted to add logic to handle the recurrence, I started getting the following error:

descriptor 'date' requires a 'datetime.datetime' object but received a 'unicode'

Here is my handlers.py:

from piston.handler import BaseHandler
from lessons.models import NewEvent, EachEvent
import calendar
from datetime import datetime, timedelta

class CalendarHandler(BaseHandler):
allowed_methods = ('GET',)
model = EachEvent
fields = ('actualDate', ('manager', ('firstName', 'lastName')))

def next_date(startDate, recurrence, rangeStart):
    sd = startDate
    while (sd < rangeStart):
        print sd;
        sd += datetime.timedelta(recurrence)
    return sd

def read(self, request, uid, month, year):
    qs = NewEvent.objects.filter(manager__boss = request.user).filter(endDate__gte=datetime.date(year, month, 1)).filter(startDate__lte=datetime.date(year, month, calendar.mdays[month]))
    lessonList = []
    qsList = list(qs)
    for l in qsList:
        if l.frequency == 0:
            x = EachLesson()
            x.lessonID = l.id
            x.actualDate = l.startDate
            x.manager = l.manager
            lessonList.append(x)
        else:
            sd = next_date(l.startDate, l.frequency, datetime.date(year, month, 1))
            while (sd <= datetime.date(year, month, calendar.mdays[month])):
                x = EachLesson()
                x.lessonID = l.id
                x.actualDate = sd
                x.manager = l.manager
                lessonList.append(x)
                sd += datetime.timedelta(recurrence)

    return lessonList

frequency is an IntegerField, actualDate, startDate, and endDate are all DateField.

My URLconf accepts a uid, year, and month, all of which are passed as parameters to the CalendarHandler.read method.

Matthew Calabresi
  • 487
  • 2
  • 11
  • 20

1 Answers1

10

By using from datetime import datetime, timedelta you have imported the datetime type from the datetime module. Thus when you call datetime.date you are calling a method on the datetime type.

I think what you want is to use the date type from the datetime module:

  1. Change your import to from datetime import datetime, timedelta, date.
  2. Call date(year, month, 1) instead of datetime.date(year, month, 1).
BrenBarn
  • 242,874
  • 37
  • 412
  • 384
  • Oops, I forgot to mention the other part of what you need to change. See my edited answer. – BrenBarn Jun 02 '12 at 19:40
  • Now it's telling me that an integer is required. I tried various combinations of changing datetime.date to just date, and it seems the first call (in the filter) is the one that is changing the error message. Changing the other two (so far) has no effect. – Matthew Calabresi Jun 02 '12 at 19:52
  • What are the contents of your variables year and month? – BrenBarn Jun 02 '12 at 20:00
  • 2012 and 5, respectively. uid is 1. – Matthew Calabresi Jun 02 '12 at 20:06
  • Are you sure they are integers and not, say, strings? If you're getting them from a URL they may be passed as string data. If so you may need to convert them with, e.g., int(year). – BrenBarn Jun 02 '12 at 20:08
  • This fixed my original error (and revealed another error in list processing: 'list indices must be integers, not unicode')... but I think I can fix that, that's a bit more descriptive than my original error). Thanks! – Matthew Calabresi Jun 02 '12 at 20:16
  • Fantastic, incredibly useful answer! – Andy Hayden May 15 '13 at 21:07