'Sets a cookie.
``expires`` can be:
- a string in the correct format,
- a naive ``datetime.datetime`` object in UTC,
- an aware ``datetime.datetime`` object in any time zone.
If it is a ``datetime.datetime`` object then ``max_age`` will be calculated.'

def set_cookie(self, key, value=u'', max_age=None, expires=None, path=u'/', domain=None, secure=False, httponly=False):

    self.cookies[key] = value
    if (expires is not None):
        if isinstance(expires, datetime.datetime):
            if timezone.is_aware(expires):
                expires = timezone.make_naive(expires, timezone.utc)
            delta = (expires - expires.utcnow())
            delta = (delta + datetime.timedelta(seconds=1))
            expires = None
            max_age = max(0, ((delta.days * 86400) + delta.seconds))
        else:
            self.cookies[key][u'expires'] = expires
    if (max_age is not None):
        self.cookies[key][u'max-age'] = max_age
        if (not expires):
            self.cookies[key][u'expires'] = cookie_date((time.time() + max_age))
    if (path is not None):
        self.cookies[key][u'path'] = path
    if (domain is not None):
        self.cookies[key][u'domain'] = domain
    if secure:
        self.cookies[key][u'secure'] = True
    if httponly:
        self.cookies[key][u'httponly'] = True
